Skip to content

Commit

Permalink
Add possibility for having update() function on GuiComponents as well
Browse files Browse the repository at this point in the history
  • Loading branch information
amaliejvik committed Jun 28, 2024
1 parent 565a48b commit 4af059a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Components/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ export type Draggable =

export interface GuiComponent {
htmlElement: HTMLElement;
update?(camera?: OrthographicCamera): void;
}
15 changes: 12 additions & 3 deletions src/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Core {
components: Component[];
draggables: Component[];
updateComponents: Component[];
updateGuiComponents: GuiComponent[];
plotCount: number = 0;
stats?: Stats;
renderer: WebGLRenderer;
Expand Down Expand Up @@ -124,6 +125,7 @@ class Core {
this.components = [];
this.draggables = [];
this.updateComponents = [];
this.updateGuiComponents = [];
if (process.env.NODE_ENV === "development") {
this.stats = new Stats();
this.stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom
Expand Down Expand Up @@ -202,6 +204,9 @@ class Core {
this.updateComponents.forEach((component) => {
if (component.update) component.update(this.camera);
});
this.updateGuiComponents.forEach((component) => {
if (component.update) component.update(this.camera);
});
if (this.onUpdateFunction)
this.onUpdateFunction(this.clock.getElapsedTime());
this.domRenderer.render(this.scene, this.camera);
Expand Down Expand Up @@ -245,12 +250,16 @@ class Core {

addGui(component: GuiComponent) {
this.guiRoot.appendChild(component.htmlElement);
this.updateGuiComponents(component);
this.updateLegend(component);

if (component.update && !this.updateGuiComponents.includes(component)) {
this.updateGuiComponents.push(component);
}
}

removeGui(component: GuiComponent) {
this.guiRoot.removeChild(component.htmlElement);
this.updateGuiComponents(component);
this.updateLegend(component);
}

startClock(): void {
Expand Down Expand Up @@ -278,7 +287,7 @@ class Core {
return this.components;
}

private updateGuiComponents(guiComponent: GuiComponent) {
private updateLegend(guiComponent: GuiComponent) {
if (guiComponent instanceof LegendBox) {
guiComponent.updatePlots();
}
Expand Down

0 comments on commit 4af059a

Please sign in to comment.