Skip to content

Commit 6279034

Browse files
author
Yuri Pourre
committed
Refactoring
1 parent 16a3f63 commit 6279034

5 files changed

Lines changed: 19 additions & 22 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { AlertDialog, AlertDialogCancel, AlertDialogContent, AlertDialogFooter,
1010

1111
import { trySetExperimentalFeaturesEnabledInLocalStorage } from "../../../tools/local-storage";
1212
import {
13-
GIZMO_SNAP_MIN_STEP,
13+
gizmoSnapMinStep,
1414
IGizmoSnapPreferences,
1515
loadGizmoSnapPreferences,
1616
roundGizmoSnapSteps,
@@ -206,7 +206,7 @@ export class EditorEditPreferencesComponent extends Component<IEditorEditPrefere
206206

207207
private _getGizmoSnapPreferencesSection(): ReactNode {
208208
const snap = this.state.gizmoSnap;
209-
const min = GIZMO_SNAP_MIN_STEP;
209+
const min = gizmoSnapMinStep;
210210

211211
return (
212212
<div className="flex flex-col gap-[10px] w-full">

editor/src/editor/layout/inspector/fields/number.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ export interface IEditorInspectorNumberFieldProps extends Partial<IEditorInspect
3131

3232
/** When set, value is driven from React state; object/property and inspector mutation are skipped. */
3333
controlledValue?: number;
34-
/** Overrides fractional digits for display/scrub (defaults from step). */
35-
decimals?: number;
36-
3734
wrapperClassName?: string;
3835
inputClassName?: string;
3936
title?: string;
@@ -48,7 +45,7 @@ export function EditorInspectorNumberField(props: IEditorInspectorNumberFieldPro
4845
const [warning, setWarning] = useState(false);
4946

5047
const step = props.step ?? 0.01;
51-
const digitCount = props.decimals ?? (props.step?.toString().split(".")[1]?.length ?? 2);
48+
const digitCount = props.step?.toString().split(".")[1]?.length ?? 2;
5249

5350
const [value, setValue] = useState<string>(() => formatInitial());
5451
const [oldValue, setOldValue] = useState<string>(() => formatInitial());

editor/src/editor/layout/preview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import { ITweenConfiguration, Tween } from "../../tools/animation/tween";
5959
import { checkProjectCachedCompressedTextures } from "../../tools/assets/ktx";
6060
import { createSceneLink, getRootSceneLink } from "../../tools/scene/scene-link";
6161
import {
62-
GIZMO_SNAP_MIN_STEP,
62+
gizmoSnapMinStep,
6363
IGizmoSnapPreferences,
6464
loadGizmoSnapPreferences,
6565
roundGizmoSnapSteps,
@@ -903,7 +903,7 @@ export class EditorPreview extends Component<IEditorPreviewProps, IEditorPreview
903903

904904
private _getGizmoSnapToolbarControls(): ReactNode {
905905
const snap = this.state.gizmoSnap;
906-
const min = GIZMO_SNAP_MIN_STEP;
906+
const min = gizmoSnapMinStep;
907907

908908
const bumpTranslation = (v: number) => this._commitGizmoSnap({ ...snap, translationStep: Math.max(min, v) });
909909
const bumpRotation = (v: number) => this._commitGizmoSnap({ ...snap, rotationStepDegrees: Math.max(min, v) });

editor/src/editor/layout/preview/gizmo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
Sprite,
1717
} from "babylonjs";
1818

19-
import { DEFAULT_GIZMO_SNAP_PREFERENCES, IGizmoSnapPreferences } from "../../../tools/gizmo-snap-preferences";
19+
import { defaultGizmoSnapPreferences, IGizmoSnapPreferences } from "../../../tools/gizmo-snap-preferences";
2020
import { isSprite } from "../../../tools/guards/sprites";
2121
import { registerUndoRedo } from "../../../tools/undoredo";
2222
import { isNodeLocked } from "../../../tools/node/metadata";
@@ -46,7 +46,7 @@ export class EditorPreviewGizmo {
4646

4747
private _spriteTransformNode: TransformNode;
4848

49-
private _snapPreferences: IGizmoSnapPreferences = { ...DEFAULT_GIZMO_SNAP_PREFERENCES };
49+
private _snapPreferences: IGizmoSnapPreferences = { ...defaultGizmoSnapPreferences };
5050

5151
public constructor(scene: Scene) {
5252
this._gizmosLayer = new UtilityLayerRenderer(scene);

editor/src/tools/gizmo-snap-preferences.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export const EDITOR_GIZMO_SNAP_STORAGE_KEY = "editor-gizmo-snap";
1+
export const editorGizmoSnapStorageKey = "editor-gizmo-snap";
22

33
/** Minimum snap step (two-decimal increments cannot be smaller than 0.01). */
4-
export const GIZMO_SNAP_MIN_STEP = 0.01;
4+
export const gizmoSnapMinStep = 0.01;
55

6-
const SNAP_DECIMAL_ROUND_FACTOR = 100;
6+
const snapDecimalRoundFactor = 100;
77

88
export interface IGizmoSnapPreferences {
99
translationEnabled: boolean;
@@ -19,9 +19,9 @@ export interface IGizmoSnapPreferences {
1919
*/
2020
export function roundGizmoSnapSteps(prefs: IGizmoSnapPreferences): IGizmoSnapPreferences {
2121
const roundStep = (value: number): number => {
22-
const clampedLow = Math.max(GIZMO_SNAP_MIN_STEP, value);
23-
const rounded = Math.round(clampedLow * SNAP_DECIMAL_ROUND_FACTOR) / SNAP_DECIMAL_ROUND_FACTOR;
24-
return Math.max(GIZMO_SNAP_MIN_STEP, rounded);
22+
const clampedLow = Math.max(gizmoSnapMinStep, value);
23+
const rounded = Math.round(clampedLow * snapDecimalRoundFactor) / snapDecimalRoundFactor;
24+
return Math.max(gizmoSnapMinStep, rounded);
2525
};
2626

2727
return {
@@ -32,7 +32,7 @@ export function roundGizmoSnapSteps(prefs: IGizmoSnapPreferences): IGizmoSnapPre
3232
};
3333
}
3434

35-
export const DEFAULT_GIZMO_SNAP_PREFERENCES: IGizmoSnapPreferences = {
35+
export const defaultGizmoSnapPreferences: IGizmoSnapPreferences = {
3636
translationEnabled: false,
3737
translationStep: 1,
3838
rotationEnabled: false,
@@ -58,12 +58,12 @@ function asNumber(value: unknown, fallback: number): number {
5858

5959
export function loadGizmoSnapPreferences(): IGizmoSnapPreferences {
6060
try {
61-
const raw = localStorage.getItem(EDITOR_GIZMO_SNAP_STORAGE_KEY);
61+
const raw = localStorage.getItem(editorGizmoSnapStorageKey);
6262
if (!raw) {
63-
return roundGizmoSnapSteps({ ...DEFAULT_GIZMO_SNAP_PREFERENCES });
63+
return roundGizmoSnapSteps({ ...defaultGizmoSnapPreferences });
6464
}
6565
const parsed = JSON.parse(raw) as Partial<IGizmoSnapPreferences>;
66-
const base = DEFAULT_GIZMO_SNAP_PREFERENCES;
66+
const base = defaultGizmoSnapPreferences;
6767
return roundGizmoSnapSteps({
6868
translationEnabled: asBoolean(parsed.translationEnabled, base.translationEnabled),
6969
translationStep: clampPositive(asNumber(parsed.translationStep, base.translationStep), base.translationStep),
@@ -73,10 +73,10 @@ export function loadGizmoSnapPreferences(): IGizmoSnapPreferences {
7373
scaleStep: clampPositive(asNumber(parsed.scaleStep, base.scaleStep), base.scaleStep),
7474
});
7575
} catch {
76-
return roundGizmoSnapSteps({ ...DEFAULT_GIZMO_SNAP_PREFERENCES });
76+
return roundGizmoSnapSteps({ ...defaultGizmoSnapPreferences });
7777
}
7878
}
7979

8080
export function saveGizmoSnapPreferences(prefs: IGizmoSnapPreferences): void {
81-
localStorage.setItem(EDITOR_GIZMO_SNAP_STORAGE_KEY, JSON.stringify(roundGizmoSnapSteps(prefs)));
81+
localStorage.setItem(editorGizmoSnapStorageKey, JSON.stringify(roundGizmoSnapSteps(prefs)));
8282
}

0 commit comments

Comments
 (0)