Files
18xchat/assets/Scripts/Main/Common/GameRootUI.ts
T

234 lines
6.3 KiB
TypeScript
Raw Normal View History

import {
_decorator,
assetManager,
Camera,
Canvas,
Component,
director,
DynamicAtlasManager,
instantiate,
macro,
Node,
sys,
tween,
UIOpacity,
Vec3,
} from "cc";
2025-07-17 17:18:21 +08:00
import { CommonConfig } from "../Config/CommonConfig";
import ResManager from "../Manager/ResManager";
import Utils from "./Utils";
2025-09-11 17:07:29 +08:00
import { UITransitionHelper } from "../../chat18x/utils/UITransitionHelper";
2025-07-17 17:18:21 +08:00
const { ccclass, property, executeInEditMode, disallowMultiple } = _decorator;
//游戏根目录 每个场景都要加一下
@ccclass
// @executeInEditMode
@disallowMultiple
export default class GameRootUI extends Component {
public static I: GameRootUI = null;
//当前场景摄像机
private static _MainCamera: Camera = null;
public static get MainCamera(): Camera {
return GameRootUI._MainCamera;
}
public static set MainCamera(value: Camera) {
GameRootUI._MainCamera = value;
}
public LayerNodeGroups = {}; //界面组
@property(Node)
UIRoot: Node = null;
@property(Node)
UILayerMod: Node = null;
@property
public DefaultUI: string = ""; //默认打开的界面
private defaultView: Node;
showDefaultView() {
this.defaultView.active = true;
}
2025-09-11 17:07:29 +08:00
/**
* 带过渡动画显示默认视图
* @param transitionType 过渡动画类型:'slide_left', 'slide_right', 'fade', 'none'
* @param duration 动画时长,默认0.3秒
* @param callback 动画完成回调
*/
showDefaultViewWithTransition(
transitionType: string = "none",
duration: number = 0.1,
callback?: Function
) {
if (!this.defaultView) {
console.warn("GameRootUI: defaultView not found");
callback && callback();
return;
}
// 确保节点是激活的
this.defaultView.active = true;
if (transitionType === "none") {
callback && callback();
return;
}
console.log(
`GameRootUI: Executing ${transitionType} animation on defaultView`
);
switch (transitionType) {
case "slide_left":
console.log(
"GameRootUI: About to call slideInFromLeft, node valid:",
this.defaultView.isValid
);
UITransitionHelper.slideInFromLeft(this.defaultView, duration, () => {
console.log("GameRootUI: slideInFromLeft animation completed");
callback && callback();
});
break;
case "slide_right":
UITransitionHelper.slideInFromRight(
this.defaultView,
duration,
callback
);
break;
case "fade":
// 如果UITransitionHelper有fadeIn方法的话可以用,否则简单显示
callback && callback();
break;
default:
callback && callback();
break;
}
}
hideDefaultView(): void {
this.defaultView.active = false;
2025-09-11 17:07:29 +08:00
this.defaultView.position = Vec3.ZERO;
}
onLoad() {
GameRootUI.I = this;
}
start() {
//创建界面层
for (const key in CommonConfig.UILayerGroup) {
if (isNaN(Number(key))) {
let newNode = instantiate(this.UILayerMod);
newNode.name = key;
this.UIRoot.addChild(newNode);
newNode.setPosition(Vec3.ZERO);
newNode.setScale(Vec3.ONE);
let l_idx = CommonConfig.UILayerGroup[key];
newNode.setSiblingIndex(Number(l_idx));
this.LayerNodeGroups[key] = newNode;
}
2025-07-17 17:18:21 +08:00
}
//默认打开的界面
if (this.DefaultUI.length > 0) {
ResManager.I.loadSubpackagePrefab(this.DefaultUI, (res) => {
this.defaultView = res;
let viewSP = res.getComponent(res.name);
viewSP && viewSP.openUIData && viewSP.openUIData(null);
2025-07-17 17:18:21 +08:00
this.AddUIToLayer(res, CommonConfig.UILayerGroup.Layer_navigationbar);
});
2025-07-25 18:27:30 +08:00
}
}
2025-07-25 18:27:30 +08:00
protected update(dt: number): void {
GameRootUI.I.CheckDisableOperTime(dt);
}
2025-07-17 17:18:21 +08:00
//手动创建时需要调用一下初始化
public InitByCreate(_UIRoot: Node, _UILayerMod: Node, _DefaultUI: string) {
this.UIRoot = _UIRoot;
this.UILayerMod = _UILayerMod;
this.DefaultUI = _DefaultUI;
}
//添加一个界面到某个层上
public AddUIToLayer(
ui: Node,
e_layer: CommonConfig.UILayerGroup,
zorder: number = 0
) {
if (ui == null) return;
var n_layer = CommonConfig.UILayerGroup[e_layer];
if (this.LayerNodeGroups[n_layer] != null) {
this.LayerNodeGroups[n_layer].addChild(ui);
ui.setSiblingIndex(zorder);
}
}
//禁用操作界面----------------------------------------------------------------------------
private static DisableOperUI: Node = null;
private static DisableOperSwt: boolean = false;
private static DisableOperDura: number = 0;
private static DisableOperTime: number = 0;
public static InitDisableOperUI(isshow: boolean) {
if (GameRootUI.DisableOperUI == null) {
ResManager.I.loadSubpackagePrefab(
"UI/Cross/DisableOper_UI",
(res: Node) => {
if (!GameRootUI.DisableOperUI) {
GameRootUI.DisableOperUI = res;
director.getScene().addChild(res);
director.addPersistRootNode(res);
res.setSiblingIndex(9999);
let croot = GameRootUI.DisableOperUI.getChildByName("croot");
croot.active = false;
}
if (isshow) {
GameRootUI.DisableOper(GameRootUI.DisableOperDura);
}
2025-07-17 17:18:21 +08:00
}
);
}
}
/**开启禁用操作
* @param dura 禁用时间 单位秒
*/
public static DisableOper(dura: number) {
if (GameRootUI.DisableOperDura < dura) {
GameRootUI.DisableOperDura = dura;
}
if (GameRootUI.DisableOperUI == null) {
GameRootUI.InitDisableOperUI(true);
return;
2025-07-17 17:18:21 +08:00
}
Utils.Log("禁用操作 开启");
GameRootUI.DisableOperSwt = true;
let croot = GameRootUI.DisableOperUI.getChildByName("croot");
croot.active = true;
}
/**关闭禁用操作 */
public static EnableOper() {
GameRootUI.DisableOperSwt = false;
GameRootUI.DisableOperDura = 0;
GameRootUI.DisableOperTime = 0;
if (GameRootUI.DisableOperUI) {
let croot = GameRootUI.DisableOperUI.getChildByName("croot");
croot.active = false;
2025-07-17 17:18:21 +08:00
}
Utils.Log("禁用操作 关闭");
}
private CheckDisableOperTime(dt: number) {
if (!GameRootUI.DisableOperSwt) return;
GameRootUI.DisableOperTime += dt;
if (GameRootUI.DisableOperTime >= GameRootUI.DisableOperDura) {
GameRootUI.EnableOper();
2025-07-17 17:18:21 +08:00
}
}
2025-07-17 17:18:21 +08:00
}