not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
9.2 KiB

  1. // Copyright (c) 2015-16 Walter Bender
  2. //
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the The GNU Affero General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 3 of the License, or (at your option) any later version.
  7. //
  8. // You should have received a copy of the GNU Affero General Public
  9. // License along with this library; if not, write to the Free Software
  10. // Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
  11. //
  12. const UTILITYBOXSVG = '<svg xmlns="http://www.w3.org/2000/svg" height="133" width="360" version="1.1"> <rect style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:none" y="0" x="0" height="133" width="360" /> <g style="fill:#000000;display:block" transform="translate(306.943,-1.053)"> <path style="fill:#000000;display:inline" d="m 27.557,5.053 c -12.43,0 -22.5,10.076 -22.5,22.497 0,12.432 10.07,22.503 22.5,22.503 12.431,0 22.5,-10.071 22.5,-22.503 0,-12.421 -10.07,-22.497 -22.5,-22.497 z m 10.199,28.159 c 1.254,1.256 1.257,3.291 0,4.545 -0.628,0.629 -1.451,0.943 -2.274,0.943 -0.822,0 -1.644,-0.314 -2.27,-0.94 l -5.76,-5.761 -5.76,5.761 c -0.627,0.626 -1.449,0.94 -2.271,0.94 -0.823,0 -1.647,-0.314 -2.275,-0.943 -1.254,-1.254 -1.254,-3.289 0.004,-4.545 l 5.758,-5.758 -5.758,-5.758 c -1.258,-1.254 -1.258,-3.292 -0.004,-4.546 1.255,-1.254 3.292,-1.259 4.546,0 l 5.76,5.759 5.76,-5.759 c 1.252,-1.259 3.288,-1.254 4.544,0 1.257,1.254 1.254,3.292 0,4.546 l -5.758,5.758 5.758,5.758 z" /> </g> <rect style="fill:#92b5c8;fill-opacity:1;stroke:none" y="51" x="0" height="82" width="360" /> <rect y="0.76763773" x="0.76764059" height="131.46472" width="358.46472" style="display:inline;visibility:visible;opacity:1;fill:none;fill-opacity:1;stroke:#000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;" /></svg>';
  13. // A pop up for utility functions, e.g., loading plugins, changing block size
  14. function UtilityBox () {
  15. this._stage = null;
  16. this._refreshCanvas = null;
  17. this._doBigger = null;
  18. this._doSmaller = null;
  19. this._doPlugins = null;
  20. this._doStats = null;
  21. this._doScroller = null;
  22. this._scrollStatus = false;
  23. this._container = null;
  24. this._scale = 1;
  25. this.setStage = function (stage) {
  26. this._stage = stage;
  27. return this;
  28. };
  29. this.setRefreshCanvas = function (refreshCanvas) {
  30. this._refreshCanvas = refreshCanvas;
  31. return this;
  32. };
  33. this.setBigger = function (bigger) {
  34. this._doBigger = bigger;
  35. return this;
  36. };
  37. this.setSmaller = function (smaller) {
  38. this._doSmaller = smaller;
  39. return this;
  40. };
  41. this.setPlugins = function (plugins) {
  42. this._doPlugins = plugins;
  43. return this;
  44. };
  45. this.setStats = function (stats) {
  46. this._doStats = stats;
  47. return this;
  48. };
  49. this.setScroller = function (scroller) {
  50. this._doScroller = scroller;
  51. return this;
  52. };
  53. this.init = function (scale, x, y, makeButton) {
  54. if (this._container === null) {
  55. this._createBox(scale, x, y);
  56. var that = this;
  57. this._smallerButton = makeButton('smaller-button', _('Decrease block size'), this._container.x + 55, this._container.y + 85, 55, 0, this._stage);
  58. this._smallerButton.visible = true;
  59. this._positionHoverText(this._smallerButton);
  60. this._smallerButton.on('click', function (event) {
  61. that._doSmaller();
  62. that._hide();
  63. });
  64. this._biggerButton = makeButton('bigger-button', _('Increase block size'), this._container.x + 120, this._container.y + 85, 55, 0, this._stage);
  65. this._biggerButton.visible = true;
  66. this._positionHoverText(this._biggerButton);
  67. this._biggerButton.on('click', function (event) {
  68. that._doBigger();
  69. that._hide();
  70. });
  71. this._statsButton = makeButton('stats-button', _('Display statistics'), this._container.x + 185, this._container.y + 85, 55, 0, this._stage);
  72. this._statsButton.visible = true;
  73. this._positionHoverText(this._statsButton);
  74. this._statsButton.on('click', function (event) {
  75. that._doStats();
  76. that._hide();
  77. });
  78. this.pluginsButton = makeButton('plugins-button', _('Load plugin from file'), this._container.x + 250, this._container.y + 85, 55, 0, this._stage);
  79. this.pluginsButton.visible = true;
  80. this._positionHoverText(this.pluginsButton);
  81. this.pluginsButton.on('click', function (event) {
  82. that._doPlugins();
  83. that._hide();
  84. });
  85. this._scrollButton = makeButton('scroll-unlock-button', _('Enable scrolling'), this._container.x + 315, this._container.y + 85, 55, 0, this._stage);
  86. this._scrollButton.visible = true;
  87. this._positionHoverText(this._scrollButton);
  88. this._scrollButton.on('click', function (event) {
  89. that._doScroller();
  90. that._hide();
  91. that._scrollStatus = !that._scrollStatus;
  92. });
  93. this._scrollButton2 = makeButton('scroll-lock-button', _('Disable scrolling'), this._container.x + 315, this._container.y + 85, 55, 0, this._stage);
  94. this._scrollButton2.visible = false;
  95. this._positionHoverText(this._scrollButton2);
  96. this._scrollButton2.on('click', function (event) {
  97. that._doScroller();
  98. that._hide();
  99. that._scrollStatus = !that._scrollStatus;
  100. });
  101. } else {
  102. this._show();
  103. }
  104. };
  105. this._positionHoverText = function (button) {
  106. for (var c = 0; c < button.children.length; c++) {
  107. if (button.children[c].text != undefined) {
  108. button.children[c].textAlign = 'left';
  109. button.children[c].x = -27;
  110. button.children[c].y = 27;
  111. break;
  112. }
  113. }
  114. };
  115. this._hide = function () {
  116. if (this._container !== null) {
  117. this._smallerButton.visible = false;
  118. this._biggerButton.visible = false;
  119. this._statsButton.visible = false;
  120. this.pluginsButton.visible = false;
  121. this._scrollButton.visible = false;
  122. this._scrollButton2.visible = false;
  123. this._container.visible = false;
  124. this._refreshCanvas();
  125. }
  126. };
  127. this._show = function () {
  128. if (this._container !== null) {
  129. this._smallerButton.visible = true;
  130. this._biggerButton.visible = true;
  131. this._statsButton.visible = true;
  132. this.pluginsButton.visible = true;
  133. this._scrollButton.visible = !this._scrollStatus;
  134. this._scrollButton2.visible = this._scrollStatus;
  135. this._container.visible = true;
  136. this._refreshCanvas();
  137. }
  138. };
  139. this._createBox = function (scale, x, y) {
  140. this._scale = scale;
  141. console.log(scale);
  142. function __processBackground(that, name, bitmap, extras) {
  143. that._container.addChild(bitmap);
  144. that._loadUtilityContainerHandler();
  145. that.bounds = that._container.getBounds();
  146. that._container.cache(that.bounds.x, that.bounds.y, that.bounds.width, that.bounds.height);
  147. var hitArea = new createjs.Shape();
  148. hitArea.graphics.beginFill('#FFF').drawRect(that.bounds.x, that.bounds.y, that.bounds.width, that.bounds.height);
  149. hitArea.x = 0;
  150. hitArea.y = 0;
  151. that._container.hitArea = hitArea;
  152. };
  153. if (this._container == null) {
  154. this._container = new createjs.Container();
  155. this._stage.addChild(this._container);
  156. this._container.x = x - 360;
  157. this._container.y = y - 133;
  158. var UTILITYBOX = UTILITYBOXSVG;
  159. this._makeBoxBitmap(UTILITYBOX, 'box', __processBackground, null);
  160. }
  161. };
  162. this._makeBoxBitmap = function (data, name, callback, extras) {
  163. // Async creation of bitmap from SVG data
  164. // Works with Chrome, Safari, Firefox (untested on IE)
  165. var img = new Image();
  166. var that = this;
  167. img.onload = function () {
  168. bitmap = new createjs.Bitmap(img);
  169. callback(that, name, bitmap, extras);
  170. };
  171. img.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(data)));
  172. };
  173. this._loadUtilityContainerHandler = function () {
  174. var locked = false;
  175. var that = this;
  176. that._container.on('click', function (event) {
  177. // We need a lock to "debouce" the click.
  178. if (locked) {
  179. console.log('debouncing click');
  180. return;
  181. }
  182. locked = true;
  183. setTimeout(function () {
  184. locked = false;
  185. }, 500);
  186. var x = (event.stageX / that._scale) - that._container.x;
  187. var y = (event.stageY / that._scale) - that._container.y;
  188. if (y < 55) {
  189. that._hide();
  190. }
  191. });
  192. };
  193. };