exchange_menu.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. if UiContainer == nil then UiContainer = {} end
  2. --- The materia exchange menu (Materia > Order > Exchange).
  3. --
  4. -- The left window lists every character enabled in the PHS, with the slots of their weapon and
  5. -- armor. The right window is the materia of the inventory. The cursor goes through all the slots
  6. -- and all the positions of the inventory.
  7. --
  8. -- Confirm on a position with a materia picks it up: its cursor stays blinking while the real one
  9. -- moves. Confirm on another position swaps the two (the destination can be empty). Confirm on the
  10. -- picked position, or cancel, drops it. Cancel with nothing picked closes the menu.
  11. --
  12. -- A position is a table: {kind = "equip", char = index in `chars`, part = "weapon" or "armor",
  13. -- slot = 0-7} or {kind = "inv", index = slot of Materia}.
  14. UiContainer.ExchangeMenu = UiContainer.Menu.new{
  15. id = "exchange",
  16. --- Menu that opened this one.
  17. parent = "materia",
  18. --- Character blocks shown at once in the left window.
  19. BLOCKS = 4,
  20. --- Rows shown at once in the inventory window.
  21. INV_ROWS = 14,
  22. --- Position and size of the elements of the left window.
  23. BLOCK_Y = 0,
  24. BLOCK_HEIGHT = 58,
  25. ROW_Y = {weapon = 22, armor = 39},
  26. SLOT_X = 70,
  27. SLOT_PITCH = 16,
  28. --- Position of the rows of the inventory window.
  29. INV_Y = 6,
  30. INV_PITCH = 16,
  31. --- IDs of the characters in the list (enabled in the PHS).
  32. chars = {},
  33. --- First character shown in the left window.
  34. first = 1,
  35. --- First inventory slot shown in the right window.
  36. inv_first = 1,
  37. --- Position of the cursor.
  38. pos = nil,
  39. --- Position of the picked materia, or nil.
  40. held = nil,
  41. --- Last position of the left window and of the inventory the cursor was on, to come back to.
  42. last_equip = nil,
  43. last_inv = 1,
  44. states = {
  45. [""] = {
  46. confirm_sound = false,
  47. cancel_sound = false,
  48. confirm = function(self) return self:confirm() end,
  49. cancel = function(self) return self:cancel() end,
  50. buttons = {},
  51. },
  52. },
  53. -----------------------------------------------------------------------------------------------
  54. -- Opening and closing
  55. -----------------------------------------------------------------------------------------------
  56. --- Opens the menu.
  57. show = function(self)
  58. self.chars = {}
  59. for char_id = 0, 8 do
  60. local character = Characters[char_id]
  61. if type(character) == "table" and character.enabled == true then
  62. self.chars[#self.chars + 1] = char_id
  63. end
  64. end
  65. self.first = 1
  66. self.inv_first = 1
  67. self.held = nil
  68. self.last_inv = 1
  69. self.last_equip = nil
  70. local rows = self:rows()
  71. if #rows > 0 then
  72. self.pos = {kind = "equip", char = rows[1].char, part = rows[1].part, slot = 0}
  73. else
  74. self.pos = {kind = "inv", index = 1}
  75. end
  76. self:activate()
  77. ui_manager:get_widget("MateriaMenu"):set_visible(false)
  78. ui_manager:get_widget("ExchangeMenu"):set_visible(true)
  79. self:refresh()
  80. return 0
  81. end,
  82. --- Closes the menu and goes back to the materia menu.
  83. hide = function(self)
  84. ui_manager:get_widget("ExchangeMenu"):set_visible(false)
  85. ui_manager:get_widget("MateriaMenu"):set_visible(true)
  86. UiContainer.current_menu = "materia"
  87. UiContainer.current_submenu = "order"
  88. -- The materia menu shows the equipment, so it has to know what changed.
  89. UiContainer.MateriaMenu:on_exchange_closed()
  90. return 0
  91. end,
  92. -----------------------------------------------------------------------------------------------
  93. -- Positions
  94. -----------------------------------------------------------------------------------------------
  95. --- Number of materia slots of the weapon or armor of a character of the list.
  96. slot_count = function(self, char, part)
  97. local equipment = Characters[self.chars[char]][part]
  98. return #(Game.Items[equipment.id].slots)
  99. end,
  100. --- Rows of the left window that have slots, in order.
  101. --
  102. -- @return List of {char = index in `chars`, part = "weapon" or "armor", slots = count}.
  103. rows = function(self)
  104. local rows = {}
  105. for char = 1, #self.chars do
  106. for _, part in ipairs({"weapon", "armor"}) do
  107. local count = self:slot_count(char, part)
  108. if count > 0 then rows[#rows + 1] = {char = char, part = part, slots = count} end
  109. end
  110. end
  111. return rows
  112. end,
  113. --- Gets the materia in a position (nil if empty).
  114. get = function(self, pos)
  115. if pos.kind == "inv" then return Materia[pos.index] end
  116. return Characters[self.chars[pos.char]][pos.part].materia[pos.slot]
  117. end,
  118. --- Puts a materia (or nil) in a position.
  119. set = function(self, pos, materia)
  120. if pos.kind == "inv" then
  121. Materia[pos.index] = materia
  122. else
  123. Characters[self.chars[pos.char]][pos.part].materia[pos.slot] = materia
  124. end
  125. end,
  126. --- Are two positions the same?
  127. same = function(self, a, b)
  128. if a == nil or b == nil or a.kind ~= b.kind then return false end
  129. if a.kind == "inv" then return a.index == b.index end
  130. return a.char == b.char and a.part == b.part and a.slot == b.slot
  131. end,
  132. --- Moves the cursor up or down.
  133. --
  134. -- In the left window it goes through the rows that have slots (wrapping around), keeping the
  135. -- column or going to the last slot of a shorter row. In the inventory it goes through all the
  136. -- slots, without wrapping.
  137. move_vertical = function(self, delta)
  138. local pos = self.pos
  139. if pos.kind == "inv" then
  140. local index = pos.index + delta
  141. if index < 1 or index > MAX_MATERIA_SLOTS then
  142. UiContainer.play_sound("Error")
  143. return
  144. end
  145. pos.index = index
  146. else
  147. local rows = self:rows()
  148. local current = 1
  149. for i, row in ipairs(rows) do
  150. if row.char == pos.char and row.part == pos.part then current = i end
  151. end
  152. local target = rows[(current - 1 + delta) % #rows + 1]
  153. pos.char, pos.part = target.char, target.part
  154. pos.slot = math.min(pos.slot, target.slots - 1)
  155. end
  156. UiContainer.play_sound("Cursor")
  157. self:refresh()
  158. end,
  159. --- Moves the cursor left or right.
  160. --
  161. -- Right from the last slot of a row goes to the inventory, and left from the inventory goes
  162. -- back to the slot it came from.
  163. move_horizontal = function(self, delta)
  164. local pos = self.pos
  165. if pos.kind == "inv" then
  166. if delta > 0 then
  167. UiContainer.play_sound("Error")
  168. return
  169. end
  170. local rows = self:rows()
  171. if #rows == 0 then
  172. UiContainer.play_sound("Error")
  173. return
  174. end
  175. self.last_inv = pos.index
  176. local back = self.last_equip or {char = rows[1].char, part = rows[1].part, slot = 0}
  177. self.pos = {kind = "equip", char = back.char, part = back.part, slot = back.slot}
  178. else
  179. local slot = pos.slot + delta
  180. if slot < 0 then
  181. UiContainer.play_sound("Error")
  182. return
  183. elseif slot >= self:slot_count(pos.char, pos.part) then
  184. self.last_equip = {char = pos.char, part = pos.part, slot = pos.slot}
  185. self.pos = {kind = "inv", index = self.last_inv}
  186. else
  187. pos.slot = slot
  188. end
  189. end
  190. UiContainer.play_sound("Cursor")
  191. self:refresh()
  192. end,
  193. --- Moves the cursor in the inventory by a page.
  194. move_page = function(self, delta)
  195. if self.pos.kind ~= "inv" then return end
  196. local index = math.max(1, math.min(MAX_MATERIA_SLOTS, self.pos.index + delta * self.INV_ROWS))
  197. if index == self.pos.index then
  198. UiContainer.play_sound("Error")
  199. return
  200. end
  201. UiContainer.play_sound("Cursor")
  202. self.pos.index = index
  203. self:refresh()
  204. end,
  205. -----------------------------------------------------------------------------------------------
  206. -- Actions
  207. -----------------------------------------------------------------------------------------------
  208. --- Confirm: picks up the materia of the cursor, or swaps it with the picked one.
  209. confirm = function(self)
  210. if self.held == nil then
  211. if self:get(self.pos) == nil then return false end
  212. UiContainer.play_sound("Cursor")
  213. self.held = {}
  214. for key, value in pairs(self.pos) do self.held[key] = value end
  215. self:refresh()
  216. return
  217. end
  218. if self:same(self.held, self.pos) then
  219. UiContainer.play_sound("Back")
  220. self.held = nil
  221. self:refresh()
  222. return
  223. end
  224. local from, to = self.held, self.pos
  225. local moved, replaced = self:get(from), self:get(to)
  226. self:set(from, replaced)
  227. self:set(to, moved)
  228. self.held = nil
  229. -- The materia change the stats of the characters involved, and the current HP and MP can't
  230. -- be over the new maximums.
  231. local recalculated = {}
  232. for _, pos in ipairs({from, to}) do
  233. if pos.kind == "equip" and not recalculated[pos.char] then
  234. recalculated[pos.char] = true
  235. Characters.recalculate_stats(self.chars[pos.char])
  236. end
  237. end
  238. UiContainer.play_sound("Cursor")
  239. self:refresh()
  240. end,
  241. --- Cancel: drops the picked materia, or closes the menu.
  242. cancel = function(self)
  243. if self.held ~= nil then
  244. self.held = nil
  245. self:refresh()
  246. UiContainer.play_sound("Back")
  247. return
  248. end
  249. UiContainer.play_sound("Back")
  250. script:request_end_sync(Script.UI, "ExchangeMenu", "hide", 0)
  251. end,
  252. -----------------------------------------------------------------------------------------------
  253. -- Widgets
  254. -----------------------------------------------------------------------------------------------
  255. --- Scrolls the windows so that the cursor is in them, and redraws everything.
  256. refresh = function(self)
  257. local pos = self.pos
  258. if pos.kind == "equip" then
  259. if pos.char < self.first then
  260. self.first = pos.char
  261. elseif pos.char > self.first + self.BLOCKS - 1 then
  262. self.first = pos.char - self.BLOCKS + 1
  263. end
  264. else
  265. if pos.index < self.inv_first then
  266. self.inv_first = pos.index
  267. elseif pos.index > self.inv_first + self.INV_ROWS - 1 then
  268. self.inv_first = pos.index - self.INV_ROWS + 1
  269. end
  270. end
  271. self:populate_equip()
  272. self:populate_inventory()
  273. self:place_cursors()
  274. local materia = self:get(pos)
  275. self:populate_info(materia)
  276. ui_manager:get_widget("ExchangeMenu.Container.Help.Help"):set_text(
  277. materia ~= nil and Game.Materia[materia.id].description or "")
  278. end,
  279. --- Fills the top bar with the icon, name and stars of a materia (nothing if it is nil).
  280. populate_info = function(self, materia)
  281. local bar = "ExchangeMenu.Container.Info."
  282. ui_manager:get_widget(bar .. "Icon"):set_visible(materia ~= nil)
  283. ui_manager:get_widget(bar .. "Name"):set_visible(materia ~= nil)
  284. local data = materia ~= nil and Game.Materia[materia.id] or nil
  285. local levels = data ~= nil and data.levels_ap or {}
  286. local level = 0
  287. for _, level_ap in ipairs(levels) do
  288. if level_ap > (materia.ap or 0) then break end
  289. level = level + 1
  290. end
  291. if data ~= nil then
  292. ui_manager:get_widget(bar .. "Icon"):set_image("icons/materia_" .. tostring(data.type) .. ".png")
  293. ui_manager:get_widget(bar .. "Name"):set_text(data.name)
  294. end
  295. for star = 1, 5 do
  296. local widget = ui_manager:get_widget(bar .. "Stars.Star" .. star)
  297. if data == nil or star > #levels then
  298. widget:set_visible(false)
  299. else
  300. widget:set_image((star <= level and "icons/materia_star_" or "icons/materia_star_empty_")
  301. .. tostring(data.type) .. ".png")
  302. widget:set_visible(true)
  303. end
  304. end
  305. end,
  306. --- Fills the blocks of the left window.
  307. populate_equip = function(self)
  308. local window = "ExchangeMenu.Container.Characters."
  309. for b = 1, self.BLOCKS do
  310. local block = window .. "Block" .. b
  311. local char_id = self.chars[self.first + b - 1]
  312. ui_manager:get_widget(block):set_visible(char_id ~= nil)
  313. if char_id ~= nil then
  314. local character = Characters[char_id]
  315. ui_manager:get_widget(block .. ".Portrait"):set_image("characters/" .. tostring(char_id) .. ".png")
  316. ui_manager:get_widget(block .. ".Name"):set_text(character.name)
  317. ui_manager:get_widget(block .. ".WIcon"):set_image("icons/item_weapon_" .. tostring(char_id) .. ".png")
  318. self:populate_slots(block, "W", character.weapon)
  319. self:populate_slots(block, "A", character.armor)
  320. end
  321. end
  322. -- Scroll bar.
  323. local scroll = ui_manager:get_widget(window .. "Scroll")
  324. local hidden = math.max(0, #self.chars - self.BLOCKS)
  325. local fraction = hidden > 0 and (self.first - 1) / hidden or 0
  326. scroll:set_y(fraction * 100, 3 - fraction * 24)
  327. scroll:set_visible(hidden > 0)
  328. ui_manager:get_widget(window .. "ScrollBg"):set_visible(hidden > 0)
  329. end,
  330. --- Fills the slots of the weapon or armor of a block.
  331. --
  332. -- @param block Path of the block.
  333. -- @param prefix "W" or "A".
  334. -- @param equipment Weapon or armor of the character (an entry of Characters).
  335. populate_slots = function(self, block, prefix, equipment)
  336. local slots = Game.Items[equipment.id].slots
  337. for s = 1, 8 do
  338. local slot = ui_manager:get_widget(block .. "." .. prefix .. "Slot" .. s)
  339. local mat = ui_manager:get_widget(block .. "." .. prefix .. "Mat" .. s)
  340. slot:set_visible(#slots >= s)
  341. local materia = equipment.materia[s - 1]
  342. if #slots >= s and materia ~= nil then
  343. mat:set_image("icons/materia_" .. tostring(Game.Materia[materia.id].type) .. ".png")
  344. mat:set_visible(true)
  345. else
  346. mat:set_visible(false)
  347. end
  348. -- A link joins an odd slot with the next one.
  349. if s % 2 ~= 0 then
  350. ui_manager:get_widget(block .. "." .. prefix .. "Link" .. (math.floor(s / 2) + 1)):set_visible(
  351. #slots > s and slots[s] == 1)
  352. end
  353. end
  354. end,
  355. --- Fills the rows of the inventory window.
  356. populate_inventory = function(self)
  357. local window = "ExchangeMenu.Container.Inventory."
  358. for r = 1, self.INV_ROWS do
  359. local materia = Materia[self.inv_first + r - 1]
  360. local icon = ui_manager:get_widget(window .. "Icon" .. r)
  361. local name = ui_manager:get_widget(window .. "Name" .. r)
  362. if materia ~= nil then
  363. local data = Game.Materia[materia.id]
  364. icon:set_image("icons/materia_" .. tostring(data.type) .. ".png")
  365. icon:set_visible(true)
  366. name:set_text(data.name)
  367. name:set_visible(true)
  368. else
  369. icon:set_visible(false)
  370. name:set_visible(false)
  371. end
  372. end
  373. local scroll = ui_manager:get_widget(window .. "Scroll")
  374. local fraction = (self.inv_first - 1) / (MAX_MATERIA_SLOTS - self.INV_ROWS)
  375. scroll:set_y(fraction * 100, 3 - fraction * 24)
  376. end,
  377. --- Coordinates of a position inside its window, and the window.
  378. --
  379. -- @return Window ("Characters" or "Inventory"), x, y, or nil if the position is not visible.
  380. cursor_place = function(self, pos)
  381. if pos.kind == "inv" then
  382. local row = pos.index - self.inv_first
  383. if row < 0 or row >= self.INV_ROWS then return nil end
  384. return "Inventory", -8, self.INV_Y + self.INV_PITCH * row - 1
  385. end
  386. local block = pos.char - self.first
  387. if block < 0 or block >= self.BLOCKS then return nil end
  388. return "Characters", self.SLOT_X + self.SLOT_PITCH * pos.slot - 22,
  389. self.BLOCK_Y + self.BLOCK_HEIGHT * block + self.ROW_Y[pos.part] - 1
  390. end,
  391. --- Places the cursor and the blinking cursor of the picked materia.
  392. place_cursors = function(self)
  393. local base = "ExchangeMenu.Container."
  394. for _, window in ipairs({"Characters", "Inventory"}) do
  395. ui_manager:get_widget(base .. window .. ".Cursor"):set_visible(false)
  396. ui_manager:get_widget(base .. window .. ".Held"):set_visible(false)
  397. end
  398. local function place(pos, name)
  399. if pos == nil then return end
  400. local window, x, y = self:cursor_place(pos)
  401. if window == nil then return end
  402. local cursor = ui_manager:get_widget(base .. window .. "." .. name)
  403. cursor:set_x(0, x)
  404. cursor:set_y(0, y)
  405. cursor:set_visible(true)
  406. if name == "Held" then cursor:set_default_animation("Blink") end
  407. end
  408. place(self.pos, "Cursor")
  409. place(self.held, "Held")
  410. end,
  411. }
  412. --- Directional buttons and pages.
  413. local function direction(handler)
  414. return function(self, event)
  415. if event == "Press" or event == "Repeat" then handler(self) end
  416. end
  417. end
  418. local buttons = UiContainer.ExchangeMenu.states[""].buttons
  419. buttons[Config.CONTROLS.UP] = direction(function(self) self:move_vertical(-1) end)
  420. buttons[Config.CONTROLS.DOWN] = direction(function(self) self:move_vertical(1) end)
  421. buttons[Config.CONTROLS.LEFT] = direction(function(self) self:move_horizontal(-1) end)
  422. buttons[Config.CONTROLS.RIGHT] = direction(function(self) self:move_horizontal(1) end)
  423. buttons[Config.ACTIONS.L1] = direction(function(self) self:move_page(-1) end)
  424. buttons[Config.ACTIONS.R1] = direction(function(self) self:move_page(1) end)