Skip to content

Commit 688c244

Browse files
author
Les Moffat
committed
feat(cubemap / gradient): add logic for choosing priority of options.
1 parent 4fb055d commit 688c244

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/custom-layers/CubemapLayer/CubemapLayer.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@ const defaultConstructorOptions: CubemapLayerConstructorOptions = {
2323
preset: "universe-dark",
2424
};
2525

26+
function configureOptions(inputOptions: CubemapLayerConstructorOptions | true, defaults: CubemapLayerConstructorOptions) {
27+
if (inputOptions === true) {
28+
return defaults;
29+
}
30+
31+
const outputOptions = {
32+
...defaults,
33+
...inputOptions,
34+
};
35+
36+
// if input has faces defined, this takes precendence
37+
if (inputOptions.faces) {
38+
delete outputOptions.preset;
39+
return outputOptions as CubemapLayerConstructorOptions;
40+
}
41+
42+
// - Use path if defined.
43+
// - Path takes precendence over preset.
44+
// - Because we would have returned faces if it was defined
45+
// we don't need to delete it
46+
if (inputOptions.path) {
47+
delete outputOptions.preset;
48+
return outputOptions as CubemapLayerConstructorOptions;
49+
}
50+
51+
// path / faces will not be defined at this point
52+
// so we don't need to delete them
53+
return outputOptions as CubemapLayerConstructorOptions;
54+
}
55+
2656
class CubemapLayer implements CustomLayerInterface {
2757
public id: string = "Cubemap Layer";
2858
public type: CustomLayerInterface["type"] = "custom";
@@ -39,14 +69,8 @@ class CubemapLayer implements CustomLayerInterface {
3969
private cubemap?: Object3D<(typeof ATTRIBUTES_KEYS)[number], (typeof UNIFORMS_KEYS)[number]>;
4070
private texture?: WebGLTexture;
4171

42-
constructor(cubemapConfig: CubemapLayerConstructorOptions | boolean) {
43-
const options =
44-
typeof cubemapConfig === "boolean"
45-
? defaultConstructorOptions
46-
: {
47-
...defaultConstructorOptions,
48-
...cubemapConfig,
49-
};
72+
constructor(cubemapConfig: CubemapLayerConstructorOptions | true) {
73+
const options = configureOptions(cubemapConfig, defaultConstructorOptions);
5074

5175
this.bgColor = parseColorStringToVec4(options.color);
5276
this.faces = getCubemapFaces(options as CubemapDefinition);

src/custom-layers/extractCustomLayerStyle.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export interface IExtractCustomLayerStyleOptions {
1919
property: "halo" | "space";
2020
}
2121

22-
export type CustomLayerDefinitionType = CubemapLayerConstructorOptions | RadialGradientLayerOptions | null
23-
;
22+
export type CustomLayerDefinitionType = CubemapLayerConstructorOptions | RadialGradientLayerOptions | null;
2423

2524
export default function extractCustomLayerStyle(options: IExtractCustomLayerStyleOptions): CustomLayerDefinitionType {
2625
const { map, property } = options;

0 commit comments

Comments
 (0)