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.

219 lines
8.5 KiB

  1. // Copyright (c) 2017 Walter Bender
  2. // Copyright (c) 2017 Ayush Kumar
  3. //
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the The GNU Affero General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 3 of the License, or (at your option) any later version.
  8. //
  9. // You should have received a copy of the GNU Affero General Public
  10. // License along with this library; if not, write to the Free Software
  11. // Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
  12. const SAVEBOXSVG = '<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. function SaveBox () {
  14. this._canvas = null;
  15. this._stage = null;
  16. this._refreshCanvas = null;
  17. this._doSaveTB = null;
  18. this._doSaveSVG = null;
  19. this._doSavePNG = null;
  20. this._doUploadToPlanet = null;
  21. this._doShareOnFacebook = null;
  22. this._container = null;
  23. this._bounds = null;
  24. this.save = null;
  25. this.close = null;
  26. this._scale = 1;
  27. this.setCanvas = function (canvas) {
  28. this._canvas = canvas;
  29. return this;
  30. };
  31. this.setStage = function (stage) {
  32. this._stage = stage;
  33. return this;
  34. };
  35. this.setSaveTB = function (doSaveTB) {
  36. this._doSaveTB = doSaveTB;
  37. return this;
  38. };
  39. this.setSaveSVG = function (doSaveSVG) {
  40. this._doSaveSVG = doSaveSVG;
  41. return this;
  42. };
  43. this.setSavePNG = function (doSavePNG) {
  44. this._doSavePNG = doSavePNG;
  45. return this;
  46. };
  47. this.setSaveFB = function (doSaveFB) {
  48. this._doShareOnFacebook = doSaveFB;
  49. return this;
  50. };
  51. this.setSavePlanet = function (doSavePlanet) {
  52. this._doUploadToPlanet = doSavePlanet;
  53. return this;
  54. };
  55. this.setRefreshCanvas = function (refreshCanvas) {
  56. this._refreshCanvas = refreshCanvas;
  57. return this;
  58. };
  59. this.init = function(scale, x, y, makeButton) {
  60. if (this._container === null) {
  61. this.createBox(scale, x, y);
  62. var that = this;
  63. this.saveTB = makeButton('save-tb', _('Save as .tb'), this._container.x + 50, this._container.y + 85, 55, 0, this._stage);
  64. this.saveTB.visible = true;
  65. this.positionHoverText(this.saveTB);
  66. this.saveTB.on('click', function(event) {
  67. that.hide();
  68. that._doSaveTB();
  69. });
  70. this.saveSVG = makeButton('save-svg', _('Save as .svg'), this._container.x + 115, this._container.y + 85, 55, 0, this._stage);
  71. this.saveSVG.visible = true;
  72. this.positionHoverText(this.saveSVG);
  73. this.saveSVG.on('click', function(event) {
  74. that.hide();
  75. that._doSaveSVG();
  76. });
  77. this.savePNG = makeButton('save-png-inactive', _('Save as .png'), this._container.x + 180, this._container.y + 85, 55, 0, this._stage);
  78. this.savePNG.visible = true;
  79. this.positionHoverText(this.savePNG);
  80. this.savePNG.on('click', function(event) {
  81. that.hide();
  82. that._doSavePNG();
  83. });
  84. this.uploadToPlanet = makeButton('upload-planet', _('Upload to Planet'), this._container.x + 245, this._container.y + 85, 55, 0, this._stage);
  85. this.uploadToPlanet.visible = true;
  86. this.positionHoverText(this.uploadToPlanet);
  87. this.uploadToPlanet.on('click', function(event) {
  88. that.hide();
  89. that._doUploadToPlanet();
  90. });
  91. this.shareOnFb = makeButton('fb-inactive', _('Share on Facebook'), this._container.x + 310, this._container.y + 85, 55, 0, this._stage);
  92. this.shareOnFb.visible = true;
  93. this.positionHoverText(this.shareOnFb);
  94. this.shareOnFb.on('click', function(event) {
  95. that.hide();
  96. that._doShareOnFacebook();
  97. // change 'fb-inactive' to 'fb' when the button becomes operational
  98. });
  99. } else {
  100. this.show();
  101. }
  102. };
  103. this.positionHoverText = function(button) {
  104. for (var c = 0; c < button.children.length; c++) {
  105. if (button.children[c].text != undefined) {
  106. button.children[c].textAlign = 'left';
  107. button.children[c].x = -27;
  108. button.children[c].y = 27;
  109. break;
  110. }
  111. }
  112. };
  113. this.hide = function() {
  114. if (this._container !== null) {
  115. this.saveTB.visible = false;
  116. this.saveSVG.visible = false;
  117. this.savePNG.visible = false;
  118. this.uploadToPlanet.visible = false;
  119. this.shareOnFb.visible = false;
  120. this._container.visible = false;
  121. this._refreshCanvas();
  122. }
  123. };
  124. this.show = function() {
  125. if (this._container !== null) {
  126. this.saveTB.visible = true;
  127. this.saveSVG.visible = true;
  128. this.savePNG.visible = true;
  129. this.uploadToPlanet.visible = true;
  130. this.shareOnFb.visible = true;
  131. this._container.visible = true;
  132. this._refreshCanvas();
  133. }
  134. };
  135. this.createBox = function(scale, x, y) {
  136. this._scale = scale;
  137. function __processBackground(that, name, bitmap, extras) {
  138. that._container.addChild(bitmap);
  139. that._loadUtilityContainerHandler();
  140. var hitArea = new createjs.Shape();
  141. that._bounds = that._container.getBounds();
  142. that._container.cache(that._bounds.x, that._bounds.y, that._bounds.width, that._bounds.height);
  143. hitArea.graphics.beginFill('#FFF').drawRect(that._bounds.x, that._bounds.y, that._bounds.width, that._bounds.height);
  144. hitArea.x = 0;
  145. hitArea.y = 0;
  146. that._container.hitArea = hitArea;
  147. };
  148. if (this._container == null) {
  149. this._container = new createjs.Container();
  150. this._stage.addChild(this._container);
  151. this._container.x = Math.floor(((this._canvas.width / scale) - 180) / 2);
  152. this._container.y = 27;
  153. this._makeBoxBitmap(SAVEBOXSVG, 'box', __processBackground, null);
  154. }
  155. };
  156. this._makeBoxBitmap = function(data, name, callback, extras) {
  157. // Async creation of bitmap from SVG data
  158. // Works with Chrome, Safari, Firefox (untested on IE)
  159. var img = new Image();
  160. var that = this;
  161. img.onload = function() {
  162. var bitmap = new createjs.Bitmap(img);
  163. callback(that, name, bitmap, extras);
  164. };
  165. img.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(data)));
  166. };
  167. this._loadUtilityContainerHandler = function() {
  168. var locked = false;
  169. var that = this;
  170. this._container.on('click', function(event) {
  171. if (locked) {
  172. console.log('debouncing click');
  173. return;
  174. }
  175. locked = true;
  176. setTimeout(function() {
  177. locked = false;
  178. }, 500);
  179. var x = (event.stageX / that._scale) - that._container.x;
  180. var y = (event.stageY / that._scale) - that._container.y;
  181. if (y < 55) {
  182. that.hide();
  183. }
  184. });
  185. };
  186. };