-
Notifications
You must be signed in to change notification settings - Fork 58
/
lodeRunner.colorSelector.js
333 lines (282 loc) · 8.14 KB
/
lodeRunner.colorSelector.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
function colorSelectorClass(_screenX1, _screenY1, baseX)
{
var radius = baseX*3/4|0;
var px = (baseX *7/5)|0;
var py = (baseX *7/5)|0;
var left = screenBorder;
var top = baseX/4|0;
var colorCanvas, colorStage;
var colorSelector;
var colorShape=[];
var activeCircle;
var smallScale = 0.4;
var colorSelectorIsSmall = 0;
var outFocusTimer = null;
var colorStageTicker = null;
var self = this;
var enabled = 0;
var bgColor= backgroundColor;
init();
function init()
{
createColorCanvas();
createColorSelector();
}
this.enable = function ()
{
if(enabled) return;
enabled = 1;
colorSelector.set({alpha:1});
colorStage.enableMouseOver(30);
colorStage.update();
}
this.disable = function (hidden)
{
enabled = 0;
if(hidden) {
colorSelector.set({alpha:0});
} else {
colorSelector.set({alpha:1})
}
colorStage.enableMouseOver(0);
colorStage.update();
}
this.themeChange = function()
{
for(var i=0; i < maxThemeColor;i++) {
setColorShape(colorShape[i], i, pinker(getThemeTileColor(i)) );
}
setActiveCircle();
colorStage.update();
}
this.activeColorChange = function()
{
setActiveCircle();
colorStage.update();
}
function createColorCanvas()
{
colorCanvas = document.createElement('canvas');
colorCanvas.id = "theme_color_selector";
colorCanvas.width = px * 2;
colorCanvas.height = py * 2;
colorCanvas.style.left = left + "px";
colorCanvas.style.top = top + "px";
colorCanvas.style.position = "absolute";
document.body.appendChild(colorCanvas);
}
function createColorSelector()
{
var bgCircle = new createjs.Shape();
colorStage = new createjs.Stage(colorCanvas);
colorSelector = new createjs.Container();
bgCircle.graphics.beginFill(bgColor).drawCircle(px,py, radius*5/3|0);
colorSelector.addChild(bgCircle);
//colorSelector.regY =radius*5/3*2;
//colorSelector.y = radius*5/3*2;
addColorShape();
setColorSelectorMouseEvent();
colorSelectorTransform(1);
addActiveCircle();
setActiveCircleMouseEvent();
colorSelector.set({alpha:0}); //disabled
colorStage.addChild(colorSelector);
colorStage.update();
}
//= 2 =========================
function addColorShape()
{
for(var i=0; i < maxThemeColor;i++) createColorShape(i);
for(var i=0; i < maxThemeColor;i++) createBGBar(i);
function createColorShape(id)
{
var color = pinker( getThemeTileColor(i));
colorShape[id] = new createjs.Shape();
setColorShape(colorShape[id], id, color);
setColorShapeMouseEvent(colorShape[id], id)
colorSelector.addChild(colorShape[id]);
}
function createBGBar(id)
{
var s = Math.PI*2/5 + (2*Math.PI) * id/5;
var bar = new createjs.Shape();
bar.graphics.setStrokeStyle(baseX/6|0)
.beginStroke(bgColor).moveTo(px, py)
.lineTo(px+ Math.sin(s)*radius*3/2, py+Math.cos(s)*radius*3/2)
.endStroke();
colorSelector.addChild(bar);
}
}
function setColorSelectorMouseEvent()
{
colorSelector.on("mouseover", mouseOver);
colorSelector.on("mouseout", mouseOut);
colorSelector.on("click", mouseClick);
function mouseOver()
{
if(gameState == GAME_PAUSE || (gameState == GAME_WAITING && playMode != PLAY_EDIT)) {
colorSelector.cursor = "default";
return;
}
if(colorSelectorIsSmall) {
colorSelector.cursor = "pointer";
return;
}
setTransformTimeout(0); //clear timeout
}
function mouseOut()
{
if(colorSelectorIsSmall) return;
setTransformTimeout(1); //set timeout
}
function mouseClick()
{
if(gameState == GAME_PAUSE || (gameState == GAME_WAITING && playMode != PLAY_EDIT)) return;
if(!colorSelectorIsSmall) return;
colorSelectorTransform(0);
}
}
function addActiveCircle()
{
activeCircle = new createjs.Shape();
colorSelector.addChild(activeCircle);
setActiveCircle();
}
function setActiveCircleMouseEvent()
{
activeCircle.on("mouseover", function(){
if(gameState == GAME_PAUSE || (gameState == GAME_WAITING && playMode != PLAY_EDIT)) return;
if(colorSelectorIsSmall) return;
colorSelector.cursor = "pointer";
});
activeCircle.on("mouseout", function(){
if(colorSelectorIsSmall) return;
colorSelector.cursor = "default";
});
activeCircle.on("click",function() {
if(gameState == GAME_PAUSE || (gameState == GAME_WAITING && playMode != PLAY_EDIT)) return;
if(colorSelectorIsSmall) return;
setTransformTimeout(0); //clear timeout
colorSelectorTransform(0); //transform to small
});
}
function colorSelectorTransform(immediate)
{
if(colorSelectorIsSmall) {
colorCanvas.width = px * 2;
colorCanvas.height = py * 2;
if(immediate) {
colorSelector.scaleX = colorSelector.scaleY = 1;
colorSelectorIsSmall = 0;
colorSelector.cursor = "default";
} else {
createjs.Tween.get(colorSelector)
.to({scaleX: 1, scaleY: 1}, 100)
.call(function(){
colorSelectorIsSmall = 0;
setActiveCircle();
colorSelector.cursor = "default";
createjs.Ticker.off("tick", colorStageTicker);
colorStageTicker = null;
colorStage.update();
});
colorStageTicker = createjs.Ticker.on("tick", colorStage);
}
} else {
if(immediate) {
colorSelector.scaleX = colorSelector.scaleY = smallScale;
colorSelectorIsSmall = 1;
colorSelector.cursor = "pointer";
colorCanvas.width = px * 2 * smallScale;
colorCanvas.height = py * 2 * smallScale;
} else {
createjs.Tween.get(colorSelector)
.to({scaleX: smallScale, scaleY: smallScale}, 100)
.call(function(){
colorCanvas.width = px * 2 * smallScale;
colorCanvas.height = py * 2 * smallScale;
colorSelectorIsSmall = 1;
//setActiveCircle();
//self.themeChange();
colorSelector.cursor = "pointer";
createjs.Ticker.off("tick", colorStageTicker);
colorStageTicker = null;
colorStage.update();
});
colorStageTicker = createjs.Ticker.on("tick", colorStage);
}
}
}
//= 3 ===========================
function pinker(hexColor)
{
var orgRGB = hexToRGB(hexColor);
var percent = 1/2, whitePercent = 1-percent;
for(var i=0; i < 3; i++) {
orgRGB[i] = (orgRGB[i]*percent+255*whitePercent)|0;
if(orgRGB[i]>255) orgRGB[i]=255;
}
return rgbToHex(orgRGB)
}
function setColorShape(shape, id, color)
{
var s = (2*Math.PI) * id/5-(Math.PI)*7/10+(Math.PI)/40;
var e = (2*Math.PI) * (id+1)/5-(Math.PI)*7/10-(Math.PI)/40;
shape.graphics.clear().setStrokeStyle(radius-2)
.beginStroke(color).arc(px, py, radius, s, e).endStroke();
}
function setColorShapeMouseEvent(shape, id)
{
shape.on("mouseover", mouseOver, null, false, {id:id});
shape.on("mouseout", mouseOut, null, false, {id:id});
shape.on("click", mouseClick, null, false, {id:id});
function mouseOver(evt, data)
{
var id = data.id;
var curColorId = getCurColorId();
if(gameState == GAME_PAUSE || (gameState == GAME_WAITING && playMode != PLAY_EDIT)) return;
if(colorSelectorIsSmall) return;
colorSelector.cursor = "pointer";
setColorShape(colorShape[id], id, getThemeTileColor(id));
colorStage.update();
}
function mouseOut(evt, data)
{
var id = data.id;
var curColorId = getCurColorId();
if(gameState == GAME_PAUSE || (gameState == GAME_WAITING && playMode != PLAY_EDIT)) return;
if(colorSelectorIsSmall) return;
colorSelector.cursor = "default";
setColorShape(colorShape[id], id, pinker(getThemeTileColor(id)));
colorStage.update();
}
function mouseClick(evt, data)
{
var id = data.id;
var curColorId = getCurColorId();
if(gameState == GAME_PAUSE || (gameState == GAME_WAITING && playMode != PLAY_EDIT)) return;
if(colorSelectorIsSmall || curColorId == id) return;
themeColorChange(id);
setActiveCircle();
colorStage.update();
}
}
function setTransformTimeout(enable)
{
//clear timeout
if(outFocusTimer) {
clearTimeout(outFocusTimer);
outFocusTimer = null;
}
//set timeout
if(enable) {
outFocusTimer = setTimeout(function(){ colorSelectorTransform(0); }, 1000);
}
}
function setActiveCircle()
{
var color = getThemeTileColor(getCurColorId());
activeCircle.graphics.clear().beginFill(color).drawCircle(px,py, radius*3/8|0);
//activeCircle.alpha = colorSelectorIsSmall?0:1;
}
}