main_menu.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. if UiContainer == nil then UiContainer = {} end
  2. --- Entries of the main menu, by cursor position.
  3. local ENTRY = {
  4. ITEM = 1, MAGIC = 2, MATERIA = 3, EQUIP = 4, STATUS = 5, ORDER = 6, LIMIT = 7,
  5. CONFIG = 8, PHS = 9, SAVE = 10, QUIT = 11,
  6. }
  7. --- Width in pixels of the inside of the level and limit bars.
  8. local BAR_WIDTH = 62
  9. --- Returned by a character action to keep the character selection open.
  10. local STAY = "stay"
  11. local CHARACTERS_CURSOR = "MainMenu.Container.Characters.Cursor"
  12. local CHARACTERS_HELD = "MainMenu.Container.Characters.Held"
  13. --- The main menu.
  14. UiContainer.MainMenu = UiContainer.Menu.new{
  15. id = "main",
  16. --- Cursor over the menu entries (1-11).
  17. cursor = UiContainer.Cursor.new{
  18. widget = "MainMenu.Container.Menu.Cursor",
  19. total = 11,
  20. },
  21. --- Cursor over the party slots (1-3).
  22. -- TODO: Skip empty character slots.
  23. character_cursor = UiContainer.Cursor.new{
  24. widget = CHARACTERS_CURSOR,
  25. total = 3,
  26. },
  27. --- When selecting a character, the menu entry that will be opened.
  28. --
  29. -- If 0, not selecting character.
  30. select_character_for_menu = 0,
  31. --- Entries that don't need a character. Return false to signal they can't be opened.
  32. entry_actions = {
  33. [ENTRY.ITEM] = function(self)
  34. script:request_end_sync(Script.UI, "ItemMenu", "show", 0)
  35. end,
  36. [ENTRY.CONFIG] = function(self)
  37. script:request_end_sync(Script.UI, "ConfigMenu", "show", 0)
  38. end,
  39. [ENTRY.PHS] = function(self)
  40. script:request_end_sync(Script.UI, "PhsMenu", "show", 0)
  41. end,
  42. [ENTRY.SAVE] = function(self)
  43. UiContainer.SaveMenu.operation = "save"
  44. script:request_end_sync(Script.UI, "SaveMenu", "show", 0)
  45. end,
  46. [ENTRY.QUIT] = function(self)
  47. script:request_end_sync(Script.UI, "QuitMenu", "show", 0)
  48. end,
  49. },
  50. --- Entries that need a character. Receive the party slot (1-3).
  51. --
  52. -- Return false if the entry can't be opened (error sound), or STAY to keep the character
  53. -- selection open instead of handing the control to another menu.
  54. character_actions = {
  55. [ENTRY.MAGIC] = function(self, slot)
  56. UiContainer.current_character_id = Party[slot]
  57. script:request_end_sync(Script.UI, "MagicMenu", "show", 0)
  58. end,
  59. [ENTRY.MATERIA] = function(self, slot)
  60. UiContainer.current_character_id = Party[slot]
  61. script:request_end_sync(Script.UI, "MateriaMenu", "show", 0)
  62. end,
  63. [ENTRY.EQUIP] = function(self, slot)
  64. UiContainer.current_character_id = Party[slot]
  65. script:request_end_sync(Script.UI, "EquipMenu", "show", 0)
  66. end,
  67. [ENTRY.STATUS] = function(self, slot)
  68. UiContainer.current_character_id = Party[slot]
  69. script:request_end_sync(Script.UI, "StatusMenu", "show", 0)
  70. end,
  71. --- Picks the character to change its row or swap it with another party slot.
  72. -- A blinking cursor stays on it while the cursor moves (see the order_swap state).
  73. [ENTRY.ORDER] = function(self, slot)
  74. self.order_held = slot
  75. self:set_order_held(slot)
  76. self:set_state("order_swap")
  77. return STAY
  78. end,
  79. [ENTRY.LIMIT] = function(self, slot)
  80. UiContainer.current_character_id = Party[slot]
  81. script:request_end_sync(Script.UI, "LimitMenu", "show", 0)
  82. end,
  83. },
  84. --- Party slot of the character picked with Order.
  85. order_held = 0,
  86. states = {
  87. --- Choosing an entry of the menu.
  88. [""] = {
  89. cursor = function(self) return self.cursor end,
  90. axis = "vertical",
  91. cancel = function(self)
  92. script:request_end_sync(Script.UI, "MainMenu", "hide", 0)
  93. end,
  94. confirm = function(self, cursor)
  95. local position = cursor:index()
  96. local action = self.entry_actions[position]
  97. if action ~= nil then
  98. return action(self)
  99. end
  100. -- Any other entry needs a character.
  101. self.select_character_for_menu = position
  102. self:set_state("main_character")
  103. ui_manager:get_widget(CHARACTERS_CURSOR):set_visible(true)
  104. end,
  105. },
  106. --- Choosing the character to open the entry for.
  107. main_character = {
  108. cursor = function(self) return self.character_cursor end,
  109. axis = "vertical",
  110. cancel = function(self)
  111. self:set_state("")
  112. ui_manager:get_widget(CHARACTERS_CURSOR):set_visible(false)
  113. end,
  114. confirm = function(self, cursor)
  115. local slot = cursor:index()
  116. local action = self.character_actions[self.select_character_for_menu]
  117. if action == nil or Party[slot] == nil or Characters[Party[slot]] == nil then
  118. return false
  119. end
  120. local result = action(self, slot)
  121. if result == false or result == STAY then
  122. return result
  123. end
  124. -- The chosen menu takes over. When it closes, the main menu is back at the entry
  125. -- list, so the character cursor must not stay behind.
  126. self.select_character_for_menu = 0
  127. self:set_state("")
  128. ui_manager:get_widget(CHARACTERS_CURSOR):set_visible(false)
  129. end,
  130. },
  131. --- Order: a character is picked (blinking cursor) and the cursor moves over the three
  132. -- party slots, empty ones included. Confirm on the picked character toggles its row;
  133. -- confirm on another slot swaps the two.
  134. order_swap = {
  135. cursor = function(self) return self.character_cursor end,
  136. axis = "vertical",
  137. confirm_sound = false,
  138. cancel = function(self)
  139. self:set_order_held(nil)
  140. self:set_state("main_character")
  141. end,
  142. confirm = function(self, cursor)
  143. local held, slot = self.order_held, cursor:index()
  144. if slot == held then
  145. local character = Characters[Party[held]]
  146. character.back_row = not character.back_row
  147. self:update_portrait(held)
  148. UiContainer.play_sound("Cursor")
  149. else
  150. -- The first slot can't be left empty.
  151. if Party[slot] == nil and held == 1 then return false end
  152. Party[held], Party[slot] = Party[slot], Party[held]
  153. self:populate_character(held)
  154. self:populate_character(slot)
  155. UiContainer.play_sound("Cursor")
  156. end
  157. self:set_order_held(nil)
  158. self:set_state("main_character")
  159. end,
  160. },
  161. },
  162. --- Shows (or hides, with nil) the blinking cursor that marks the character picked with Order.
  163. set_order_held = function(self, slot)
  164. local held = ui_manager:get_widget(CHARACTERS_HELD)
  165. held:set_visible(slot ~= nil)
  166. if slot ~= nil then
  167. local animation = "Blink" .. tostring(slot)
  168. held:set_default_animation(animation)
  169. held:play_animation(animation)
  170. else
  171. held:set_alpha(1.0)
  172. end
  173. end,
  174. --- Handles the menu button while the main menu is closed.
  175. on_inactive_button = function(self, button, event)
  176. if ui_manager:get_widget("MainMenu"):is_visible() == false
  177. and MenuSettings.available == true
  178. and button == Config.ACTIONS.MENU and event == "Press"
  179. then
  180. UiContainer.play_sound("Back")
  181. script:request_end_sync(Script.UI, "MainMenu", "show", 0)
  182. end
  183. end,
  184. --- Moves the portrait of a party slot according to the row: back row is 20 px to the right.
  185. update_portrait = function(self, slot)
  186. local character = Characters[Party[slot]]
  187. local portrait = ui_manager:get_widget("MainMenu.Container.Characters.Character" .. tostring(slot) .. ".Portrait")
  188. portrait:set_default_animation(character.back_row and "RowBack" or "RowFront")
  189. end,
  190. --- Shows the data of one party slot.
  191. populate_character = function(self, slot)
  192. local path = "MainMenu.Container.Characters.Character" .. tostring(slot)
  193. local character = Party[slot] ~= nil and Characters[Party[slot]] or nil
  194. UiContainer.populate_character_data(path, character)
  195. if character == nil then return end
  196. ui_manager:get_widget(path .. ".Portrait"):set_image("characters/" .. tostring(Party[slot]) .. ".png")
  197. self:update_portrait(slot)
  198. ui_manager:get_widget(path .. ".LimitLevel"):set_text(tostring(character.limit.current))
  199. UiContainer.set_limit_bar(path .. ".LimitLinePart", character, BAR_WIDTH)
  200. UiContainer.set_bar(path .. ".NextLevelLinePart", UiContainer.exp_progress(character), BAR_WIDTH)
  201. end,
  202. --- Refreshes the data a submenu may have changed (level, HP/MP, limit, row, gil...).
  203. --
  204. -- Runs when a submenu closes and gives the control back to the main menu.
  205. on_resume = function(self)
  206. for slot = 1, 3 do
  207. self:populate_character(slot)
  208. end
  209. ui_manager:get_widget("MainMenu.Container.TimeGil.Gil"):set_text(UiContainer.format_money(Inventory.money))
  210. end,
  211. --- Opens the main menu.
  212. --
  213. -- Populates and updates displayed data before opening. It also pauses the game.
  214. show = function(self)
  215. self:activate()
  216. entity_manager:set_paused(true)
  217. MenuSettings.pause_available = false
  218. ui_manager:get_widget("MainMenu"):set_visible(true)
  219. for slot = 1, 3 do
  220. self:populate_character(slot)
  221. end
  222. -- TODO: Set portraits of the correct char
  223. ui_manager:get_widget("MainMenu.Container.Menu.SaveText"):set_colour(0.4, 0.4, 0.4)
  224. ui_manager:get_widget("MainMenu.Container.Location.Text"):set_text(System["MapChanger"].location_name)
  225. ui_manager:get_widget("MainMenu.Container.TimeGil.Gil"):set_text(UiContainer.format_money(Inventory.money))
  226. local location = ui_manager:get_widget("MainMenu.Container.Location")
  227. ui_manager:get_widget("MainMenu.Container.Characters"):play_animation_stop("Appear")
  228. ui_manager:get_widget("MainMenu.Container.Menu"):play_animation_stop("Appear")
  229. ui_manager:get_widget("MainMenu.Container.TimeGil"):play_animation_stop("Appear")
  230. location:play_animation_stop("Appear")
  231. -- The cursor starts at the first entry, or where it was left if the cursor memory is on.
  232. if not UiContainer.cursor_memory() then self.cursor:set(1) end
  233. self.cursor:refresh()
  234. location:animation_sync()
  235. return 0
  236. end,
  237. --- Hides the main menu and goes back to the game.
  238. --
  239. -- It also unpauses the game.
  240. hide = function(self)
  241. self:deactivate("")
  242. local location = ui_manager:get_widget("MainMenu.Container.Location")
  243. ui_manager:get_widget("MainMenu.Container.Characters"):play_animation_stop("Disappear")
  244. ui_manager:get_widget("MainMenu.Container.Menu"):play_animation_stop("Disappear")
  245. ui_manager:get_widget("MainMenu.Container.TimeGil"):play_animation_stop("Disappear")
  246. location:play_animation_stop("Disappear")
  247. location:animation_sync()
  248. ui_manager:get_widget("MainMenu"):set_visible(false)
  249. MenuSettings.pause_available = true
  250. entity_manager:set_paused(false)
  251. return 0
  252. end,
  253. }