Ticket #3511: cleanOptions.diff
File cleanOptions.diff, 8.3 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/gui/options/options.js
87 87 body.hidden = false; 88 88 } 89 89 } 90 // TODO we may have unsaved changes when opening this window 91 g_hasChanges = false; 92 Engine.GetGUIObjectByName("loadOptions").enabled = true; 93 Engine.GetGUIObjectByName("saveOptions").enabled = true; 90 94 } 91 95 92 96 /** … … 98 102 function setupControl(option, i, prefix, reload) 99 103 { 100 104 var control; 101 var onPress = function(){ g_hasChanges = true; };102 105 103 106 switch (option[3]) 104 107 { … … 110 113 text.size = size; 111 114 control = Engine.GetGUIObjectByName(prefix + "Tickbox[" + i + "]"); 112 115 var checked; 113 // Different option action load and save differently, so this switch is needed. 116 var keyRenderer; 117 var keyConfig; 118 var functionBody; 119 114 120 for (let action of Object.keys(option[2])) 115 121 { 116 122 switch (action) 117 123 { 118 124 case "config": 119 // Load initial value if not yet loaded. 120 if (!checked || typeof checked != "boolean") 121 checked = Engine.ConfigDB_GetValue("user", option[2][action]) === "true"; 122 // Hacky macro to create the callback. 123 var callback = function(key) 124 { 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); 125 keyConfig = option[2].config; 126 if (checked === undefined || reload) 127 checked = Engine.ConfigDB_GetValue("user", keyConfig) === "true"; 128 else if ((Engine.ConfigDB_GetValue("user", keyConfig) === "true") !== checked) 129 Engine.ConfigDB_CreateValue("user", keyConfig, String(checked)); 130 130 break; 131 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 + ")"); 139 } 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); 132 keyRenderer = option[2].renderer; 133 if (checked === undefined) 134 checked = eval("Engine.Renderer_Get" + keyRenderer + "Enabled()"); 135 else if (eval("Engine.Renderer_Get" + keyRenderer + "Enabled()") !== checked) 136 eval("Engine.Renderer_Set" + keyRenderer + "Enabled(" + checked + ")"); 154 137 break; 155 138 case "function": 156 139 // This allows for doing low-level actions, like hiding/showing UI elements. 157 onPress = mergeFunctions(eval("function(){" + option[2][action] + "}"), onPress);140 functionBody = option[2].function; 158 141 break; 159 142 default: 160 143 warn("Unknown option source type '" + action + "'"); 161 144 } 162 145 } 146 147 var onPress = function(keyRenderer, keyConfig, functionBody) 148 { 149 return function() 150 { 151 if (keyRenderer) 152 eval("Engine.Renderer_Set" + keyRenderer + "Enabled(" + this.checked + ")"); 153 if (keyConfig) 154 Engine.ConfigDB_CreateValue("user", keyConfig, String(this.checked)); 155 if (functionBody) 156 eval(functionBody); 157 setChanges(true); 158 }; 159 }(keyRenderer, keyConfig, functionBody); 160 163 161 // Load final data to the control element. 164 162 control.checked = checked; 165 163 control.onPress = onPress; … … 169 167 case "string": 170 168 control = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]"); 171 169 var caption; 170 var key; 171 var functionBody; 172 172 173 for (let action of Object.keys(option[2])) 173 174 { 174 175 switch (action) 175 176 { 176 177 case "config": 177 onPress = function(){}; 178 caption = Engine.ConfigDB_GetValue("user", option[2][action]); 179 // Hacky macro to create the callback. 180 var callback = function(key) 181 { 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); 178 key = option[2].config; 179 caption = Engine.ConfigDB_GetValue("user", key); 192 180 break; 193 181 case "function": 194 182 // This allows for doing low-level actions, like hiding/showing UI elements. 195 onPress = mergeFunctions(function(){eval(option[2][action]);}, onPress);183 functionBody = option[2].function; 196 184 break; 197 185 default: 198 186 warn("Unknown option source type '" + action + "'"); 199 187 } 200 188 } 189 190 // as we use MouseLeave events, we must ensure that something has been modified 191 var onMouseLeave = function(key, functionBody) 192 { 193 return function() 194 { 195 if (Engine.ConfigDB_GetValue("user", key) === this.caption) 196 return; 197 Engine.ConfigDB_CreateValue("user", key, String(this.caption)); 198 if (functionBody) 199 eval(functionBody); 200 setChanges(true); 201 }; 202 }(key, functionBody); 203 201 204 control.caption = caption; 202 control.onPress = onPress; 203 control.onMouseLeave = onPress; 205 control.onMouseLeave = onMouseLeave; 204 206 break; 205 207 default: 206 warn("Unknown option type '" + option s[3] + "', assuming string. Valid types are 'number', 'string', or 'bool'.");208 warn("Unknown option type '" + option[3] + "', assuming string. Valid types are 'number', 'string', or 'bool'."); 207 209 control = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]"); 208 210 break; 209 211 } … … 212 214 return control; 213 215 } 214 216 215 /** 216 * Merge two functions which don't expect arguments. 217 * 218 * @return Merged function. 219 */ 220 function mergeFunctions(function1, function2) 217 function setChanges(val) 221 218 { 222 return function() 223 { 224 function1.apply(this); 225 function2.apply(this); 226 }; 219 g_hasChanges = val; 220 Engine.GetGUIObjectByName("loadOptions").enabled = val; 221 Engine.GetGUIObjectByName("saveOptions").enabled = val; 227 222 } 228 223 229 224 function reloadDefaults() … … 230 225 { 231 226 Engine.ConfigDB_Reload("user"); 232 227 init({ "reload": true }); 233 g_hasChanges = false;228 setChanges(false); 234 229 } 235 230 236 231 function saveDefaults() 237 232 { 238 g_hasChanges = false;239 233 Engine.ConfigDB_WriteFile("user", "config/user.cfg"); 234 setChanges(false); 240 235 } 241 236 242 237 /** -
binaries/data/mods/public/gui/options/options.xml
65 65 </object> 66 66 </repeat> 67 67 </object> 68 <object type="button" style="ModernButtonRed" size="50%-170 100%-44 50%-70 100%-16" hotkey="cancel">68 <object name="loadOptions" type="button" style="ModernButtonRed" size="50%-170 100%-44 50%-70 100%-16" hotkey="cancel"> 69 69 <translatableAttribute id="caption">Reload</translatableAttribute> 70 <translatableAttribute id="tooltip">Reload default values from file</translatableAttribute>70 <translatableAttribute id="tooltip">Reload previous saved settings</translatableAttribute> 71 71 <action on="Press">reloadDefaults();</action> 72 72 </object> 73 <object type="button" style="ModernButtonRed" size="50%-62 100%-44 50%+38 100%-16">73 <object name="saveOptions" type="button" style="ModernButtonRed" size="50%-62 100%-44 50%+38 100%-16"> 74 74 <translatableAttribute id="caption">Save</translatableAttribute> 75 <translatableAttribute id="tooltip">Save changes to file</translatableAttribute>75 <translatableAttribute id="tooltip">Save changes</translatableAttribute> 76 76 <action on="Press">saveDefaults();</action> 77 77 </object> 78 78 <object type="button" style="ModernButtonRed" size="50%+70 100%-44 50%+170 100%-16">