First of: This uses IceUI but does not require IceUI. If you use this option definition, your widget will still work without IceUI and someone could even write a standalone widget to handle the options.
The idea is to have a simple way to define options in any widget without having to implement a GUI to change them (and without having to change the source code to change them).
Option types
These belong into a global table named options (see examples). It won't work with a local table and no other name is checked for options.
- bool
Code: Select all
deployment = { name = 'Deployment Mode', desc = 'Players deploy a limited number of units before the game starts', type = 'bool', value = false, }
- number
Code: Select all
NearIdle = { name = 'Near Idle', desc = 'This means that factories with only 8 build items left will be shown as idle.', type = 'number', value = 8, min = 0, max = 25, step = 1 }
- string: the input is kinda weird atm but should be better next Spring version (I hope)
Code: Select all
string_opt = { name = 'String Option', desc = 'an unused string option', type = 'string', value = 'BiteMe', maxlen = 12, }
- color
Code: Select all
BorderColor = { name = 'Border Color', desc = 'The color of the boxes surrounding an icon.', type = 'color', value = { 0.0, 1.0, 0.0, 0.65 }, disabled = true }
- list: atm a list with 3 items will be "radioButtons", more than 3 will be a dropdown boxor
Code: Select all
gamemode = { name = 'Game Modes', desc = 'Change the game mode', type = 'list', items = { { key = 'normal', name = 'Normal', desc = 'Normal game mode', }, { key = 'deploy', name = 'Deployment', desc = 'Players deploy a limited number of units before the game starts', }, { key = 'tactics', name = 'Tactics', desc = 'Players select a limited number of units before the game starts (without any factories)', } }, value = 'normal' }
Code: Select all
list_test = { name = 'List Test', desc = 'List Test Option', type = 'list', value = 'opt1', items = { 'opt1', 'opt2', 'opt3', 'opt4' } }
I tried to make it as close to the mod/map/ai options as possible but I did change a few things:
- The "key" component of the option is now the actual key of the table:
becomes
Code: Select all
options = { { key = 'deployment', name = 'Deployment Mode', desc = 'Players deploy a limited number of units before the game starts', type = 'bool', def = false, } }
The reason it was like that is to be able to have a userdefined order. The reason I changed it is because you can access them in your code easier:Code: Select all
options = { deployment = { name = 'Deployment Mode', desc = 'Players deploy a limited number of units before the game starts', type = 'bool', def = false, } }
Code: Select all
if options.deployment.value then [...] end
- The "def" component becomes a "value" component because it does specify the default value in the source code but it actually contains the changed value from the widgets point of view. This leaves us at:
Code: Select all
options = { deployment = { name = 'Deployment Mode', desc = 'Players deploy a limited number of units before the game starts', type = 'bool', value = false, } }
- I'm not sure if the map/mod options have a "disabled" component but I guess it doesn't make much sense for them. I added it for options depending on each other or whatever you want (see Interaction).
- I added a 'color' type which has a table of four floats (a color) as value.
Interaction
The main feature of the interface is the fact that options.x.value gets updated automatically with the user-defined value.
You can however also define a function that gets called whenever an option is changed:
Code: Select all
function options.Test:OnChange()
Spring.Echo('Setting Test changed to '..self.value)
end
To change anything, just assign new values (ie options.Test.disabled = true).
I'm not sure if everything is already changeable (haven't worked on this for three weeks now) but if something isn't, please post it here.
Try it yourself
First, download the latest version of IceUI and then add an options table to your widget as shown above. I attached to simple examples to get you going.
CustomModOptions.lua as another reference