Skip to content

Commit

Permalink
feat(core): centralize configuration in component
Browse files Browse the repository at this point in the history
  • Loading branch information
agviegas committed Aug 31, 2024
1 parent 468d425 commit df3350d
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 75 deletions.
26 changes: 26 additions & 0 deletions packages/core/src/core/ConfigManager/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Component } from "../Types";
import { Components } from "../Components";
import { Configurator } from "./src";

export * from "./src";

/**
* A tool to manage all the configuration from the app centrally.
*/
export class ConfigManager extends Component {
list = new Set<Configurator<any, any>>();

/** {@link Component.enabled} */
enabled = true;

/**
* A unique identifier for the component.
* This UUID is used to register the component within the Components system.
*/
static readonly uuid = "dc86e7e9-a8fd-5473-9ef6-724c67fecb0f" as const;

constructor(components: Components) {
super(components);
components.add(ConfigManager.uuid, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,70 +1,25 @@
import * as THREE from "three";
import {
ControlsSchema,
ControlEntry,
BooleanSettingsControl,
ColorSettingsControl,
TextSettingsControl,
NumberSettingControl,
SelectSettingControl,
Vector3SettingControl,
TextSetSettingControl,
NoControl,
} from "../../Types";
import { Components } from "../../Components";
import { ConfigManager } from "../index";

export interface BooleanSettingsControl {
type: "Boolean";
value: boolean;
}

export interface ColorSettingsControl {
type: "Color";
opacity: number;
value: THREE.Color;
}

export interface TextSettingsControl {
type: "Text";
value: string;
}

export interface NumberSettingControl {
type: "Number";
interpolable: boolean;
min?: number;
max?: number;
value: number;
}

export interface SelectSettingControl {
type: "Select";
multiple: boolean;
options: Set<string>;
value: string;
}

export interface Vector3SettingControl {
type: "Vector3";
value: THREE.Vector3;
}

export interface TextSetSettingControl {
type: "TextSet";
value: Set<string>;
}

export interface NoControl {
type: "None";
value: any;
}

type ControlEntry =
| BooleanSettingsControl
| ColorSettingsControl
| TextSettingsControl
| NumberSettingControl
| SelectSettingControl
| Vector3SettingControl
| TextSetSettingControl
| NoControl;

interface ControlsSchema {
[name: string]: ControlEntry | ControlsSchema;
}

export abstract class ConfigManager<T, U extends ControlsSchema> {
export abstract class Configurator<T, U extends ControlsSchema> {
protected abstract _config: U;

protected _component: T;

name: string;

get controls(): U {
const copy: any = {};
for (const name in this._config) {
Expand All @@ -74,8 +29,11 @@ export abstract class ConfigManager<T, U extends ControlsSchema> {
return copy as U;
}

constructor(component: T) {
constructor(component: T, components: Components, name: string) {
this._component = component;
this.name = name;
const configManager = components.get(ConfigManager);
configManager.list.add(this);
}

copyEntry(controlEntry: ControlEntry): ControlEntry {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/core/ConfigManager/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./configurator";
61 changes: 61 additions & 0 deletions packages/core/src/core/Types/src/config-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as THREE from "three";

export interface BooleanSettingsControl {
type: "Boolean";
value: boolean;
}

export interface ColorSettingsControl {
type: "Color";
opacity: number;
value: THREE.Color;
}

export interface TextSettingsControl {
type: "Text";
value: string;
}

export interface NumberSettingControl {
type: "Number";
interpolable: boolean;
min?: number;
max?: number;
value: number;
}

export interface SelectSettingControl {
type: "Select";
multiple: boolean;
options: Set<string>;
value: string;
}

export interface Vector3SettingControl {
type: "Vector3";
value: THREE.Vector3;
}

export interface TextSetSettingControl {
type: "TextSet";
value: Set<string>;
}

export interface NoControl {
type: "None";
value: any;
}

export type ControlEntry =
| BooleanSettingsControl
| ColorSettingsControl
| TextSettingsControl
| NumberSettingControl
| SelectSettingControl
| Vector3SettingControl
| TextSetSettingControl
| NoControl;

export interface ControlsSchema {
[name: string]: ControlEntry | ControlsSchema;
}
2 changes: 1 addition & 1 deletion packages/core/src/core/Types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export * from "./world";
export * from "./data-set";
export * from "./data-map";
export * from "./component-with-ui";
export * from "./config-manager";
export * from "./config-types";
8 changes: 5 additions & 3 deletions packages/core/src/core/Viewpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import {
import { Components } from "../Components";
import { BCFViewpoint, Viewpoint } from "./src";
import {
ViewpointsConfigManger,
ViewpointsConfigManager,
ViewpointsConfig,
} from "./src/viewpoints-config";

export * from "./src";

export class Viewpoints
extends Component
implements Disposable, Configurable<ViewpointsConfigManger, ViewpointsConfig>
implements
Disposable,
Configurable<ViewpointsConfigManager, ViewpointsConfig>
{
static readonly uuid = "ee867824-a796-408d-8aa0-4e5962a83c66" as const;

Expand Down Expand Up @@ -54,7 +56,7 @@ export class Viewpoints

onSetup = new Event();

config = new ViewpointsConfigManger(this);
config = new ViewpointsConfigManager(this, this.components, "Viewpoints");

readonly onDisposed = new Event();

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/core/Viewpoints/src/viewpoints-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BooleanSettingsControl, ConfigManager } from "../../Types";
import { BooleanSettingsControl } from "../../Types";
import { Viewpoints } from "../index";
import { Configurator } from "../../ConfigManager";

/**
* Configuration interface for the Viewpoints general behavior.
Expand All @@ -17,7 +18,7 @@ type ViewpointsConfigType = {
overwriteColors: BooleanSettingsControl;
};

export class ViewpointsConfigManger extends ConfigManager<
export class ViewpointsConfigManager extends Configurator<
Viewpoints,
ViewpointsConfigType
> {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/core/Worlds/src/simple-scene-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import * as THREE from "three";
import { SimpleScene } from "./simple-scene";
import {
ColorSettingsControl,
ConfigManager,
NumberSettingControl,
Vector3SettingControl,
} from "../../Types";
import { Configurator } from "../../ConfigManager";

type SimpleSceneConfigType = {
backgroundColor: ColorSettingsControl;
Expand Down Expand Up @@ -112,7 +112,7 @@ export interface SimpleSceneConfig {
};
}

export class SimpleSceneConfigManager extends ConfigManager<
export class SimpleSceneConfigManager extends Configurator<
SimpleScene,
SimpleSceneConfigType
> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/Worlds/src/simple-scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class SimpleScene
readonly onSetup = new Event<SimpleScene>();

/** {@link Configurable.config} */
config = new SimpleSceneConfigManager(this);
config = new SimpleSceneConfigManager(this, this.components, "Scene");

protected _defaultConfig: SimpleSceneConfig = {
backgroundColor: new THREE.Color(0x202932),
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./Cullers";
export * from "./Viewpoints";
export * from "./MiniMap";
export * from "./OrthoPerspectiveCamera";
export * from "./ConfigManager";
2 changes: 1 addition & 1 deletion packages/core/src/openbim/BCFTopics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class BCFTopics
ignoreIncompleteTopicsOnImport: false,
};

config = new BCFTopicsConfigManager(this);
config = new BCFTopicsConfigManager(this, this.components, "BCF Topics");

readonly list = new DataMap<string, Topic>();

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/openbim/BCFTopics/src/bcf-topics-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BCFTopics, BCFVersion } from "../index";
import {
BooleanSettingsControl,
ConfigManager,
Configurator,
SelectSettingControl,
TextSetSettingControl,
TextSettingsControl,
Expand Down Expand Up @@ -110,7 +110,7 @@ type BCFTopicsConfigType = {
ignoreIncompleteTopicsOnImport: BooleanSettingsControl;
};

export class BCFTopicsConfigManager extends ConfigManager<
export class BCFTopicsConfigManager extends Configurator<
BCFTopics,
BCFTopicsConfigType
> {
Expand Down

0 comments on commit df3350d

Please sign in to comment.