airstruck / luigi

Lovely User Interfaces for Game Inventors
MIT License
114 stars 21 forks source link

Setting default states for radio buttons #34

Closed rm-code closed 8 years ago

rm-code commented 8 years ago

Is it possible to assign default states to radio buttons? Let's say I want to create an options screen where the user can select a few things: screenshot 2016-01-31 12 32 06

I'd store these settings in a file on the user's system and want to reload them next time the program is started. How would I load default states for the selected buttons?

airstruck commented 8 years ago

For 'check' and 'radio' type widgets, value = true means it's selected.

local settings = loadUserSettings()

local ui = Layout {
    { type = 'check', text = 'Show Avatars', value = settings.showAvatars },
    { type = 'check', text = 'Show Timeline', value = settings.showTimeline },
}

At some point we'll have proper data-bound controls, but for now you'll also need a way to get the value of those widgets back out to update the settings.

{ type = 'check', text = 'Show Avatars', value = settings.showAvatars, id = 'showAvatars' },
...
ui.showAvatars:onChange(function (event) settings.showAvatars = event.value end)

Or use event delegation here:

{ type = 'check', text = 'Show Avatars', value = settings.showAvatars, foo = 'showAvatars' },
...
ui:onChange(function (event)
    local foo = event.target.foo
    if foo then settings[foo] = event.value end
end)