/
ScrollBar.js
714 lines (679 loc) · 25.6 KB
/
ScrollBar.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
var isOne = true;
var isSwaped = false;
item1Pos = new point(-1, -1);
item2Pos = new point(-1, -1);
function ScrollBar(posX, posY, orientation, arr, name) {
//Локализуем полученные данные внутри класса
this.name = name;
var barPositionX = posX;
var barPositionY = posY;
var backGroundPosX = barPositionX;
var backGroundPosY = barPositionY;
var backGroundW = 0;
var backGroundH = 0;
var barOrientation = orientation;
var arrayForBar = arr;
var lineCount = 1;
var backGroundAplha = 0.31;
var backGround = undefined;
var indicator = undefined;
var scrollBarCase = undefined;
var items = undefined;
this.itemW = 0
this.scrollRowCount = 0;
var caseVisible = false;
if (orientation == "Vertical") {
// backGroundH = gameSpaceH - (height - gameSpaceH);
// backGroundW = (height - gameSpaceH)*lineCount;
backGroundH = height; // / 100 * 100;
backGroundW = (height / 100 * 15) * lineCount;
}
if (orientation == "Horizontal") {
// backGroundW = gameSpaceW - (height - gameSpaceH);
// backGroundH = (height - (gameSpaceY+gameSpaceH))*lineCount;
backGroundH = (height / 100 * 15) * lineCount;
backGroundW = height / 100 * 70;
}
//методы для установкии получения координат скролл скроллБара
this.setPosition = function (x, y) {
barPositionX = x;
barPositionY = y;
}
this.getScrollBarCase = function () {
return scrollBarCase;
}
this.getPositionX = function () {
var tmpX = barPositionX;
return tmpX;
}
this.swapItems = function (item1, item2) {
var items = this.getArrayItems();
if (items !== undefined) {
items.move(items.indexOf(item1), items.indexOf(item2));
//var i = this.sortElements(this.getArrayItems(),this.GetBackGround());
// this.setArrayItems(i)
}
}
this.swapItemPosition = function (isTuped, item1, item2, selItemPos) {
if (isTuped) { //log("тапнули позиция выделенного элемента "+ selItemPos.x)
if (isOne && item2 !== undefined) { //log("зашли в первый раз")
item1Pos = selItemPos;
isOne = false;
}
if (item2 !== undefined) { //log("второй элемент не андефайн меняем позиции")
item2Pos = item2.getPositionC();
item1.setPositionC(item2Pos);
item2.setPositionC(item1Pos);
if (Math.floor(item2.getPositionC().y) == Math.floor(item1Pos.y)) { //log("установили новую позицию выделенному элементу")
item1Pos = item1.getPositionC();
}
isSwaped = true;
} //else log("второй элемент андефайн")
} else {
// log("отпустили тап")
if (item1Pos.y < 0) {
// log("item1Pos < 0")
item1.setPositionC(selItemPos);
} else {
if (isSwaped) {
item1.setPositionC(item1Pos);
isSwaped = false;
} else item1.setPositionC(selItemPos);
//log("item1Pos > 0 " + item1Pos.x + " selectItemPos " + selItemPos.x)
}
isOne = true;
item1.setAlpha(1);
}
}
this.isItemWhollyInGB = function (itemY, itemH) {
var bg = this.GetBackGround();
if (itemY + 10 >= bg.w && ((itemY + itemH) - 10 <= (bg.y + bg.h)))
return true;
else return false;
}
this.objectEntryC = function (obj1) {
var item;
var objX = Math.floor(obj1.getPositionC().y);
var items = this.getArrayItems();
if (items !== undefined) {
var rectW = 20;
OOP.forArr(items, function (el, i) {
if (el != obj1) {
var elX = Math.floor(el.getPositionC().y);
if ((objX < elX + rectW && objX > elX - rectW)) {
item = el;
}
}
});
return item;
}
}
this.getPositionY = function () {
var tmpY = barPositionY;
return tmpY;
}
//
this.setWidthScroll = function (w) {
backGround.setWidth(w)
items = this.sortElements(arrayForBar, this.GetBackGround());
indicator = new Indicator(this.GetBackGround());
this.setArrayItems(items);
}
this.setHeightScroll = function (h) {
backGround.setHeight(h)
items = this.sortElements(arrayForBar, this.GetBackGround());
indicator = new Indicator(this.GetBackGround());
this.setArrayItems(items);
}
//методы для работы с массивом элементов скролл бара
this.setArrayItems = function (arrayItems) {
arrayForBar = arrayItems;
}
this.initArrayItems = function (arrayToInit) {
//arrayForBar = arrayToInit;
items = this.sortElements(arrayToInit, backGround.getBackGround());
this.setArrayItems(items);
indicator = new Indicator(backGround.getBackGround());
}
this.getArrayItems = function () {
var tmpArray = arrayForBar;
return tmpArray;
}
this.AddItem = function (item) {
if (imte.type == "ImageObject()") //проверяем тип объекта, который передали для добавления
{
arrayForBar.push(item);
items = this.sortElements(arrayForBar, backGround.getBackGround());
this.setArrayItems(items);
} //else
//log("Тип передоваемого объекта не ImageObject()");
}
this.getItem = function (i) {
if (arrayForBar.length > 0 && (arrayForBar.length - 1) >= i)
return arrayForBar[i];
//else
//log("Всего элементов в массиве: " + arrayForBar.length + " Вы хотете получить из массива: " + i + " Элемент, правильно?");
}
this.removeItem = function (i) {
if (arrayForBar.length > 0 && (arrayForBar.length - 1) >= i) {
arrayForBar.splice(i, 1);
items = this.sortElements(arrayForBar, backGround.getBackGround());
this.setArrayItems(items);
}// else
//log("Всего элементов в массиве: " + arrayForBar.length + " Вы хотете удалить из массива: " + i + " Элемент, правильно?");
}
this.isEmptyBarArray = function () {
if (arrayForBar === undefined || arrayForBar.length <= 0)
return true;
else return false;
}
//
this.getIndicator = function () {
var tmpInd = indicator;
return tmpInd;
}
//Задний фон скороллбара, кнему будут привязаны элементы движения, изчезнования будут зависить от него, это не просто обычный фон, это основа бара
function BackGround(x, y, w, h) {
var bgX = x;
var bgY = y;
var bgW = w;
var bgH = h;
var alpha = 1;
// /e1edeb
var bgColor = "#000000";
if (orientation == "Vertical") {
bgW *= lineCount;
}
if (orientation == "Horizontal") {
bgH *= lineCount;
}
var tmpBackGround = game.newRoundRectObject({
x: bgX,
y: bgY,
w: bgW,
h: bgH,
radius: 0,
fillColor: bgColor
});
this.setWidth = function (w) {
tmpBackGround.w = w;
// items = sortElements(arrayForBar,this.GetBackGround());
// indicator = new Indicator(this.GetBackGround());
// this.setArrayItems(items);
}
this.setHeight = function (h) {
tmpBackGround.h = h;
}
this.setX = function (x) {
tmpBackGround.x = x;
}
this.setY = function (y) {
tmpBackGround.y = y;
}
this.setColor = function (color) {
tmpBackGround.fillColor = color;
}
this.setAlphaBG = function (alph) {
if (alph >= 0 && alph <= 1)
tmpBackGround.setAlpha(alph);
}
this.getBackGround = function () {
return tmpBackGround;
}
this.getActivityArea = function () {
var area = game.newRoundRectObject({
x: bgX,
y: bgY - bgH,
w: bgW + oneTileWidth,
h: bgH * 3,
radius: 20,
fillColor: bgColor
});
return area;
}
}
//кейс для скроллБара на вход получаем задний фон Скролла и левую и правую картинку дальше рисуем кейс соответствующим методом
//внимание!! рисовать кейс для бара нужно обязательно на слове выше чем сам скроллБар
function ScrollBarrCase(scrollBackGround, leftImg, rightImg) {
var X = scrollBackGround.x;
var Y = scrollBackGround.y;
var W = scrollBackGround.w;
var H = scrollBackGround.h;
var LImg = leftImg;
var RImg = rightImg;
var tmpH = 0;
var tmpX = 0;
var tmpY = 0;
var tmpW = 0;
if (orientation == "Vertical") {
tmpH = H;
tmpW = W;
tmpY = Y + H + W;
}
if (orientation == "Horizontal") {
tmpH = H;
tmpW = W;
tmpY = Y;
}
this.setLeftImg = function (img) {
/*if(img === undefined)
img = game.newImageObject({x: 0, y: 0, h: 0, w: 0, file: nonePath});*/
img.x = X;
img.y = Y - tmpW;
img.h = tmpW;
img.w = tmpW;
LImg = img;
}
this.setRightImg = function (img) {
img.x = X;
//scrollBackGround.h -= tmpW;
//H = scrollBackGround.h;
img.y = H + tmpW;
img.h = tmpW;
img.w = tmpW;
RImg = img;
}
this.setLeftImgVisible = function (visible) {
LImg.setVisible(visible);
}
this.setRightImgVisible = function (visible) {
RImg.setVisible(visible);
}
this.getLeftImg = function () {
var tmpLImg = LImg;
return tmpLImg;
}
this.getRightImg = function () {
var tmpRImg = RImg;
return tmpRImg;
}
this.getImgWidth = function () {
return tmpH;
}
if (LImg == undefined) {
var tmp = game.newImageObject({
x: 0,
y: 0,
h: 0,
w: 0,
file: nonePath
});
this.setLeftImg(tmp);
} else this.setLeftImg(LImg);
if (RImg == undefined) {
var tmp = game.newImageObject({
x: 0,
y: 0,
h: 0,
w: 0,
file: nonePath
});
this.setRightImg(tmp);
} else this.setRightImg(RImg);
this.DrawCase = function () {
if (LImg != undefined)
LImg.draw();
if (RImg != undefined)
RImg.draw();
}
}
//индикатор который показывает сколько в скоролле элементов полоской, на вход получает задний фон скролла и по соответствии него рисует полоску
function Indicator(scrollBackGround) {
var X = 0;
var Y = 0;
var W = 0;
var H = 0;
var barBackGroud = undefined;
var bar = undefined;
var tmpW = 0;
var tmpH = 0;
if (orientation == "Vertical") {
X = scrollBackGround.x;
Y = scrollBackGround.y;
W = scrollBackGround.w;
H = scrollBackGround.h;
tmpW = 5;
tmpH = H;
}
if (orientation == "Horizontal") {
X = scrollBackGround.x;
Y = scrollBackGround.y;
W = scrollBackGround.w;
H = scrollBackGround.h;
tmpW = W;
tmpH = 5;
}
this.setBarAlpha = function (alpha) {
if (indicator != undefined) {
if (alpha >= 0 && alpha <= 1)
bar.setAlpha(alpha);
}
}
this.setBarBackGroundAlpha = function (alpha) {
if (barBackGroud != undefined) {
if (alpha >= 0 && alpha <= 1)
barBackGroud.setAlpha(alpha);
}
}
this.setBarReletion = function () {
if (arrayForBar === undefined || arrayForBar.length == 0) return;
if (orientation == "Horizontal")
bar.w *= backGround.getBackGround().w / ((arrayForBar.length * (backGround.getBackGround().h / lineCount)) / lineCount);
else if (orientation == "Vertical")
bar.h *= backGround.getBackGround().h / ((arrayForBar.length * (backGround.getBackGround().w / lineCount)) / lineCount);
}
this.getBar = function () {
if (bar != undefined)
return bar;
}
this.setX = function (x) {
bar.x = x;
barBackGroud.x = x;
}
bar = game.newRoundRectObject({
x: X,
y: Y,
h: H,
w: W,
radius: 3,
fillColor: guiTextColor
});
bar.h = tmpH;
this.setBarReletion();
//bar.y = Y + H - bar.h;
if (name == "LEFT")
bar.y = Y + H - bar.h;
else bar.y = Y
bar.w = tmpW;
bar.x = (scrollBackGround.x + scrollBackGround.w) - bar.w;
if (orientation == "Horizontal")
barBackGroud = game.newRoundRectObject({
x: scrollBackGround.x,
y: bar.y,
h: tmpH,
w: tmpW,
radius: 3,
fillColor: "#01afc8"
});
else barBackGroud = game.newRoundRectObject({
x: bar.x,
y: Y,
h: tmpH,
w: tmpW,
radius: 3,
fillColor: "#01afc8"
});
this.DrawIndicator = function () {
if (arrayForBar != undefined) {
if (barOrientation == "Vertical") {
var tmp = (backGround.getBackGround().w / lineCount) * arrayForBar.length / lineCount; //видемые элементы, если больше то врубаем скролл
if (tmp > backGround.getBackGround().h) {
barBackGroud.draw();
bar.draw();
}
} else {
var tmp = (backGround.getBackGround().h / lineCount) * arrayForBar.length / lineCount; //видемые элементы, если больше то врубаем скролл
if (tmp > backGround.getBackGround().w) {
barBackGroud.draw();
bar.draw();
}
}
}
}
}
this.setLineCount = function (count) {
lineCount = count;
backGround = new BackGround(backGroundPosX, backGroundPosY, backGroundW, backGroundH);
items = this.sortElements(arrayForBar, backGround.getBackGround());
indicator = new Indicator(backGround.getBackGround());
this.setArrayItems(items);
backGround.setAlphaBG(backGroundAplha);
this.scrollUpdate(0);
}
this.getLineCount = function () {
var tmpCount = lineCount;
return tmpCount;
}
this.scrollToEnd = function () {
if (arrayForBar !== undefined) {
var BGRx = backGround.getBackGround().y + backGround.getBackGround().h;
var lastItemRx = arrayForBar[0].y + arrayForBar[0].h;
var dw = 0;
if (BGRx !== undefined && lastItemRx !== undefined) {
if (lastItemRx > BGRx) {
dw = lastItemRx - BGRx;
OOP.forArr(arrayForBar, function (el) {
el.move(point(0, -dw));
if (el.isIntersect(backGround.getBackGround())) {
el.setVisible(true);
} else el.setVisible(false)
});
}
}
}
}
this.sortElements = function (array, scrollBackGround) {
if (array === undefined || array.length <= 0)
return;
var X = 0;
var Y = 0;
var W = 0;
var H = 0;
var oldX = 0;
var oldY = 0;
var tmpH = 0;
var tmpX = 0;
var bgW = scrollBackGround.w;
var bgH = scrollBackGround.h;
var scrollLineCount = lineCount;
var sortArr = array;
var arrMediana = Math.ceil(sortArr.length / scrollLineCount);
var itemHW = 0;
if (orientation == "Vertical") {
X = scrollBackGround.x;
Y = scrollBackGround.y;
W = scrollBackGround.w;
H = scrollBackGround.h;
if (this.name != "LEFT") {
if (!isVerticalScreen)
scrollBackGround.h = (gameSpaceX + gameSpaceH) - 1;
}
tmpH = W;
// tmpH = (gameSpaceY+gameSpaceH)- (height / 100 * 15)
oldX = Y;
oldY = X;
tmpX = Y;
}
if (orientation == "Horizontal") {
X = scrollBackGround.x;
Y = scrollBackGround.y;
W = scrollBackGround.w;
H = scrollBackGround.h;
tmpH = H;
oldX = X;
oldY = Y;
tmpX = X;
}
//считываем сколько процентов размер беграунда составляет от общего размера, делим на 10 и берем по меньшей части через floor это и будет количество строк элементов
if (tmpH >= (height / 100 * 40)) {
//if(!isVerticalScreen)
//{
var d = tmpH * 100 / width;
if (!isVerticalScreen)
d = Math.floor(d / 10);
else d = Math.floor(d / 20);
scrollLineCount = d;
lineCount = d;
//}
arrMediana = Math.ceil(sortArr.length / scrollLineCount);
}
itemHW = tmpH / scrollLineCount;
this.itemW = itemHW;
//
var iter = 0;
var rowCount = 1;
OOP.forArr(sortArr, function (el, i) {
iter++;
if (el.name == "saveItem") {
el.setW(itemHW);
el.setH(itemHW);
} else {
el.w = itemHW;
el.h = itemHW;
}
if (orientation == "Vertical") {
if (el.name == "saveItem") {
el.setX(oldY);
el.setY(oldX);
} else {
el.x = oldY;
el.y = oldX;
}
if (iter % scrollLineCount == 0) {
rowCount++;
oldY = X - itemHW;
oldX += itemHW
iter = 0;
}
oldY += itemHW;
if (lineCount > 1) {}
} else {
if (el.name == "saveItem") {
el.setX(oldX);
el.setY(oldY);
} else {
el.x = oldX;
el.y = oldY;
}
oldX += itemHW;
}
if (el.isIntersect && !el.isIntersect(backGround.getBackGround())) {
el.setVisible(false);
}
});
this.scrollRowCount = rowCount;
return sortArr;
}
this.GetBackGround = function () {
if (backGround != undefined) {
return backGround.getBackGround();
}
}
//ВОЗВРАЩАЕТ ОБЛАСТЬ ВОКРУГ ЗАДНЕГО ФОНА СКРОЛА
this.GetActivityArea = function () {
if (backGround != undefined) {
return backGround.getActivityArea();
}
}
this.getOrientation = function () {
var tmpOrin = barOrientation;
return tmpOrin;
}
this.setCaseVisible = function (visible) {
if (lineCount == 1) {
if (visible) {
if (orientation == "Vertical") {
backGround.setY(backGroundPosY + backGroundW);
}
if (orientation == "Horizontal") {
backGround.setX(backGroundPosX + backGroundH);
}
scrollBarCase = new ScrollBarrCase(this.GetBackGround());
items = this.sortElements(arrayForBar, this.GetBackGround());
indicator = new Indicator(this.GetBackGround());
this.setArrayItems(items);
caseVisible = true;
} else {
caseVisible = false;
}
}
}
this.isCaseVisible = function () {
return caseVisible;
}
backGround = new BackGround(backGroundPosX, backGroundPosY, backGroundW, backGroundH);
if (arrayForBar != undefined) {
items = this.sortElements(arrayForBar, this.GetBackGround());
indicator = new Indicator(this.GetBackGround());
this.setArrayItems(items);
}
backGround.setAlphaBG(backGroundAplha);
this.scrollUpdate = function (scrollVal) {
if (!this.isEmptyBarArray()) {
var rXElLast = arrayForBar[0].x + arrayForBar[0].w; // координаты правого верхнего по Х угла 0 элемент
var rYElLast = arrayForBar[0].y + arrayForBar[0].w;
var FrsElemX = arrayForBar[arrayForBar.length - 1].x;
var FrsElemY = arrayForBar[arrayForBar.length - 1].y;
var rXBG = backGround.getBackGround().x + backGround.getBackGround().w; // координаты правого верхнегопо Х заднего
var rYBG = backGround.getBackGround().y + backGround.getBackGround().h;
var dx = 0;
var dy = 0;
var ctxtW = (arrayForBar.length / lineCount) * arrayForBar[0].w;
var ctxtH = arrayForBar.length / lineCount * arrayForBar[0].w;
indicator.setBarAlpha(1);
this.lastElRX = rXElLast;
this.fristElX = FrsElemX;
//обход всех дочерных элементов (графические элементы меню)
OOP.forArr(arrayForBar, function (el) {
if (barOrientation === "Horizontal") {
if (scrollVal.x > 0) {
if (FrsElemX < backGround.getBackGround().x) {
dx = scrollVal.x * scrollSpeed;
if (FrsElemX + dx > backGround.getBackGround().x) {
dx = 0;
}
}
} else if (scrollVal.x < 0) {
if (rXElLast >= rXBG) {
dx = scrollVal.x * scrollSpeed;
if (rXElLast + dx < rXBG) {
var dSpeed = rXBG - (rXElLast + dx)
dx = 0;
}
}
}
el.x += dx; // перемещаем элементы по Х с динамической скорость
if (el.isIntersect(backGround.getBackGround())) {
el.setVisible(true);
} else el.setVisible(false)
} else {
if (scrollVal.y > 0) {
if (FrsElemY < backGround.getBackGround().y) {
dy = scrollVal.y * scrollSpeed;
if (FrsElemY + dy > backGround.getBackGround().y) {
dy = backGround.getBackGround().y - FrsElemY;
}
}
} else if (scrollVal.y < 0) {
if (rYElLast >= rYBG) {
dy = scrollVal.y * scrollSpeed;
if (rYElLast + dy < rYBG) {
dy = rYBG - rYElLast;
}
}
}
el.y += dy;
if (el.isIntersect(backGround.getBackGround())) {
el.setVisible(true);
} else el.setVisible(false)
}
});
indicator.getBar().x += (this.GetBackGround().w / 100) * -((dx / ctxtW) * 100);
indicator.getBar().y += (this.GetBackGround().h / 100) * -((dy / ctxtH) * 100);
}
}
this.DrawScrollBar = function () {
this.GetBackGround().draw();
if (arrayForBar != undefined) {
OOP.forArr(arrayForBar, function (el) {
if (el.isInCameraStatic()) {
el.draw();
}
});
indicator.DrawIndicator();
}
if (this.isCaseVisible()) {
scrollBarCase.DrawCase();
}
}
}