Add archive model & status
This commit is contained in:
parent
045a0377ee
commit
586c7b959d
53
source/Context/Status.tsx
Normal file
53
source/Context/Status.tsx
Normal file
@ -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<Status>(new Status());
|
||||
|
||||
StatusContext.displayName = "Status";
|
||||
const StatusProvider = StatusContext.Provider;
|
||||
const StatusConsumer = StatusContext.Consumer;
|
||||
|
||||
type RenderComponent = (new (...p: any) => Component<any, any, any>) | FunctionComponent<any>;
|
||||
|
||||
/**
|
||||
* 修饰器
|
||||
*/
|
||||
function useStatus<R extends RenderComponent>(components: R): R {
|
||||
return ((props: any) => {
|
||||
const C = components;
|
||||
return <StatusConsumer>
|
||||
{(status: Status) => <C {...props} status={status}></C>}
|
||||
</StatusConsumer>
|
||||
}) as any;
|
||||
}
|
||||
|
||||
export {
|
||||
Status, StatusContext, useStatus,
|
||||
IMixinStatusProps, StatusProvider, StatusConsumer
|
||||
};
|
@ -156,7 +156,7 @@ abstract class BasicRenderer<
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
abstract onLoad(): void;
|
||||
abstract onLoad(): this;
|
||||
|
||||
/**
|
||||
* 渲染器执行
|
||||
|
@ -34,7 +34,7 @@ class ClassicRenderer extends BasicRenderer<{}, IClassicRendererParams> {
|
||||
*/
|
||||
private objectPool = new Map<ObjectID, DisplayObject>();
|
||||
|
||||
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 {
|
||||
|
42
source/Model/Archive.ts
Normal file
42
source/Model/Archive.ts
Normal file
@ -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<EventType, any> = {}
|
||||
> extends Emitter<E> {
|
||||
|
||||
/**
|
||||
* 是否为新文件
|
||||
*/
|
||||
public isNewFile: boolean = true;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
public fileName?: string;
|
||||
|
||||
/**
|
||||
* 文件数据
|
||||
*/
|
||||
public fileData?: M;
|
||||
|
||||
/**
|
||||
* 保存文件
|
||||
* 模型转换为文件
|
||||
*/
|
||||
public save() {};
|
||||
|
||||
/**
|
||||
* 加载文件为模型
|
||||
* return Model
|
||||
*/
|
||||
public load() {};
|
||||
}
|
||||
|
||||
export { Archive };
|
||||
export default Archive;
|
@ -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();
|
||||
|
@ -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 <SettingProvider value={this.setting}>
|
||||
<StatusProvider value={this.status}>
|
||||
{this.renderContent()}
|
||||
</StatusProvider>
|
||||
</SettingProvider>
|
||||
}
|
||||
|
||||
private renderContent(): ReactNode {
|
||||
return <div className="app-root">
|
||||
<HeaderBar/>
|
||||
<Theme
|
||||
className="test"
|
||||
@ -32,7 +67,7 @@ class SimulatorWeb extends Component {
|
||||
Theme
|
||||
</Theme>
|
||||
<Localization i18nKey="EN_US"/>
|
||||
</SettingProvider>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user