options.rml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <rml>
  2. <head>
  3. <title>Options</title>
  4. <link type="text/template" href="window.rml" />
  5. <style>
  6. body
  7. {
  8. width: 350dp;
  9. height: 350dp;
  10. margin: auto;
  11. }
  12. div#title_bar div#icon
  13. {
  14. decorator: image( icon-game );
  15. display: none;
  16. }
  17. form div
  18. {
  19. width: 200dp;
  20. margin: auto;
  21. }
  22. </style>
  23. <script>
  24. Options = Options or {}
  25. if Options.datamodel == nil then
  26. -- Create a new data model
  27. Options.datamodel = rmlui.contexts["main"]:OpenDataModel("options", {
  28. graphics = 'ok',
  29. options_changed = false,
  30. audios = {
  31. {id="reverb", label="Reverb", checked=true},
  32. {id="3d", label="3D Spatialisation"},
  33. }
  34. })
  35. end
  36. function Options.Serialize(filename,options)
  37. local file,message = io.open(filename,'w+') --w+ will erase the previous data
  38. if file == nil then Log.Message(Log.logtype.error, "Error saving options in options.rml: " .. message) return end
  39. --cache the function
  40. local format = string.format
  41. for key,value in pairs(options) do
  42. file:write(tostring(key)..'='..tostring(value)..'\n')
  43. end
  44. file:close()
  45. end
  46. function Options.Deserialize(filename)
  47. local ret = {} --table to return
  48. --cache functions that are used a lot for faster lookup
  49. local gmatch = string.gmatch
  50. local f = io.open(filename)
  51. if f then
  52. for line in f:lines() do
  53. for k,v in gmatch(line, '(%w+)=(%w+)') do ret[k] = v end
  54. end
  55. f:close()
  56. else
  57. return nil
  58. end
  59. return ret
  60. end
  61. function Options.LoadOptions(document)
  62. local options = Options.Deserialize('options.dat')
  63. if options == nil then return end --short circuit if the file doesn't exist
  64. --because everything is loaded as a string, we have to fool around with the boolean variables
  65. Options.datamodel.audios[1].checked = (options['reverb'] == 'true')
  66. Options.datamodel.audios[2].checked = (options['3d'] == 'true')
  67. Options.datamodel.graphics = options['graphics']
  68. Options.datamodel.options_changed = false
  69. end
  70. function Options.SaveOptions(event)
  71. if event.parameters['button'] == 'cancel' then
  72. return
  73. end
  74. local options = {}
  75. local params = event.parameters
  76. options['graphics'] = params['graphics']
  77. --because of how checkboxes won't be in the event params if they aren't checked,
  78. --we return false if they don't exist. This is Lua's ternary operator.
  79. options['reverb'] = params['reverb'] and true or false
  80. options['3d'] = params['3d'] and true or false
  81. Options.Serialize('options.dat',options)
  82. end
  83. </script>
  84. </head>
  85. <body template="luawindow" onload="Window.OnWindowLoad(document) Options.LoadOptions(document)" onkeydown="if Window.EscapePressed(event) then Window.LoadMenu('main_menu',document) end">
  86. <form data-model="options" onsubmit="Options.SaveOptions(event) Window.LoadMenu('main_menu',document)" data-event-change="options_changed = true">
  87. <div>
  88. <p>
  89. Graphics<br />
  90. <label><input id="good" type="radio" name="graphics" value="good" data-checked="graphics" /> Good</label><br />
  91. <label><input id="ok" type="radio" name="graphics" value="ok" data-checked="graphics" /> OK</label><br />
  92. <label><input id="bad" type="radio" name="graphics" value="bad" data-checked="graphics" /> Bad</label><br />
  93. </p>
  94. <p data-if="graphics == 'bad'">Are you sure about this? Bad graphics are just plain <em>bad.</em></p>
  95. <p>
  96. Audio<br />
  97. <span data-for="audio : audios">
  98. <label><input data-attr-id="audio.id" type="checkbox" data-attr-name="audio.id" data-attrif-checked="audio.checked" /> {{audio.label}}</label><br />
  99. </span>
  100. </p>
  101. </div>
  102. <input type="submit" name="button" value="accept" data-attrif-disabled="!options_changed">Accept</input>
  103. <input type="submit" name="button" value="cancel">Cancel</input>
  104. </form>
  105. </body>
  106. </rml>