options.rml 3.7 KB

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