app.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. const tool = _getSelectedTool();
  35. if (tool != undefined) {
  36. const fun = tool.draw.bind(tool);
  37. fun();
  38. return;
  39. }
  40. if (e.target === _stage) {
  41. _setState(APP_STATE.NONE);
  42. return;
  43. }
  44. });
  45. }
  46. function _setSelectedTool(tool) {
  47. _clearSelectedTool();
  48. _setState(APP_STATE.TOOL_SELECTED);
  49. if (tool.id != undefined) {
  50. document.getElementById("btn-" + tool.id).disabled = true;
  51. }
  52. _selectedTool.push(tool);
  53. }
  54. function _clearSelectedTool() {
  55. let tool = _getSelectedTool();
  56. if (tool != undefined) {
  57. if (tool.id != undefined) {
  58. document.getElementById("btn-" + tool.id).disabled = false;
  59. }
  60. }
  61. _selectedTool = [];
  62. _setStatus("");
  63. _setState(APP_STATE.NONE);
  64. }
  65. function _getSelectedTool() {
  66. if (_selectedTool.length > 0) return _selectedTool[0];
  67. return undefined;
  68. }
  69. function _getRelativePointerPosition() {
  70. return _stage.getPointerPosition();
  71. }
  72. function _setStatus(status) {
  73. document.getElementById("status").innerHTML = status;
  74. }
  75. function _pushObject(object) {
  76. Objects.add(object);
  77. }
  78. function _getObjects() {
  79. return Objects.get();
  80. }
  81. function _setState(e) {
  82. State.setState(e);
  83. }
  84. function _refreshMenu() {
  85. Menu.refresh();
  86. }
  87. function requireAll(r) {
  88. r.keys().forEach(r);
  89. }
  90. _bootstrap();
  91. return {
  92. stage: _stage,
  93. currentLayer: _currentLayer,
  94. bootstrap: _bootstrap,
  95. setSelectedTool: _setSelectedTool,
  96. clearSelectedTool: _clearSelectedTool,
  97. getSelectedTool: _getSelectedTool,
  98. pos: _getRelativePointerPosition,
  99. setStatus: _setStatus,
  100. pushObject: _pushObject,
  101. getObjects: _getObjects
  102. };
  103. })();