From 586c7b959d1da4d89470d394c39caa2afd6df62d Mon Sep 17 00:00:00 2001 From: MrKBear Date: Thu, 24 Feb 2022 17:12:57 +0800 Subject: [PATCH] Add archive model & status --- source/Context/Status.tsx | 53 +++++++++++++++++++++++ source/GLRender/BasicRenderer.ts | 2 +- source/GLRender/ClassicRenderer.ts | 4 +- source/Model/Archive.ts | 42 ++++++++++++++++++ source/Page/Laboratory/Laboratory.tsx | 3 +- source/Page/SimulatorWeb/SimulatorWeb.tsx | 37 +++++++++++++++- 6 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 source/Context/Status.tsx create mode 100644 source/Model/Archive.ts diff --git a/source/Context/Status.tsx b/source/Context/Status.tsx new file mode 100644 index 0000000..f7a034f --- /dev/null +++ b/source/Context/Status.tsx @@ -0,0 +1,53 @@ +import { createContext, Component, FunctionComponent } from "react"; +import { Emitter } from "@Model/Emitter"; +import { Model } from "@Model/Model"; +import { Archive } from "@Model/Archive"; +import { AbstractRenderer } from "@Model/Renderer"; + +class Status extends Emitter<{}> { + + /** + * 渲染器 + */ + public renderer: AbstractRenderer = undefined as any; + + /** + * 文件状态 + */ + public archive: Archive = new Archive(); + + /** + * 模型状态 + */ + public model: Model = new Model(); + +} + +interface IMixinStatusProps { + status?: Status; +} + +const StatusContext = createContext(new Status()); + +StatusContext.displayName = "Status"; +const StatusProvider = StatusContext.Provider; +const StatusConsumer = StatusContext.Consumer; + +type RenderComponent = (new (...p: any) => Component) | FunctionComponent; + +/** + * 修饰器 + */ +function useStatus(components: R): R { + return ((props: any) => { + const C = components; + return + {(status: Status) => } + + }) as any; +} + +export { + Status, StatusContext, useStatus, + IMixinStatusProps, StatusProvider, StatusConsumer +}; \ No newline at end of file diff --git a/source/GLRender/BasicRenderer.ts b/source/GLRender/BasicRenderer.ts index c747987..115cf27 100644 --- a/source/GLRender/BasicRenderer.ts +++ b/source/GLRender/BasicRenderer.ts @@ -156,7 +156,7 @@ abstract class BasicRenderer< /** * 初始化 */ - abstract onLoad(): void; + abstract onLoad(): this; /** * 渲染器执行 diff --git a/source/GLRender/ClassicRenderer.ts b/source/GLRender/ClassicRenderer.ts index 84161c6..ed6dadf 100644 --- a/source/GLRender/ClassicRenderer.ts +++ b/source/GLRender/ClassicRenderer.ts @@ -34,7 +34,7 @@ class ClassicRenderer extends BasicRenderer<{}, IClassicRendererParams> { */ private objectPool = new Map(); - public onLoad(): void { + public onLoad(): this { // 自动调节分辨率 this.autoResize(); @@ -69,6 +69,8 @@ class ClassicRenderer extends BasicRenderer<{}, IClassicRendererParams> { // setInterval(() => { // this.basicGroup.upLoadData(new Array(100 * 3).fill(0).map(() => (Math.random() - .5) * 2)); // }, 500); + + return this; } loop(t: number): void { diff --git a/source/Model/Archive.ts b/source/Model/Archive.ts new file mode 100644 index 0000000..0c0da10 --- /dev/null +++ b/source/Model/Archive.ts @@ -0,0 +1,42 @@ +import { Emitter, EventType, EventMixin } from "./Emitter"; + +interface IArchiveEvent { + save: Archive; + load: Archive; +} + +class Archive< + M extends any = any, + E extends Record = {} +> extends Emitter { + + /** + * 是否为新文件 + */ + public isNewFile: boolean = true; + + /** + * 文件名 + */ + public fileName?: string; + + /** + * 文件数据 + */ + public fileData?: M; + + /** + * 保存文件 + * 模型转换为文件 + */ + public save() {}; + + /** + * 加载文件为模型 + * return Model + */ + public load() {}; +} + +export { Archive }; +export default Archive; \ No newline at end of file diff --git a/source/Page/Laboratory/Laboratory.tsx b/source/Page/Laboratory/Laboratory.tsx index 19cb789..b98892f 100644 --- a/source/Page/Laboratory/Laboratory.tsx +++ b/source/Page/Laboratory/Laboratory.tsx @@ -21,9 +21,8 @@ class Laboratory extends Component { throw new Error("Laboratory: 重复引用 canvas 节点"); } - const renderer = new ClassicRenderer({ className: "canvas" }); + const renderer = new ClassicRenderer({ className: "canvas" }).onLoad(); this.canvasContRef.current.appendChild(renderer.canvas.dom); - renderer.onLoad(); let model = new Model().bindRenderer(renderer); let group = model.addGroup(); diff --git a/source/Page/SimulatorWeb/SimulatorWeb.tsx b/source/Page/SimulatorWeb/SimulatorWeb.tsx index 0387759..7f8afb6 100644 --- a/source/Page/SimulatorWeb/SimulatorWeb.tsx +++ b/source/Page/SimulatorWeb/SimulatorWeb.tsx @@ -4,6 +4,8 @@ import { HeaderBar } from "@Component/HeaderBar/HeaderBar"; import { Theme, FontLevel, BackgroundLevel } from "@Component/Theme/Theme"; import { Localization } from "@Component/Localization/Localization"; import { Entry } from "../Entry/Entry"; +import { StatusProvider, Status } from "@Context/Status"; +import { ClassicRenderer } from "@GLRender/ClassicRenderer"; import "./SimulatorWeb.scss"; class SimulatorWeb extends Component { @@ -13,16 +15,49 @@ class SimulatorWeb extends Component { */ private setting: Setting; + /** + * 全局状态 + */ + private status: Status; + public constructor(props: any) { super(props); // TODO: 这里要读取设置 this.setting = new Setting(); (window as any).setting = (this.setting as any); + + // TODO: 这里要读取存档 + this.status = new Status(); + this.status.renderer = new ClassicRenderer({ className: "canvas" }).onLoad(); + this.status.model.bindRenderer(this.status.renderer); + + // 测试代码 + if (true) { + let group = this.status.model.addGroup(); + let range = this.status.model.addRange(); + range.color = [.1, .5, .9]; + group.new(100); + group.color = [.8, .1, .6]; + group.individuals.forEach((individual) => { + individual.position[0] = (Math.random() - .5) * 2; + individual.position[1] = (Math.random() - .5) * 2; + individual.position[2] = (Math.random() - .5) * 2; + }) + this.status.model.update(0); + } } public render(): ReactNode { return + + {this.renderContent()} + + + } + + private renderContent(): ReactNode { + return
- +
} }