forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trash.js
255 lines (223 loc) · 7.32 KB
/
trash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) 2014-2021 Walter Bender
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
// The trashcan is an area at the bottom of the screen where stacks of
// blocks can be dragged. Once in the trash area, they are marked as
// trash and hidden. There is a menu button that can be used to
// restore trash.
/* global createjs, platformColor, BORDER, TRASHICON, last */
/* exported Trashcan */
class Trashcan {
static TRASHWIDTH = 120;
static TRASHHEIGHT = 120;
/**
* @constructor
*/
constructor(activity) {
this.activity = activity;
this.isVisible = false;
this._scale = 1;
this._iconsize = 55; // default value
this._container = new createjs.Container();
this._borderHighlightBitmap = null;
this._isHighlightInitialized = false;
this._inAnimation = false;
this._animationInterval = null;
this._highlightPower = 255;
this._animationLevel = 0;
this.animationTime = 500;
this.activity.trashContainer.addChild(this._container);
this.activity.trashContainer.setChildIndex(this._container, 0);
this.resizeEvent(1);
this._makeTrash();
}
/**
* @private
* @param {boolean} isActive
* @returns {void}
*/
_makeBorderHighlight(isActive) {
const img = new Image();
img.onload = () => {
this._borderHighlightBitmap = new createjs.Bitmap(img);
this._borderHighlightBitmap.scaleX = this.activity.cellSize / this._iconsize;
this._borderHighlightBitmap.scaleY = this.activity.cellSize / this._iconsize;
if (!this._isHighlightInitialized) {
this._container.visible = false;
this._isHighlightInitialized = true;
} else {
this._container.removeChildAt(this._container.children.length - 1);
}
this._container.addChild(this._borderHighlightBitmap);
this._borderHighlightBitmap.visible = true;
};
let highlightString =
"rgb(" +
this._highlightPower +
"," +
this._highlightPower +
"," +
this._highlightPower +
")";
if (isActive) {
// When trash is activated, warn the user with red highlight.
highlightString = platformColor.trashActive;
}
img.src =
"data:image/svg+xml;base64," +
window.btoa(
unescape(encodeURIComponent(BORDER.replace("stroke_color", highlightString)))
);
}
/**
* @private
* @returns {void}
*/
_makeBorder() {
const img = new Image();
img.onload = () => {
const border = new createjs.Bitmap(img);
border.scaleX = this.activity.cellSize / this._iconsize;
border.scaleY = this.activity.cellSize / this._iconsize;
this._container.addChild(border);
this._makeBorderHighlight(false);
};
img.src =
"data:image/svg+xml;base64," +
window.btoa(
unescape(
encodeURIComponent(BORDER.replace("stroke_color", platformColor.trashBorder))
)
);
}
/**
* @private
* @returns {void}
*/
_makeTrash() {
const img = new Image();
img.onload = () => {
const bitmap = new createjs.Bitmap(img);
this._container.addChild(bitmap);
this._iconsize = bitmap.getBounds().width;
bitmap.scaleX = this.activity.cellSize / this._iconsize;
bitmap.scaleY = this.activity.cellSize / this._iconsize;
bitmap.x = ((Trashcan.TRASHWIDTH - this.activity.cellSize) / 2) * bitmap.scaleX;
bitmap.y = ((Trashcan.TRASHHEIGHT - this.activity.cellSize) / 2) * bitmap.scaleY;
this._makeBorder();
};
img.src =
"data:image/svg+xml;base64," +
window.btoa(
unescape(
encodeURIComponent(TRASHICON.replace(/fill_color/g, platformColor.trashBorder))
)
);
}
/**
* @public
* @param {number} scale
* @returns {void}
*/
resizeEvent(scale) {
this._scale = scale;
this._container.x = (this.activity.canvas.width / this._scale - Trashcan.TRASHWIDTH) / 2;
this._container.y = this.activity.canvas.height / this._scale - Trashcan.TRASHHEIGHT;
}
/**
* @public
* @returns {void}
*/
hide() {
createjs.Tween.get(this._container).to({ alpha: 0 }, 200).set({ visible: false });
}
/**
* @public
* @returns {void}
*/
show() {
this.stopHighlightAnimation();
createjs.Tween.get(this._container)
.to({ alpha: 0.0, visible: true })
.to({ alpha: 1.0 }, 200);
}
/**
* @public
* @returns {void}
*/
startHighlightAnimation() {
if (this._inAnimation) {
return;
}
this._inAnimation = true;
this._animationInterval = setInterval(() => {
this._animationLevel += 20;
if (this._animationLevel >= this.animationTime) {
this.isVisible = true;
this._makeBorderHighlight(true); // Make it active.
this.activity.refreshCanvas();
clearInterval(this._animationInterval); // Autostop animation.
return;
}
this._highlightPower = parseInt(
255 - 255 * (this._animationLevel / this.animationTime),
10
);
this._makeBorderHighlight(false);
this.activity.refreshCanvas();
}, 20);
this._switchHighlightVisibility(true);
}
/**
* @public
* @returns {void}
*/
stopHighlightAnimation() {
if (!this._inAnimation) {
return;
}
clearInterval(this._animationInterval);
this._inAnimation = false;
this.isVisible = false;
this._animationLevel = 0;
this._highlightPower = 255;
this._makeBorderHighlight(false);
this._switchHighlightVisibility(false);
}
/**
* @private
* @returns {void}
*/
_switchHighlightVisibility(bool) {
last(this._container.children).visible = bool;
this._container.children[1].visible = !bool;
this._container.visible = true;
this.activity.refreshCanvas();
}
/**
* @public
* @param {number} x - x coordinate
* @param {number} y - y coordinate
* @returns {boolean}
*/
overTrashcan(x, y) {
const tx = this._container.x;
const ty = this._container.y;
if (x < tx) {
return false;
} else if (x > tx + Trashcan.TRASHWIDTH) {
return false;
}
if (y < ty) {
return false;
}
return true;
}
}