diff --git a/packages-user/client-base/src/components/textbox.tsx b/packages-user/client-base/src/components/textbox.tsx index c91fb97..89ed635 100644 --- a/packages-user/client-base/src/components/textbox.tsx +++ b/packages-user/client-base/src/components/textbox.tsx @@ -15,7 +15,7 @@ import { TextContentType, ITextContentTyperHooks } from './textboxTyper'; -import { SetupComponentOptions } from '@motajs/system'; +import { GameUI, SetupComponentOptions } from '@motajs/system'; import { DefaultProps, lwh, useRendererRoot, xywh } from '@motajs/render-vue'; import { Icon } from './icon'; import { ITextureManager } from '../material'; @@ -476,4 +476,6 @@ export const Textbox = defineComponent< ); }, textboxOptions); +export const TextboxUI = new GameUI('textbox', Textbox); + //#endregion diff --git a/packages-user/client-base/src/components/textboxManager.ts b/packages-user/client-base/src/components/textboxManager.ts new file mode 100644 index 0000000..935a983 --- /dev/null +++ b/packages-user/client-base/src/components/textboxManager.ts @@ -0,0 +1,71 @@ +import { IUIController } from '@motajs/system'; +import { IGameUIPropsBase } from '../types'; +import { ITextboxInfo, ITextboxManager } from './types'; +import { TextboxProps, TextboxUI } from './textbox'; +import { TextContentTyper } from './textboxTyper'; +import { ITextureManager } from '../material'; + +export class TextboxManager implements ITextboxManager { + /** 文本框映射 */ + private readonly textboxMap: Map = new Map(); + + constructor( + readonly ui: IUIController, + readonly materials: ITextureManager + ) { + ui.showAll(false); + } + + createTextbox(code: number, props?: TextboxProps): ITextboxInfo { + const exist = this.textboxMap.get(code); + if (exist) return exist; + const typer = + props?.typer ?? new TextContentTyper({ materials: this.materials }); + const instance = this.ui.open( + TextboxUI, + props ? { ...props, typer, zIndex: code } : { typer, zIndex: code } + ); + const info: ITextboxInfo = { + instance, + typer + }; + this.textboxMap.set(code, info); + return info; + } + + deleteTextbox(code: number): void { + const info = this.textboxMap.get(code); + if (!info) return; + this.ui.close(info.instance); + this.textboxMap.delete(code); + } + + deleteAllTextbox(): void { + this.ui.closeAll(); + this.textboxMap.clear(); + } + + getTextboxInfo(code: number): ITextboxInfo | null { + return this.textboxMap.get(code) ?? null; + } + + hideTextbox(code: number): void { + const info = this.textboxMap.get(code); + if (!info) return; + info.instance.hide(); + } + + showTextbox(code: number): void { + const info = this.textboxMap.get(code); + if (!info) return; + info.instance.show(); + } + + hideAllTextbox(): void { + this.textboxMap.forEach(v => v.instance.hide()); + } + + showAllTextbox(): void { + this.textboxMap.forEach(v => v.instance.show()); + } +} diff --git a/packages-user/client-base/src/components/types.ts b/packages-user/client-base/src/components/types.ts new file mode 100644 index 0000000..81287af --- /dev/null +++ b/packages-user/client-base/src/components/types.ts @@ -0,0 +1,65 @@ +import { IUIController, IUIInstance } from '@motajs/system'; +import { IGameUIPropsBase } from '../types'; +import { TextboxProps } from './textbox'; +import { TextContentTyper } from './textboxTyper'; +import { ITextureManager } from '../material'; + +export interface ITextboxInfo { + /** 文本框对应的 UI 实例 */ + readonly instance: IUIInstance; + /** 打字机对象 */ + readonly typer: TextContentTyper; +} + +export interface ITextboxManager { + /** 使用的 UI 控制器 */ + readonly ui: IUIController; + /** 使用的贴图素材管理器 */ + readonly materials: ITextureManager; + + /** + * 创建一个新的文本框,如果代码已存在则会返回已存在的那个。`code` 越大,显示层级越高 + * @param code 文本框使用的代码,同时负责文本框的标识与 `zIndex` 排序 + * @param props 传递给 Textbox 的初始参数,填写 `typer` 将会覆盖默认使用的打字机对象,不填也可以通过 `getTextboxInfo` 获取 + */ + createTextbox(code: number, props?: TextboxProps): ITextboxInfo; + + /** + * 删除指定代码对应的文本框 + * @param code 文本框使用的代码 + */ + deleteTextbox(code: number): void; + + /** + * 删除所有文本框 + */ + deleteAllTextbox(): void; + + /** + * 获取指定代码对应的文本框信息 + * @param code 文本框使用的代码 + */ + getTextboxInfo(code: number): ITextboxInfo | null; + + /** + * 隐藏指定的文本框 + * @param code 文本框使用的代码 + */ + hideTextbox(code: number): void; + + /** + * 显示指定的文本框 + * @param code 文本框使用的代码 + */ + showTextbox(code: number): void; + + /** + * 隐藏所有文本框 + */ + hideAllTextbox(): void; + + /** + * 显示所有文本框 + */ + showAllTextbox(): void; +}