You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.7 KiB
123 lines
3.7 KiB
|
1 week ago
|
import { _decorator, Component, director, Node } from 'cc';
|
||
|
|
import { Singleton } from 'db://assets/mx/tools/Singleton';
|
||
|
|
import { checkRedDotShow, DotConfig, DotType, RedDotID } from './RedDotComp';
|
||
|
|
import { GEvent } from 'db://assets/mx/module/event/GEvent';
|
||
|
|
const { ccclass, property } = _decorator;
|
||
|
|
|
||
|
|
/** 红点数据 */
|
||
|
|
export class Dot {
|
||
|
|
/** 红点id */
|
||
|
|
id: RedDotID = RedDotID.无;
|
||
|
|
/** 红点类型 */
|
||
|
|
type: DotType = DotType.默认;
|
||
|
|
/** 红点父id */
|
||
|
|
parentId: RedDotID = RedDotID.无;
|
||
|
|
/** 红点父节点 */
|
||
|
|
parent: Dot = null;
|
||
|
|
/** 红点状态 */
|
||
|
|
status: number = 0;
|
||
|
|
/** 红点子节点 */
|
||
|
|
children: Array<Dot> = [];
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 红点模块 */
|
||
|
|
@ccclass('RedDot')
|
||
|
|
export class RedDot extends Singleton {
|
||
|
|
|
||
|
|
/** 红点缓存 */
|
||
|
|
private _dotCache: Map<RedDotID, Dot> = new Map();
|
||
|
|
|
||
|
|
/** 初始化红点树 */
|
||
|
|
public init() {
|
||
|
|
this.buildDotTree();
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 更新红点状态,项目逻辑中在合适的时机或者合适的页面开启合适的刷新频率调用 */
|
||
|
|
public updateRedDot() {
|
||
|
|
for (let dot of this._dotCache.values()) {
|
||
|
|
if (dot.children.length == 0) {
|
||
|
|
let status = checkRedDotShow(dot.id);
|
||
|
|
this.setDotStatus(dot.id, status);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
GEvent.Ins.emit(GEvent.UpdateRedDotEvent);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**获取红点数据 */
|
||
|
|
public getDotData(id: RedDotID | number): Dot {
|
||
|
|
if (!this._dotCache.has(id)) return null;
|
||
|
|
return this._dotCache.get(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 根据红点id获取红点状态
|
||
|
|
* @param id
|
||
|
|
*/
|
||
|
|
public getDotStatus(id: RedDotID | number): number {
|
||
|
|
if (!this._dotCache.has(id)) return 0;
|
||
|
|
let dot = this._dotCache.get(id);
|
||
|
|
if (dot) return dot.status;
|
||
|
|
else return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 根据红点id设置红点状态(本接口随对外开放,但不需要外部调用,红点的状态都由checkRedDotShow函数设置,除非有极其特殊的情况需要无视判定条件直接设置)
|
||
|
|
* @param id
|
||
|
|
*/
|
||
|
|
public setDotStatus(id: RedDotID | number, status: number) {
|
||
|
|
if (!this._dotCache.has(id)) return;
|
||
|
|
let dot = this._dotCache.get(id);
|
||
|
|
dot.status = status;
|
||
|
|
this.setParentDotStatus(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 构建红点树结构
|
||
|
|
*/
|
||
|
|
private buildDotTree() {
|
||
|
|
this._dotCache.clear();
|
||
|
|
//根据配置创建红点
|
||
|
|
for (let i = 0; i < DotConfig.length; i++) {
|
||
|
|
let data = DotConfig[i];
|
||
|
|
if (this._dotCache.has(data.id)) continue;
|
||
|
|
let dot = new Dot();
|
||
|
|
dot.id = data.id;
|
||
|
|
dot.type = data.type;
|
||
|
|
dot.parentId = data.parentId;
|
||
|
|
this._dotCache.set(dot.id, dot);
|
||
|
|
}
|
||
|
|
|
||
|
|
//找到对应的父节点和所有子节点
|
||
|
|
for (let d of this._dotCache.values()) {
|
||
|
|
if (d.parentId && this._dotCache.has(d.parentId))
|
||
|
|
d.parent = this._dotCache.get(d.parentId);
|
||
|
|
d.children = Array.from(this._dotCache.values()).filter(x => x.parentId == d.id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 根据当前节点状态设置父节点红点状态(递归设置所有父节点)
|
||
|
|
* @param id
|
||
|
|
*/
|
||
|
|
private setParentDotStatus(id) {
|
||
|
|
let dot = this._dotCache.get(id);
|
||
|
|
if (dot.parentId) {
|
||
|
|
if (dot.status == 0) {
|
||
|
|
let s = true;
|
||
|
|
for (let i = 0; i < dot.parent.children.length; i++) {
|
||
|
|
if (dot.parent.children[i].status == 1)
|
||
|
|
s = false;
|
||
|
|
}
|
||
|
|
if (s) {
|
||
|
|
dot.parent.status = 0;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
dot.parent.status = 1;
|
||
|
|
}
|
||
|
|
this.setParentDotStatus(dot.parentId);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|