File size: 3,245 Bytes
9332801
e6b949c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18a4acc
9332801
 
 
 
 
e6b949c
9332801
 
e6b949c
 
 
 
 
 
 
 
 
 
 
 
 
 
9332801
 
e6b949c
 
 
 
 
 
 
 
 
9332801
 
e6b949c
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as SPLAT from "gsplat";
import { UndoManager } from "./UndoManager";
import { ModeManager } from "./ModeManager";
import { SelectionManager } from "./SelectionManager";
import { InputMode } from "./InputMode";
import { Engine } from "./Engine";

class DefaultMode implements InputMode {
    exit: () => void;

    constructor(engine: Engine) {
        const handleEnterGrabMode = () => {
            if (SelectionManager.selectedSplat) {
                ModeManager.enterMode("grab");
            }
        };

        const handleEnterRotateMode = () => {
            if (SelectionManager.selectedSplat) {
                ModeManager.enterMode("rotate");
            }
        };

        const handleEnterScaleMode = () => {
            if (SelectionManager.selectedSplat) {
                ModeManager.enterMode("scale");
            }
        };

        const handleDelete = () => {
            if (SelectionManager.selectedSplat) {
                engine.scene.removeObject(SelectionManager.selectedSplat);
                SelectionManager.selectedSplat = null;
            }
        };

        const handleUndo = () => {
            UndoManager.undo();
        };

        const handleClearSelection = (event: KeyboardEvent) => {
            if (event.altKey) {
                SelectionManager.selectedSplat = null;
            }
        };

        let mouseDownPosition: SPLAT.Vector3 = new SPLAT.Vector3();
        const handleMouseDown = () => {
            mouseDownPosition = engine.mouseManager.currentMousePosition;
        };

        const handleMouseUp = () => {
            const mousePosition = engine.mouseManager.currentMousePosition;
            const distance = mousePosition.distanceTo(mouseDownPosition);
            if (distance > 0.01) return;
            const result = engine.intersectionTester.testPoint(mousePosition.x, mousePosition.y);
            if (result !== null) {
                SelectionManager.selectedSplat = result;
            } else {
                SelectionManager.selectedSplat = null;
            }
        };

        engine.keyboardManager.registerKey("g", handleEnterGrabMode);
        engine.keyboardManager.registerKey("r", handleEnterRotateMode);
        engine.keyboardManager.registerKey("s", handleEnterScaleMode);
        engine.keyboardManager.registerKey("x", handleDelete);
        engine.keyboardManager.registerKey("z", handleUndo);
        engine.keyboardManager.registerKey("a", handleClearSelection);
        engine.mouseManager.registerMouse("mousedown", handleMouseDown);
        engine.mouseManager.registerMouse("mouseup", handleMouseUp);
        engine.orbitControls.enabled = true;

        this.exit = () => {
            engine.keyboardManager.unregisterKey("g");
            engine.keyboardManager.unregisterKey("r");
            engine.keyboardManager.unregisterKey("s");
            engine.keyboardManager.unregisterKey("x");
            engine.keyboardManager.unregisterKey("z");
            engine.keyboardManager.unregisterKey("a");
            engine.mouseManager.unregisterMouse("mousedown");
            engine.mouseManager.unregisterMouse("mouseup");
            engine.orbitControls.enabled = false;
        };
    }
}

export { DefaultMode };