11import * as THREE from "three" ;
2- import {
3- ControlsSchema ,
4- ControlEntry ,
5- BooleanSettingsControl ,
6- ColorSettingsControl ,
7- TextSettingsControl ,
8- NumberSettingControl ,
9- SelectSettingControl ,
10- Vector3SettingControl ,
11- TextSetSettingControl ,
12- NoControl ,
13- } from "../../Types" ;
2+ import { ControlsSchema , ControlsUtils } from "../../Types" ;
143import { Components } from "../../Components" ;
154import { ConfigManager } from "../index" ;
165import { UUID } from "../../../utils" ;
@@ -28,12 +17,7 @@ export abstract class Configurator<
2817 uuid : string ;
2918
3019 get controls ( ) : U {
31- const copy : any = { } ;
32- for ( const name in this . _config ) {
33- const entry = this . _config [ name ] as ControlEntry ;
34- copy [ name ] = this . copyEntry ( entry ) ;
35- }
36- return copy as U ;
20+ return ControlsUtils . copySchema ( this . _config ) ;
3721 }
3822
3923 constructor (
@@ -58,116 +42,61 @@ export abstract class Configurator<
5842 }
5943 }
6044
61- export ( ) {
62- const serializedData : any = { } ;
63- for ( const id in this . _config ) {
64- const control = this . _config [ id ] ;
65-
66- if ( control . type === "Color" ) {
67- const { r, g, b } = control . value ;
68- serializedData [ id ] = { ...control , value : { r, g, b } } ;
69- } else if ( control . type === "Vector3" ) {
70- const { x, y, z } = control . value ;
71- serializedData [ id ] = { ...control , value : { x, y, z } } ;
72- } else if ( control . type === "TextSet" ) {
73- const value = Array . from ( control . value ) ;
74- serializedData [ id ] = { ...control , value } ;
75- } else if ( control . type === "Select" ) {
76- const options = Array . from ( control . options ) ;
77- serializedData [ id ] = { ...control , options } ;
45+ export ( controls = this . _config as ControlsSchema , exported : any = { } ) {
46+ for ( const id in controls ) {
47+ const control = controls [ id ] ;
48+ const isControl = ControlsUtils . isEntry ( control ) ;
49+ if ( isControl ) {
50+ if ( control . type === "Color" ) {
51+ const { r, g, b } = control . value ;
52+ exported [ id ] = { ...control , value : { r, g, b } } ;
53+ } else if ( control . type === "Vector3" ) {
54+ const { x, y, z } = control . value ;
55+ exported [ id ] = { ...control , value : { x, y, z } } ;
56+ } else if ( control . type === "TextSet" ) {
57+ const value = Array . from ( control . value ) ;
58+ exported [ id ] = { ...control , value } ;
59+ } else if ( control . type === "Select" ) {
60+ const options = Array . from ( control . options ) ;
61+ exported [ id ] = { ...control , options } ;
62+ } else {
63+ exported [ id ] = { ...control } ;
64+ }
7865 } else {
79- serializedData [ id ] = { ...control } ;
66+ exported [ id ] = { } ;
67+ this . export ( control as ControlsSchema , exported [ id ] ) ;
8068 }
8169 }
8270
83- return serializedData ;
71+ return exported ;
8472 }
8573
86- import ( serializedData : any ) {
87- const imported : any = { } ;
88-
89- for ( const id in serializedData ) {
90- const control = serializedData [ id ] ;
91- if ( control . type === "Color" ) {
92- const { r, g, b } = control . value ;
93- imported [ id ] = { ...control , value : new THREE . Color ( r , g , b ) } ;
94- } else if ( control . type === "Vector3" ) {
95- const { x, y, z } = control . value ;
96- imported [ id ] = { ...control , value : new THREE . Vector3 ( x , y , z ) } ;
97- } else if ( control . type === "TextSet" ) {
98- imported [ id ] = { ...control , value : new Set ( control . value ) } ;
99- } else if ( control . type === "Select" ) {
100- imported [ id ] = { ...control , options : new Set ( control . options ) } ;
74+ import ( exported : any , imported : any = { } , first = true ) {
75+ for ( const id in exported ) {
76+ const control = exported [ id ] ;
77+ const isControl = ControlsUtils . isEntry ( control ) ;
78+ if ( isControl ) {
79+ if ( control . type === "Color" ) {
80+ const { r, g, b } = control . value ;
81+ imported [ id ] = { ...control , value : new THREE . Color ( r , g , b ) } ;
82+ } else if ( control . type === "Vector3" ) {
83+ const { x, y, z } = control . value ;
84+ imported [ id ] = { ...control , value : new THREE . Vector3 ( x , y , z ) } ;
85+ } else if ( control . type === "TextSet" ) {
86+ imported [ id ] = { ...control , value : new Set ( control . value ) } ;
87+ } else if ( control . type === "Select" ) {
88+ imported [ id ] = { ...control , options : new Set ( control . options ) } ;
89+ } else {
90+ imported [ id ] = { ...control } ;
91+ }
10192 } else {
102- imported [ id ] = { ...control } ;
93+ imported [ id ] = { } ;
94+ this . import ( control , imported [ id ] , false ) ;
10395 }
10496 }
10597
106- this . set ( imported ) ;
107- }
108-
109- copyEntry ( controlEntry : ControlEntry ) : ControlEntry {
110- if ( controlEntry . type === "Boolean" ) {
111- const entry = controlEntry as BooleanSettingsControl ;
112- return {
113- type : entry . type ,
114- value : entry . value ,
115- } ;
116- }
117- if ( controlEntry . type === "Color" ) {
118- const entry = controlEntry as ColorSettingsControl ;
119- return {
120- type : entry . type ,
121- value : entry . value . clone ( ) ,
122- } ;
123- }
124- if ( controlEntry . type === "Text" ) {
125- const entry = controlEntry as TextSettingsControl ;
126- return {
127- type : entry . type ,
128- value : entry . value ,
129- } ;
130- }
131- if ( controlEntry . type === "Number" ) {
132- const entry = controlEntry as NumberSettingControl ;
133- return {
134- type : entry . type ,
135- value : entry . value ,
136- min : entry . min ,
137- max : entry . max ,
138- interpolable : entry . interpolable ,
139- } ;
140- }
141- if ( controlEntry . type === "Select" ) {
142- const entry = controlEntry as SelectSettingControl ;
143- return {
144- type : entry . type ,
145- value : entry . value ,
146- multiple : entry . multiple ,
147- options : new Set ( entry . options ) ,
148- } ;
149- }
150- if ( controlEntry . type === "Vector3" ) {
151- const entry = controlEntry as Vector3SettingControl ;
152- return {
153- type : entry . type ,
154- value : entry . value . clone ( ) ,
155- } ;
156- }
157- if ( controlEntry . type === "TextSet" ) {
158- const entry = controlEntry as TextSetSettingControl ;
159- return {
160- type : entry . type ,
161- value : new Set ( entry . value ) ,
162- } ;
163- }
164- if ( controlEntry . type === "None" ) {
165- const entry = controlEntry as NoControl ;
166- return {
167- type : entry . type ,
168- value : entry . value ,
169- } ;
98+ if ( first ) {
99+ this . set ( imported ) ;
170100 }
171- throw new Error ( "Invalid entry!" ) ;
172101 }
173102}
0 commit comments