first commit
This commit is contained in:
123
assets/scripts/utils/BgScale.ts
Normal file
123
assets/scripts/utils/BgScale.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import { _decorator, Component, Enum, sys, director, UITransform, Widget, view, Vec3 } from 'cc';
|
||||
const {ccclass, property} = _decorator;
|
||||
|
||||
export enum BGScaleType {
|
||||
FULL_SCREEN,
|
||||
SCALE_BY_WIDTH,
|
||||
SCALE_BY_HEIGHT,
|
||||
SCALE_ONLY_WIDTH,
|
||||
SCALE_ONLY_HEIGHT,
|
||||
}
|
||||
|
||||
export enum BGAlignmentType {
|
||||
TOP,
|
||||
BOTTOM,
|
||||
CENTER,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
}
|
||||
|
||||
@ccclass
|
||||
export default class BgScale extends Component {
|
||||
@property({type: Enum(BGAlignmentType)})
|
||||
alignmentType:BGAlignmentType = BGAlignmentType.CENTER;
|
||||
@property({type: Enum(BGScaleType)})
|
||||
scaleType:BGScaleType = BGScaleType.FULL_SCREEN;
|
||||
|
||||
private realW:number = 0;
|
||||
private realH:number = 0;
|
||||
private _resizeCallback:any = null;
|
||||
|
||||
protected onLoad ():void {
|
||||
this.realW = this.node.getComponent(UITransform).width;
|
||||
this.realH = this.node.getComponent(UITransform).height;
|
||||
this.setMyFrameSize();
|
||||
if (sys.isBrowser) {
|
||||
this._resizeCallback = this.setMyFrameSizeAgain.bind(this);
|
||||
window.addEventListener('resize', this._resizeCallback);
|
||||
window.addEventListener('orientationchange', this._resizeCallback);
|
||||
document.addEventListener('rotateScreen', this._resizeCallback);
|
||||
document.addEventListener('resetScreen', this._resizeCallback);
|
||||
}
|
||||
}
|
||||
|
||||
protected onDestroy():void {
|
||||
if (sys.isBrowser) {
|
||||
window.removeEventListener('resize', this._resizeCallback);
|
||||
window.removeEventListener('orientationchange', this._resizeCallback);
|
||||
document.removeEventListener('rotateScreen', this._resizeCallback);
|
||||
document.removeEventListener('resetScreen', this._resizeCallback);
|
||||
this._resizeCallback = null;
|
||||
}
|
||||
}
|
||||
|
||||
protected setMyFrameSize():void {
|
||||
|
||||
if (!this.node) {
|
||||
return;
|
||||
}
|
||||
var wsize = null;
|
||||
wsize = view.getVisibleSize();
|
||||
|
||||
var scale1 = wsize.width / this.realW;
|
||||
var scale2 = wsize.height / this.realH;
|
||||
var max_scale = Math.max(scale1, scale2);
|
||||
var scaleX, scaleY;
|
||||
if (this.scaleType == BGScaleType.SCALE_BY_WIDTH) {
|
||||
scaleX = scaleY = scale1;
|
||||
} else if (this.scaleType == BGScaleType.SCALE_BY_HEIGHT) {
|
||||
scaleX = scaleY = scale2;
|
||||
} else if (this.scaleType == BGScaleType.SCALE_ONLY_WIDTH) {
|
||||
scaleX = scale1;
|
||||
scaleY = 1;
|
||||
} else if (this.scaleType == BGScaleType.SCALE_ONLY_HEIGHT) {
|
||||
scaleX = 1;
|
||||
scaleY = scale2;
|
||||
} else if (sys.isBrowser) {
|
||||
//横向浏览器 只缩放宽度
|
||||
scaleX = scaleY = max_scale;
|
||||
// scaleY = 1;
|
||||
} else {
|
||||
scaleX = scaleY = max_scale;
|
||||
}
|
||||
|
||||
this.node.getComponent(UITransform).width = this.realW * scaleX;
|
||||
this.node.getComponent(UITransform).height = this.realH * scaleY;
|
||||
|
||||
var widget = this.node.getComponent(Widget);
|
||||
if (widget == null) {
|
||||
widget = this.node.addComponent(Widget);
|
||||
}
|
||||
|
||||
if (this.alignmentType == BGAlignmentType.BOTTOM) {
|
||||
widget.isAlignHorizontalCenter = true;
|
||||
widget.isAlignBottom = true;
|
||||
widget.bottom = 0;
|
||||
} else if (this.alignmentType == BGAlignmentType.TOP) {
|
||||
widget.isAlignHorizontalCenter = true;
|
||||
widget.isAlignTop = true;
|
||||
widget.top = 0;
|
||||
} else if (this.alignmentType == BGAlignmentType.LEFT) {
|
||||
widget.isAlignVerticalCenter = true;
|
||||
widget.isAlignLeft = true;
|
||||
widget.left = 0;
|
||||
} else if (this.alignmentType == BGAlignmentType.RIGHT) {
|
||||
widget.isAlignVerticalCenter = true;
|
||||
widget.isAlignRight = true;
|
||||
widget.right = 0;
|
||||
} else if (this.alignmentType == BGAlignmentType.CENTER) {
|
||||
widget.isAlignHorizontalCenter = true;
|
||||
widget.isAlignVerticalCenter = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected setMyFrameSizeAgain():void {
|
||||
this.scheduleOnce(function () {
|
||||
this.setMyFrameSize();
|
||||
}.bind(this), 0.05);
|
||||
}
|
||||
|
||||
protected changeOrientation(flag:boolean):void {
|
||||
this.setMyFrameSize();
|
||||
}
|
||||
}
|
||||
11
assets/scripts/utils/BgScale.ts.meta
Normal file
11
assets/scripts/utils/BgScale.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "95c8cb36-6355-4fb9-94e7-92b99b9f7a68",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
89
assets/scripts/utils/DateUtil.ts
Normal file
89
assets/scripts/utils/DateUtil.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
|
||||
export default class DateUtil {
|
||||
protected static _serverTime: number = 0;
|
||||
protected static _getServerTime: number = 0;
|
||||
|
||||
public static setServerTime(time: number): void {
|
||||
this._serverTime = time;
|
||||
this._getServerTime = Date.now();
|
||||
}
|
||||
|
||||
public static getServerTime(): number {
|
||||
let nowTime: number = Date.now();
|
||||
return nowTime - this._getServerTime + this._serverTime;
|
||||
}
|
||||
|
||||
//是否在该时间之后
|
||||
public static isAfterServerTime(stms:number):boolean{
|
||||
var st = this.getServerTime();
|
||||
return st - stms > 0;
|
||||
}
|
||||
|
||||
public static leftTime(stms:number):number{
|
||||
var st = this.getServerTime();
|
||||
return stms - st;
|
||||
}
|
||||
|
||||
public static leftTimeStr(stms:number):string{
|
||||
var diff = this.leftTime(stms);
|
||||
return this.converSecondStr(diff);
|
||||
}
|
||||
|
||||
/**补零*/
|
||||
public static fillZero(str: string, num: number = 2): string {
|
||||
while (str.length < num) {
|
||||
str = "0" + str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/**时间字符串格式转换
|
||||
* 年 YYYY
|
||||
* 月 MM
|
||||
* 日 DD
|
||||
* 时 hh
|
||||
* 分 mm
|
||||
* 秒 ss
|
||||
* 毫秒 zzz*/
|
||||
public static converTimeStr(ms: number, format: string = "hh:mm:ss"): string {
|
||||
let date: Date = new Date(ms);
|
||||
let year: string = this.fillZero(date.getFullYear() + "", 4);
|
||||
let month: string = this.fillZero((date.getMonth() + 1) + "", 2);
|
||||
let dat: string = this.fillZero(date.getDate() + "", 2);
|
||||
let hour: string = this.fillZero(date.getHours() + "", 2);
|
||||
let minute: string = this.fillZero(date.getMinutes() + "", 2);
|
||||
let second: string = this.fillZero(date.getSeconds() + "", 2);
|
||||
let minSecond: string = this.fillZero(date.getMilliseconds() + "", 3);
|
||||
|
||||
let str: string = format + "";
|
||||
str = format.replace(/YYYY/, year);
|
||||
str = str.replace(/MM/, month);
|
||||
str = str.replace(/DD/, dat);
|
||||
str = str.replace(/hh/, hour);
|
||||
str = str.replace(/mm/, minute);
|
||||
str = str.replace(/ss/, second);
|
||||
str = str.replace(/zzz/, minSecond);
|
||||
return str;
|
||||
}
|
||||
|
||||
/**简易时间字符串格式转换*/
|
||||
public static converSecondStr(ms: number, format: string = "hh:mm:ss"): string {
|
||||
let second: number = Math.floor(ms / 1000);
|
||||
let hour: number = Math.floor(second / 3600);
|
||||
// console.log("hour:", hour);
|
||||
|
||||
second -= hour * 3600;
|
||||
let minute:number = Math.floor(second / 60);
|
||||
second -= minute * 60;
|
||||
|
||||
let str: string = format + "";
|
||||
if (hour > 0) {
|
||||
str = str.replace(/hh/, this.fillZero(hour + "", 2));
|
||||
} else {
|
||||
str = str.replace(/hh:/, "");
|
||||
}
|
||||
str = str.replace(/mm/, this.fillZero(minute + "", 2));
|
||||
str = str.replace(/ss/, this.fillZero(second + "", 2));
|
||||
return str;
|
||||
}
|
||||
}
|
||||
11
assets/scripts/utils/DateUtil.ts.meta
Normal file
11
assets/scripts/utils/DateUtil.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ffdad324-b605-47bf-9ccc-c564f59e981d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
102
assets/scripts/utils/EventMgr.ts
Normal file
102
assets/scripts/utils/EventMgr.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
|
||||
class EventHandler {
|
||||
public handler!:Function;
|
||||
public target!:object;
|
||||
|
||||
constructor(handler:Function, target:object){
|
||||
this.handler = handler;
|
||||
this.target = target;
|
||||
}
|
||||
}
|
||||
|
||||
class mgr {
|
||||
private static _instance: mgr = null;
|
||||
private events:Map<string, EventHandler[]> = new Map();
|
||||
private targetInName:Map<object, Set<string>> = new Map();
|
||||
|
||||
public static instance(): mgr {
|
||||
if (this._instance == null) {
|
||||
this._instance = new mgr();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
public on(name:string, handler:Function, target:object){
|
||||
if(!this.events.has(name)){
|
||||
this.events.set(name, []);
|
||||
}
|
||||
|
||||
var events = this.events.get(name);
|
||||
for (let i = 0; i < events.length; i++) {
|
||||
const eh:EventHandler = events[i];
|
||||
if(eh.handler == handler && eh.target == target){
|
||||
//已经添加过了
|
||||
console.log("已经添加过了:", name, handler, target);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var eh:EventHandler = new EventHandler(handler, target);
|
||||
events.push(eh);
|
||||
|
||||
if(!this.targetInName.has(target)){
|
||||
this.targetInName.set(target, new Set())
|
||||
}
|
||||
var set = this.targetInName.get(target);
|
||||
set.add(name);
|
||||
}
|
||||
|
||||
public off(name:string, handler:Function, target:object){
|
||||
if(!this.events.has(name)){
|
||||
return;
|
||||
}
|
||||
|
||||
var events = this.events.get(name);
|
||||
for (let i = 0; i < events.length; i++) {
|
||||
const eh:EventHandler = events[i];
|
||||
if(eh.handler == handler && eh.target == target){
|
||||
events.splice(i, i+1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public emit(name:string, ...args:any){
|
||||
if(!this.events.has(name)){
|
||||
return;
|
||||
}
|
||||
|
||||
var events = this.events.get(name);
|
||||
for (let i = 0; i < events.length; i++) {
|
||||
const eh:EventHandler = events[i];
|
||||
eh.handler.apply(eh.target, args);
|
||||
}
|
||||
}
|
||||
|
||||
public targetOff(target:object){
|
||||
|
||||
if(!this.targetInName.has(target)){
|
||||
return;
|
||||
}
|
||||
|
||||
var targetInName = this.targetInName.get(target);
|
||||
|
||||
targetInName.forEach(name => {
|
||||
if(this.events.has(name)){
|
||||
let events = this.events.get(name);
|
||||
for (let i = 0; i < events.length; i++) {
|
||||
const eh = events[i];
|
||||
if(eh.target == target){
|
||||
events.splice(i, i+1);
|
||||
i = i-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
targetInName.clear();
|
||||
}
|
||||
}
|
||||
|
||||
var EventMgr = mgr.instance();
|
||||
export { EventMgr };
|
||||
9
assets/scripts/utils/EventMgr.ts.meta
Normal file
9
assets/scripts/utils/EventMgr.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bb5c1959-ed2c-4727-bf11-1cfa4b4fbffe",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
466
assets/scripts/utils/ListLogic.ts
Normal file
466
assets/scripts/utils/ListLogic.ts
Normal file
@@ -0,0 +1,466 @@
|
||||
import { _decorator, Component, ScrollView, Prefab, Node, NodePool, EventHandler, UITransform, instantiate, CCBoolean, CCString, Vec3 } from 'cc';
|
||||
const {ccclass, property} = _decorator;
|
||||
|
||||
@ccclass('ListLogic')
|
||||
export default class ListLogic extends Component {
|
||||
|
||||
@property(ScrollView)
|
||||
scrollView: ScrollView = null;
|
||||
|
||||
@property(Prefab)
|
||||
itemPrefab: Prefab = null;
|
||||
|
||||
|
||||
@property(Node)
|
||||
itemNode: Node = null;
|
||||
|
||||
@property(CCString)
|
||||
itemLogicScriptName:string = "";
|
||||
|
||||
|
||||
@property(CCBoolean)
|
||||
isHorizontal:boolean = false;
|
||||
|
||||
|
||||
@property
|
||||
columnCount = 1;
|
||||
|
||||
|
||||
@property(CCBoolean)
|
||||
autoColumnCount:boolean = false;
|
||||
|
||||
|
||||
@property
|
||||
spaceColumn = 1;
|
||||
|
||||
@property
|
||||
spaceRow = 1;
|
||||
|
||||
|
||||
@property
|
||||
updateInterval = 0.1;
|
||||
|
||||
@property
|
||||
scale = 1;
|
||||
|
||||
@property([EventHandler])
|
||||
itemClickEvents:EventHandler[] = [];
|
||||
|
||||
|
||||
|
||||
@property(CCBoolean)
|
||||
isVirtual:boolean = false;
|
||||
|
||||
private _curOffset:number = 0;
|
||||
private _maxOffset:number = 0;
|
||||
private _startIndex:number = 0;
|
||||
private _itemCount:number = 0;
|
||||
private _updateTimer:number = 0;
|
||||
private _curIndex:number = 0;
|
||||
private _newOffset:number = 0;
|
||||
private _initContentPos:number = 0;
|
||||
private _maxRowColSize:number = 0;
|
||||
private _itemWidth:number = 0;
|
||||
private _itemHeight:number = 0;
|
||||
private _isUpdateList:boolean = false;
|
||||
private _itemPool:NodePool = null;
|
||||
private _items:any = [];
|
||||
private _datas:any = null;
|
||||
|
||||
protected onLoad():void{
|
||||
this._updateTimer = 0;//上次更新间隔时间
|
||||
this._curIndex = -1;
|
||||
this._newOffset = 0;
|
||||
this._initContentPos = 0;
|
||||
this._maxRowColSize = 0;//当前一行或者一列可以显示的最大宽度或者高度
|
||||
this._itemWidth = this._itemHeight = 0;
|
||||
if (this.itemPrefab) {
|
||||
|
||||
this._itemWidth = this.itemPrefab.data.getComponent(UITransform).width * this.scale;//item宽度
|
||||
this._itemHeight = this.itemPrefab.data.getComponent(UITransform).height * this.scale;//item高度
|
||||
} else if (this.itemNode) {
|
||||
this.itemNode.active = false;
|
||||
this._itemWidth = this.itemNode.getComponent(UITransform).width * this.scale;//item宽度
|
||||
this._itemHeight = this.itemNode.getComponent(UITransform).height * this.scale;//item高度
|
||||
}
|
||||
|
||||
if (this.isHorizontal) {
|
||||
this.scrollView.content.getComponent(UITransform).anchorX = 0;
|
||||
} else {
|
||||
this.scrollView.content.getComponent(UITransform).anchorY = 1;
|
||||
}
|
||||
|
||||
this._isUpdateList = false;//是否正在更新列表
|
||||
this._itemPool = new NodePool();//item缓存对象池
|
||||
this._items = [];//item列表
|
||||
this.updateList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
protected onDestroy():void {
|
||||
this._itemPool.clear();
|
||||
this._items.length = 0;
|
||||
this._datas = null;
|
||||
}
|
||||
|
||||
|
||||
protected update (dt):void {
|
||||
this._updateTimer += dt;
|
||||
if (this._updateTimer < this.updateInterval) {
|
||||
return;//更新间隔太短
|
||||
}
|
||||
this._updateTimer = 0;
|
||||
// if (this.isVirtual == false) {
|
||||
// return;//非虚拟列表 不需要刷新位置和数据
|
||||
// }
|
||||
if (this._isUpdateList) {
|
||||
return;//正在重新构建列表的时候 是不刷新的
|
||||
}
|
||||
let curOffset = 0;
|
||||
|
||||
if (this.isHorizontal) {
|
||||
curOffset = this._initContentPos - this.scrollView.content.position.x;
|
||||
} else {
|
||||
curOffset = this.scrollView.content.position.y - this._initContentPos;
|
||||
}
|
||||
curOffset = Math.max(Math.min(curOffset, this._maxOffset), 0);
|
||||
this.setCurOffset(curOffset);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected setCurOffset(curOffset):void {
|
||||
|
||||
if (this._datas == null || this._datas.length == 0) {
|
||||
return;//没有数据不执行刷新
|
||||
}
|
||||
if (this._items == null || this._items.length == 0) {
|
||||
return;//没有显示对象也不执行刷新
|
||||
}
|
||||
if (this._curOffset != curOffset) {
|
||||
// console.log("setCurOffset", this._curOffset, curOffset);
|
||||
this._curOffset = curOffset;
|
||||
if (this.isVirtual) {
|
||||
if (this.isHorizontal) {
|
||||
var startIndex = Math.floor(this._curOffset / (this._itemWidth + this.spaceColumn)) * this.columnCount;
|
||||
this.setStartIndex(startIndex);
|
||||
} else {
|
||||
var startIndex = Math.floor(this._curOffset / (this._itemHeight + this.spaceRow)) * this.columnCount;
|
||||
this.setStartIndex(startIndex);
|
||||
}
|
||||
} else {
|
||||
this.setStartIndex(0);//非虚拟列表startIndex不变
|
||||
}
|
||||
//console.log("updatelist11 y", this.scrollView.content.y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected setStartIndex(index) {
|
||||
if (this._startIndex != index && this._items.length > 0) {
|
||||
//console.log("setStartIndex", this._startIndex, index);
|
||||
this._startIndex = index;
|
||||
let suit = this.scrollView.content.getComponent(UITransform);
|
||||
for (var i = 0; i < this._items.length; i++) {
|
||||
var item:Node = this._items[i];
|
||||
var index1 = this._startIndex + i;
|
||||
let iuit = item.getComponent(UITransform);
|
||||
let pos = item.position.clone();
|
||||
|
||||
if (this.isHorizontal) {
|
||||
let _row = i % this.columnCount;
|
||||
let _toY = _row * (this._itemHeight + this.spaceRow) + iuit.anchorY * this._itemHeight - suit.height * suit.anchorY;
|
||||
pos.y = -_toY - (suit.height - this._maxRowColSize) / 2;
|
||||
pos.x = Math.floor(index1 / this.columnCount) * (this._itemWidth + this.spaceColumn) + this.spaceColumn + (1 - iuit.anchorX) * this._itemWidth;
|
||||
} else {
|
||||
let _col = i % this.columnCount;
|
||||
let _toX = _col * (this._itemWidth + this.spaceColumn) + iuit.anchorX * this._itemWidth - suit.width * suit.anchorX;
|
||||
pos.x = _toX + (suit.width - this._maxRowColSize) / 2;
|
||||
pos.y = -Math.floor(index1 / this.columnCount) * (this._itemHeight + this.spaceRow) - this.spaceRow - (1 - iuit.anchorY) * this._itemHeight;
|
||||
}
|
||||
item.itemIdx = index1;
|
||||
item.setPosition(pos);
|
||||
//console.log("update item position x: " + item.x + ", y: " + item.y);
|
||||
}
|
||||
|
||||
this.updateItems();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**设置item实例数量*/
|
||||
protected updateItemCount(count):boolean {
|
||||
if (this._itemCount != count) {
|
||||
this._itemCount = count;
|
||||
//清空列表
|
||||
var children = this.scrollView.content.children.slice();
|
||||
this.scrollView.content.removeAllChildren();
|
||||
for (var i = 0; i < children.length; i++) {
|
||||
let item = children[i];
|
||||
if (item.isValid) {
|
||||
item.off(Node.EventType.TOUCH_END, this.onItemClick, this);
|
||||
this._itemPool.put(item);//加入对象池
|
||||
}
|
||||
}
|
||||
this._items.length = 0;
|
||||
for (var i = 0; i < this._itemCount; i++) {
|
||||
let item = this.createItem();
|
||||
item.active = false;
|
||||
item.itemIdx = i;//在item上纪录当前下标
|
||||
item.on(Node.EventType.TOUCH_END, this.onItemClick, this);
|
||||
this.scrollView.content.addChild(item);
|
||||
this._items.push(item);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 更新列表
|
||||
*/
|
||||
protected updateList():void {
|
||||
if (this._datas == null || this._items == null || this._itemPool == null) {
|
||||
return;
|
||||
}
|
||||
//计算布局
|
||||
if (this._itemWidth <= 0 || this._itemHeight <= 0) {
|
||||
console.log("the list item has no width or height");
|
||||
return;
|
||||
}
|
||||
if (this._datas.length <= 0) {
|
||||
this._curOffset = this._startIndex = -1;//重置纪录
|
||||
this.hideItems();
|
||||
return;
|
||||
}
|
||||
this._isUpdateList = true;
|
||||
this.scrollView.stopAutoScroll();//更新时 停止滚动
|
||||
var rowCount = 1;
|
||||
var showCount = 1;
|
||||
var dataLen = this._datas.length;
|
||||
let uit = this.scrollView.content.parent.getComponent(UITransform);
|
||||
let cuit = this.scrollView.content.getComponent(UITransform);
|
||||
if (this.isHorizontal) {
|
||||
if (this.autoColumnCount) {
|
||||
//自动排列
|
||||
this.columnCount = Math.floor(uit.height / this._itemHeight);
|
||||
}
|
||||
if (this.columnCount < 1) {
|
||||
this.columnCount = 1;
|
||||
}
|
||||
this._maxRowColSize = this.columnCount * (this._itemHeight + this.spaceRow) - this.spaceRow;
|
||||
rowCount = Math.ceil(uit.width / (this._itemWidth + this.spaceColumn)) + 1;
|
||||
if (this.isVirtual) {
|
||||
showCount = rowCount * this.columnCount;
|
||||
} else {
|
||||
showCount = dataLen;
|
||||
}
|
||||
cuit.width = Math.ceil(dataLen / this.columnCount) * (this._itemWidth + this.spaceColumn);
|
||||
this._maxOffset = this.scrollView.getMaxScrollOffset().x;
|
||||
this._initContentPos = uit.width * (0 - uit.anchorX);
|
||||
} else {
|
||||
if (this.autoColumnCount) {
|
||||
//自动排列
|
||||
this.columnCount = Math.floor(uit.width / this._itemWidth);
|
||||
}
|
||||
if (this.columnCount < 1) {
|
||||
this.columnCount = 1;
|
||||
}
|
||||
this._maxRowColSize = this.columnCount * (this._itemWidth + this.spaceColumn) - this.spaceColumn;
|
||||
rowCount = Math.ceil(uit.height / (this._itemHeight + this.spaceRow)) + 1;
|
||||
if (this.isVirtual) {
|
||||
showCount = rowCount * this.columnCount;
|
||||
} else {
|
||||
showCount = dataLen;
|
||||
}
|
||||
cuit.height = Math.ceil(dataLen / this.columnCount) * (this._itemHeight + this.spaceRow);
|
||||
this._maxOffset = this.scrollView.getMaxScrollOffset().y;
|
||||
this._initContentPos = uit.height * (1 - uit.anchorY);
|
||||
}
|
||||
|
||||
var isItemChange = this.updateItemCount(showCount);
|
||||
this._newOffset = Math.max(Math.min(this._newOffset, this._maxOffset), 0);
|
||||
|
||||
|
||||
if ((isItemChange || this._newOffset != this._curOffset)) {
|
||||
let pos = this.scrollView.content.position.clone();
|
||||
this._curOffset = this._newOffset;
|
||||
if (this.isHorizontal) {
|
||||
pos.x = -Math.abs(this._initContentPos - this._newOffset);
|
||||
} else {
|
||||
pos.y = Math.abs(this._initContentPos + this._newOffset);
|
||||
}
|
||||
this._curOffset = -1;//重置纪录
|
||||
this._startIndex = -1;//重置纪录
|
||||
this.setCurOffset(this._newOffset);
|
||||
this.scrollView.content.setPosition(pos);
|
||||
} else {
|
||||
this.updateItems();
|
||||
}
|
||||
this._isUpdateList = false;
|
||||
//console.log("updatelist y", this.scrollView.content.y);
|
||||
|
||||
console.log("this.scrollView:", this.scrollView);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//刷新所有item数据
|
||||
protected updateItems():void {
|
||||
|
||||
for (var i = 0; i < this._items.length; i++) {
|
||||
var item = this._items[i];
|
||||
console.log("updateItems:", item, item.itemIdx, this._datas.length, item.itemIdx < this._datas.length)
|
||||
item.active = item.itemIdx < this._datas.length;
|
||||
if (item.active) {
|
||||
this.updateItem(item, item.itemIdx);
|
||||
this.selectItem(item, item.itemIdx == this._curIndex);
|
||||
}
|
||||
//console.log("update item i: " + item.itemIdx + ", active: " + item.active);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected hideItems():void {
|
||||
for (var i = 0; i < this._items.length; i++) {
|
||||
this._items[i].active = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected updateItem(item, index):void {
|
||||
var comp = null;
|
||||
if (this.itemLogicScriptName) {
|
||||
comp = item.getComponent(this.itemLogicScriptName);
|
||||
if (comp && comp.updateItem) {
|
||||
comp.updateItem(this._datas[index], index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据下标获取item对象
|
||||
*/
|
||||
protected getItem(index):any{
|
||||
var item = null;
|
||||
if (this._items) {
|
||||
for (var i = 0; i < this._items.length; i++) {
|
||||
if (this._items[i].itemIdx == index) {
|
||||
item = this._items[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 选中item
|
||||
*/
|
||||
protected selectItem(item, isSelected):void {
|
||||
var comp = null;
|
||||
if (this.itemLogicScriptName) {
|
||||
comp = item.getComponent(this.itemLogicScriptName);
|
||||
if (comp && comp.isSelected) {
|
||||
comp.isSelected(isSelected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建item
|
||||
*/
|
||||
protected createItem():any {
|
||||
var item = null;
|
||||
if (this._itemPool.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
|
||||
item = this._itemPool.get();
|
||||
} else if (this.itemPrefab) { // 如果没有空闲对象,也就是对象池中备用对象不够时,我们就用 instantiate 重新创建
|
||||
item = instantiate(this.itemPrefab);
|
||||
|
||||
} else if (this.itemNode) { // 如果没有空闲对象,也就是对象池中备用对象不够时,我们就用 instantiate 重新创建
|
||||
item = instantiate(this.itemNode);
|
||||
|
||||
}
|
||||
item.scale = new Vec3(this.scale, this.scale, this.scale);
|
||||
item.acitve = true;
|
||||
item.on(Node.EventType.TOUCH_END, this.onItemClick, this);
|
||||
return item;
|
||||
}
|
||||
|
||||
protected setIndex(index):void {
|
||||
if (this._curIndex != index) {
|
||||
if (this._curIndex >= 0 && this._curIndex < this._datas.length) {
|
||||
var oldItem = this.getItem(this._curIndex);
|
||||
if (oldItem) {
|
||||
this.selectItem(oldItem, false);
|
||||
}
|
||||
}
|
||||
var newItem = this.getItem(index);
|
||||
if (newItem) {
|
||||
this.selectItem(newItem, true);
|
||||
}
|
||||
this._curIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* item点击回调
|
||||
*/
|
||||
protected onItemClick(event):void {
|
||||
var index = event.target.itemIdx;
|
||||
this.setIndex(index);
|
||||
this.itemClickEvents.forEach(function (handler) {
|
||||
handler.emit([this._datas[index], index, event.target]);
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置列表数据
|
||||
* scrollOffset 没有传值代表刷新到初始位置 其他整数代表刷新到当前位置的相对偏移量
|
||||
*/
|
||||
public setData(data, scrollOffset?:any):void{
|
||||
this._datas = data;
|
||||
if (scrollOffset != null && scrollOffset != undefined && !isNaN(scrollOffset)) {
|
||||
this._newOffset = this._curOffset + scrollOffset;
|
||||
} else {
|
||||
this._newOffset = 0;
|
||||
}
|
||||
// console.log("list logiv setData", data, scrollOffset, this._newOffset);
|
||||
this.updateList();
|
||||
}
|
||||
|
||||
protected scrollToIndex(index):void {
|
||||
if (this._datas == null || this._items == null || this._itemPool == null) {
|
||||
return;
|
||||
}
|
||||
if (this._isUpdateList) {
|
||||
return;//正在重新构建列表的时候 是不刷新的
|
||||
}
|
||||
if (index < 0 || index >= this._datas.length) {
|
||||
return;//数据不合法
|
||||
}
|
||||
var curOffset = 0;
|
||||
if (this.isHorizontal) {
|
||||
curOffset = Math.ceil(index / this.columnCount) * (this._itemWidth + this.spaceColumn);
|
||||
} else {
|
||||
curOffset = Math.ceil(index / this.columnCount) * (this._itemHeight + this.spaceRow);
|
||||
}
|
||||
curOffset = Math.max(Math.min(curOffset, this._maxOffset), 0);
|
||||
this.setCurOffset(curOffset);
|
||||
}
|
||||
}
|
||||
11
assets/scripts/utils/ListLogic.ts.meta
Normal file
11
assets/scripts/utils/ListLogic.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "551e9368-d4b1-4acf-b1b3-4b9605a27411",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
96
assets/scripts/utils/LocalCache.ts
Normal file
96
assets/scripts/utils/LocalCache.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { sys } from "cc";
|
||||
|
||||
export class LocalCache{
|
||||
public static userListKey = "userListKey";
|
||||
|
||||
|
||||
public static setPersonMemory(keyStr, Value):void {
|
||||
//log("setPersonMemory:" + keyStr + ", " + Value);
|
||||
|
||||
if (keyStr === undefined || keyStr === null || keyStr === "") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Value === undefined || Value === null || Value === "") {
|
||||
Value = false;
|
||||
}
|
||||
|
||||
var jsonContent = LocalCache.getListForJson();
|
||||
if (jsonContent === undefined || jsonContent === null || jsonContent === "") {
|
||||
jsonContent = {};
|
||||
}
|
||||
jsonContent[keyStr] = Value;
|
||||
|
||||
var jsonstring = JSON.stringify(jsonContent);
|
||||
sys.localStorage.setItem(LocalCache.userListKey, jsonstring);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
public static getPersonMemory(keyStr, defaultValue):any {
|
||||
//log("getPersonMemory:" + keyStr + ", " + defaultValue);
|
||||
|
||||
//key不存在就gg了
|
||||
if (keyStr === undefined || keyStr === null || keyStr === "") {
|
||||
return;
|
||||
}
|
||||
|
||||
//获取本地已经保存的
|
||||
var jsonContent = LocalCache.getListForJson();
|
||||
if (jsonContent === null || jsonContent === undefined || jsonContent === "") {
|
||||
jsonContent = {};
|
||||
}
|
||||
|
||||
|
||||
//如果本身值存在就返回本身
|
||||
if (jsonContent[keyStr] !== null && jsonContent[keyStr] !== undefined && jsonContent[keyStr] !== "") {
|
||||
return jsonContent[keyStr];
|
||||
} else//如果本身不存在就判断默认是否存在
|
||||
{
|
||||
//默认也不存在 返回false
|
||||
if (defaultValue === undefined || defaultValue === null || defaultValue === "") {
|
||||
return false;
|
||||
} else {
|
||||
//默认存在 设置默认保存并且返回默认值
|
||||
jsonContent[keyStr] = defaultValue;
|
||||
var jsonstring = JSON.stringify(jsonContent);
|
||||
sys.localStorage.setItem(LocalCache.userListKey, jsonstring);
|
||||
return jsonContent[keyStr];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static getListForJson():any {
|
||||
var jsondata = sys.localStorage.getItem(LocalCache.userListKey);
|
||||
if (0 == Number(jsondata) || jsondata == undefined)
|
||||
return;
|
||||
|
||||
var jsonArray = JSON.parse(jsondata);
|
||||
return jsonArray;
|
||||
};
|
||||
|
||||
|
||||
public static getUuid():any{
|
||||
return LocalCache.getPersonMemory("deviceuuid", "");
|
||||
}
|
||||
|
||||
public static setUuid(uuid):void {
|
||||
LocalCache.setPersonMemory("deviceuuid", uuid);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
public static setLoginValidation(data:any):void{
|
||||
LocalCache.setPersonMemory("loginvalidation", data);
|
||||
}
|
||||
|
||||
public static getLoginValidation():any{
|
||||
return LocalCache.getPersonMemory("loginvalidation", "");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
assets/scripts/utils/LocalCache.ts.meta
Normal file
11
assets/scripts/utils/LocalCache.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "9b475e6a-430a-4abb-971f-9b6d2351b7c0",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
32
assets/scripts/utils/Modal.ts
Normal file
32
assets/scripts/utils/Modal.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
|
||||
import { _decorator, Component, Node, Event } from 'cc';
|
||||
const {ccclass, property} = _decorator;
|
||||
|
||||
@ccclass('Modal')
|
||||
export default class Modal extends Component {
|
||||
|
||||
@property(Node)
|
||||
mask: Node = null;
|
||||
|
||||
|
||||
start () {
|
||||
|
||||
}
|
||||
|
||||
protected onEnable() :void{
|
||||
this.mask.active = true;
|
||||
this.mask.on(Node.EventType.TOUCH_START, this.stopPropagation, this, true);
|
||||
this.mask.on(Node.EventType.TOUCH_END, this.stopPropagation, this, true);
|
||||
}
|
||||
|
||||
protected onDisable():void{
|
||||
this.mask.active = false;
|
||||
this.mask.off(Node.EventType.TOUCH_START, this.stopPropagation, this, true);
|
||||
this.mask.off(Node.EventType.TOUCH_END, this.stopPropagation, this, true);
|
||||
}
|
||||
|
||||
protected stopPropagation(event: Event):void {
|
||||
event.propagationStopped = true;
|
||||
}
|
||||
}
|
||||
11
assets/scripts/utils/Modal.ts.meta
Normal file
11
assets/scripts/utils/Modal.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "f99ce21c-ceec-4cde-b0f6-a5bfb0d9b9f5",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
16
assets/scripts/utils/Toast.ts
Normal file
16
assets/scripts/utils/Toast.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { _decorator, Component, Label } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('Toast')
|
||||
export default class Toast extends Component {
|
||||
@property(Label)
|
||||
msgLabel: Label | null = null;
|
||||
protected onRemove():void{
|
||||
this.node.active = false;
|
||||
}
|
||||
public setText(text:string):void{
|
||||
this.unschedule(this.onRemove);
|
||||
this.schedule(this.onRemove, 2);
|
||||
this.msgLabel.string = text;
|
||||
}
|
||||
}
|
||||
11
assets/scripts/utils/Toast.ts.meta
Normal file
11
assets/scripts/utils/Toast.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "4e72350f-504f-45c8-880f-844a115675ec",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
126
assets/scripts/utils/Tools.ts
Normal file
126
assets/scripts/utils/Tools.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
|
||||
import { LocalCache } from "./LocalCache";
|
||||
|
||||
export class Tools{
|
||||
public static getUUID():string{
|
||||
let uuid_str = '';
|
||||
let cache = LocalCache.getUuid();
|
||||
if(cache == ""){
|
||||
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
|
||||
var uuid = [];
|
||||
let radix = 16 | chars.length;
|
||||
for (let i = 0; i < 36; i++) uuid[i] = chars[0 | Math.random() * radix];
|
||||
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
|
||||
uuid_str = uuid.join('');
|
||||
LocalCache.setUuid(uuid_str);
|
||||
}else{
|
||||
uuid_str = cache;
|
||||
}
|
||||
|
||||
return uuid_str
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static getCodeStr(code:number = 0):string{
|
||||
let str = "";
|
||||
|
||||
var codeObj = {};
|
||||
codeObj[-4] = "代理连接失败";
|
||||
codeObj[-3] = "代理错误";
|
||||
codeObj[-2] = "链接没有找到用户";
|
||||
codeObj[-1] = "链接没有找到角色";
|
||||
codeObj[0] = "成功";
|
||||
|
||||
codeObj[1] = "参数有误";
|
||||
codeObj[2] = "数据库异常";
|
||||
codeObj[3] = "用户已存在";
|
||||
codeObj[4] = "密码不正确";
|
||||
codeObj[5] = "用户不存在";
|
||||
codeObj[6] = "session无效";
|
||||
codeObj[7] = "Hardware错误";
|
||||
codeObj[8] = "已经创建过角色了";
|
||||
codeObj[9] = "角色不存在";
|
||||
codeObj[10] = "城市不存在";
|
||||
|
||||
codeObj[11] = "城市不是自己的";
|
||||
codeObj[12] = "升级失败";
|
||||
codeObj[13] = "武将不存在";
|
||||
codeObj[14] = "武将不是自己的";
|
||||
codeObj[15] = "军队不是自己的";
|
||||
codeObj[16] = "资源不足";
|
||||
codeObj[17] = "超过带兵限制";
|
||||
codeObj[18] = "军队再忙";
|
||||
codeObj[19] = "将领再忙";
|
||||
codeObj[20] = "不能放弃";
|
||||
|
||||
codeObj[21] = "领地不是自己的";
|
||||
codeObj[22] = "军队没有主将";
|
||||
codeObj[23] = "不可到达";
|
||||
codeObj[24] = "体力不足";
|
||||
codeObj[25] = "政令不足";
|
||||
codeObj[26] = "金币不足";
|
||||
codeObj[27] = "重复上阵";
|
||||
codeObj[28] = "cost不足";
|
||||
codeObj[29] = "没有该合成武将";
|
||||
codeObj[30] = "合成武将非同名";
|
||||
|
||||
codeObj[31] = "统帅不足";
|
||||
codeObj[32] = "升级失败";
|
||||
codeObj[33] = "升级到最大星级";
|
||||
codeObj[34] = "联盟创建失败";
|
||||
codeObj[35] = "联盟不存在";
|
||||
codeObj[36] = "权限不足";
|
||||
codeObj[37] = "已经有联盟";
|
||||
codeObj[38] = "不允许退出";
|
||||
codeObj[39] = "内容太长";
|
||||
codeObj[40] = "不属于该联盟";
|
||||
|
||||
codeObj[41] = "用户已满";
|
||||
codeObj[42] = "已经申请过了";
|
||||
codeObj[43] = "不能驻守";
|
||||
codeObj[44] = "不能占领";
|
||||
codeObj[45] = "没有募兵所";
|
||||
codeObj[46] = "免战中";
|
||||
codeObj[47] = "征兵中";
|
||||
codeObj[48] = "领地已经在放弃了";
|
||||
codeObj[49] = "不能再新建建筑在领地上";
|
||||
codeObj[50] = "不能调兵";
|
||||
|
||||
codeObj[51] = "坑位已满";
|
||||
codeObj[52] = "队伍在城外";
|
||||
codeObj[53] = "不能升级建筑";
|
||||
codeObj[54] = "不能拆除建筑";
|
||||
codeObj[55] = "超过征收次数";
|
||||
codeObj[56] = "cd内不能操作";
|
||||
codeObj[57] = "武将超过上限了";
|
||||
codeObj[58] = "没有集市";
|
||||
codeObj[59] = "超过了收藏上限";
|
||||
|
||||
codeObj[60] = "超过了技能上限";
|
||||
codeObj[61] = "装备技能失败";
|
||||
codeObj[62] = "取下技能失败";
|
||||
codeObj[63] = "兵种不符";
|
||||
codeObj[64] = "该位置没有技能";
|
||||
codeObj[65] = "技能等级已满";
|
||||
|
||||
if (codeObj[code] == null){
|
||||
str = "错误:" + code;
|
||||
}else{
|
||||
str = codeObj[code]
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
public static numberToShow(num:number = 0):string{
|
||||
if (num >= 100000000){
|
||||
return Math.floor(num/100000000) + "亿"
|
||||
}
|
||||
else if (num >= 10000){
|
||||
return Math.floor(num/10000) + "万"
|
||||
}else{
|
||||
return num + ""
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
assets/scripts/utils/Tools.ts.meta
Normal file
11
assets/scripts/utils/Tools.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "81a225ef-e6ee-4855-82dd-85a26b1ba65f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user