mirror of
https://github.com/Gongxh0901/kunpolibrary
synced 2025-10-09 16:45:45 +00:00
first commit
This commit is contained in:
85
src/global/Adapter.ts
Normal file
85
src/global/Adapter.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2024-12-07
|
||||
* @Description: 适配用的类
|
||||
*/
|
||||
|
||||
import { ResolutionPolicy, view } from "cc";
|
||||
import { info } from "../tool/log";
|
||||
import { WindowManager } from "../ui/WindowManager";
|
||||
import { Screen } from "./Screen";
|
||||
import { size } from "./header";
|
||||
|
||||
export abstract class Adapter {
|
||||
public init() {
|
||||
// 设计尺寸 不会变化
|
||||
let designSize = this.getDesignSize();
|
||||
Screen.DesignHeight = designSize.height;
|
||||
Screen.DesignWidth = designSize.width;
|
||||
view.setDesignResolutionSize(Screen.DesignWidth, Screen.DesignWidth, ResolutionPolicy.SHOW_ALL);
|
||||
|
||||
this.resize();
|
||||
this.registerResizeCallback((...args: any) => {
|
||||
info("屏幕发生变化", ...args);
|
||||
this.resize();
|
||||
});
|
||||
}
|
||||
|
||||
protected resize(): void {
|
||||
Screen.SafeAreaHeight = 60;
|
||||
// 屏幕像素尺寸
|
||||
const winSize = this.getScreenSize();
|
||||
const isDesignLandscape = Screen.DesignWidth > Screen.DesignHeight;
|
||||
const isLandscape = winSize.width > winSize.height;
|
||||
if (isDesignLandscape == isLandscape) {
|
||||
Screen.ScreenWidth = winSize.width;
|
||||
Screen.ScreenHeight = winSize.height;
|
||||
} else {
|
||||
Screen.ScreenWidth = winSize.height;
|
||||
Screen.ScreenHeight = winSize.width;
|
||||
}
|
||||
if (isDesignLandscape) {
|
||||
// 横屏
|
||||
/** 安全区的宽度 */
|
||||
Screen.SafeWidth = Screen.ScreenWidth - Screen.SafeAreaHeight * 2;
|
||||
/** 安全区的高度 */
|
||||
Screen.SafeHeight = Screen.ScreenHeight;
|
||||
} else {
|
||||
// 竖屏
|
||||
/** 安全区的宽度 */
|
||||
Screen.SafeWidth = Screen.ScreenWidth;
|
||||
/** 安全区的高度 */
|
||||
Screen.SafeHeight = Screen.ScreenHeight - Screen.SafeAreaHeight * 2;
|
||||
}
|
||||
WindowManager._screenResize();
|
||||
this.printScreen();
|
||||
}
|
||||
|
||||
private printScreen() {
|
||||
info(`设计分辨率: ${Screen.DesignWidth}x${Screen.DesignHeight}`);
|
||||
info(`屏幕分辨率: ${Screen.ScreenWidth}x${Screen.ScreenHeight}`);
|
||||
info(`安全区域高度: ${Screen.SafeAreaHeight}`);
|
||||
info(`安全区宽高: ${Screen.SafeWidth}x${Screen.SafeHeight}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取屏幕尺寸
|
||||
* @abstract 子类实现
|
||||
* @returns {size}
|
||||
*/
|
||||
protected abstract getScreenSize(): size;
|
||||
|
||||
/**
|
||||
* 获取设计尺寸
|
||||
* @abstract 子类实现
|
||||
* @returns {size}
|
||||
*/
|
||||
protected abstract getDesignSize(): size;
|
||||
|
||||
/**
|
||||
* 设置尺寸发生变化的监听
|
||||
* @abstract 子类实现
|
||||
* @param callback
|
||||
*/
|
||||
protected abstract registerResizeCallback(callback: () => void): void;
|
||||
}
|
44
src/global/GlobalEvent.ts
Normal file
44
src/global/GlobalEvent.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2024-12-22
|
||||
* @Description: 全局事件
|
||||
*/
|
||||
|
||||
import { EventManager } from "../event/EventManager";
|
||||
|
||||
export class GlobalEvent {
|
||||
private static _globalEvent: EventManager = null;
|
||||
public static add(eventName: string, callback: (...args: any[]) => void, target: any): void {
|
||||
this._globalEvent.addEvent(eventName, callback, target);
|
||||
}
|
||||
|
||||
public static addOnce(eventName: string, callback: (...args: any[]) => void, target: any): void {
|
||||
this._globalEvent.addEventOnce(eventName, callback, target);
|
||||
}
|
||||
|
||||
public static send(eventName: string, ...args: any[]): void {
|
||||
this._globalEvent.send(eventName, null, ...args);
|
||||
}
|
||||
|
||||
public static sendToTarget(eventName: string, target: any, ...args: any[]) {
|
||||
this._globalEvent.send(eventName, target, ...args);
|
||||
}
|
||||
|
||||
public static remove(eventName: string, callback: (...args: any[]) => void, target?: any): void {
|
||||
this._globalEvent.remove(eventName, callback, target);
|
||||
}
|
||||
|
||||
public static removeByNameAndTarget(eventName: string, target: any) {
|
||||
this._globalEvent.removeByNameAndTarget(eventName, target);
|
||||
}
|
||||
|
||||
public static removeByTarget(target: any): void {
|
||||
this._globalEvent.removeList(target);
|
||||
}
|
||||
|
||||
public static _initGlobalEvent(): void {
|
||||
if (!this._globalEvent) {
|
||||
this._globalEvent = new EventManager();
|
||||
}
|
||||
}
|
||||
}
|
76
src/global/GlobalTimer.ts
Normal file
76
src/global/GlobalTimer.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2024-12-07
|
||||
* @Description:
|
||||
*/
|
||||
|
||||
import { Timer } from "../tool/timer/Timer";
|
||||
|
||||
export class GlobalTimer {
|
||||
private static _timer: Timer = null;
|
||||
/**
|
||||
* 初始化全局定时器,设置定时器间隔为16毫秒。
|
||||
* 此方法用于启动一个定时器实例,以便在整个应用程序中跟踪时间相关的操作。
|
||||
*/
|
||||
public static initTimer(): void {
|
||||
this._timer = new Timer(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全局定时器实例。如果定时器尚未初始化,则进行初始化。
|
||||
* @returns {Timer} 全局定时器实例
|
||||
*/
|
||||
public static get Timer(): Timer {
|
||||
if (this._timer) {
|
||||
return this._timer;
|
||||
}
|
||||
this.initTimer();
|
||||
return this._timer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动一个定时器,执行指定的回调函数。
|
||||
* @param callback - 要定时执行的回调函数。
|
||||
* @param interval - 定时器的时间间隔(秒)。
|
||||
* @param loop - [loop=0] 重复次数:0:回调一次,1~n:回调n次,-1:无限重复
|
||||
* @returns 返回定时器的ID。
|
||||
*/
|
||||
public static startTimer(callback: () => void, interval: number, loop: number = 0): number {
|
||||
return this.Timer.start(callback, interval, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止指定ID的计时器。
|
||||
* @param timerId - 要停止的计时器的唯一标识符。
|
||||
*/
|
||||
public static stopTimer(timerId: number): void {
|
||||
this.Timer.stop(timerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂停指定ID的计时器。
|
||||
* @param timerId - 要暂停的计时器的唯一标识符。
|
||||
*/
|
||||
public static pauseTimer(timerId: number): void {
|
||||
this.Timer.pause(timerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复指定ID的计时器。
|
||||
* @param timerId - 要恢复的计时器的唯一标识符。
|
||||
*/
|
||||
public static resumeTimer(timerId: number): void {
|
||||
this.Timer.resume(timerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有定时器。
|
||||
*/
|
||||
public static clearAllTimer(): void {
|
||||
this.Timer.clear();
|
||||
}
|
||||
|
||||
public static update(dt: number): void {
|
||||
this._timer?.update(dt);
|
||||
}
|
||||
}
|
13
src/global/IModule.ts
Normal file
13
src/global/IModule.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2024-12-07
|
||||
* @Description: 模块接口
|
||||
*/
|
||||
|
||||
export interface IModule {
|
||||
/** 模块名称 */
|
||||
moduleName: string;
|
||||
|
||||
/** 模块初始化 */
|
||||
init(): void;
|
||||
}
|
40
src/global/InnerTimer.ts
Normal file
40
src/global/InnerTimer.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2025-02-14
|
||||
* @Description: 内部使用的全局定时器
|
||||
*/
|
||||
import { Timer } from "../tool/timer/Timer";
|
||||
|
||||
export class InnerTimer {
|
||||
private static _timer: Timer = null;
|
||||
/**
|
||||
* 初始化全局定时器,设置定时器间隔为16毫秒。
|
||||
* 此方法用于启动一个定时器实例,以便在整个应用程序中跟踪时间相关的操作。
|
||||
*/
|
||||
public static initTimer(): void {
|
||||
this._timer = new Timer(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动一个定时器,执行指定的回调函数。
|
||||
* @param callback - 要定时执行的回调函数。
|
||||
* @param interval - 定时器的时间间隔(秒)。
|
||||
* @param loop - [loop=0] 重复次数:0:回调一次,1~n:回调n次,-1:无限重复
|
||||
* @returns 返回定时器的ID。
|
||||
*/
|
||||
public static startTimer(callback: () => void, interval: number, loop: number = 0): number {
|
||||
return this._timer.start(callback, interval, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止指定ID的计时器。
|
||||
* @param timerId - 要停止的计时器的唯一标识符。
|
||||
*/
|
||||
public static stopTimer(timerId: number): void {
|
||||
this._timer.stop(timerId);
|
||||
}
|
||||
|
||||
public static update(dt: number): void {
|
||||
this._timer?.update(dt);
|
||||
}
|
||||
}
|
101
src/global/Platform.ts
Normal file
101
src/global/Platform.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2024-12-07
|
||||
* @Description: 平台相关
|
||||
*/
|
||||
|
||||
export enum PlatformType {
|
||||
Android = 1,
|
||||
IOS,
|
||||
HarmonyOS,
|
||||
/** 微信小游戏 */
|
||||
WX,
|
||||
/** 支付宝小游戏 */
|
||||
Alipay,
|
||||
/** 字节小游戏 */
|
||||
Bytedance,
|
||||
/** 华为快游戏 */
|
||||
HuaweiQuick,
|
||||
/** 其他都为Browser */
|
||||
Browser,
|
||||
}
|
||||
|
||||
export class Platform {
|
||||
/**
|
||||
* 是否为原生平台
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isNative: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否为移动平台
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isMobile: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否为原生移动平台
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isNativeMobile: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否为安卓平台
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isAndroid: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否为IOS平台
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isIOS: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否为HarmonyOS平台
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isHarmonyOS: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否为微信小游戏
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isWX: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否为支付宝小游戏
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isAlipay: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否为字节小游戏
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isBytedance: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否是华为快游戏
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isHuaweiQuick: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否为浏览器
|
||||
* @type {boolean}
|
||||
*/
|
||||
public static isBrowser: boolean = false;
|
||||
|
||||
/**
|
||||
* 平台名
|
||||
* @type {string}
|
||||
*/
|
||||
public static platform: PlatformType;
|
||||
|
||||
/**
|
||||
* 设备ID
|
||||
* @type {string}
|
||||
*/
|
||||
public static deviceId: string;
|
||||
}
|
21
src/global/Screen.ts
Normal file
21
src/global/Screen.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2024-12-08
|
||||
* @Description: 屏幕尺寸信息接口
|
||||
*/
|
||||
export class Screen {
|
||||
/** 屏幕宽度 */
|
||||
public static ScreenWidth: number;
|
||||
/** 屏幕高度 */
|
||||
public static ScreenHeight: number;
|
||||
/** 设计分辨率宽 */
|
||||
public static DesignWidth: number;
|
||||
/** 设计分辨率高 */
|
||||
public static DesignHeight: number;
|
||||
/** 安全区外一侧的高度 或 宽度 */
|
||||
public static SafeAreaHeight: number;
|
||||
/** 安全区的宽度 */
|
||||
public static SafeWidth: number;
|
||||
/** 安全区的高度 */
|
||||
public static SafeHeight: number;
|
||||
}
|
31
src/global/header.ts
Normal file
31
src/global/header.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2024-12-08
|
||||
* @Description: 一些数据结构
|
||||
*/
|
||||
|
||||
import { warn } from "../tool/log";
|
||||
|
||||
export interface size {
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
export interface FrameConfig {
|
||||
/** 开启debug 默认: false */
|
||||
debug?: boolean;
|
||||
}
|
||||
|
||||
export let KUNPO_DEBUG: boolean = false;
|
||||
/**
|
||||
* 启用或禁用调试模式。
|
||||
* @param enable - 如果为 true,则启用调试模式;如果为 false,则禁用调试模式。不设置默认不开启
|
||||
*/
|
||||
export function enableDebugMode(enable: boolean): void {
|
||||
if (enable == true) {
|
||||
KUNPO_DEBUG = true;
|
||||
warn("调试模式已开启");
|
||||
} else {
|
||||
KUNPO_DEBUG = false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user