Added pos param parsing

+Added an option to provide start camera position through url `pos` param
This commit is contained in:
izawartka 2025-08-04 22:06:50 +02:00
parent 82c3c7cfbb
commit 354f797f6c
2 changed files with 34 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import { showCustomDialog, showDialog } from '../services/dialogService';
import { resetHoveredTracksStack } from '../services/trackHoverInfoService';
import SceneryLoadedDialog from './scenery-loaded-dialog/SceneryLoadedDialog';
import Constants from '../helpers/constants';
import MiscHelper from '../helpers/miscHelper';
export default function SceneryManager(props) {
const [isLoading, setIsLoading] = useState(false);
@ -54,6 +55,7 @@ export default function SceneryManager(props) {
const urlParams = new URLSearchParams(window.location.search);
urlParams.delete('scenery');
urlParams.delete('pos');
window.history.replaceState({}, '', urlParams.size ? `${window.location.pathname}?${urlParams.toString()}` : window.location.pathname);
const reader = new FileReader();
@ -127,7 +129,17 @@ export default function SceneryManager(props) {
}
await loadScenery(sceneryName);
}, [findSceneryInList, loadScenery]);
const posQuery = urlParams.get('pos');
const pos = MiscHelper.parsePosQuery(posQuery);
if(!posQuery) return;
if (!pos) {
console.warn(`Invalid position query "${posQuery}"`);
return;
}
setCamera(pos.x, -pos.z, 0, 0);
}, [findSceneryInList, loadScenery, setCamera]);
useEffect(() => {
if(Constants.sceneryFiles.fetchDisable) return;

View File

@ -7,4 +7,25 @@ export default class MiscHelper {
return `rgb(${out[0]}, ${out[1]}, ${out[2]})`;
}
static parsePosQuery(posQuery) {
if (!posQuery) return null;
if (typeof posQuery !== 'string') return null;
const parts = posQuery.split(/[,;]/);
if (parts.length === 3) {
// eslint-disable-next-line no-unused-vars
const [x, _, z] = parts.map(part => parseFloat(part.trim()));
if (isNaN(x) || isNaN(z)) return null;
return { x, z };
}
if (parts.length === 2) {
const [x, z] = parts.map(part => parseFloat(part.trim()));
if (isNaN(x) || isNaN(z)) return null;
return { x, z };
}
return null;
}
}