config_menu.lua 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. if UiContainer == nil then UiContainer = {} end
  2. local ROOT = "ConfigMenu.Container."
  3. --- Range of the values of the red, green and blue sliders of the window colour.
  4. local COLOUR_MIN = 0
  5. local COLOUR_MAX = 255
  6. --- Step of the colour sliders: left and right, and L1 and R1 (big step).
  7. local COLOUR_STEP = 1
  8. local COLOUR_BIG_STEP = 16
  9. --- Position and width in pixels of the colour sliders, relative to the colour window.
  10. local SLIDER_X = 70
  11. local SLIDER_WIDTH = 92
  12. --- Corners of the window, in the order they are shown (2x2 grid), and the corner of the colours
  13. -- table (top-left, top-right, bottom-right, bottom-left) each one is.
  14. local CORNER_COLOUR = { 1, 2, 4, 3 }
  15. --- Actions the player can choose the keys of. The first entry is the button that restores the
  16. -- default keys.
  17. --
  18. -- * name: Name of the row widget (with the prefix "Row", because the window prototype has children
  19. -- called Left and Right) and of the texts.
  20. -- * events: Game events (see bind_game_event in config.cfg) that use the keys of the action. The
  21. -- default keys are the ones config.cfg gives to the first one (see get_default_keys).
  22. local CONTROLS = {
  23. { name = "Default" },
  24. { name = "Up", events = { "Up", "walk_up", "message_up" } },
  25. { name = "Down", events = { "Down", "walk_down", "message_down" } },
  26. { name = "Left", events = { "Left", "walk_left" } },
  27. { name = "Right", events = { "Right", "walk_right" } },
  28. { name = "Ok", events = { "CONFIRM" } },
  29. { name = "Cancel", events = { "CANCEL" } },
  30. { name = "Menu", events = { "MENU" } },
  31. { name = "Extra", events = { "EXTRA" } },
  32. { name = "Select", events = { "SELECT" } },
  33. { name = "Start", events = { "START" } },
  34. { name = "L1", events = { "L1" } },
  35. { name = "R1", events = { "R1" } },
  36. { name = "Run", events = { "run" } },
  37. }
  38. --- File where the keys are saved. The game runs it after config.cfg when it starts.
  39. local CONTROLS_FILE = "./user/controls.cfg"
  40. --- Key that cancels the choice of a key. It can't be assigned.
  41. local CAPTURE_CANCEL_KEY = "Escape"
  42. --- Maximum value of the volume sliders (0 is muted).
  43. local VOLUME_MAX = 4
  44. --- Range of the sliders. 0 is the left end (fast), SLIDER_MAX the right end (slow).
  45. local SLIDER_MAX = 10
  46. --- Types of row.
  47. local KIND = {
  48. ACTION = "action", -- Opens something when OK is pressed.
  49. CHOICE = "choice", -- Left/right choose one of several options.
  50. SLIDER = "slider", -- Left/right move a slider.
  51. ORDER = "order", -- Left/right choose one of the orders of the magic menu.
  52. }
  53. --- The rows of the menu, in the order they are shown.
  54. --
  55. -- * name: Name of the row widget.
  56. -- * kind: Type of row (KIND). Sound and controller open their own window, like window color.
  57. -- * key: Field of MenuSettings.config where the value is stored.
  58. -- * count: Number of options (choices and orders).
  59. local ROWS = {
  60. { name = "WindowColor", kind = KIND.ACTION },
  61. { name = "Sound", kind = KIND.ACTION },
  62. { name = "Controller", kind = KIND.ACTION },
  63. { name = "CursorMode", kind = KIND.CHOICE, key = "cursor", count = 2 },
  64. { name = "Atb", kind = KIND.CHOICE, key = "atb", count = 3 },
  65. { name = "BattleSpeed", kind = KIND.SLIDER, key = "battle_speed" },
  66. { name = "BattleMessage", kind = KIND.SLIDER, key = "battle_message" },
  67. { name = "FieldMessage", kind = KIND.SLIDER, key = "field_message" },
  68. { name = "Movement", kind = KIND.CHOICE, key = "movement", count = 2 },
  69. { name = "CameraAngle", kind = KIND.CHOICE, key = "camera", count = 2 },
  70. { name = "MagicOrder", kind = KIND.ORDER, key = "magic_order", count = 6 },
  71. }
  72. --- Values of the options. Choices and orders start at 1, sliders at 0.
  73. --
  74. -- TODO: These are placeholders. They don't change the game yet. Each option must be connected to
  75. -- its setting as it is implemented.
  76. -- The settings are saved with the game (save_settings) and loaded with it (load_settings). The keys of
  77. -- the controller are not: they are saved in controls.cfg.
  78. MenuSettings.config = MenuSettings.config or {
  79. cursor = 1, -- 1: Initial, 2: Memory.
  80. atb = 2, -- 1: Active, 2: Recommended, 3: Wait.
  81. battle_speed = 5,
  82. battle_message = 5,
  83. field_message = 5,
  84. movement = 1, -- 1: Walk, 2: Run.
  85. camera = 1, -- 1: Auto, 2: Fix.
  86. magic_order = 1, -- Permutations of restore, attack and indirect.
  87. music_volume = 4, -- 0-4.
  88. sound_volume = 4, -- 0-4. Sound effects.
  89. --- Colours of the windows (red, green, blue, 0-255) at the top-left, top-right, bottom-right
  90. -- and bottom-left corners. They are the ones the windows have in the XML.
  91. window_colours = {
  92. { 0, 0, 178 },
  93. { 0, 0, 127 },
  94. { 0, 0, 32 },
  95. { 0, 0, 79 },
  96. },
  97. }
  98. --- Gets the colours of the windows as the 12 numbers (0-1) the engine takes.
  99. local function window_colours_as_floats()
  100. local values = {}
  101. for _, colour in ipairs(MenuSettings.config.window_colours) do
  102. for channel = 1, 3 do
  103. values[#values + 1] = colour[channel] / 255
  104. end
  105. end
  106. return values
  107. end
  108. --- Splits a list of keys separated by commas.
  109. local function split_keys(text)
  110. local keys = {}
  111. for key in string.gmatch(text, "[^,]+") do keys[#keys + 1] = key end
  112. return keys
  113. end
  114. --- Gets the keys of an action of CONTROLS.
  115. local function get_keys(action)
  116. return split_keys(input_manager:get_game_event_keys(action.events[1]))
  117. end
  118. --- Gets the default keys of an action of CONTROLS: the ones of config.cfg.
  119. local function get_default_keys(action)
  120. return split_keys(input_manager:get_default_game_event_keys(action.events[1]))
  121. end
  122. --- Sets the keys of an action of CONTROLS, in all its game events.
  123. local function set_keys(action, keys)
  124. local text = table.concat(keys, ",")
  125. for _, event in ipairs(action.events) do
  126. input_manager:set_game_event_keys(event, text)
  127. end
  128. end
  129. --- Settings (fields of MenuSettings.config) saved with the game, apart from the window colours.
  130. -- The values are numbers: choices start at 1, and sliders and volumes at 0.
  131. local SAVED_SETTINGS = {
  132. "music_volume", "sound_volume", "cursor", "atb", "battle_speed", "battle_message",
  133. "field_message", "movement", "camera", "magic_order",
  134. }
  135. --- Handlers of the buttons that change the value of the colour under the cursor.
  136. local function colour_buttons()
  137. local buttons = {}
  138. buttons[Config.CONTROLS.LEFT] = function(self, event) self:change_colour(-COLOUR_STEP) end
  139. buttons[Config.CONTROLS.RIGHT] = function(self, event) self:change_colour(COLOUR_STEP) end
  140. buttons[Config.ACTIONS.L1] = function(self, event) self:change_colour(-COLOUR_BIG_STEP) end
  141. buttons[Config.ACTIONS.R1] = function(self, event) self:change_colour(COLOUR_BIG_STEP) end
  142. return buttons
  143. end
  144. --- Handlers of Left and Right, which change the value of the row under the cursor.
  145. local function value_buttons()
  146. local buttons = {}
  147. buttons[Config.CONTROLS.LEFT] = function(self, event) self:change_value(-1) end
  148. buttons[Config.CONTROLS.RIGHT] = function(self, event) self:change_value(1) end
  149. return buttons
  150. end
  151. --- The config menu.
  152. --
  153. -- Up and down choose a row, and left and right change its value right away. Window color, sound
  154. -- and controller open their own window with OK.
  155. --
  156. -- Sound: opens a window with two sliders, one for the volume of the music and another for the
  157. -- sound effects (0-4). The volume changes as soon as the slider moves.
  158. --
  159. -- Controller: opens a window with the keys of every action (the directions, OK, cancel...). OK on
  160. -- an action asks for the key to assign to it. If the key was used by another action, they swap
  161. -- their keys. The first entry restores the default keys. The changes are kept apart and are only
  162. -- applied (and saved in controls.cfg, which the game reads when it starts) when the window is
  163. -- closed, if they are valid (see controls_are_valid), so the player can't be left without a way to
  164. -- control the game.
  165. --
  166. -- Window color: the cursor goes to the preview of the window to the right, where one of the four
  167. -- corners is chosen. Each corner opens a window with three sliders (red, green and blue). The
  168. -- preview shows the changes, and the colours are applied to all the windows when the cursor goes
  169. -- back to the list.
  170. --
  171. -- TODO: All the options are placeholders for now.
  172. UiContainer.ConfigMenu = UiContainer.Menu.new{
  173. id = "config",
  174. parent = "main",
  175. --- Cursor over the rows.
  176. cursor = UiContainer.Cursor.new{
  177. widget = ROOT .. "Cursor",
  178. total = #ROWS,
  179. on_move = function(position)
  180. UiContainer.ConfigMenu:update_help(position)
  181. end,
  182. },
  183. --- Cursor over the corners of the window preview (2x2 grid).
  184. corner_cursor = UiContainer.Cursor.new{
  185. widget = ROOT .. "CornerCursor",
  186. total = 4,
  187. sound = false,
  188. },
  189. --- Cursor over the actions of the controller window.
  190. controller_cursor = UiContainer.Cursor.new{
  191. widget = ROOT .. "ControllerWindow.Cursor",
  192. total = #CONTROLS,
  193. },
  194. --- Action (entry of CONTROLS) that is waiting for a key.
  195. assigning = nil,
  196. --- Keys the player has chosen in the controller window, not applied yet. Lists of key names by
  197. -- position in CONTROLS.
  198. pending_keys = {},
  199. --- Cursor over the music and sound effects sliders.
  200. sound_cursor = UiContainer.Cursor.new{
  201. widget = ROOT .. "SoundWindow.Cursor",
  202. total = 2,
  203. },
  204. --- Cursor over the red, green and blue sliders.
  205. channel_cursor = UiContainer.Cursor.new{
  206. widget = ROOT .. "ColourWindow.Cursor",
  207. total = 3,
  208. },
  209. states = {
  210. [""] = {
  211. cursor = function(self) return self.cursor end,
  212. axis = "vertical",
  213. buttons = value_buttons(),
  214. confirm = function(self, cursor)
  215. local row = ROWS[cursor:index()]
  216. if row.name == "WindowColor" then
  217. self.corner_cursor:set(1)
  218. ui_manager:get_widget(ROOT .. "CornerCursor"):set_visible(true)
  219. self:set_state("corner")
  220. return
  221. elseif row.name == "Sound" then
  222. self.sound_cursor:set(1)
  223. self:refresh_sound_window()
  224. ui_manager:get_widget(ROOT .. "SoundWindow"):set_visible(true)
  225. self:set_state("sound")
  226. return
  227. elseif row.name == "Controller" then
  228. self.controller_cursor:set(1)
  229. self:load_pending_keys()
  230. self:refresh_controller_window()
  231. ui_manager:get_widget(ROOT .. "ControllerWindow"):set_visible(true)
  232. self:set_state("controller")
  233. return
  234. elseif row.kind == KIND.ACTION then
  235. print("TODO: Open the window of " .. row.name)
  236. end
  237. -- The other rows change with left and right.
  238. return false
  239. end,
  240. cancel = function(self)
  241. script:request_end_sync(Script.UI, "ConfigMenu", "hide", 0)
  242. end,
  243. },
  244. --- Choosing the action to change the key of.
  245. controller = {
  246. cursor = function(self) return self.controller_cursor end,
  247. axis = "vertical",
  248. confirm = function(self, cursor)
  249. local index = cursor:index()
  250. if index == 1 then
  251. self:reset_controls()
  252. self:refresh_controller_window()
  253. return
  254. end
  255. -- The next key pressed is the new key of the action.
  256. self.assigning = index
  257. input_manager:capture_next_key()
  258. self:set_cursor_blinking(true)
  259. self:update_help(#ROWS + 1)
  260. self:set_state("assign")
  261. end,
  262. cancel = function(self)
  263. -- The keys are only applied if they are valid. If not, the window stays open.
  264. if not self:controls_are_valid() then return false end
  265. ui_manager:get_widget(ROOT .. "ControllerWindow"):set_visible(false)
  266. self:apply_controls()
  267. self:set_state("")
  268. end,
  269. },
  270. --- Waiting for the key to assign to an action.
  271. assign = {
  272. capture = function(self, key)
  273. if key == "UNASSIGNED" then
  274. -- A key the game doesn't know: ask again.
  275. UiContainer.play_sound("Error")
  276. input_manager:capture_next_key()
  277. return
  278. elseif key == CAPTURE_CANCEL_KEY then
  279. UiContainer.play_sound("Back")
  280. else
  281. self:assign_key(self.assigning, key)
  282. UiContainer.play_sound("Cursor")
  283. self:refresh_controller_window()
  284. end
  285. self:set_cursor_blinking(false)
  286. self:update_help(self.cursor:index())
  287. self:set_state("controller")
  288. end,
  289. },
  290. --- Changing the volume of the music and of the sound effects.
  291. sound = {
  292. cursor = function(self) return self.sound_cursor end,
  293. axis = "vertical",
  294. buttons = {
  295. [Config.CONTROLS.LEFT] = function(self, event) self:change_volume(-1) end,
  296. [Config.CONTROLS.RIGHT] = function(self, event) self:change_volume(1) end,
  297. },
  298. confirm = function(self)
  299. ui_manager:get_widget(ROOT .. "SoundWindow"):set_visible(false)
  300. self:set_state("")
  301. end,
  302. cancel = function(self)
  303. ui_manager:get_widget(ROOT .. "SoundWindow"):set_visible(false)
  304. self:set_state("")
  305. end,
  306. },
  307. --- Choosing the corner of the window to change.
  308. corner = {
  309. buttons = UiContainer.grid_buttons("corner_cursor", 2),
  310. confirm = function(self)
  311. self.channel_cursor:set(1)
  312. self:refresh_colour_window()
  313. ui_manager:get_widget(ROOT .. "ColourWindow"):set_visible(true)
  314. self:set_state("colour")
  315. end,
  316. cancel = function(self)
  317. -- Back to the list: the colours are applied now.
  318. ui_manager:get_widget(ROOT .. "CornerCursor"):set_visible(false)
  319. self:apply_window_colours()
  320. self:set_state("")
  321. end,
  322. },
  323. --- Changing the red, green and blue of a corner.
  324. colour = {
  325. cursor = function(self) return self.channel_cursor end,
  326. axis = "vertical",
  327. buttons = colour_buttons(),
  328. confirm = function(self)
  329. ui_manager:get_widget(ROOT .. "ColourWindow"):set_visible(false)
  330. self:set_state("corner")
  331. end,
  332. cancel = function(self)
  333. ui_manager:get_widget(ROOT .. "ColourWindow"):set_visible(false)
  334. self:set_state("corner")
  335. end,
  336. },
  337. },
  338. --- Makes the cursor of the controller window blink (while a key is asked for), or stop.
  339. --
  340. -- @param blinking True to blink, false to go back to the fixed cursor.
  341. set_cursor_blinking = function(self, blinking)
  342. local cursor = ui_manager:get_widget(ROOT .. "ControllerWindow.Cursor")
  343. local animation = (blinking and "Blink" or "Position") .. tostring(self.controller_cursor:index())
  344. cursor:set_default_animation(animation)
  345. cursor:play_animation(animation)
  346. -- The blink leaves it transparent when it stops at the wrong moment.
  347. if not blinking then cursor:set_alpha(1.0) end
  348. end,
  349. --- Shows the keys of every action in the controller window.
  350. refresh_controller_window = function(self)
  351. for index = 2, #CONTROLS do
  352. local widget = ui_manager:get_widget(ROOT .. "ControllerWindow.Row" .. CONTROLS[index].name .. ".Keys")
  353. widget:set_colour(1.0, 1.0, 1.0)
  354. widget:set_text(table.concat(self.pending_keys[index], ", "))
  355. end
  356. end,
  357. --- Copies the keys the actions have now to the pending keys.
  358. load_pending_keys = function(self)
  359. self.pending_keys = {}
  360. for index = 2, #CONTROLS do
  361. self.pending_keys[index] = get_keys(CONTROLS[index])
  362. end
  363. end,
  364. --- Checks that the pending keys leave the game controllable.
  365. --
  366. -- Every action must have at least one key, and no key can be used by two actions.
  367. --
  368. -- @return True if they can be applied.
  369. controls_are_valid = function(self)
  370. local used = {}
  371. for index = 2, #CONTROLS do
  372. local keys = self.pending_keys[index]
  373. if keys == nil or #keys == 0 then return false end
  374. for _, key in ipairs(keys) do
  375. if used[key] then return false end
  376. used[key] = true
  377. end
  378. end
  379. return true
  380. end,
  381. --- Applies the pending keys to the game and saves them.
  382. apply_controls = function(self)
  383. for index = 2, #CONTROLS do
  384. set_keys(CONTROLS[index], self.pending_keys[index])
  385. end
  386. self:save_controls()
  387. end,
  388. --- Gives a key to an action, in the pending keys.
  389. --
  390. -- The action keeps only that key. If another action had it, it loses it and, if it was its
  391. -- only key, it takes the keys the action had before (they swap).
  392. --
  393. -- @param index Position of the action in CONTROLS.
  394. -- @param key Name of the key.
  395. assign_key = function(self, index, key)
  396. local previous = self.pending_keys[index]
  397. for other_index = 2, #CONTROLS do
  398. if other_index ~= index then
  399. local keys = self.pending_keys[other_index]
  400. for position, existing in ipairs(keys) do
  401. if existing == key then
  402. table.remove(keys, position)
  403. if #keys == 0 then
  404. keys = (#previous > 0) and previous or get_default_keys(CONTROLS[other_index])
  405. end
  406. self.pending_keys[other_index] = keys
  407. break
  408. end
  409. end
  410. end
  411. end
  412. self.pending_keys[index] = { key }
  413. end,
  414. --- Puts the default keys of all the actions in the pending keys.
  415. reset_controls = function(self)
  416. for index = 2, #CONTROLS do
  417. self.pending_keys[index] = get_default_keys(CONTROLS[index])
  418. end
  419. end,
  420. --- Saves the keys in the controls file, which the game runs when it starts.
  421. save_controls = function(self)
  422. local file = io.open(CONTROLS_FILE, "w")
  423. if file == nil then
  424. print("Can't write the controller settings in " .. CONTROLS_FILE)
  425. return
  426. end
  427. for index = 2, #CONTROLS do
  428. local action = CONTROLS[index]
  429. local keys = table.concat(self.pending_keys[index], ",")
  430. for _, event in ipairs(action.events) do
  431. file:write('set_game_event_keys "' .. event .. '" "' .. keys .. '"\n')
  432. end
  433. end
  434. file:close()
  435. end,
  436. --- Changes the volume (music or sound effects, the one under the cursor).
  437. --
  438. -- @param delta -1 for left, 1 for right.
  439. change_volume = function(self, delta)
  440. local key = (self.sound_cursor:index() == 1) and "music_volume" or "sound_volume"
  441. local values = MenuSettings.config
  442. local value = math.max(0, math.min(VOLUME_MAX, values[key] + delta))
  443. if value == values[key] then return end
  444. values[key] = value
  445. self:apply_volumes()
  446. self:refresh_sound_window()
  447. -- Played after applying the volume, so the sound effect is heard with the new volume.
  448. UiContainer.play_sound("Cursor")
  449. end,
  450. --- Gives the settings to the savemap, to be saved with the game. See save_game in save.lua.
  451. save_settings = function(self)
  452. local config = MenuSettings.config
  453. for _, key in ipairs(SAVED_SETTINGS) do
  454. savemap_manager:set_setting(key, config[key])
  455. end
  456. local c = config.window_colours
  457. savemap_manager:set_window_colours(
  458. c[1][1], c[1][2], c[1][3], c[2][1], c[2][2], c[2][3],
  459. c[3][1], c[3][2], c[3][3], c[4][1], c[4][2], c[4][3]
  460. )
  461. end,
  462. --- Loads the settings of a saved game and applies them. See load_game in save.lua.
  463. --
  464. -- Games saved before the settings were saved don't have them, and don't change the ones the
  465. -- player has. Values that are out of range (a damaged file) are brought inside it.
  466. --
  467. -- @param slot Slot of the saved game (0-15).
  468. load_settings = function(self, slot)
  469. if not savemap_manager:is_slot_setting_set(slot, SAVED_SETTINGS[1]) then return end
  470. local config = MenuSettings.config
  471. -- Range of the values of each setting.
  472. local ranges = {
  473. music_volume = { 0, VOLUME_MAX }, sound_volume = { 0, VOLUME_MAX },
  474. }
  475. for _, row in ipairs(ROWS) do
  476. if row.kind == KIND.SLIDER then ranges[row.key] = { 0, SLIDER_MAX }
  477. elseif row.key ~= nil then ranges[row.key] = { 1, row.count } end
  478. end
  479. for _, key in ipairs(SAVED_SETTINGS) do
  480. if savemap_manager:is_slot_setting_set(slot, key) then
  481. local range = ranges[key]
  482. config[key] = math.max(range[1], math.min(range[2], savemap_manager:get_slot_setting(slot, key)))
  483. end
  484. end
  485. -- Corners: top-left, top-right, bottom-right and bottom-left, as in the savemap.
  486. for corner = 1, 4 do
  487. for channel = 1, 3 do
  488. config.window_colours[corner][channel] = math.max(COLOUR_MIN, math.min(COLOUR_MAX,
  489. savemap_manager:get_slot_window_colour(slot, corner - 1, channel - 1)))
  490. end
  491. end
  492. self:apply_all()
  493. end,
  494. --- Applies all the settings that act on the game right away: volumes, movement and the
  495. -- colours of the windows. The rest are read by the game when it needs them.
  496. apply_all = function(self)
  497. self:apply_volumes()
  498. self:apply_movement()
  499. self:apply_window_colours()
  500. end,
  501. --- Applies the movement setting (walk or run by default) to the player.
  502. apply_movement = function(self)
  503. entity_manager:set_player_default_run(MenuSettings.config.movement == 2)
  504. end,
  505. --- Applies the volumes to the audio.
  506. apply_volumes = function(self)
  507. audio_manager:set_music_volume(MenuSettings.config.music_volume / VOLUME_MAX)
  508. audio_manager:set_sound_volume(MenuSettings.config.sound_volume / VOLUME_MAX)
  509. end,
  510. --- Shows the position of the volume sliders.
  511. refresh_sound_window = function(self)
  512. local values = MenuSettings.config
  513. ui_manager:get_widget(ROOT .. "SoundWindow.Music.Handle")
  514. :set_default_animation("Position" .. tostring(values.music_volume))
  515. ui_manager:get_widget(ROOT .. "SoundWindow.Effects.Handle")
  516. :set_default_animation("Position" .. tostring(values.sound_volume))
  517. end,
  518. --- Colour (entry of MenuSettings.config.window_colours) of the corner under the cursor.
  519. current_colour = function(self)
  520. return MenuSettings.config.window_colours[CORNER_COLOUR[self.corner_cursor:index()]]
  521. end,
  522. --- Changes the red, green or blue (the one under the cursor) of the corner.
  523. --
  524. -- @param delta Amount added to the value.
  525. change_colour = function(self, delta)
  526. local colour = self:current_colour()
  527. local channel = self.channel_cursor:index()
  528. local value = math.max(COLOUR_MIN, math.min(COLOUR_MAX, colour[channel] + delta))
  529. if value == colour[channel] then return end
  530. colour[channel] = value
  531. self:refresh_colour_window()
  532. self:refresh_preview()
  533. end,
  534. --- Shows the sliders of the corner under the cursor.
  535. refresh_colour_window = function(self)
  536. local colour = self:current_colour()
  537. local names = { "Red", "Green", "Blue" }
  538. for channel = 1, 3 do
  539. local path = ROOT .. "ColourWindow." .. names[channel]
  540. local value = colour[channel]
  541. ui_manager:get_widget(path .. ".Value"):set_text(UiContainer.pad_value(value, 3))
  542. local offset = math.floor((value - COLOUR_MIN) / (COLOUR_MAX - COLOUR_MIN) * SLIDER_WIDTH)
  543. ui_manager:get_widget(path .. ".Handle"):set_x(0, SLIDER_X + offset)
  544. end
  545. end,
  546. --- Shows the chosen colours in the preview of the window.
  547. refresh_preview = function(self)
  548. local v = window_colours_as_floats()
  549. ui_manager:get_widget(ROOT .. "Body.WindowColor.Preview.Center"):set_colours(
  550. v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9], v[10], v[11], v[12])
  551. end,
  552. --- Applies the chosen colours to all the windows.
  553. apply_window_colours = function(self)
  554. local v = window_colours_as_floats()
  555. ui_manager:set_window_colours(
  556. v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9], v[10], v[11], v[12])
  557. end,
  558. --- Shows only the help text of a row.
  559. update_help = function(self, position)
  560. -- There is one more help text than rows: the one shown while a key is asked for.
  561. for index = 1, #ROWS + 1 do
  562. ui_manager:get_widget(ROOT .. "Help.H" .. tostring(index)):set_visible(index == position)
  563. end
  564. end,
  565. --- Changes the value of the row under the cursor.
  566. --
  567. -- @param delta -1 for left, 1 for right.
  568. change_value = function(self, delta)
  569. local row = ROWS[self.cursor:index()]
  570. if row.kind == KIND.ACTION then return end
  571. local values = MenuSettings.config
  572. local min = (row.kind == KIND.SLIDER) and 0 or 1
  573. local max = (row.kind == KIND.SLIDER) and SLIDER_MAX or row.count
  574. local value = values[row.key] + delta
  575. if value < min or value > max then return end
  576. values[row.key] = value
  577. if row.key == "movement" then self:apply_movement() end
  578. UiContainer.play_sound("Cursor")
  579. self:refresh_row(row)
  580. end,
  581. --- Shows the value of a row.
  582. refresh_row = function(self, row)
  583. local path = ROOT .. "Body." .. row.name
  584. local value = MenuSettings.config[row.key]
  585. if row.kind == KIND.CHOICE then
  586. -- Every option has a white copy (shown when selected) and a gray one (shown otherwise).
  587. -- The gray one uses its own text ("...Off") in the texts file, because the colour
  588. -- attribute of the XML is not applied to texts that come from the texts file.
  589. for option = 1, row.count do
  590. local selected = (option == value)
  591. ui_manager:get_widget(path .. ".Opt" .. tostring(option) .. "On"):set_visible(selected)
  592. ui_manager:get_widget(path .. ".Opt" .. tostring(option) .. "Off"):set_visible(not selected)
  593. end
  594. elseif row.kind == KIND.SLIDER then
  595. ui_manager:get_widget(path .. ".Handle"):set_default_animation("Position" .. tostring(value))
  596. elseif row.kind == KIND.ORDER then
  597. ui_manager:get_widget(path .. ".Number"):set_text(tostring(value))
  598. for order = 1, row.count do
  599. ui_manager:get_widget(path .. ".Order" .. tostring(order)):set_visible(order == value)
  600. end
  601. end
  602. end,
  603. --- Opens the config menu.
  604. show = function(self)
  605. self:activate()
  606. for _, row in ipairs(ROWS) do
  607. if row.kind ~= KIND.ACTION then self:refresh_row(row) end
  608. end
  609. self.cursor:set(1)
  610. ui_manager:get_widget(ROOT .. "CornerCursor"):set_visible(false)
  611. ui_manager:get_widget(ROOT .. "ColourWindow"):set_visible(false)
  612. ui_manager:get_widget(ROOT .. "SoundWindow"):set_visible(false)
  613. ui_manager:get_widget(ROOT .. "ControllerWindow"):set_visible(false)
  614. self:refresh_preview()
  615. ui_manager:get_widget("ConfigMenu"):set_visible(true)
  616. return 0
  617. end,
  618. --- Closes the config menu and goes back to the main menu.
  619. hide = function(self)
  620. ui_manager:get_widget("ConfigMenu"):set_visible(false)
  621. self:deactivate()
  622. return 0
  623. end,
  624. }