| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <rml>
- <head>
- <title>Options</title>
- <link type="text/template" href="window.rml" />
- <style>
- body
- {
- width: 350dp;
- height: 350dp;
- margin: auto;
- }
- div#title_bar div#icon
- {
- decorator: image( icon-game );
- display: none;
- }
- form div
- {
- width: 200dp;
- margin: auto;
- }
- </style>
- <script>
- Options = Options or {}
- if Options.datamodel == nil then
- -- Create a new data model
- Options.datamodel = rmlui.contexts["main"]:OpenDataModel("options", {
- graphics = 'ok',
- options_changed = false,
- audios = {
- {id="reverb", label="Reverb", checked=true},
- {id="3d", label="3D Spatialisation"},
- }
- })
- end
- function Options.Serialize(filename,options)
- local file,message = io.open(filename,'w+') --w+ will erase the previous data
- if file == nil then Log.Message(Log.logtype.error, "Error saving options in options.rml: " .. message) return end
- --cache the function
- local format = string.format
- for key,value in pairs(options) do
- file:write(tostring(key)..'='..tostring(value)..'\n')
- end
- file:close()
- end
- function Options.Deserialize(filename)
- local ret = {} --table to return
- --cache functions that are used a lot for faster lookup
- local gmatch = string.gmatch
- local f = io.open(filename)
- if f then
- for line in f:lines() do
- for k,v in gmatch(line, '(%w+)=(%w+)') do ret[k] = v end
- end
- f:close()
- else
- return nil
- end
- return ret
- end
- function Options.LoadOptions(document)
- local options = Options.Deserialize('options.dat')
- if options == nil then return end --short circuit if the file doesn't exist
- --because everything is loaded as a string, we have to fool around with the boolean variables
- Options.datamodel.audios[1].checked = (options['reverb'] == 'true')
- Options.datamodel.audios[2].checked = (options['3d'] == 'true')
- Options.datamodel.graphics = options['graphics']
- Options.datamodel.options_changed = false
- end
- function Options.SaveOptions(event)
- if event.parameters['button'] == 'cancel' then
- return
- end
- local options = {}
- local params = event.parameters
- options['graphics'] = params['graphics']
- --because of how checkboxes won't be in the event params if they aren't checked,
- --we return false if they don't exist. This is Lua's ternary operator.
- options['reverb'] = params['reverb'] and true or false
- options['3d'] = params['3d'] and true or false
- Options.Serialize('options.dat',options)
- end
- </script>
- </head>
- <body template="luawindow" onload="Window.OnWindowLoad(document) Options.LoadOptions(document)" onkeydown="if Window.EscapePressed(event) then Window.LoadMenu('main_menu',document) end">
- <form data-model="options" onsubmit="Options.SaveOptions(event) Window.LoadMenu('main_menu',document)" data-event-change="options_changed = true">
- <div>
- <p>
- Graphics<br />
- <label><input id="good" type="radio" name="graphics" value="good" data-checked="graphics" /> Good</label><br />
- <label><input id="ok" type="radio" name="graphics" value="ok" data-checked="graphics" /> OK</label><br />
- <label><input id="bad" type="radio" name="graphics" value="bad" data-checked="graphics" /> Bad</label><br />
- </p>
- <p data-if="graphics == 'bad'">Are you sure about this? Bad graphics are just plain <em>bad.</em></p>
- <p>
- Audio<br />
- <span data-for="audio : audios">
- <label><input data-attr-id="audio.id" type="checkbox" data-attr-name="audio.id" data-attrif-checked="audio.checked" /> {{audio.label}}</label><br />
- </span>
- </p>
- </div>
- <input type="submit" name="button" value="accept" data-attrif-disabled="!options_changed">Accept</input>
- <input type="submit" name="button" value="cancel">Cancel</input>
- </form>
- </body>
- </rml>
|