td2-visualizer/src/hooks/useZoomPubSub.js
izawartka 7578202e18 Improved camera hook
+When the scenery loads, the view is now rotated to match the orientation of the main SignalBox
+When the scenery loads, the zoom is set to the default value (5)
+When clicking on a SignalBox, the camera rotation is set according to its orientation
*Renamed the onCenter hook to setCamera and added rot and zoom options
2025-07-23 02:53:47 +02:00

50 lines
1.3 KiB
JavaScript

import { useEffect } from 'react';
import { Subject, BehaviorSubject } from 'rxjs';
const setCamera$ = new Subject();
const viewAlign$ = new Subject();
// BehaviorSubjects to store current viewBox, clientRect and camera transform
export const viewBox$ = new BehaviorSubject(null);
export const clientRect$ = new BehaviorSubject(null);
export const camera$ = new BehaviorSubject(0);
export function getCurrentViewBox() {
return viewBox$.getValue();
}
export function getCurrentClientRect() {
return clientRect$.getValue();
}
export function getCurrentCamera() {
return camera$.getValue();
}
export function useZoomPanSubscriber(setCamera, onAlign) {
useEffect(() => {
const sub = setCamera$.subscribe(({ x, y, rot, zoom }) => {
setCamera(x, y, rot, zoom);
});
return () => sub.unsubscribe();
}, [setCamera]);
useEffect(() => {
const sub = viewAlign$.subscribe((angleDeg) => {
onAlign(angleDeg);
});
return () => sub.unsubscribe();
}, [onAlign]);
}
export function useZoomPanEmitter() {
return {
setCamera: (x, y, rot = null, zoom = null) => {
setCamera$.next({ x, y, rot, zoom });
},
alignView: (angleDeg) => {
viewAlign$.next(angleDeg);
},
};
}