-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): improve configurable behavior
- Loading branch information
Showing
9 changed files
with
303 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Component } from "./component"; | ||
|
||
export type ComponentUIElement = { | ||
name: string; | ||
componentID: string; | ||
attributes: { [name: string]: string }; | ||
get: () => HTMLElement; | ||
}; | ||
|
||
export abstract class ComponentWithUI extends Component { | ||
abstract name: string; | ||
abstract getUI(): ComponentUIElement[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export interface BooleanSettingsControl { | ||
type: "Boolean"; | ||
value: boolean; | ||
} | ||
|
||
export interface ColorSettingsControl { | ||
type: "Color"; | ||
opacity: number; | ||
value: number; | ||
} | ||
|
||
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"; | ||
options?: string[]; | ||
value: string; | ||
} | ||
|
||
export interface VectorSettingControl { | ||
type: "Vector"; | ||
value: number[]; | ||
} | ||
|
||
type ControlEntry = | ||
| BooleanSettingsControl | ||
| ColorSettingsControl | ||
| TextSettingsControl | ||
| NumberSettingControl | ||
| SelectSettingControl | ||
| VectorSettingControl; | ||
|
||
interface ControlsSchema { | ||
[name: string]: ControlEntry | ControlsSchema; | ||
} | ||
|
||
export abstract class ConfigManager<T, U extends ControlsSchema> { | ||
protected abstract _list: U; | ||
|
||
protected _component: T; | ||
|
||
get controls() { | ||
return JSON.parse(JSON.stringify(this._list)); | ||
} | ||
|
||
constructor(component: T) { | ||
this._component = component; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
packages/core/src/core/Worlds/src/simple-scene-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// eslint-disable-next-line max-classes-per-file | ||
import * as THREE from "three"; | ||
import { SimpleScene } from "./simple-scene"; | ||
import { | ||
ColorSettingsControl, | ||
ConfigManager, | ||
NumberSettingControl, | ||
VectorSettingControl, | ||
} from "../../Types"; | ||
|
||
type SimpleSceneConfigType = { | ||
backgroundColor: ColorSettingsControl; | ||
ambientLight: { | ||
color: ColorSettingsControl; | ||
intensity: NumberSettingControl; | ||
}; | ||
directionalLight: { | ||
color: ColorSettingsControl; | ||
intensity: NumberSettingControl; | ||
position: VectorSettingControl; | ||
}; | ||
}; | ||
|
||
class DirectionalLightConfig { | ||
private _list: SimpleSceneConfigType; | ||
private _scene: SimpleScene; | ||
|
||
constructor(list: SimpleSceneConfigType, scene: SimpleScene) { | ||
this._list = list; | ||
this._scene = scene; | ||
} | ||
|
||
get color() { | ||
return this._list.directionalLight.color.value; | ||
} | ||
|
||
set color(value: number) { | ||
this._list.directionalLight.color.value = value; | ||
for (const [, light] of this._scene.directionalLights) { | ||
light.color = new THREE.Color(value); | ||
} | ||
} | ||
|
||
get intensity() { | ||
return this._list.directionalLight.intensity.value; | ||
} | ||
|
||
set intensity(value: number) { | ||
this._list.directionalLight.intensity.value = value; | ||
for (const [, light] of this._scene.directionalLights) { | ||
light.intensity = value; | ||
} | ||
} | ||
|
||
get position() { | ||
return this._list.directionalLight.position.value; | ||
} | ||
|
||
set position(value: number[]) { | ||
this._list.directionalLight.position.value = value; | ||
const [x, y, z] = value; | ||
for (const [, light] of this._scene.directionalLights) { | ||
light.position.set(x, y, z); | ||
} | ||
} | ||
} | ||
|
||
class AmbientLightConfig { | ||
private _list: SimpleSceneConfigType; | ||
private _scene: SimpleScene; | ||
|
||
constructor(list: SimpleSceneConfigType, scene: SimpleScene) { | ||
this._list = list; | ||
this._scene = scene; | ||
} | ||
|
||
get color() { | ||
return this._list.ambientLight.color.value; | ||
} | ||
|
||
set color(value: number) { | ||
this._list.ambientLight.color.value = value; | ||
for (const [, light] of this._scene.ambientLights) { | ||
light.color = new THREE.Color(value); | ||
} | ||
} | ||
|
||
get intensity() { | ||
return this._list.ambientLight.intensity.value; | ||
} | ||
|
||
set intensity(value: number) { | ||
this._list.ambientLight.intensity.value = value; | ||
for (const [, light] of this._scene.ambientLights) { | ||
light.intensity = value; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Configuration interface for the {@link SimpleScene}. | ||
*/ | ||
export interface SimpleSceneConfig { | ||
directionalLight: { | ||
color: THREE.Color; | ||
intensity: number; | ||
position: THREE.Vector3; | ||
}; | ||
ambientLight: { | ||
color: THREE.Color; | ||
intensity: number; | ||
}; | ||
} | ||
|
||
export class SimpleSceneConfigManager extends ConfigManager< | ||
SimpleScene, | ||
SimpleSceneConfigType | ||
> { | ||
protected _list = { | ||
backgroundColor: { | ||
value: 0, | ||
opacity: 1, | ||
type: "Color" as const, | ||
}, | ||
ambientLight: { | ||
color: { | ||
type: "Color" as const, | ||
opacity: 1, | ||
value: 1, | ||
}, | ||
intensity: { | ||
type: "Number" as const, | ||
interpolable: false, | ||
value: 2, | ||
}, | ||
}, | ||
directionalLight: { | ||
color: { | ||
type: "Color" as const, | ||
opacity: 1, | ||
value: 1, | ||
}, | ||
intensity: { | ||
type: "Number" as const, | ||
interpolable: false, | ||
value: 2, | ||
}, | ||
position: { | ||
type: "Vector" as const, | ||
value: [], | ||
}, | ||
}, | ||
}; | ||
|
||
ambientLight = new AmbientLightConfig(this._list, this._component); | ||
|
||
directionalLight = new DirectionalLightConfig(this._list, this._component); | ||
|
||
get backgroundColor() { | ||
return this._list.backgroundColor.value; | ||
} | ||
|
||
set backgroundColor(value: number) { | ||
this._list.backgroundColor.value = value; | ||
this._component.three.background = new THREE.Color(value); | ||
} | ||
} |
Oops, something went wrong.