difenduandada
2024-12-31 34abe6963b344c882358274957f4b992456fee40
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
"use strict";
cc._RF.push(module, '0f354SBZypEzpnpwVRFIkg5', 'redbag_list_panel');
// Scripts/mod/redbag/view/redbag_list_panel.js
 
"use strict";
 
// --------------------------------------------------------------------
// @author: shiraho@syg.com(必填, 创建模块的人员)
// @description:
//      无线滚动容器,子对象必须是锚点0.5,0.5, 而且一定要继承 basepanel
// <br/>Create: new Date().toISOString()
// --------------------------------------------------------------------
var LoaderManager = require("loadermanager");
 
var PathTool = require("pathtool");
 
var RedBagListPanel = cc.Class({
  "extends": BaseClass,
  ctor: function ctor() {
    this.initConfig();
  },
  initConfig: function initConfig() {
    this.parent = null;
    this.prefabPath = PathTool.getPrefabPath("common", "common_scroll_view");
    this.pos = cc.v2(0, 0);
    this.dir = ScrollViewDir.vertical;
    this.start_pos = ScrollViewStartPos.top;
    this.size = cc.size(100, 100);
    this.ap = cc.v2(0, 0);
    this.total_cache_list = []; // 因为滚动容器可能每次存放不同的对象,因为多标签的时候
 
    this.item_click_callback = null;
    this.extend = null; //扩展参数
 
    this.cur_item_class = null; //当前显示的对象
 
    this.is_run = false;
    this.item_num = 0;
    this.move_over_index = 0;
    this.posx_list = {
      "1": this.size.width / 2,
      "2": this.size.width / 2 + 140,
      "3": this.size.width / 2 - 140
    };
    this.cur_min_index = 0;
  },
 
  /**
   * 创建
   * @param {*} parent 所在父节点
   * @param {*} pos 滑动组件位置
   * @param {*} dir 滑动对齐方式
   * @param {*} start_pos 滑动列表开始位置
   * @param {*} size 滑动框大小
   * @param {*} setting 设置信息
   * @param {*} ap 锚点
   */
  createScroll: function createScroll(parent, pos, dir, start_pos, size, setting, ap) {
    this.parent = parent;
    this.pos = pos || cc.v2(0, 0);
    this.dir = dir || ScrollViewDir.vertical;
    this.start_pos = start_pos || ScrollViewStartPos.top;
    this.size = size || cc.size(100, 100);
    this.ap = ap || cc.v2(0.5, 0.5);
    this.analysisSetting(setting);
    this.createRootWnd();
  },
  // 初始化创建对象
  createRootWnd: function createRootWnd() {
    var node = new cc.Node();
    this.container = new cc.Node("container");
    this.container.setParent(node);
    this.root_wnd = node;
    this.root_wnd.setParent(this.parent);
    this.root_wnd.setContentSize(this.size);
    this.root_wnd.setAnchorPoint(this.ap);
    this.root_wnd.setPosition(this.pos.x, this.pos.y);
    this.registerEvent();
  },
  registerEvent: function registerEvent() {
    var self = this;
    var eventTarget = new cc.EventTarget();
    this.touchListener = cc.EventListener.create({
      event: cc.EventListener.TOUCH_ONE_BY_ONE,
 
      /*
      可选event类型列表:
      cc.EventListener.TOUCH_ONE_BY_ONE (单点触摸)
      cc.EventListener.TOUCH_ALL_AT_ONCE (多点触摸)
      cc.EventListener.KEYBOARD (键盘)
      cc.EventListener.MOUSE (鼠标)
      cc.EventListener.ACCELERATION (加速计)
      cc.EventListener.CUSTOM (自定义)
      */
      onTouchBegin: function onTouchBegin(touch, event) {
        self.last_point = null;
        self.is_move = false;
 
        if (self.cur_item_class == null) {
          return false;
        }
 
        if (self.screenSize == null) {
          var pos = self.convertToWorldSpace(cc.p(0, 0));
          self.screenSize = cc.rect(pos.x, pos.y, self.size.width, self.size.height);
        }
 
        var pos = cc.v2(touch.getLocation().x, touch.getLocation().y);
 
        if (!cc.rectContainsPoint(self.screenSize, pos)) {
          // 判断触摸点是否在按钮范围内
          return false;
        }
 
        return true;
      },
      onTouchMoved: function onTouchMoved(touch, event) {
        self.last_point = touch.getDelta();
        self.moveContainer(self.last_point);
      },
      onTouchEnded: function onTouchEnded() {
        self.is_move = false;
        if (self.last_point == null) return;
      }
    });
    eventTarget.on(this.touchListener, this.container);
  },
  //移动
  moveContainer: function moveContainer(pos) {
    var target_pos = this.checkPosition(pos.x, pos.y);
    var is_left = pos.x < 0;
 
    if (Math.abs(pos.x) >= 5 && this.is_move != true) {
      this.is_move = true;
      this.is_run = true;
      var cache_type_list = this.total_cache_list[this.cur_item_class];
 
      for (var i = 1; i <= this.item_num; i++) {
        var item = cache_type_list.item[i];
        this.setMoveItemPos(item, is_left);
      }
    }
  },
  //跳转指定位置
  jumpToMove: function jumpToMove(pos, time, callback) {
    var target_pos = this.checkPosition(pos.x, pos.y);
    time = time || 1;
    var move_to = cc.moveTo.create(time, cc.v2(target_pos.x, target_pos.y));
    this.container.runAction(cc.sequence.create(move_to, cc.callFunc.create(function () {
      if (callback) {
        callback();
      }
    })));
  },
  //设置数据
  setData: function setData(data_list, click_callback, extend) {
    this.clearCacheList();
 
    if (data_list == null || Utils.next(data_list) == null) {
      return;
    } // 转换一下存储,保证不改变原数据
    // var switchList = []
    // for (let index = 0; index < data_list.length; index++) {
    //     const element = data_list[index];
    //     switchList.push({ data: element, index: index })
    // }
 
 
    this.item_click_callback = click_callback;
    this.data_list = data_list;
    this.extend = extend;
    var size = data_list.length;
    this.item_num = size;
    var container_width = this.size.width;
    var container_height = this.size.height;
    var num = 0;
 
    if (this.dir == ScrollViewDir.horizontal) {
      num = Math.ceil(size / this.row);
      container_width = num * this.item_width + 2 * this.start_y + (num - 1) * this.space_x;
    } else {
      num = Math.ceil(size / this.col);
      container_height = num * this.item_height + 2 * this.start_y + (num - 1) * this.space_y;
    }
 
    container_width = Math.max(container_width, this.size.width);
    container_height = Math.max(container_height, this.size.height);
    this.container_size = cc.size(container_width, container_height);
    this.container.setContentSize(this.container_size);
 
    if (this.start_pos > 0) {
      var height = Math.max(0 - container_height * this.start_pos + 35, this.size.height - this.container_size.height);
      this.container.setPosition(cc.v2(0, height));
    } else if (this.start_pos == 0) {
      this.container.setPosition(cc.v2(0, 0));
    } //储存一下当前需要的单元类
 
 
    this.cur_item_class = this.item_class;
    var index = 0;
    var once_num = this.once_num || 1;
    var data = null;
 
    if (this.time_ticket == null && Utils.next(this.data_list || {}) != null) {
      this.time_ticket = gcore.Timer.set(function () {
        if (this.container) {
          if (this.data_list == null) {
            if (this.end_callBack) {
              this.end_callBack();
            }
 
            this.clearTimeTicket();
          } else {
            for (var i = index; i < index + once_num; i++) {
              var data = this.data_list[i];
 
              if (data != null) {
                // if (data._index == null) {
                //     data._index = i;
                // }
                this.createList(data);
              }
            }
 
            index = index + once_num;
 
            if (index > size) {
              if (this.end_callBack) {
                this.end_callBack();
              }
 
              this.clearTimeTicket();
            }
          }
        } else {}
      }.bind(this), this.delay * 10, -1);
    }
  },
  clearTimeTicket: function clearTimeTicket() {
    if (this.time_ticket != null) {
      gcore.Timer.del(this.time_ticket);
      this.time_ticket = null;
    }
  },
 
  /**
   * 暂停计时器,以及回收掉之前创建的对象
   */
  clearCacheList: function clearCacheList() {
    this.clearTimeTicket();
 
    if (this.cur_item_class != null) {
      var cache_type_list = this.total_cache_list[this.cur_item_class] || {};
 
      if (cache_type_list != null || Utils.next(cache_type_list) != null) {
        for (var i in cache_type_list.item) {
          var item = cache_type_list.item[i];
          item.setVisible(false);
          cache_type_list.pool.push(item);
        }
      }
 
      cache_type_list.item = {};
    }
 
    this.cur_item_class = null;
  },
  addEndCallBack: function addEndCallBack(call_back) {
    this.end_callBack = call_back;
  },
 
  /**
   * 创建具体事例
   * @param {*} data 这个数据是转换之后的数据,因为为了不改变数据结构,所以转换了 包含 data 和 index,其中data才是真实数据
   */
  createList: function createList(switch_data) {
    var data = switch_data;
    if (this.cur_item_class == null) return;
 
    if (this.total_cache_list[this.cur_item_class] == null) {
      this.total_cache_list[this.cur_item_class] = {};
    }
 
    var cache_type_list = this.total_cache_list[this.cur_item_class];
 
    if (cache_type_list.item == null) {
      cache_type_list.item = [];
    }
 
    if (cache_type_list.pool == null) {
      cache_type_list.pool = [];
    }
 
    var item = null;
 
    if (Utils.next(cache_type_list.pool) == null) {
      item = new this.item_class(true, true, cc.size(this.item_width, this.item_height));
      if (item.setSwallowTouches) item.setSwallowTouches(false); // item.setScale(this.scale);
 
      item.setParent(this.container);
      item.show();
    } else {
      item = cache_type_list.splice(cache_type_list.pool, 0);
    }
 
    cache_type_list.item.push(item);
    var cur_item_index = cache_type_list.item.length;
    item.item_index = cur_item_index;
 
    if (this.extend != null && item.setExtendData) {
      item.setExtendData(this.extend);
    }
 
    this.setItemPosition(item, switch_data.index); // 回调方法
 
    if (this.item_click_callback != null) {
      if (item.addCallBack) {
        item.addCallBack(this.item_click_callback);
      }
    }
 
    item.setData(data);
  },
  //设置当前对象的位置,根据数据的临时_index去确定
  setItemPosition: function setItemPosition(item) {
    var cache_type_list = this.total_cache_list[this.cur_item_class];
    if (cache_type_list == null || cache_type_list.item == null) return;
    var cur_item_index = cache_type_list.item.length;
    var anchor_point = cc.v2(0.5, 0.5);
    var _x = 0;
    var _y = 0;
 
    if (this.dir == ScrollViewDir.horizontal) {
      _x = this.posx_list[cur_item_index];
      _y = this.container_size.height - (this.start_y + this.item_height * (1 - anchor_point.y) + (cur_item_index - 1) % this.row * (this.item_height + this.space_y));
    }
 
    item.setPosition(_x, _y);
 
    if (cur_item_index == 1) {
      item.setLocalZOrder(10);
      item.showBlackBg(false);
      item.setScale(1.05);
      this.select_item = item;
    } else {
      item.setLocalZOrder(1);
      item.showBlackBg(true);
      item.setScale(0.85);
    }
  },
  //获取已创建的全部对象
  getItemList: function getItemList() {
    var item_list = [];
 
    for (var index = 0; index < this.total_cache_list.length; index++) {
      var element = this.total_cache_list[index];
      item_list.push(element);
    }
 
    return item_list;
  },
  setMoveItemPos: function setMoveItemPos(item, is_left) {
    var cache_type_list = this.total_cache_list[this.cur_item_class];
    if (cache_type_list == null || cache_type_list.item == null) return;
    var cur_item_index = item.item_index || 0;
 
    if (is_left == true) {
      if (cur_item_index <= 1) {
        cur_item_index = this.item_num;
      } else {
        cur_item_index = cur_item_index - 1;
      }
    } else {
      if (cur_item_index >= this.item_num) {
        cur_item_index = 1;
      } else {
        cur_item_index = cur_item_index + 1;
      }
    }
 
    item.item_index = cur_item_index;
    var anchor_point = cc.v2(0.5, 0.5);
    var _x = 0;
    var _y = 0;
 
    if (this.dir == ScrollViewDir.horizontal) {
      _x = this.posx_list[cur_item_index];
      _y = this.container_size.height - (this.start_y + this.item_height * (1 - anchor_point.y) + (cur_item_index - 1) % this.row * (this.item_height + this.space_y));
    }
 
    var scale = 1;
 
    if (cur_item_index == 1) {
      item.setLocalZOrder(10);
      item.showBlackBg(false);
      scale = 1.05;
      this.select_item = item;
    } else {
      item.setLocalZOrder(1);
      item.showBlackBg(true);
      scale = 0.85;
    }
 
    var move_to = cc.moveTo(0.3, cc.v2(_x, _y));
    var scale_start = cc.scaleTo(0, 1);
    var scale_to = cc.scaleTo(0.3, scale);
    item.runActionFunc(cc.sequence(scale_start, scale_to));
    var self = this;
    item.runActionFunc(cc.sequence(move_to, cc.callFunc(function () {
      self.is_run = false;
 
      if (self.end_callBack) {
        self.end_callBack();
      }
    })));
  },
  //外部按钮左移动一个
  runLeftPostion: function runLeftPostion() {
    if (this.is_run == true) return;
    var cache_type_list = this.total_cache_list[this.cur_item_class];
    this.is_run = true;
 
    for (var i = 0; i < this.item_num; i++) {
      var item = cache_type_list.item[i];
      this.setMoveItemPos(item, true);
    }
  },
  //外部按钮右移动一个
  runRightPostion: function runRightPostion() {
    if (this.is_run == true) return;
    this.is_run = true;
    var cache_type_list = this.total_cache_list[this.cur_item_class];
 
    for (var i = 0; i < this.item_num; i++) {
      var item = cache_type_list.item[i];
      this.setMoveItemPos(item, false);
    }
  },
  getSelectItem: function getSelectItem() {
    return this.select_item;
  },
  setSelectItem: function setSelectItem(item) {
    if (item) this.select_item = item;
  },
  //解析数据
  analysisSetting: function analysisSetting(setting) {
    this.setting = setting || {};
    this.item_class = this.setting.item_class;
    this.start_x = this.setting.start_x || 0; //第一个单元的起点X
 
    this.space_x = this.setting.space_x || 3; // 横向间隔空间
 
    this.start_y = this.setting.start_y || 0; // 第一个单元的起点Y
 
    this.space_y = this.setting.space_y || 3; // 竖向间隔空间
 
    this.item_width = this.setting.item_width || 115; // 单元的宽度
 
    this.item_height = this.setting.item_height || 115; // 单元的高度
 
    this.is_radian = this.setting.is_radian || false; //是否要弧度
 
    this.row = this.setting.row || 5; // 行数,作用于水平方向的滚动
 
    this.col = this.setting.col || 5; // 列数,作用于垂直方向的滚动
 
    this.delay = this.setting.delay || 4; // 创建延迟时间
 
    this.once_num = this.setting.once_num || 1; // 每次创建的数量
 
    this.scale = this.setting.scale || 1; //缩放值
  },
  getMaxSize: function getMaxSize() {
    return this.container_size;
  },
  getContainer: function getContainer() {
    return this.container;
  },
  deleteMe: function deleteMe() {
    this.DeleteMe();
  },
  //移除对象
  DeleteMe: function DeleteMe() {
    // doStopAllActions(self.container);    //不清楚H5还需不需要安全判断,先注释掉
    this.clearTimeTicket();
 
    for (var k in this.total_cache_list) {
      var v = this.total_cache_list[k];
 
      for (var i in v) {
        var list = v[i];
 
        for (var j in list) {
          if (list[j].deleteMe) {
            list[j].deleteMe();
            list[j] = null;
          }
        }
      }
    }
 
    this.total_cache_list = null;
    this.root_wnd.destroy();
    this.root_wnd = null;
    this.container.destroy();
    this.container = null;
  }
});
 
cc._RF.pop();