Skip to content

Commit df3350d

Browse files
committed
feat(core): centralize configuration in component
1 parent 468d425 commit df3350d

File tree

12 files changed

+125
-75
lines changed

12 files changed

+125
-75
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Component } from "../Types";
2+
import { Components } from "../Components";
3+
import { Configurator } from "./src";
4+
5+
export * from "./src";
6+
7+
/**
8+
* A tool to manage all the configuration from the app centrally.
9+
*/
10+
export class ConfigManager extends Component {
11+
list = new Set<Configurator<any, any>>();
12+
13+
/** {@link Component.enabled} */
14+
enabled = true;
15+
16+
/**
17+
* A unique identifier for the component.
18+
* This UUID is used to register the component within the Components system.
19+
*/
20+
static readonly uuid = "dc86e7e9-a8fd-5473-9ef6-724c67fecb0f" as const;
21+
22+
constructor(components: Components) {
23+
super(components);
24+
components.add(ConfigManager.uuid, this);
25+
}
26+
}

packages/core/src/core/Types/src/config-manager.ts renamed to packages/core/src/core/ConfigManager/src/configurator.ts

Lines changed: 21 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,25 @@
1-
import * as THREE from "three";
1+
import {
2+
ControlsSchema,
3+
ControlEntry,
4+
BooleanSettingsControl,
5+
ColorSettingsControl,
6+
TextSettingsControl,
7+
NumberSettingControl,
8+
SelectSettingControl,
9+
Vector3SettingControl,
10+
TextSetSettingControl,
11+
NoControl,
12+
} from "../../Types";
13+
import { Components } from "../../Components";
14+
import { ConfigManager } from "../index";
215

3-
export interface BooleanSettingsControl {
4-
type: "Boolean";
5-
value: boolean;
6-
}
7-
8-
export interface ColorSettingsControl {
9-
type: "Color";
10-
opacity: number;
11-
value: THREE.Color;
12-
}
13-
14-
export interface TextSettingsControl {
15-
type: "Text";
16-
value: string;
17-
}
18-
19-
export interface NumberSettingControl {
20-
type: "Number";
21-
interpolable: boolean;
22-
min?: number;
23-
max?: number;
24-
value: number;
25-
}
26-
27-
export interface SelectSettingControl {
28-
type: "Select";
29-
multiple: boolean;
30-
options: Set<string>;
31-
value: string;
32-
}
33-
34-
export interface Vector3SettingControl {
35-
type: "Vector3";
36-
value: THREE.Vector3;
37-
}
38-
39-
export interface TextSetSettingControl {
40-
type: "TextSet";
41-
value: Set<string>;
42-
}
43-
44-
export interface NoControl {
45-
type: "None";
46-
value: any;
47-
}
48-
49-
type ControlEntry =
50-
| BooleanSettingsControl
51-
| ColorSettingsControl
52-
| TextSettingsControl
53-
| NumberSettingControl
54-
| SelectSettingControl
55-
| Vector3SettingControl
56-
| TextSetSettingControl
57-
| NoControl;
58-
59-
interface ControlsSchema {
60-
[name: string]: ControlEntry | ControlsSchema;
61-
}
62-
63-
export abstract class ConfigManager<T, U extends ControlsSchema> {
16+
export abstract class Configurator<T, U extends ControlsSchema> {
6417
protected abstract _config: U;
6518

6619
protected _component: T;
6720

21+
name: string;
22+
6823
get controls(): U {
6924
const copy: any = {};
7025
for (const name in this._config) {
@@ -74,8 +29,11 @@ export abstract class ConfigManager<T, U extends ControlsSchema> {
7429
return copy as U;
7530
}
7631

77-
constructor(component: T) {
32+
constructor(component: T, components: Components, name: string) {
7833
this._component = component;
34+
this.name = name;
35+
const configManager = components.get(ConfigManager);
36+
configManager.list.add(this);
7937
}
8038

8139
copyEntry(controlEntry: ControlEntry): ControlEntry {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./configurator";
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as THREE from "three";
2+
3+
export interface BooleanSettingsControl {
4+
type: "Boolean";
5+
value: boolean;
6+
}
7+
8+
export interface ColorSettingsControl {
9+
type: "Color";
10+
opacity: number;
11+
value: THREE.Color;
12+
}
13+
14+
export interface TextSettingsControl {
15+
type: "Text";
16+
value: string;
17+
}
18+
19+
export interface NumberSettingControl {
20+
type: "Number";
21+
interpolable: boolean;
22+
min?: number;
23+
max?: number;
24+
value: number;
25+
}
26+
27+
export interface SelectSettingControl {
28+
type: "Select";
29+
multiple: boolean;
30+
options: Set<string>;
31+
value: string;
32+
}
33+
34+
export interface Vector3SettingControl {
35+
type: "Vector3";
36+
value: THREE.Vector3;
37+
}
38+
39+
export interface TextSetSettingControl {
40+
type: "TextSet";
41+
value: Set<string>;
42+
}
43+
44+
export interface NoControl {
45+
type: "None";
46+
value: any;
47+
}
48+
49+
export type ControlEntry =
50+
| BooleanSettingsControl
51+
| ColorSettingsControl
52+
| TextSettingsControl
53+
| NumberSettingControl
54+
| SelectSettingControl
55+
| Vector3SettingControl
56+
| TextSetSettingControl
57+
| NoControl;
58+
59+
export interface ControlsSchema {
60+
[name: string]: ControlEntry | ControlsSchema;
61+
}

‎packages/core/src/core/Types/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export * from "./world";
1111
export * from "./data-set";
1212
export * from "./data-map";
1313
export * from "./component-with-ui";
14-
export * from "./config-manager";
14+
export * from "./config-types";

‎packages/core/src/core/Viewpoints/index.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ import {
99
import { Components } from "../Components";
1010
import { BCFViewpoint, Viewpoint } from "./src";
1111
import {
12-
ViewpointsConfigManger,
12+
ViewpointsConfigManager,
1313
ViewpointsConfig,
1414
} from "./src/viewpoints-config";
1515

1616
export * from "./src";
1717

1818
export class Viewpoints
1919
extends Component
20-
implements Disposable, Configurable<ViewpointsConfigManger, ViewpointsConfig>
20+
implements
21+
Disposable,
22+
Configurable<ViewpointsConfigManager, ViewpointsConfig>
2123
{
2224
static readonly uuid = "ee867824-a796-408d-8aa0-4e5962a83c66" as const;
2325

@@ -54,7 +56,7 @@ export class Viewpoints
5456

5557
onSetup = new Event();
5658

57-
config = new ViewpointsConfigManger(this);
59+
config = new ViewpointsConfigManager(this, this.components, "Viewpoints");
5860

5961
readonly onDisposed = new Event();
6062

‎packages/core/src/core/Viewpoints/src/viewpoints-config.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { BooleanSettingsControl, ConfigManager } from "../../Types";
1+
import { BooleanSettingsControl } from "../../Types";
22
import { Viewpoints } from "../index";
3+
import { Configurator } from "../../ConfigManager";
34

45
/**
56
* Configuration interface for the Viewpoints general behavior.
@@ -17,7 +18,7 @@ type ViewpointsConfigType = {
1718
overwriteColors: BooleanSettingsControl;
1819
};
1920

20-
export class ViewpointsConfigManger extends ConfigManager<
21+
export class ViewpointsConfigManager extends Configurator<
2122
Viewpoints,
2223
ViewpointsConfigType
2324
> {

‎packages/core/src/core/Worlds/src/simple-scene-config.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import * as THREE from "three";
33
import { SimpleScene } from "./simple-scene";
44
import {
55
ColorSettingsControl,
6-
ConfigManager,
76
NumberSettingControl,
87
Vector3SettingControl,
98
} from "../../Types";
9+
import { Configurator } from "../../ConfigManager";
1010

1111
type SimpleSceneConfigType = {
1212
backgroundColor: ColorSettingsControl;
@@ -112,7 +112,7 @@ export interface SimpleSceneConfig {
112112
};
113113
}
114114

115-
export class SimpleSceneConfigManager extends ConfigManager<
115+
export class SimpleSceneConfigManager extends Configurator<
116116
SimpleScene,
117117
SimpleSceneConfigType
118118
> {

‎packages/core/src/core/Worlds/src/simple-scene.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class SimpleScene
2626
readonly onSetup = new Event<SimpleScene>();
2727

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

3131
protected _defaultConfig: SimpleSceneConfig = {
3232
backgroundColor: new THREE.Color(0x202932),

‎packages/core/src/core/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from "./Cullers";
1010
export * from "./Viewpoints";
1111
export * from "./MiniMap";
1212
export * from "./OrthoPerspectiveCamera";
13+
export * from "./ConfigManager";

0 commit comments

Comments
 (0)