Add archive model & status

This commit is contained in:
MrKBear 2022-02-24 17:12:57 +08:00
parent 045a0377ee
commit 586c7b959d
6 changed files with 136 additions and 5 deletions

53
source/Context/Status.tsx Normal file
View 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
};

View File

@ -156,7 +156,7 @@ abstract class BasicRenderer<
/**
*
*/
abstract onLoad(): void;
abstract onLoad(): this;
/**
*

View File

@ -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
View 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;

View File

@ -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();

View File

@ -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>
}
}