phs_menu.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. if UiContainer == nil then UiContainer = {} end
  2. local ROOT = "PhsMenu.Container."
  3. --- Slots of the party (left) and of the reserve (right, a grid of 3x3).
  4. local PARTY_SLOTS = 3
  5. local RESERVE_COLUMNS = 3
  6. local RESERVE_ROWS = 3
  7. local RESERVE_SLOTS = RESERVE_COLUMNS * RESERVE_ROWS
  8. --- The cursor goes over "cells": 1-3 are the party slots, 4-12 the reserve slots.
  9. local TOTAL_CELLS = PARTY_SLOTS + RESERVE_SLOTS
  10. --- Highest character id the PHS shows (Cid). Young Cloud and Sephiroth can't be chosen, and the
  11. -- nine characters fit in the party (3) and the reserve (9).
  12. local MAX_CHARACTER_ID = 8
  13. local function is_party_cell(cell)
  14. return cell <= PARTY_SLOTS
  15. end
  16. --- Gets the cell the cursor goes to when a direction is pressed.
  17. --
  18. -- @param cell Current cell.
  19. -- @param button A direction button.
  20. -- @return The new cell, or nil if the cursor can't move that way.
  21. local function neighbour(cell, button)
  22. local UP, DOWN = Config.CONTROLS.UP, Config.CONTROLS.DOWN
  23. local LEFT, RIGHT = Config.CONTROLS.LEFT, Config.CONTROLS.RIGHT
  24. if is_party_cell(cell) then
  25. if button == UP then
  26. return (cell - 2) % PARTY_SLOTS + 1
  27. elseif button == DOWN then
  28. return cell % PARTY_SLOTS + 1
  29. elseif button == RIGHT then
  30. -- First column of the reserve, in the same row.
  31. return PARTY_SLOTS + (cell - 1) * RESERVE_COLUMNS + 1
  32. end
  33. return nil
  34. end
  35. local index = cell - PARTY_SLOTS - 1
  36. local column = index % RESERVE_COLUMNS
  37. local row = math.floor(index / RESERVE_COLUMNS)
  38. if button == LEFT then
  39. if column > 0 then return cell - 1 end
  40. return math.min(row + 1, PARTY_SLOTS)
  41. elseif button == RIGHT then
  42. if column < RESERVE_COLUMNS - 1 then return cell + 1 end
  43. elseif button == UP then
  44. return PARTY_SLOTS + ((row - 1) % RESERVE_ROWS) * RESERVE_COLUMNS + column + 1
  45. elseif button == DOWN then
  46. return PARTY_SLOTS + ((row + 1) % RESERVE_ROWS) * RESERVE_COLUMNS + column + 1
  47. end
  48. return nil
  49. end
  50. --- Is a character locked in the party? Locked characters can't leave the party or join it, but they
  51. -- can change slots inside it.
  52. local function is_locked(char_id)
  53. return char_id ~= nil and Characters[char_id].locked == true
  54. end
  55. --- Number of characters in the party.
  56. local function party_count()
  57. local count = 0
  58. for slot = 1, PARTY_SLOTS do
  59. if Party[slot] ~= nil then count = count + 1 end
  60. end
  61. return count
  62. end
  63. --- Handlers of the directional buttons.
  64. local function direction_buttons()
  65. local buttons = {}
  66. for _, button in ipairs({
  67. Config.CONTROLS.UP, Config.CONTROLS.DOWN, Config.CONTROLS.LEFT, Config.CONTROLS.RIGHT
  68. }) do
  69. buttons[button] = function(self, event)
  70. local target = neighbour(self.cursor:index(), button)
  71. if target ~= nil then
  72. UiContainer.play_sound("Cursor")
  73. self.cursor:set(target)
  74. end
  75. end
  76. end
  77. return buttons
  78. end
  79. --- The PHS menu: changes the characters of the party.
  80. --
  81. -- The party is on the left and the rest of the available characters on the right. A character is
  82. -- picked with OK, and then OK on another place swaps both:
  83. -- * party <-> party: changes the order (or moves the character to an empty slot).
  84. -- * party <-> reserve: the character leaves the party and the other one (if any) takes its place.
  85. -- * reserve <-> reserve: not allowed.
  86. -- Locked characters can't leave the party or join it, but their slots can be swapped.
  87. -- The party can only be left once it has three characters, or all the available ones if there are
  88. -- fewer.
  89. --
  90. -- This menu never changes the leader of the field (see Party.get_leader). If the leader leaves the
  91. -- party, the character of the first slot becomes the leader.
  92. UiContainer.PhsMenu = UiContainer.Menu.new{
  93. id = "phs",
  94. parent = "main",
  95. --- Cell the cursor is on (see TOTAL_CELLS).
  96. cursor = UiContainer.Cursor.new{
  97. widget = ROOT .. "Cursor",
  98. total = TOTAL_CELLS,
  99. sound = false,
  100. on_move = function(cell)
  101. UiContainer.PhsMenu:update_info(cell)
  102. end,
  103. },
  104. --- Cell of the picked character, or nil.
  105. held = nil,
  106. --- IDs of the available characters that aren't in the party, in the order they are shown.
  107. reserve = {},
  108. states = {
  109. [""] = {
  110. buttons = direction_buttons(),
  111. confirm = function(self)
  112. return self:on_confirm(self.cursor:index())
  113. end,
  114. cancel = function(self)
  115. if self.held ~= nil then
  116. self:set_held(nil)
  117. return
  118. end
  119. if not self:is_party_complete() then return false end
  120. script:request_end_sync(Script.UI, "PhsMenu", "hide", 0)
  121. end,
  122. },
  123. },
  124. --- Gets the character in a cell.
  125. -- @return Character ID, or nil if the cell is empty.
  126. get_character = function(self, cell)
  127. if is_party_cell(cell) then return Party[cell] end
  128. return self.reserve[cell - PARTY_SLOTS]
  129. end,
  130. --- Puts a character in a cell, or empties it.
  131. set_character = function(self, cell, char_id)
  132. if is_party_cell(cell) then
  133. Party[cell] = char_id
  134. else
  135. self.reserve[cell - PARTY_SLOTS] = char_id
  136. end
  137. end,
  138. --- Number of characters in the reserve. The reserve can have empty cells between them.
  139. reserve_count = function(self)
  140. local count = 0
  141. for slot = 1, RESERVE_SLOTS do
  142. if self.reserve[slot] ~= nil then count = count + 1 end
  143. end
  144. return count
  145. end,
  146. --- Fills the reserve with the available characters that aren't in the party.
  147. --
  148. -- Characters are packed from the first cell, in order. From then on they keep their cell
  149. -- while the party is being formed (see swap).
  150. refresh_reserve = function(self)
  151. self.reserve = {}
  152. for id = 0, MAX_CHARACTER_ID do
  153. local character = Characters[id]
  154. if character ~= nil and character.enabled == true then
  155. local in_party = false
  156. for slot = 1, PARTY_SLOTS do
  157. if Party[slot] == id then in_party = true end
  158. end
  159. if not in_party then
  160. self.reserve[#self.reserve + 1] = id
  161. end
  162. end
  163. end
  164. end,
  165. --- Has the party all the characters it can have?
  166. --
  167. -- Three are needed, unless there are fewer characters available.
  168. is_party_complete = function(self)
  169. local available = party_count() + self:reserve_count()
  170. return party_count() >= math.min(PARTY_SLOTS, available)
  171. end,
  172. --- Picks a character, or drops the one that was picked.
  173. -- @param cell Cell that was selected. nil to drop.
  174. set_held = function(self, cell)
  175. self.held = cell
  176. local held = ui_manager:get_widget(ROOT .. "Held")
  177. if cell == nil then
  178. held:set_visible(false)
  179. else
  180. held:set_default_animation("Position" .. tostring(cell))
  181. held:set_visible(true)
  182. end
  183. end,
  184. --- OK was pressed on a cell.
  185. -- @return false if the action isn't allowed.
  186. on_confirm = function(self, cell)
  187. local character = self:get_character(cell)
  188. if self.held == nil then
  189. -- Pick a character.
  190. -- A locked character can only be moved inside the party.
  191. if character == nil or (is_locked(character) and not is_party_cell(cell)) then return false end
  192. self:set_held(cell)
  193. return
  194. end
  195. -- Picking the same one again drops it.
  196. if self.held == cell then
  197. self:set_held(nil)
  198. return
  199. end
  200. if not self:swap(self.held, cell) then return false end
  201. self:set_held(nil)
  202. self:populate()
  203. end,
  204. --- Swaps the characters of two cells.
  205. -- @return false if it isn't allowed.
  206. swap = function(self, from, to)
  207. if not is_party_cell(from) and not is_party_cell(to) then return false end
  208. local a = self:get_character(from)
  209. local b = self:get_character(to)
  210. -- Locked characters can swap slots in the party, but can't leave it or join it.
  211. if (is_locked(a) or is_locked(b)) and not (is_party_cell(from) and is_party_cell(to)) then
  212. return false
  213. end
  214. -- Both cells exchange their contents, so nobody in the reserve changes place.
  215. self:set_character(from, b)
  216. self:set_character(to, a)
  217. set_party(Party[1], Party[2], Party[3], true)
  218. return true
  219. end,
  220. --- Moves the characters of the party to the first slots, with no gaps between them.
  221. compact_party = function(self)
  222. local members = {}
  223. for slot = 1, PARTY_SLOTS do
  224. if Party[slot] ~= nil then members[#members + 1] = Party[slot] end
  225. end
  226. set_party(members[1], members[2], members[3], true)
  227. end,
  228. --- Shows the characters of the party and of the reserve.
  229. populate = function(self)
  230. self:update_info(self.cursor:index())
  231. for slot = 1, PARTY_SLOTS do
  232. local path = ROOT .. "Party.Slot" .. tostring(slot)
  233. local id = Party[slot]
  234. if id ~= nil then
  235. UiContainer.populate_character_data(path, Characters[id])
  236. ui_manager:get_widget(path .. ".Portrait"):set_image("characters/" .. tostring(id) .. ".png")
  237. else
  238. UiContainer.populate_character_data(path, nil)
  239. end
  240. end
  241. for slot = 1, RESERVE_SLOTS do
  242. local path = ROOT .. "Reserve.Slot" .. tostring(slot)
  243. local id = self.reserve[slot]
  244. ui_manager:get_widget(path):set_visible(id ~= nil)
  245. if id ~= nil then
  246. ui_manager:get_widget(path .. ".Portrait"):set_image("characters/" .. tostring(id) .. ".png")
  247. end
  248. end
  249. end,
  250. --- Shows the details of the character under the cursor in the top right window.
  251. --
  252. -- Only for the reserve: the characters of the party already show them next to their portrait.
  253. update_info = function(self, cell)
  254. local path = ROOT .. "Info.Character"
  255. local id = nil
  256. if not is_party_cell(cell) then id = self:get_character(cell) end
  257. if id ~= nil then
  258. UiContainer.populate_character_data(path, Characters[id])
  259. else
  260. UiContainer.populate_character_data(path, nil)
  261. end
  262. end,
  263. --- Is the menu opened by a field script (change party) instead of by the main menu?
  264. standalone = false,
  265. --- Opens the PHS menu.
  266. show = function(self)
  267. self:activate()
  268. if self.standalone then
  269. entity_manager:set_paused(true)
  270. MenuSettings.pause_available = false
  271. self.menu_was_available = MenuSettings.available
  272. MenuSettings.available = false
  273. end
  274. -- Empty slots may hold an invalid ID (e.g. -1). Leave them as nil.
  275. for slot = 1, PARTY_SLOTS do
  276. if Party[slot] ~= nil and Characters[Party[slot]] == nil then Party[slot] = nil end
  277. end
  278. set_party(Party[1], Party[2], Party[3], true)
  279. -- Whoever is in the party is available, even if no script enabled them.
  280. for slot = 1, PARTY_SLOTS do
  281. if Party[slot] ~= nil then Characters[Party[slot]].enabled = true end
  282. end
  283. self:refresh_reserve()
  284. self:set_held(nil)
  285. self:populate()
  286. self.cursor:set(1)
  287. ui_manager:get_widget("PhsMenu"):set_visible(true)
  288. return 0
  289. end,
  290. --- Closes the PHS menu and goes back to the main menu.
  291. hide = function(self)
  292. self:compact_party()
  293. ui_manager:get_widget("PhsMenu"):set_visible(false)
  294. if self.standalone then
  295. self.standalone = false
  296. self:deactivate("")
  297. MenuSettings.available = self.menu_was_available
  298. MenuSettings.pause_available = true
  299. entity_manager:set_paused(false)
  300. else
  301. self:deactivate()
  302. end
  303. return 0
  304. end,
  305. }