app.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { APP_STATE } from "./core/enums/app-state-enum";
  2. import { stages as Stages } from "./core/application/stages";
  3. import { objects as Objects } from "./core/application/objects";
  4. import { state as State } from "./core/application/state";
  5. import { selector as Selector } from "./core/application/selector";
  6. import { menu as Menu } from "./core/application/menu";
  7. export const app = (function() {
  8. let _selectedTool = [];
  9. let _stage = _createStage();
  10. function _bootstrap() {
  11. configureStageEvents();
  12. Selector.bootstrap();
  13. requireAll(require.context("./components/", true, /\.js$/));
  14. _refreshMenu();
  15. }
  16. function _createStage() {
  17. return Stages.getCurrentStage();
  18. }
  19. function _currentLayer() {
  20. return Stages.getCurrentLayer();
  21. }
  22. function configureStageEvents() {
  23. _stage.on("mousedown ", function(e) {
  24. if (e.target === _stage) {
  25. _setState(APP_STATE.NONE);
  26. return;
  27. }
  28. if (e.target.id != undefined) {
  29. _setState(APP_STATE.OBJECT_SELECTED);
  30. return;
  31. }
  32. });
  33. _stage.on("click tap", function(e) {
  34. let tool = _getSelectedTool();
  35. if (tool != undefined) {
  36. tool.draw(tool.object);
  37. return;
  38. }
  39. if (e.target === _stage) {
  40. _setState(APP_STATE.NONE);
  41. return;
  42. }
  43. });
  44. }
  45. function _setSelectedTool(tool) {
  46. _clearSelectedTool();
  47. _setState(APP_STATE.TOOL_SELECTED);
  48. if (tool.id != undefined) {
  49. document.getElementById("btn-" + tool.id).disabled = true;
  50. }
  51. _selectedTool.push(tool);
  52. }
  53. function _clearSelectedTool() {
  54. let tool = _getSelectedTool();
  55. if (tool != undefined) {
  56. if (tool.id != undefined) {
  57. document.getElementById("btn-" + tool.id).disabled = false;
  58. }
  59. }
  60. _selectedTool = [];
  61. _setStatus("");
  62. _setState(APP_STATE.NONE);
  63. }
  64. function _getSelectedTool() {
  65. if (_selectedTool.length > 0) return _selectedTool[0];
  66. return undefined;
  67. }
  68. function _getRelativePointerPosition() {
  69. return _stage.getPointerPosition();
  70. }
  71. function _setStatus(status) {
  72. document.getElementById("status").innerHTML = status;
  73. }
  74. function _pushObject(object) {
  75. Objects.add(object);
  76. }
  77. function _getObjects() {
  78. return Objects.get();
  79. }
  80. function _setState(e) {
  81. State.setState(e);
  82. }
  83. function _refreshMenu() {
  84. Menu.refresh();
  85. }
  86. function requireAll(r) {
  87. r.keys().forEach(r);
  88. }
  89. _bootstrap();
  90. return {
  91. stage: _stage,
  92. currentLayer: _currentLayer,
  93. bootstrap: _bootstrap,
  94. setSelectedTool: _setSelectedTool,
  95. clearSelectedTool: _clearSelectedTool,
  96. getSelectedTool: _getSelectedTool,
  97. pos: _getRelativePointerPosition,
  98. setStatus: _setStatus,
  99. pushObject: _pushObject,
  100. getObjects: _getObjects
  101. };
  102. })();