import { APP_STATE } from "./core/enums/app-state-enum"; import { stages as Stages } from "./core/application/stages"; import { objects as Objects } from "./core/application/objects"; import { state as State } from "./core/application/state"; import { selector as Selector } from "./core/application/selector"; import { menu as Menu } from "./core/application/menu"; export const app = (function() { let _selectedTool = []; let _stage = _createStage(); function _bootstrap() { configureStageEvents(); Selector.bootstrap(); requireAll(require.context("./components/", true, /\.js$/)); _refreshMenu(); } function _createStage() { return Stages.getCurrentStage(); } function _currentLayer() { return Stages.getCurrentLayer(); } function configureStageEvents() { _stage.on("mousedown ", function(e) { if (e.target === _stage) { _setState(APP_STATE.NONE); return; } if (e.target.id != undefined) { _setState(APP_STATE.OBJECT_SELECTED); return; } }); _stage.on("click tap", function(e) { const tool = _getSelectedTool(); if (tool != undefined) { const fun = tool.draw.bind(tool); fun(); return; } if (e.target === _stage) { _setState(APP_STATE.NONE); return; } }); } function _setSelectedTool(tool) { _clearSelectedTool(); _setState(APP_STATE.TOOL_SELECTED); if (tool.id != undefined) { document.getElementById("btn-" + tool.id).disabled = true; } _selectedTool.push(tool); } function _clearSelectedTool() { let tool = _getSelectedTool(); if (tool != undefined) { if (tool.id != undefined) { document.getElementById("btn-" + tool.id).disabled = false; } } _selectedTool = []; _setStatus(""); _setState(APP_STATE.NONE); } function _getSelectedTool() { if (_selectedTool.length > 0) return _selectedTool[0]; return undefined; } function _getRelativePointerPosition() { return _stage.getPointerPosition(); } function _setStatus(status) { document.getElementById("status").innerHTML = status; } function _pushObject(object) { Objects.add(object); } function _getObjects() { return Objects.get(); } function _setState(e) { State.setState(e); } function _refreshMenu() { Menu.refresh(); } function requireAll(r) { r.keys().forEach(r); } _bootstrap(); return { stage: _stage, currentLayer: _currentLayer, bootstrap: _bootstrap, setSelectedTool: _setSelectedTool, clearSelectedTool: _clearSelectedTool, getSelectedTool: _getSelectedTool, pos: _getRelativePointerPosition, setStatus: _setStatus, pushObject: _pushObject, getObjects: _getObjects }; })();