options.rml 3.6 KB

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