Skip to content

Commit dd77652

Browse files
author
Yuri Pourre
committed
Remove snap from global preferences
1 parent 6279034 commit dd77652

7 files changed

Lines changed: 28 additions & 58 deletions

File tree

editor/src/editor/dialogs/edit-preferences/edit-preferences.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import { trySetExperimentalFeaturesEnabledInLocalStorage } from "../../../tools/
1212
import {
1313
gizmoSnapMinStep,
1414
IGizmoSnapPreferences,
15-
loadGizmoSnapPreferences,
1615
roundGizmoSnapSteps,
17-
saveGizmoSnapPreferences,
1816
} from "../../../tools/gizmo-snap-preferences";
1917

2018
import { EditorInspectorKeyField } from "../../layout/inspector/fields/key";
@@ -44,13 +42,13 @@ export class EditorEditPreferencesComponent extends Component<IEditorEditPrefere
4442

4543
this.state = {
4644
theme: document.body.classList.contains("dark") ? "dark" : "light",
47-
gizmoSnap: loadGizmoSnapPreferences(),
45+
gizmoSnap: this.props.editor.state.gizmoSnap,
4846
};
4947
}
5048

5149
public componentDidUpdate(prevProps: IEditorEditPreferencesComponentProps): void {
5250
if (this.props.open && !prevProps.open) {
53-
this.setState({ gizmoSnap: loadGizmoSnapPreferences() });
51+
this.setState({ gizmoSnap: this.props.editor.state.gizmoSnap });
5452
}
5553
}
5654

@@ -196,11 +194,10 @@ export class EditorEditPreferencesComponent extends Component<IEditorEditPrefere
196194
private _commitGizmoSnapFromPreferences(next: IGizmoSnapPreferences): void {
197195
const normalized = roundGizmoSnapSteps(next);
198196
this.setState({ gizmoSnap: normalized });
197+
this.props.editor.setState({ gizmoSnap: normalized });
199198
const preview = this.props.editor.layout?.preview;
200199
if (preview) {
201200
preview.updateGizmoSnapPreferences(normalized);
202-
} else {
203-
saveGizmoSnapPreferences(normalized);
204201
}
205202
}
206203

@@ -226,7 +223,7 @@ export class EditorEditPreferencesComponent extends Component<IEditorEditPrefere
226223
inputClassName="h-9 w-28 rounded-md border border-input px-2 py-1 text-sm shadow-sm bg-background !w-28"
227224
title="Translation snap step (scene units); drag horizontally to adjust (hold Shift for ×10)"
228225
step={0.01}
229-
decimals={2}
226+
230227
min={min}
231228
onChange={(v) => this._commitGizmoSnapFromPreferences({ ...snap, translationStep: v })}
232229
/>
@@ -244,7 +241,7 @@ export class EditorEditPreferencesComponent extends Component<IEditorEditPrefere
244241
inputClassName="h-9 w-28 rounded-md border border-input px-2 py-1 text-sm shadow-sm bg-background !w-28"
245242
title="Rotation snap step in degrees; drag horizontally to adjust (hold Shift for ×10)"
246243
step={0.01}
247-
decimals={2}
244+
248245
min={min}
249246
onChange={(v) => this._commitGizmoSnapFromPreferences({ ...snap, rotationStepDegrees: v })}
250247
/>
@@ -262,7 +259,7 @@ export class EditorEditPreferencesComponent extends Component<IEditorEditPrefere
262259
inputClassName="h-9 w-28 rounded-md border border-input px-2 py-1 text-sm shadow-sm bg-background !w-28"
263260
title="Scale snap step (additive); drag horizontally to adjust (hold Shift for ×10)"
264261
step={0.01}
265-
decimals={2}
262+
266263
min={min}
267264
onChange={(v) => this._commitGizmoSnapFromPreferences({ ...snap, scaleStep: v })}
268265
/>

editor/src/editor/layout/preview.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ import { createSceneLink, getRootSceneLink } from "../../tools/scene/scene-link"
6161
import {
6262
gizmoSnapMinStep,
6363
IGizmoSnapPreferences,
64-
loadGizmoSnapPreferences,
6564
roundGizmoSnapSteps,
66-
saveGizmoSnapPreferences,
6765
} from "../../tools/gizmo-snap-preferences";
6866
import { UniqueNumber, waitNextAnimationFrame, waitUntil } from "../../tools/tools";
6967
import { isSprite, isSpriteManagerNode, isSpriteMapNode } from "../../tools/guards/sprites";
@@ -206,7 +204,7 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
206204
playEnabled: false,
207205
playSceneLoadingProgress: 0,
208206

209-
gizmoSnap: loadGizmoSnapPreferences(),
207+
gizmoSnap: this.props.editor.state.gizmoSnap,
210208
};
211209

212210
ipcRenderer.on("gizmo:position", () => this.setActiveGizmo("position"));
@@ -892,8 +890,8 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
892890

893891
private _commitGizmoSnap(next: IGizmoSnapPreferences): void {
894892
const normalized = roundGizmoSnapSteps(next);
895-
saveGizmoSnapPreferences(normalized);
896893
this.setState({ gizmoSnap: normalized });
894+
this.props.editor.setState({ gizmoSnap: normalized });
897895
this.gizmo?.setSnapPreferences(normalized);
898896
}
899897

@@ -932,7 +930,7 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
932930
inputClassName="rounded-none border-0 border-l border-input h-9 w-12 px-1 py-0 text-xs bg-transparent shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 !w-12"
933931
title="Translation snap step (scene units); drag horizontally to adjust (hold Shift for ×10)"
934932
step={0.01}
935-
decimals={2}
933+
936934
min={min}
937935
onChange={(v) => bumpTranslation(v)}
938936
/>
@@ -959,7 +957,7 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
959957
inputClassName="rounded-none border-0 border-l border-input h-9 w-12 px-1 py-0 text-xs bg-transparent shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 !w-12"
960958
title="Rotation snap step (degrees); drag horizontally to adjust (hold Shift for ×10)"
961959
step={0.01}
962-
decimals={2}
960+
963961
min={min}
964962
onChange={(v) => bumpRotation(v)}
965963
/>
@@ -986,7 +984,7 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
986984
inputClassName="rounded-none border-0 border-l border-input h-9 w-12 px-1 py-0 text-xs bg-transparent shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 !w-12"
987985
title="Scale snap step (additive, incremental); drag horizontally to adjust (hold Shift for ×10)"
988986
step={0.01}
989-
decimals={2}
987+
990988
min={min}
991989
onChange={(v) => bumpScale(v)}
992990
/>

editor/src/editor/main.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { loadProject } from "../project/load/load";
2020
import { startProjectDevProcess } from "../project/run";
2121
import { exportProject } from "../project/export/export";
2222
import { EditorProjectPackageManager } from "../project/typings";
23+
import { defaultGizmoSnapPreferences, IGizmoSnapPreferences } from "../tools/gizmo-snap-preferences";
2324

2425
import { disposeVLSPostProcess } from "./rendering/vls";
2526
import { disposeSSRRenderingPipeline } from "./rendering/ssr";
@@ -97,6 +98,11 @@ export interface IEditorState {
9798
*/
9899
compressedTexturesEnabledInPreview: boolean;
99100

101+
/**
102+
* Per-project gizmo snap preferences.
103+
*/
104+
gizmoSnap: IGizmoSnapPreferences;
105+
100106
/**
101107
* Defines wether or not experimental features are enabled.
102108
*/
@@ -155,6 +161,7 @@ export class Editor extends Component<IEditorProps, IEditorState> {
155161

156162
compressedTexturesEnabled: false,
157163
compressedTexturesEnabledInPreview: false,
164+
gizmoSnap: { ...defaultGizmoSnapPreferences },
158165
enableExperimentalFeatures: tryGetExperimentalFeaturesEnabledFromLocalStorage(),
159166
openedTabs: [],
160167

editor/src/project/load/load.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { requirePlugin } from "../../tools/plugins/require";
1111

1212
import { EditorProjectPackageManager, IEditorProject } from "../typings";
1313
import { projectConfiguration } from "../configuration";
14+
import { defaultGizmoSnapPreferences, roundGizmoSnapSteps } from "../../tools/gizmo-snap-preferences";
1415

1516
import { loadScene } from "./scene";
1617
import { LoadScenePrepareComponent } from "./prepare";
@@ -35,6 +36,7 @@ export async function loadProject(editor: Editor, path: string) {
3536

3637
compressedTexturesEnabled: project.compressedTexturesEnabled ?? false,
3738
compressedTexturesEnabledInPreview: project.compressedTexturesEnabledInPreview ?? false,
39+
gizmoSnap: roundGizmoSnapSteps(project.gizmoSnapPreferences ?? { ...defaultGizmoSnapPreferences }),
3840
});
3941

4042
editor.layout.forceUpdate();

editor/src/project/save/save.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export async function saveProjectConfiguration(editor: Editor) {
5252

5353
compressedTexturesEnabled: editor.state.compressedTexturesEnabled,
5454
compressedTexturesEnabledInPreview: editor.state.compressedTexturesEnabledInPreview,
55+
gizmoSnapPreferences: editor.state.gizmoSnap,
5556
};
5657

5758
if (!editor.props.editedScenePath) {

editor/src/project/typings.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IGizmoSnapPreferences } from "../tools/gizmo-snap-preferences";
2+
13
export interface IEditorProject {
24
/**
35
* The version of the editor that saved this project.
@@ -26,6 +28,11 @@ export interface IEditorProject {
2628
* The package manager being used by the project.
2729
*/
2830
packageManager?: EditorProjectPackageManager;
31+
32+
/**
33+
* Per-project gizmo snap preferences (translate, rotate, scale steps and enabled flags).
34+
*/
35+
gizmoSnapPreferences?: IGizmoSnapPreferences;
2936
}
3037

3138
export interface IEditorProjectPlugin {
Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
export const editorGizmoSnapStorageKey = "editor-gizmo-snap";
2-
31
/** Minimum snap step (two-decimal increments cannot be smaller than 0.01). */
42
export const gizmoSnapMinStep = 0.01;
53

@@ -40,43 +38,3 @@ export const defaultGizmoSnapPreferences: IGizmoSnapPreferences = {
4038
scaleEnabled: false,
4139
scaleStep: 0.25,
4240
};
43-
44-
function clampPositive(value: number, fallback: number): number {
45-
if (!Number.isFinite(value) || value <= 0) {
46-
return fallback;
47-
}
48-
return value;
49-
}
50-
51-
function asBoolean(value: unknown, fallback: boolean): boolean {
52-
return typeof value === "boolean" ? value : fallback;
53-
}
54-
55-
function asNumber(value: unknown, fallback: number): number {
56-
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
57-
}
58-
59-
export function loadGizmoSnapPreferences(): IGizmoSnapPreferences {
60-
try {
61-
const raw = localStorage.getItem(editorGizmoSnapStorageKey);
62-
if (!raw) {
63-
return roundGizmoSnapSteps({ ...defaultGizmoSnapPreferences });
64-
}
65-
const parsed = JSON.parse(raw) as Partial<IGizmoSnapPreferences>;
66-
const base = defaultGizmoSnapPreferences;
67-
return roundGizmoSnapSteps({
68-
translationEnabled: asBoolean(parsed.translationEnabled, base.translationEnabled),
69-
translationStep: clampPositive(asNumber(parsed.translationStep, base.translationStep), base.translationStep),
70-
rotationEnabled: asBoolean(parsed.rotationEnabled, base.rotationEnabled),
71-
rotationStepDegrees: clampPositive(asNumber(parsed.rotationStepDegrees, base.rotationStepDegrees), base.rotationStepDegrees),
72-
scaleEnabled: asBoolean(parsed.scaleEnabled, base.scaleEnabled),
73-
scaleStep: clampPositive(asNumber(parsed.scaleStep, base.scaleStep), base.scaleStep),
74-
});
75-
} catch {
76-
return roundGizmoSnapSteps({ ...defaultGizmoSnapPreferences });
77-
}
78-
}
79-
80-
export function saveGizmoSnapPreferences(prefs: IGizmoSnapPreferences): void {
81-
localStorage.setItem(editorGizmoSnapStorageKey, JSON.stringify(roundGizmoSnapSteps(prefs)));
82-
}

0 commit comments

Comments
 (0)