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.

204 lines
6.5 KiB

  1. // Copyright (c) 2014-2017 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. // The trashcan is an area at the bottom of the screen where stacks of
  12. // blocks can be dragged. Once in the trash area, they are marked as
  13. // trash and hidden. There is a menu button that can be used to
  14. // restore trash.
  15. requirejs(['activity/utils']);
  16. var TRASHWIDTH = 120;
  17. var TRASHHEIGHT = 120;
  18. function Trashcan () {
  19. this.isVisible = false;
  20. this._canvas = null;
  21. this._stage = null;
  22. this._size = null;
  23. this._refreshCanvas = null;
  24. this._scale = 1;
  25. this._iconsize = 55; // default value
  26. this._container = new createjs.Container();
  27. this._borderHighlightBitmap = null;
  28. this._isHighlightInitialized = false;
  29. this._inAnimation = false;
  30. this._animationInterval = null;
  31. this._highlightPower = 255;
  32. this._animationLevel = 0;
  33. this.animationTime = 500;
  34. this.init = function () {
  35. this._stage.addChild(this._container);
  36. this._stage.setChildIndex(this._container, 0);
  37. this.resizeEvent(1);
  38. this._makeTrash();
  39. };
  40. this.setCanvas = function (canvas) {
  41. this._canvas = canvas;
  42. return this;
  43. };
  44. this.setStage = function (stage) {
  45. this._stage = stage;
  46. return this;
  47. };
  48. this.setSize = function (size) {
  49. this._size = size;
  50. return this;
  51. };
  52. this.setRefreshCanvas = function (refreshCanvas) {
  53. this._refreshCanvas = refreshCanvas;
  54. return this;
  55. };
  56. this._makeBorderHighlight = function (isActive) {
  57. var img = new Image();
  58. var that = this;
  59. img.onload = function () {
  60. that._borderHighlightBitmap = new createjs.Bitmap(img);
  61. that._borderHighlightBitmap.scaleX = that._size / that._iconsize;
  62. that._borderHighlightBitmap.scaleY = that._size / that._iconsize;
  63. if (!that._isHighlightInitialized) {
  64. that._container.visible = false;
  65. that._isHighlightInitialized = true;
  66. } else {
  67. that._container.removeChildAt(that._container.children.length - 1);
  68. }
  69. that._container.addChild(that._borderHighlightBitmap);
  70. that._borderHighlightBitmap.visible = true;
  71. };
  72. var highlightString = 'rgb(' + this._highlightPower + ',' + this._highlightPower + ',' + this._highlightPower + ')';
  73. if (isActive) {
  74. // When trash is activated, warn the user with red highlight.
  75. highlightString = 'rgb(255, 0, 0)';
  76. }
  77. img.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(BORDER.replace('stroke_color', highlightString))));
  78. };
  79. this._makeBorder = function () {
  80. var img = new Image();
  81. var that = this;
  82. img.onload = function () {
  83. border = new createjs.Bitmap(img);
  84. bitmap.scaleX = that._size / that._iconsize;
  85. bitmap.scaleY = that._size / that._iconsize;
  86. that._container.addChild(border);
  87. that._makeBorderHighlight(false);
  88. };
  89. img.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(BORDER.replace('stroke_color', '#e0e0e0'))));
  90. };
  91. this._makeTrash = function () {
  92. var img = new Image();
  93. var that = this;
  94. img.onload = function () {
  95. bitmap = new createjs.Bitmap(img);
  96. that._container.addChild(bitmap);
  97. that._iconsize = bitmap.getBounds().width;
  98. bitmap.scaleX = that._size / that._iconsize;
  99. bitmap.scaleY = that._size / that._iconsize;
  100. bitmap.x = ((TRASHWIDTH - that._size) / 2) * bitmap.scaleX;
  101. bitmap.y = ((TRASHHEIGHT - that._size) / 2) * bitmap.scaleY;
  102. that._makeBorder();
  103. };
  104. img.src = 'images/trash.svg';
  105. };
  106. this.resizeEvent = function (scale) {
  107. this._scale = scale;
  108. this._container.x = ((this._canvas.width / this._scale) - TRASHWIDTH) / 2;
  109. this._container.y = (this._canvas.height / this._scale) - TRASHHEIGHT;
  110. };
  111. this.hide = function () {
  112. createjs.Tween.get(this._container).to({alpha: 0}, 200).set({visible: false});
  113. };
  114. this.show = function () {
  115. this.stopHighlightAnimation();
  116. createjs.Tween.get(this._container).to({alpha: 0.0, visible: true}).to({alpha: 1.0}, 200);
  117. };
  118. this.startHighlightAnimation = function () {
  119. if (this._inAnimation) {
  120. return;
  121. }
  122. this._inAnimation = true;
  123. var that = this;
  124. this._animationInterval = setInterval(function () {
  125. that._animationLevel += 20;
  126. if (that._animationLevel >= that.animationTime) {
  127. that.isVisible = true;
  128. that._makeBorderHighlight(true); // Make it active.
  129. that._refreshCanvas();
  130. clearInterval(that._animationInterval); // Autostop animation.
  131. return;
  132. }
  133. that._highlightPower = parseInt(255 - (255 * (that._animationLevel / that.animationTime)), 10);
  134. that._makeBorderHighlight(false);
  135. that._refreshCanvas();
  136. }, 20);
  137. this._switchHighlightVisibility(true);
  138. };
  139. this.stopHighlightAnimation = function () {
  140. if (!this._inAnimation) {
  141. return;
  142. }
  143. clearInterval(this._animationInterval);
  144. this._inAnimation = false;
  145. this.isVisible = false;
  146. this._animationLevel = 0;
  147. this._highlightPower = 255;
  148. this._makeBorderHighlight(false);
  149. this._switchHighlightVisibility(false);
  150. };
  151. this._switchHighlightVisibility = function (bool) {
  152. last(this._container.children).visible = bool;
  153. this._container.children[1].visible = !bool;
  154. this._container.visible = true;
  155. this._refreshCanvas();
  156. };
  157. this.overTrashcan = function (x, y) {
  158. var tx = this._container.x;
  159. var ty = this._container.y;
  160. if (x < tx) {
  161. return false;
  162. } else if (x > tx + TRASHWIDTH) {
  163. return false;
  164. }
  165. if (y < ty) {
  166. return false;
  167. }
  168. return true;
  169. };
  170. };