Application.js 9.17 KB
Newer Older
Christian Winter's avatar
Christian Winter committed
1
class App {
2 3 4
    constructor() {
        console.log("Application.constructor()");
        this.currentState; // Drawing, EditingPrefab
Christian Winter's avatar
Christian Winter committed
5
        this.currentFocus;
Christian Winter's avatar
Christian Winter committed
6 7
        this._lastView;
        this._currentView;
Christian Winter's avatar
Christian Winter committed
8

9
        this.cursor = new Cursor();
10
        this.currentBrush = new Brush();
11

12
        // logic
13
        this.logic = new Logic();
14

Christian Winter's avatar
Christian Winter committed
15 16 17
        // views
        this.sceneView;
        this.prefabEditorView;
18
        this.renderPreview;
19 20

        this.clipBoard;
Christian Winter's avatar
Christian Winter committed
21
    }
Christian Winter's avatar
Christian Winter committed
22

Christian Winter's avatar
Christian Winter committed
23
    start() {
Christian Winter's avatar
Christian Winter committed
24
        this.sceneView = new SceneView(sceneView);
Christian Winter's avatar
Christian Winter committed
25
        this._lastView = this.sceneView;
Christian Winter's avatar
Christian Winter committed
26
        this.prefabEditorView = new PrefabEditorView(prefabEditor);
27
        this.renderPreview = new FinalRenderView(renderPreview);
28 29 30

        this.initializeClasses();

31 32
        this.layoutGUI();

33 34 35
        window.addEventListener("keydown", evt => Input.keyDown(evt), false);
        window.addEventListener("keyup", evt => Input.keyUp(evt), false);

Christian Winter's avatar
Christian Winter committed
36 37
        window.addEventListener('resize', evt => this.layoutGUI(evt), false);

38

39
        this.start2();
Christian Winter's avatar
Christian Winter committed
40

41

Christian Winter's avatar
Christian Winter committed
42 43 44
        // center cameras of views
        this.sceneView.camera.resetView();
        this.prefabEditorView.camera.resetView();
45

46
        this.redrawAllViews();
47 48
    }

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    start2() {
        window.addEventListener("contextmenu", function (e) { e.preventDefault(); return false; });
        window.addEventListener('focus', function () {
            Input.clearKeys();
            if (Preferences.developerMode)
                GUI.notify("received focus");
        }, false);
        window.onbeforeunload = function () {
            if (Input.isKeyDown(Key.Control))
                localStorage.removeItem(Saver.autosaveFileName);
            else
                Saver.autoSave();
            Preferences.savePreferences();
        };

        window.onerror = function () {
            if (Preferences.developerMode == true)
                GUI.notify("<font color='red'>An error occured! Press F12 to see what went wrong.</font>");
67
            //Application.currentView.logic._clearStates();
68 69 70 71 72 73 74 75 76 77 78 79 80 81
        };

        offscreenCanvas = document.getElementById('offscreenCanvas');
        offscreenContext = offscreenCanvas.getContext('2d');

        toolbar = document.getElementById('toolbar');

        // Setup the dnd listeners.
        let dropZone = document.body;
        dropZone.addEventListener('dragover', Saver.handleDragOver, false);
        dropZone.addEventListener('drop', Saver.handleFileSelect, false);

        Saver.loadAutoSave();

82
        this.file.updateStats();
83 84 85 86 87 88 89 90 91 92 93 94 95 96

        for (var i = 0; i < waitingForStart.length; i++) {
            waitingForStart[i].start();
        }

        //ActionHistory.writeHistory();

        $(".foldingHeader").each(function () {
            $(this).mousedown(function () {
                $(this).next().slideToggle();
            })
        });
    }

97 98 99 100 101 102 103 104
    adjustRenderPreview() {
        let max = Math.max(Preferences.artBounds.width, Preferences.artBounds.height);
        renderPreview.style.width = Preferences.artBounds.width / max * 300 + "px";
        renderPreview.style.height = Preferences.artBounds.height / max * 300 + "px";
        this.renderPreview._fitCanvasToContainer();
        this.renderPreview.zoomToBounds();
    }

105
    initializeClasses() {
Christian Winter's avatar
Christian Winter committed
106
        GridManager.init();
107 108 109 110 111 112 113 114 115 116
        GUI.init();
        Preferences.init();

        Input.init();
        Saver.init();

        ActionHistory.init();
        PrefabManager.init();

    }
Christian Winter's avatar
Christian Winter committed
117 118 119

    layoutGUI() {
        let topLeft = Vector2.zero;
Christian Winter's avatar
Christian Winter committed
120
        let size = Vector2.zero;
Christian Winter's avatar
Christian Winter committed
121

122
        size.x = window.innerWidth - leftarea.offsetWidth - rightarea.offsetWidth -prefabarea.offsetWidth;
Christian Winter's avatar
Christian Winter committed
123 124
        size.y = window.innerHeight - GUI.menubar.offsetHeight - GUI.statusbar.offsetHeight;

125
        topLeft.x = leftarea.offsetWidth + prefabarea.offsetWidth
126 127

        this.sceneView.canvas.style.left = topLeft.x;
Christian Winter's avatar
Christian Winter committed
128 129 130 131
        this.sceneView.canvas.width = size.x;
        this.sceneView.canvas.height = size.y;

        this.prefabEditorView._fitCanvasToContainer();
132 133
        this.renderPreview._fitCanvasToContainer();

Christian Winter's avatar
Christian Winter committed
134 135 136 137 138 139 140
        /*
        toolbar.style.left = canvas.style.left;
        toolbar.style.width = canvas.width;
        toolbar.style.top = GUI.menubar.offsetHeight;
        */

        // TODO magic
Christian Winter's avatar
Christian Winter committed
141 142
        GUI.stats.style.right = 280;
        GUI.stats.style.top = GUI.menubar.offsetHeight + 25;
Christian Winter's avatar
Christian Winter committed
143

Christian Winter's avatar
Christian Winter committed
144 145 146 147 148
        this.redrawAllViews();
    }

    focus(focusable) {
        this.currentFocus = focusable;
Christian Winter's avatar
Christian Winter committed
149 150 151 152
        if (this.currentFocus)
            GUI.writeToStats("currentFocus", this.currentFocus.toString());
        else 
            GUI.writeToStats("currentFocus", "null");
Christian Winter's avatar
Christian Winter committed
153 154 155
    }

    setView(view) {
Christian Winter's avatar
Christian Winter committed
156 157 158 159 160
        // deactivate current view if there is one
        if (this._currentView)
            this._currentView.deactivate();

        // if no current view, store last one
Christian Winter's avatar
Christian Winter committed
161
        if (view == null) {
Christian Winter's avatar
Christian Winter committed
162 163
            this._lastView = this._currentView;
            this._currentView = null;
Christian Winter's avatar
Christian Winter committed
164
            return;
Christian Winter's avatar
Christian Winter committed
165
        }
Christian Winter's avatar
Christian Winter committed
166

Christian Winter's avatar
Christian Winter committed
167 168 169 170
        // set and activate new view
        this._currentView = view;
        this._currentView.activate();
        this._currentView.canvas.focus();
Christian Winter's avatar
Christian Winter committed
171 172

        GUI.writeToStats("currentView", this._currentView.toString());
Christian Winter's avatar
Christian Winter committed
173
    }
Christian Winter's avatar
Christian Winter committed
174

Christian Winter's avatar
Christian Winter committed
175
    get currentView() {
176
        let view;
Christian Winter's avatar
Christian Winter committed
177
        if (this._currentView)
178
            view = this._currentView;
Christian Winter's avatar
Christian Winter committed
179
        else
180 181
            view = this._lastView;

Christian Winter's avatar
Christian Winter committed
182
        /*
183 184
        if (view == this.prefabEditorView && !this.prefabEditorView.prefab)
            view = this.sceneView;
Christian Winter's avatar
Christian Winter committed
185
        */
186
        return view;
Christian Winter's avatar
Christian Winter committed
187
    }
Christian Winter's avatar
Christian Winter committed
188

Christian Winter's avatar
Christian Winter committed
189
    redrawAllViews() {
Christian Winter's avatar
Christian Winter committed
190 191
        this.sceneView.renderer.redraw();
        this.prefabEditorView.renderer.redraw();
192
        this.renderPreview.renderer.redraw();
Christian Winter's avatar
Christian Winter committed
193
    }
Christian Winter's avatar
Christian Winter committed
194 195

    setQualitySetting(setting) {
Christian Winter's avatar
Christian Winter committed
196 197
        this.sceneView.renderer.setQualitySetting(setting);
        this.prefabEditorView.renderer.setQualitySetting(setting);
Christian Winter's avatar
Christian Winter committed
198
    }
Christian Winter's avatar
Christian Winter committed
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    shouldSnap() {
        return (Preferences.snapToGrid && !this.tmpSwitchSnapToGrid()) || (!Preferences.snapToGrid && this.tmpSwitchSnapToGrid());
    }

    tmpCutLines() {
        return Input.isKeyDown(Key.Alt);
    }

    tmpSwitchSnapToGrid() {
        return Input.isKeyDown(Key.Control);
    }

    toggleGridVisiblity(senderButton) {
        Preferences.showGrid = !Preferences.showGrid;
        senderButton.innerHTML = Preferences.showGrid ? "Hide grid" : "Show grid";
        Application.redrawAllViews();
    }

218
    // TODO move to Brush class?
219
    setCurrentLineAlpha(alpha) {
220
        Application.currentBrush.lineColor.a = alpha;
221 222 223
        this.useCurrentColorForSelection();
    }

224
    // TODO move to Brush class?
225
    setCurrentLineColor(color) {
226 227 228
        let alpha = Application.currentBrush.lineColor.a;
        Application.currentBrush.lineColor = color;
        Application.currentBrush.lineColor.a = alpha;
229 230 231

        GUI.lineColor.value = color.toHexString();
        this.useCurrentColorForSelection();
Christian Winter's avatar
Christian Winter committed
232
    }
Christian Winter's avatar
Christian Winter committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

    selectionChanged(selection) {
        let color;
        let thickness;
        let alpha;

        let notInitialized = true;

        for (let line of selection.data.lines.concat(selection.data.partialLines)) {

            if (notInitialized == true) {
                color = line.color.copy();
                thickness = line.thickness;
                alpha = line.color.a;

                notInitialized = false;
            }
            else {
                if (color != undefined) {
                    if (!(color.equals(line.color)))
                        color = undefined;
                }
                if (thickness != line.thickness)
                    thickness = undefined;
                if (alpha != line.color.a)
                    alpha = undefined;
            }
        }


        if (color != undefined)
264 265
            Application.currentBrush.lineColor = color.copy();
        GUI.lineColor.value = Application.currentBrush.lineColor.toHexString();
Christian Winter's avatar
Christian Winter committed
266 267

        if (thickness != undefined)
268
            Application.currentBrush.lineThickness = thickness;
Christian Winter's avatar
Christian Winter committed
269

270
        GUI.lineThickness.value = Application.currentBrush.lineThickness;
Christian Winter's avatar
Christian Winter committed
271 272

        if (alpha != undefined)
273
            Application.currentBrush.lineColor.a = alpha;
Christian Winter's avatar
Christian Winter committed
274

275
        GUI.lineAlpha.value = Application.currentBrush.lineColor.a;
Christian Winter's avatar
Christian Winter committed
276
    }
277 278 279 280 281

    increaseCurrentLineThickness(delta) {
        for (let line of this.currentView.selection.data.lines.concat(this.currentView.selection.data.partialLines))
            line.thickness += delta;

282
        Application.currentBrush.lineThickness = Math.max(1, Application.currentBrush.lineThickness + delta);
283

284
        GUI.lineThickness.value = Application.currentBrush.lineThickness;
285 286 287 288
        Application.redrawAllViews();
    }

    setCurrentLineThickness(thickness) {
289
        Application.currentBrush.lineThickness = thickness;
290 291 292 293 294 295 296 297 298 299

        for (let line of this.currentView.selection.data.lines.concat(this.currentView.selection.data.partialLines))
            line.thickness = thickness;

        GUI.lineThickness.value = thickness;
        Application.redrawAllViews();
    }

    useCurrentColorForSelection() {
        for (let line of this.currentView.selection.data.lines.concat(this.currentView.selection.data.partialLines))
300
            line.color = Application.currentBrush.lineColor.copy();
301 302 303

        Application.redrawAllViews();
    }
304 305

    clearAllSelections() {
306 307
        this.sceneView.selection.clear();
        this.prefabEditorView.selection.clear();
308
    }
309
}