Ticket #3511: ticket-3511-noswitch.patch
File ticket-3511-noswitch.patch, 9.6 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/gui/options/options.js
1 1 var g_hasCallback = false; 2 var g_hasChanges = false; 2 3 /** 3 4 * This array holds the data to populate the general section with. 4 5 * Data is in the form [Title, Tooltip, {ActionType:Action}, InputType]. … … 54 55 { 55 56 if (data && data.callback) 56 57 g_hasCallback = true; 58 let reload = data && data.reload ? true : false; 57 59 58 60 // WARNING: We assume a strict formatting of the XML and do minimal checking. 59 for each (var prefix inObject.keys(options))61 for (let prefix of Object.keys(options)) 60 62 { 61 varlastSize;62 for ( vari = 0; i < options[prefix].length; i++)63 let lastSize; 64 for (let i = 0; i < options[prefix].length; i++) 63 65 { 64 varbody = Engine.GetGUIObjectByName(prefix + "[" + i + "]");65 varlabel = Engine.GetGUIObjectByName(prefix + "Label[" + i + "]");66 let body = Engine.GetGUIObjectByName(prefix + "[" + i + "]"); 67 let label = Engine.GetGUIObjectByName(prefix + "Label[" + i + "]"); 66 68 // Setup control. 67 setupControl(options[prefix][i], i, prefix );69 setupControl(options[prefix][i], i, prefix, reload); 68 70 // Setup label. 69 71 label.caption = options[prefix][i][0]; 70 72 label.tooltip = options[prefix][i][1]; … … 71 73 // Move each element to the correct place. 72 74 if (i > 0) 73 75 { 74 varnewSize = new GUISize();76 let newSize = new GUISize(); 75 77 newSize.left = lastSize.left; 76 78 newSize.rright = lastSize.rright; 77 79 newSize.top = lastSize.bottom; … … 80 82 lastSize = newSize; 81 83 } 82 84 else 83 {84 85 lastSize = body.size; 85 }86 86 // Show element. 87 87 body.hidden = false; 88 88 } … … 95 95 * @param option Structure containing the data to setup an option. 96 96 * @param prefix Prefix to use when accessing control, for example "generalSetting" when the tickbox name is generalSettingTickbox[i]. 97 97 */ 98 function setupControl(option, i, prefix )98 function setupControl(option, i, prefix, reload) 99 99 { 100 var control; 101 var onPress = function(){ g_hasChanges = true; }; 102 100 103 switch (option[3]) 101 104 { 102 105 case "boolean": … … 105 108 let size = text.size; 106 109 size.rright = 87; 107 110 text.size = size; 108 varcontrol = Engine.GetGUIObjectByName(prefix + "Tickbox[" + i + "]");111 control = Engine.GetGUIObjectByName(prefix + "Tickbox[" + i + "]"); 109 112 var checked; 110 var onPress = function(){};111 113 // Different option action load and save differently, so this switch is needed. 112 for each (var action inObject.keys(option[2]))114 for (let action of Object.keys(option[2])) 113 115 { 114 116 switch (action) 115 117 { … … 116 118 case "config": 117 119 // Load initial value if not yet loaded. 118 120 if (!checked || typeof checked != "boolean") 119 checked = Engine.ConfigDB_GetValue("user", option[2][action]) === "true" ? true : false;121 checked = Engine.ConfigDB_GetValue("user", option[2][action]) === "true"; 120 122 // Hacky macro to create the callback. 121 123 var callback = function(key) 122 124 { … … 127 129 onPress = mergeFunctions(callback, onPress); 128 130 break; 129 131 case "renderer": 130 // Load initial value if not yet loaded. 131 if (!checked || typeof checked != "boolean") 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") 132 141 checked = eval("Engine.Renderer_Get" + option[2][action] + "Enabled()"); 133 // Hacky macro to create the callback .134 var callback = function(key )142 // Hacky macro to create the callback (updating also the config value if any). 143 var callback = function(key, keyConfig) 135 144 { 136 145 return function() 146 { 137 147 eval("Engine.Renderer_Set" + key + "Enabled(" + this.checked + ")"); 138 }(option[2][action]); 148 if (keyConfig) 149 Engine.ConfigDB_CreateValue("user", keyConfig, String(this.checked)); 150 } 151 }(option[2][action], option[2].config); 139 152 // Merge the new callback with any existing callbacks. 140 153 onPress = mergeFunctions(callback, onPress); 141 154 break; … … 154 167 case "number": 155 168 // TODO: Slider 156 169 case "string": 157 varcontrol = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]");170 control = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]"); 158 171 var caption; 159 var onPress = function(){}; 160 for each (var action in Object.keys(option[2])) 172 for (let action of Object.keys(option[2])) 161 173 { 162 174 switch (action) 163 175 { 164 176 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]); 177 onPress = function(){}; 178 caption = Engine.ConfigDB_GetValue("user", option[2][action]); 168 179 // Hacky macro to create the callback. 169 180 var callback = function(key) 170 181 { 171 182 return function() 183 { 184 if (Engine.ConfigDB_GetValue("user", key) == this.caption) 185 return; 172 186 Engine.ConfigDB_CreateValue("user", key, String(this.caption)); 187 g_hasChanges = true; 188 } 173 189 }(option[2][action]); 174 190 // Merge the new callback with any existing callbacks. 175 191 onPress = mergeFunctions(callback, onPress); … … 184 200 } 185 201 control.caption = caption; 186 202 control.onPress = onPress; 203 control.onMouseLeave = onPress; 187 204 break; 188 205 default: 189 206 warn("Unknown option type '" + options[3] + "', assuming string. Valid types are 'number', 'string', or 'bool'."); 190 varcontrol = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]");207 control = Engine.GetGUIObjectByName(prefix + "Input[" + i + "]"); 191 208 break; 192 209 } 193 210 control.hidden = false; … … 209 226 }; 210 227 } 211 228 229 function reloadDefaults() 230 { 231 Engine.ConfigDB_Reload("user"); 232 init({ "reload": true }); 233 g_hasChanges = false; 234 } 235 236 function saveDefaults() 237 { 238 g_hasChanges = false; 239 Engine.ConfigDB_WriteFile("user", "config/user.cfg"); 240 } 241 212 242 /** 213 243 * Close GUI page and call callbacks if they exist. 214 244 **/ 215 245 function closePage() 216 246 { 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 } 219 257 258 function closePageWithoutConfirmation() 259 { 220 260 if (g_hasCallback) 221 261 Engine.PopGuiPageCB(); 222 262 else -
binaries/data/mods/public/gui/options/options.xml
17 17 <object style="ModernLabelText" type="text" size="50%-128 -16 50%+128 16"> 18 18 <translatableAttribute id="caption">Game Options</translatableAttribute> 19 19 </object> 20 <object name="GeneralSettings" type="image" sprite="ModernDarkBoxGold" size="16 16 312 100%- 16">20 <object name="GeneralSettings" type="image" sprite="ModernDarkBoxGold" size="16 16 312 100%-52"> 21 21 <object style="ModernLabelText" type="text" size="0 5 100% 25"> 22 22 <translatableAttribute id="caption">General</translatableAttribute> 23 23 </object> … … 53 53 </object> 54 54 </repeat> 55 55 </object> 56 <object name="LobbySettings" type="image" sprite="ModernDarkBoxGold" size="620 50%+4 916 100%- 16">56 <object name="LobbySettings" type="image" sprite="ModernDarkBoxGold" size="620 50%+4 916 100%-52"> 57 57 <object style="ModernLabelText" type="text" size="0 5 100% 25"> 58 58 <translatableAttribute id="caption">Lobby Settings</translatableAttribute> 59 59 </object> … … 65 65 </object> 66 66 </repeat> 67 67 </object> 68 <object type="button" style="ModernButtonRed" size="320 100%-44 50%-4 100%-16" hotkey="cancel"> 69 <translatableAttribute id="caption">Cancel</translatableAttribute> 70 <action on="Press">closePage();</action> 68 <object type="button" style="ModernButtonRed" size="50%-170 100%-44 50%-70 100%-16" hotkey="cancel"> 69 <translatableAttribute id="caption">Reload</translatableAttribute> 70 <translatableAttribute id="tooltip">Reload defaults values from file</translatableAttribute> 71 <action on="Press">reloadDefaults();</action> 71 72 </object> 72 <object type="button" style="ModernButtonRed" size="50% +4 100%-44 612100%-16">73 <object type="button" style="ModernButtonRed" size="50%-62 100%-44 50%+38 100%-16"> 73 74 <translatableAttribute id="caption">Save</translatableAttribute> 74 <action on="Press">Engine.ConfigDB_WriteFile("user", "config/user.cfg");closePage();</action> 75 <translatableAttribute id="tooltip">Save changes on file</translatableAttribute> 76 <action on="Press">saveDefaults();</action> 75 77 </object> 78 <object type="button" style="ModernButtonRed" size="50%+70 100%-44 50%+170 100%-16"> 79 <translatableAttribute id="caption">Quit</translatableAttribute> 80 <translatableAttribute id="tooltip">non-saved changes affect this session only</translatableAttribute> 81 <action on="Press">closePage();</action> 82 </object> 76 83 </object> 77 84 </objects>