102 | | case "boolean": |
103 | | // More space for the label |
104 | | let text = Engine.GetGUIObjectByName(prefix + "Label[" + i + "]"); |
105 | | let size = text.size; |
106 | | size.rright = 87; |
107 | | text.size = size; |
108 | | var control = Engine.GetGUIObjectByName(prefix + "Tickbox[" + i + "]"); |
109 | | var checked; |
110 | | var onPress = function(){}; |
111 | | // Different option action load and save differently, so this switch is needed. |
112 | | for each (var action in Object.keys(option[2])) |
| 105 | case "boolean": |
| 106 | // More space for the label |
| 107 | let text = Engine.GetGUIObjectByName(prefix + "Label[" + i + "]"); |
| 108 | let size = text.size; |
| 109 | size.rright = 87; |
| 110 | text.size = size; |
| 111 | control = Engine.GetGUIObjectByName(prefix + "Tickbox[" + i + "]"); |
| 112 | var checked; |
| 113 | // Different option action load and save differently, so this switch is needed. |
| 114 | for (let action of Object.keys(option[2])) |
| 115 | { |
| 116 | switch (action) |
116 | | case "config": |
117 | | // Load initial value if not yet loaded. |
118 | | if (!checked || typeof checked != "boolean") |
119 | | checked = Engine.ConfigDB_GetValue("user", option[2][action]) === "true" ? true : false; |
120 | | // Hacky macro to create the callback. |
121 | | var callback = function(key) |
122 | | { |
123 | | return function() |
124 | | Engine.ConfigDB_CreateValue("user", key, String(this.checked)); |
125 | | }(option[2][action]); |
126 | | // Merge the new callback with any existing callbacks. |
127 | | onPress = mergeFunctions(callback, onPress); |
128 | | break; |
129 | | case "renderer": |
130 | | // Load initial value if not yet loaded. |
131 | | if (!checked || typeof checked != "boolean") |
132 | | checked = eval("Engine.Renderer_Get" + option[2][action] + "Enabled()"); |
133 | | // Hacky macro to create the callback. |
134 | | var callback = function(key) |
135 | | { |
136 | | return function() |
137 | | eval("Engine.Renderer_Set" + key + "Enabled(" + this.checked + ")"); |
138 | | }(option[2][action]); |
139 | | // Merge the new callback with any existing callbacks. |
140 | | onPress = mergeFunctions(callback, onPress); |
141 | | break; |
142 | | case "function": |
143 | | // This allows for doing low-level actions, like hiding/showing UI elements. |
144 | | onPress = mergeFunctions(eval("function(){" + option[2][action] + "}"), onPress); |
145 | | break; |
146 | | default: |
147 | | warn("Unknown option source type '" + action + "'"); |
| 125 | return function() |
| 126 | Engine.ConfigDB_CreateValue("user", key, String(this.checked)); |
| 127 | }(option[2][action]); |
| 128 | // Merge the new callback with any existing callbacks. |
| 129 | onPress = mergeFunctions(callback, onPress); |
| 130 | break; |
| 131 | case "renderer": |
| 132 | // If reloading, config values have priority, otherwise load initial value if not yet loaded |
| 133 | if (reload && option[2].config) |
| 134 | { |
| 135 | checked = Engine.ConfigDB_GetValue("user", option[2].config) === "true"; |
| 136 | let rendererChecked = eval("Engine.Renderer_Get" + option[2].renderer + "Enabled()"); |
| 137 | if (rendererChecked != checked) |
| 138 | eval("Engine.Renderer_Set" + option[2].renderer + "Enabled(" + checked + ")"); |
| 140 | else if (!checked || typeof checked != "boolean") |
| 141 | checked = eval("Engine.Renderer_Get" + option[2][action] + "Enabled()"); |
| 142 | // Hacky macro to create the callback (updating also the config value if any). |
| 143 | var callback = function(key, keyConfig) |
| 144 | { |
| 145 | return function() |
| 146 | { |
| 147 | eval("Engine.Renderer_Set" + key + "Enabled(" + this.checked + ")"); |
| 148 | if (keyConfig) |
| 149 | Engine.ConfigDB_CreateValue("user", keyConfig, String(this.checked)); |
| 150 | } |
| 151 | }(option[2][action], option[2].config); |
| 152 | // Merge the new callback with any existing callbacks. |
| 153 | onPress = mergeFunctions(callback, onPress); |
| 154 | break; |
| 155 | case "function": |
| 156 | // This allows for doing low-level actions, like hiding/showing UI elements. |
| 157 | onPress = mergeFunctions(eval("function(){" + option[2][action] + "}"), onPress); |
| 158 | break; |
| 159 | default: |
| 160 | warn("Unknown option source type '" + action + "'"); |
150 | | // Load final data to the control element. |
151 | | control.checked = checked; |
152 | | control.onPress = onPress; |
153 | | break; |
154 | | case "number": |
155 | | // TODO: Slider |
156 | | case "string": |
157 | | var control = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]"); |
158 | | var caption; |
159 | | var onPress = function(){}; |
160 | | for each (var action in Object.keys(option[2])) |
| 162 | } |
| 163 | // Load final data to the control element. |
| 164 | control.checked = checked; |
| 165 | control.onPress = onPress; |
| 166 | break; |
| 167 | case "number": |
| 168 | // TODO: Slider |
| 169 | case "string": |
| 170 | control = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]"); |
| 171 | var caption; |
| 172 | for (let action of Object.keys(option[2])) |
| 173 | { |
| 174 | switch (action) |
164 | | case "config": |
165 | | // Load initial value if not yet loaded. |
166 | | if (!checked || typeof checked != boolean) |
167 | | caption = Engine.ConfigDB_GetValue("user", option[2][action]); |
168 | | // Hacky macro to create the callback. |
169 | | var callback = function(key) |
170 | | { |
171 | | return function() |
172 | | Engine.ConfigDB_CreateValue("user", key, String(this.caption)); |
173 | | }(option[2][action]); |
174 | | // Merge the new callback with any existing callbacks. |
175 | | onPress = mergeFunctions(callback, onPress); |
176 | | break; |
177 | | case "function": |
178 | | // This allows for doing low-level actions, like hiding/showing UI elements. |
179 | | onPress = mergeFunctions(function(){eval(option[2][action])}, onPress); |
180 | | break; |
181 | | default: |
182 | | warn("Unknown option source type '" + action + "'"); |
183 | | } |
| 182 | return function() |
| 183 | { |
| 184 | if (Engine.ConfigDB_GetValue("user", key) == this.caption) |
| 185 | return; |
| 186 | Engine.ConfigDB_CreateValue("user", key, String(this.caption)); |
| 187 | g_hasChanges = true; |
| 188 | } |
| 189 | }(option[2][action]); |
| 190 | // Merge the new callback with any existing callbacks. |
| 191 | onPress = mergeFunctions(callback, onPress); |
| 192 | break; |
| 193 | case "function": |
| 194 | // This allows for doing low-level actions, like hiding/showing UI elements. |
| 195 | onPress = mergeFunctions(function(){eval(option[2][action])}, onPress); |
| 196 | break; |
| 197 | default: |
| 198 | warn("Unknown option source type '" + action + "'"); |
185 | | control.caption = caption; |
186 | | control.onPress = onPress; |
187 | | break; |
188 | | default: |
189 | | warn("Unknown option type '" + options[3] + "', assuming string. Valid types are 'number', 'string', or 'bool'."); |
190 | | var control = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]"); |
191 | | break; |
| 200 | } |
| 201 | control.caption = caption; |
| 202 | control.onPress = onPress; |
| 203 | control.onMouseLeave = onPress; |
| 204 | break; |
| 205 | default: |
| 206 | warn("Unknown option type '" + options[3] + "', assuming string. Valid types are 'number', 'string', or 'bool'."); |
| 207 | control = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]"); |
| 208 | break; |
217 | | // Revert all changes if they were not saved on the disk |
218 | | Engine.ConfigDB_Reload("user"); |
| 247 | if (g_hasChanges) |
| 248 | { |
| 249 | let btCaptions = [translate("No"), translate("Yes")]; |
| 250 | let btCode = [null, function(){ closePageWithoutConfirmation(); }]; |
| 251 | messageBox(500, 200, translate("You have unsaved changes, are you sure you want to quit ?"), |
| 252 | translate("Warning"), 0, btCaptions, btCode); |
| 253 | } |
| 254 | else |
| 255 | closePageWithoutConfirmation(); |
| 256 | } |