materia_menu.lua 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. if UiContainer == nil then UiContainer = {} end
  2. --- The item menu.
  3. UiContainer.MateriaMenu = {
  4. --- Current header cursor row (1: Check/Weapon, 2: Order/Armor).
  5. header_position_row = 1,
  6. --- Current header cursor position (1: Check/Order, 2-9: Weapon/Armor slots).
  7. header_position = 1,
  8. --- Current header cursor max row.
  9. header_position_row_total = 2,
  10. --- Current header cursor max position.
  11. header_position_total = 9,
  12. --- ID of the character the menu is open for.
  13. char_id = -1,
  14. --- Cursor position in the materia list.
  15. list_position = 1,
  16. --- Max cursor postion in the materia list.
  17. list_position_total = 10,
  18. --- First visible position in the materia list
  19. list_first_visible = 1,
  20. --- Max materias in the list
  21. list_size = 200,
  22. --- Commands of the character, shown in the Check window.
  23. commands = {},
  24. --- Cursor over the commands of the Check window (columns of four, filled top to bottom).
  25. command_cursor = UiContainer.Cursor.new{
  26. widget = "MateriaMenu.Container.Details.Commands.Box.Cursor",
  27. total = 1,
  28. on_move = function() UiContainer.MateriaMenu:populate_command_help() end,
  29. },
  30. --- Cursor over the options of the Order menu.
  31. order_cursor = UiContainer.Cursor.new{
  32. widget = "MateriaMenu.Container.OrderMenu.Cursor",
  33. total = 4,
  34. wrap = true,
  35. on_move = function() UiContainer.MateriaMenu:populate_order_help() end,
  36. },
  37. --- Descriptions of the options of the Order menu.
  38. ORDER_HELP = {
  39. "Sort the materia in the inventory",
  40. "Exchange materia between the characters and the inventory",
  41. "Remove all the materia equipped on the character",
  42. "Trash materia",
  43. },
  44. --- Options of the Order menu that are not available yet.
  45. ORDER_DISABLED = {},
  46. --- True while the materia list is used to choose a materia to trash (Order > Trash).
  47. trash_mode = false,
  48. --- Entries of the list of spells, summons or enemy skills of a command.
  49. abilities = {},
  50. --- Cursor over the abilities list.
  51. ability_cursor = UiContainer.ListCursor.new{
  52. widget = "MateriaMenu.Container.Abilities.Cursor",
  53. rows = 10,
  54. on_scroll = function() UiContainer.MateriaMenu:populate_abilities() end,
  55. on_move = function() UiContainer.MateriaMenu:populate_ability_help() end,
  56. },
  57. --- Run when the menu is creaded.
  58. --
  59. -- It does nothing.
  60. on_start = function(self)
  61. return 0
  62. end,
  63. --- Handles button events.
  64. --
  65. -- For the current submenu, handles directional keys, enter and escape events.
  66. -- @param button Pressed direction or action name.
  67. -- @param event Trigger event. Normally, "Press".
  68. on_button = function(self, button, event)
  69. if UiContainer.current_menu == "materia" then
  70. if UiContainer.current_submenu == "" then
  71. -- TODO: Handle L1/R1 (change character)
  72. -- TODO: Handle square: Materia menu
  73. if button == Config.ACTIONS.CANCEL and event == "Press" then
  74. audio_manager:play_sound("Back")
  75. UiContainer.current_menu = "main"
  76. script:request_end_sync(Script.UI, "MateriaMenu", "hide", 0)
  77. elseif button == Config.CONTROLS.DOWN then
  78. audio_manager:play_sound("Cursor")
  79. self.header_position_row = self.header_position_row + 1
  80. if self.header_position_row > self.header_position_row_total then
  81. self.header_position_row = 1
  82. end
  83. ui_manager:get_widget("MateriaMenu.Container.Character.Cursor"):set_default_animation("Position" .. self.header_position_row .. "-" .. self.header_position)
  84. self.populate_details(self, false)
  85. elseif button == Config.CONTROLS.UP then
  86. audio_manager:play_sound("Cursor")
  87. self.header_position_row = self.header_position_row - 1
  88. if self.header_position_row < 1 then
  89. self.header_position_row = self.header_position_row_total
  90. end
  91. ui_manager:get_widget("MateriaMenu.Container.Character.Cursor"):set_default_animation("Position" .. self.header_position_row .. "-" .. self.header_position)
  92. self.populate_details(self, false)
  93. elseif button == Config.CONTROLS.RIGHT then
  94. audio_manager:play_sound("Cursor")
  95. self.header_position = self.header_position + 1
  96. if self.header_position > self.header_position_total then
  97. self.header_position = 1
  98. end
  99. ui_manager:get_widget("MateriaMenu.Container.Character.Cursor"):set_default_animation("Position" .. self.header_position_row .. "-" .. self.header_position)
  100. self.populate_details(self, false)
  101. elseif button == Config.CONTROLS.LEFT then
  102. audio_manager:play_sound("Cursor")
  103. self.header_position = self.header_position - 1
  104. if self.header_position < 1 then
  105. self.header_position = self.header_position_total
  106. end
  107. ui_manager:get_widget("MateriaMenu.Container.Character.Cursor"):set_default_animation("Position" .. self.header_position_row .. "-" .. self.header_position)
  108. self.populate_details(self, false)
  109. elseif button == Config.ACTIONS.CONFIRM then
  110. if self.header_position_row == 1 and self.header_position == 1 then
  111. if #self.commands == 0 then
  112. audio_manager:play_sound("Error")
  113. else
  114. audio_manager:play_sound("Cursor")
  115. self.submenu_commands(self)
  116. end
  117. elseif self.header_position_row == 2 and self.header_position == 1 then
  118. audio_manager:play_sound("Cursor")
  119. self.submenu_order(self)
  120. else
  121. -- TODO: Check for avalid slot before entering list
  122. if self.header_position_row == 1 and #(Game.Items[Characters[self.char_id].weapon.id].slots) > self.header_position - 2 then
  123. self.submenu_list(self)
  124. elseif self.header_position_row == 2 and #(Game.Items[Characters[self.char_id].armor.id].slots) > self.header_position -2 then
  125. self.submenu_list(self)
  126. else
  127. audio_manager:play_sound("Error")
  128. return 0
  129. end
  130. audio_manager:play_sound("Cursor")
  131. end
  132. -- TODO: Triangle->Remove
  133. else
  134. return 0
  135. end
  136. elseif UiContainer.current_submenu == "order" then
  137. if button == Config.ACTIONS.CANCEL and event == "Press" then
  138. audio_manager:play_sound("Back")
  139. self.submenu_none(self)
  140. elseif button == Config.ACTIONS.CONFIRM and event == "Press" then
  141. self.run_order(self)
  142. elseif event == "Press" or event == "Repeat" then
  143. self.order_cursor:handle(button, "vertical")
  144. end
  145. elseif UiContainer.current_submenu == "commands" then
  146. if button == Config.ACTIONS.CANCEL and event == "Press" then
  147. audio_manager:play_sound("Back")
  148. self.submenu_none(self)
  149. elseif button == Config.ACTIONS.CONFIRM and event == "Press" then
  150. self.open_command(self)
  151. elseif event == "Press" or event == "Repeat" then
  152. self:move_command_cursor(button)
  153. end
  154. elseif UiContainer.current_submenu == "abilities" then
  155. if button == Config.ACTIONS.CANCEL and event == "Press" then
  156. audio_manager:play_sound("Back")
  157. self.submenu_commands(self)
  158. elseif event == "Press" or event == "Repeat" then
  159. self.ability_cursor:handle(button, "vertical")
  160. end
  161. elseif UiContainer.current_submenu == "list" then
  162. if button == Config.CONTROLS.DOWN then
  163. if self.list_first_visible + self.list_position > self.list_size then
  164. -- At the very end, do nothing
  165. audio_manager:play_sound("Error")
  166. return 0
  167. end
  168. audio_manager:play_sound("Cursor")
  169. if self.list_position == self.list_position_total then
  170. -- The cursor is at the end. Scroll page, but dont change cursor.
  171. self.list_first_visible = self.list_first_visible + 1
  172. self.populate_list(self)
  173. else
  174. -- The cursor is not at the end, move cursor down.
  175. self.list_position = self.list_position + 1
  176. end
  177. ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_default_animation("Position" .. self.list_position)
  178. -- Show details
  179. self.populate_details(self, true)
  180. elseif button == Config.CONTROLS.UP then
  181. if self.list_first_visible == 1 and self.list_position == 1 then
  182. -- At the very begining, do nothing
  183. audio_manager:play_sound("Error")
  184. return 0
  185. end
  186. audio_manager:play_sound("Cursor")
  187. if self.list_position == 1 then
  188. -- The cursor is at the end. Scroll page, but dont change cursor.
  189. self.list_first_visible = self.list_first_visible - 1
  190. self.populate_list(self)
  191. else
  192. -- The cursor is not at the end, move cursor down.
  193. self.list_position = self.list_position - 1
  194. end
  195. ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_default_animation("Position" .. self.list_position)
  196. -- Show details
  197. self.populate_details(self, true)
  198. elseif button == Config.ACTIONS.R1 then
  199. if self.list_first_visible + self.list_position_total < self.list_size - self.list_position_total then
  200. audio_manager:play_sound("Cursor")
  201. self.list_first_visible = self.list_first_visible + self.list_position_total
  202. self.populate_list(self)
  203. self.populate_details(self, true)
  204. else
  205. audio_manager:play_sound("Error")
  206. end
  207. elseif button == Config.ACTIONS.L1 then
  208. if self.list_first_visible - self.list_position_total > 1 then
  209. audio_manager:play_sound("Cursor")
  210. self.list_first_visible = self.list_first_visible - self.list_position_total
  211. self.populate_list(self)
  212. self.populate_details(self, true)
  213. else
  214. audio_manager:play_sound("Error")
  215. end
  216. elseif button == Config.ACTIONS.CANCEL and event == "Press" then
  217. audio_manager:play_sound("Back")
  218. if self.trash_mode then
  219. self.submenu_order(self)
  220. self.order_cursor:set(4)
  221. else
  222. self.submenu_none(self)
  223. end
  224. elseif button == Config.ACTIONS.CONFIRM then
  225. if self.trash_mode then
  226. self.submenu_trash_confirm(self)
  227. else
  228. -- TODO: Replace for "Equip materia" sound.
  229. audio_manager:play_sound("Cursor")
  230. self.equip_materia(self)
  231. end
  232. end
  233. elseif UiContainer.current_submenu == "trash_confirm" then
  234. if button == Config.ACTIONS.CANCEL and event == "Press" then
  235. audio_manager:play_sound("Back")
  236. self.submenu_trash(self)
  237. elseif button == Config.ACTIONS.CONFIRM and event == "Press" then
  238. self.trash_materia(self)
  239. end
  240. end
  241. end
  242. return 0
  243. end,
  244. --- Opens the materia menu.
  245. --
  246. -- Populates and updates displayed data before opening.
  247. show = function(self)
  248. self.char_id = UiContainer.current_character_id
  249. ui_manager:get_widget("MateriaMenu"):set_visible(true)
  250. UiContainer.current_menu = "materia"
  251. UiContainer.current_submenu = ""
  252. self.header_position_row = 1
  253. self.header_position = 1
  254. self.list_position = 1
  255. self.list_first_visible = 1
  256. self.populate_list(self)
  257. self.submenu_none(self)
  258. self.populate_commands(self)
  259. return 0;
  260. end,
  261. --- Enters or returns to the main equip menu.
  262. submenu_none = function(self)
  263. UiContainer.current_submenu = ""
  264. self.trash_mode = false
  265. ui_manager:get_widget("MateriaMenu.Container.TrashWindow"):set_visible(false)
  266. UiContainer.populate_character_data("MateriaMenu.Container.Character", Characters[self.char_id])
  267. ui_manager:get_widget("MateriaMenu.Container.Character.Portrait"):set_image("characters/" .. tostring(self.char_id) .. ".png")
  268. self.populate_equip(self)
  269. -- TODO: Do something for chars 9 and 10
  270. ui_manager:get_widget("MateriaMenu.Container.Character.WpnIcon"):set_image("icons/item_weapon_" .. tostring(self.char_id) .. ".png")
  271. ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_visible(false)
  272. ui_manager:get_widget("MateriaMenu.Container.Abilities"):set_visible(false)
  273. ui_manager:get_widget("MateriaMenu.Container.OrderMenu"):set_visible(false)
  274. ui_manager:get_widget("MateriaMenu.Container.Details.Commands.Box.Cursor"):set_visible(false)
  275. --TODO: Uncomment when implemented: ui_manager:get_widget("MateriaMenu.Container.Order.Cursor"):set_visible(false)
  276. ui_manager:get_widget("MateriaMenu.Container.Character.Cursor"):set_visible(true)
  277. self.populate_details(self, false)
  278. end,
  279. --- Refreshes the menu after the exchange menu closed (the equipment and stats may have changed).
  280. on_exchange_closed = function(self)
  281. UiContainer.populate_character_data("MateriaMenu.Container.Character", Characters[self.char_id])
  282. self.populate_equip(self)
  283. self.populate_list(self)
  284. self.populate_order_help(self)
  285. end,
  286. --- Opens the Order menu (Arrange, Exchange, Remove all, Trash).
  287. submenu_order = function(self)
  288. UiContainer.current_submenu = "order"
  289. self.trash_mode = false
  290. ui_manager:get_widget("MateriaMenu.Container.TrashWindow"):set_visible(false)
  291. ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_visible(false)
  292. ui_manager:get_widget("MateriaMenu.Container.OrderMenu"):set_visible(true)
  293. for option = 1, 4 do
  294. local item = ui_manager:get_widget("MateriaMenu.Container.OrderMenu.Item" .. option)
  295. if self.ORDER_DISABLED[option] then item:set_colour(0.6, 0.6, 0.6) else item:set_colour(1, 1, 1) end
  296. end
  297. self.order_cursor:set(1)
  298. end,
  299. --- Enters (or returns to) the materia list to choose the materia to trash.
  300. submenu_trash = function(self)
  301. self.trash_mode = true
  302. -- The Order popup stays open (with Trash highlighted) while choosing.
  303. ui_manager:get_widget("MateriaMenu.Container.TrashWindow"):set_visible(false)
  304. self.submenu_list(self)
  305. end,
  306. --- Opens the window that shows the highlighted materia before it is trashed.
  307. submenu_trash_confirm = function(self)
  308. local materia = Materia[self.list_first_visible + self.list_position - 1]
  309. if materia == nil then
  310. audio_manager:play_sound("Error")
  311. return
  312. end
  313. audio_manager:play_sound("Cursor")
  314. UiContainer.current_submenu = "trash_confirm"
  315. local data = Game.Materia[materia.id]
  316. local window = "MateriaMenu.Container.TrashWindow."
  317. local levels = data.levels_ap or {}
  318. local level = 0
  319. for _, level_ap in ipairs(levels) do
  320. if level_ap > (materia.ap or 0) then break end
  321. level = level + 1
  322. end
  323. ui_manager:get_widget(window .. "Icon"):set_image("icons/materia_" .. tostring(data.type) .. ".png")
  324. ui_manager:get_widget(window .. "Name"):set_text(data.name)
  325. for star = 1, 5 do
  326. local widget = ui_manager:get_widget(window .. "Star" .. star)
  327. if star > #levels then
  328. widget:set_visible(false)
  329. else
  330. widget:set_image((star <= level and "icons/materia_star_" or "icons/materia_star_empty_")
  331. .. tostring(data.type) .. ".png")
  332. widget:set_visible(true)
  333. end
  334. end
  335. ui_manager:get_widget(window .. "AP"):set_text(UiContainer.pad_value(materia.ap or 0, 8))
  336. ui_manager:get_widget("MateriaMenu.Container.TrashWindow"):set_visible(true)
  337. end,
  338. --- Discards the materia shown in the trash window and goes back to the list.
  339. trash_materia = function(self)
  340. Materia[self.list_first_visible + self.list_position - 1] = nil
  341. audio_manager:play_sound("Cursor")
  342. self.populate_list(self)
  343. self.submenu_trash(self)
  344. end,
  345. --- Shows the description of the highlighted option of the Order menu.
  346. populate_order_help = function(self)
  347. ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text(self.ORDER_HELP[self.order_cursor:index()] or "")
  348. end,
  349. --- Runs the highlighted option of the Order menu.
  350. run_order = function(self)
  351. local option = self.order_cursor:index()
  352. if self.ORDER_DISABLED[option] then
  353. audio_manager:play_sound("Error")
  354. elseif option == 1 then
  355. audio_manager:play_sound("Cursor")
  356. self.arrange_materia(self)
  357. elseif option == 2 then
  358. audio_manager:play_sound("Cursor")
  359. script:request_end_sync(Script.UI, "ExchangeMenu", "show", 0)
  360. elseif option == 4 then
  361. audio_manager:play_sound("Cursor")
  362. self.submenu_trash(self)
  363. elseif option == 3 then
  364. if self.remove_all_materia(self) then
  365. audio_manager:play_sound("Cursor")
  366. else
  367. audio_manager:play_sound("Error")
  368. end
  369. end
  370. end,
  371. --- IDs of the master materia (Master Command, Master Magic and Master Summon).
  372. MASTER_MATERIA = {[48] = true, [73] = true, [90] = true},
  373. --- Sorts the materia of the inventory, as the original game does.
  374. --
  375. -- By type (the order of Materia.TYPE: magic, support, command, independent, summon); in each
  376. -- type the master materia go first, then by ID ascending and by AP descending. The empty slots
  377. -- go at the end.
  378. arrange_materia = function(self)
  379. local sorted = {}
  380. for slot = 1, MAX_MATERIA_SLOTS do
  381. if Materia[slot] ~= nil then sorted[#sorted + 1] = {materia = Materia[slot], order = #sorted} end
  382. end
  383. local master = self.MASTER_MATERIA
  384. table.sort(sorted, function(l, r)
  385. local l_id, r_id = l.materia.id, r.materia.id
  386. local l_type, r_type = Game.Materia[l_id].type, Game.Materia[r_id].type
  387. if l_type ~= r_type then return l_type < r_type end
  388. if (master[l_id] == true) ~= (master[r_id] == true) then return master[l_id] == true end
  389. if l_id ~= r_id then return l_id < r_id end
  390. if (l.materia.ap or 0) ~= (r.materia.ap or 0) then return (l.materia.ap or 0) > (r.materia.ap or 0) end
  391. return l.order < r.order
  392. end)
  393. for slot = 1, MAX_MATERIA_SLOTS do
  394. Materia[slot] = sorted[slot] ~= nil and sorted[slot].materia or nil
  395. end
  396. self.list_first_visible = 1
  397. self.list_position = 1
  398. ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_default_animation("Position1")
  399. self.populate_list(self)
  400. end,
  401. --- Moves all the materia equipped on the character to the inventory.
  402. --
  403. -- If the inventory has not room for all of it, as many as fit are removed (the ones of the weapon
  404. -- first, in slot order).
  405. -- @return False if nothing was removed.
  406. remove_all_materia = function(self)
  407. local equipped = {}
  408. local character = Characters[self.char_id]
  409. for _, equipment in ipairs({character.weapon, character.armor}) do
  410. for slot = 0, 7 do
  411. if equipment.materia[slot] ~= nil then equipped[#equipped + 1] = {equipment, slot} end
  412. end
  413. end
  414. local free = {}
  415. for slot = 1, MAX_MATERIA_SLOTS do
  416. if Materia[slot] == nil then free[#free + 1] = slot end
  417. end
  418. local count = math.min(#equipped, #free)
  419. if count == 0 then return false end
  420. for i = 1, count do
  421. local place = equipped[i]
  422. Materia[free[i]] = place[1].materia[place[2]]
  423. place[1].materia[place[2]] = nil
  424. end
  425. -- The materia change the stats, and the current HP and MP can't be over the new maximums.
  426. Characters.recalculate_stats(self.char_id)
  427. self.submenu_none(self)
  428. self.populate_list(self)
  429. return true
  430. end,
  431. --- Enters (or returns to) the navigable list of commands of the Check window.
  432. submenu_commands = function(self)
  433. UiContainer.current_submenu = "commands"
  434. ui_manager:get_widget("MateriaMenu.Container.Abilities"):set_visible(false)
  435. ui_manager:get_widget("MateriaMenu.Container.Details.Commands.Box.Cursor"):set_visible(true)
  436. self.command_cursor.total = #self.commands
  437. if self.command_cursor.position > #self.commands then self.command_cursor.position = 1 end
  438. self.command_cursor:refresh()
  439. end,
  440. --- Number of commands in each column of the Check window.
  441. COMMAND_ROWS = 4,
  442. --- Moves the command cursor. The commands fill the columns from top to bottom.
  443. --
  444. -- Up and down wrap inside the column, left and right wrap around the columns keeping the row
  445. -- (or going to the last command of a shorter column).
  446. move_command_cursor = function(self, button)
  447. local rows = self.COMMAND_ROWS
  448. local index = self.command_cursor:index() - 1
  449. local column, row = math.floor(index / rows), index % rows
  450. local columns = math.ceil(#self.commands / rows)
  451. local function length(c) return math.min(rows, #self.commands - c * rows) end
  452. if button == Config.CONTROLS.UP then
  453. row = (row - 1) % length(column)
  454. elseif button == Config.CONTROLS.DOWN then
  455. row = (row + 1) % length(column)
  456. elseif button == Config.CONTROLS.LEFT then
  457. column = (column - 1) % columns
  458. row = math.min(row, length(column) - 1)
  459. elseif button == Config.CONTROLS.RIGHT then
  460. column = (column + 1) % columns
  461. row = math.min(row, length(column) - 1)
  462. else
  463. return
  464. end
  465. audio_manager:play_sound("Cursor")
  466. self.command_cursor:set(column * rows + row + 1)
  467. end,
  468. --- Shows the description of the highlighted command.
  469. populate_command_help = function(self)
  470. local command = Game.Commands[self.commands[self.command_cursor:index()]]
  471. ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text(command ~= nil and command.description or "")
  472. end,
  473. --- Gets the kind of list a command opens.
  474. --
  475. -- @param command Definition of the command (an entry of Game.Commands).
  476. -- @return "magic", "summon" or "eskill", or nil if the command has no list.
  477. command_list_kind = function(self, command)
  478. local menu = command ~= nil and command.menu or nil
  479. if menu == Battle.Menu.MAGIC or menu == Battle.Menu.W_MAGIC then return "magic"
  480. elseif menu == Battle.Menu.SUMMON or menu == Battle.Menu.W_SUMMON then return "summon"
  481. elseif menu == Battle.Menu.E_SKILL then return "eskill"
  482. end
  483. return nil
  484. end,
  485. --- Opens the list of a command (spells, summons or enemy skills), if it has one.
  486. open_command = function(self)
  487. local command = Game.Commands[self.commands[self.command_cursor:index()]]
  488. local kind = self:command_list_kind(command)
  489. local entries = kind ~= nil and self:build_abilities(kind) or {}
  490. if #entries == 0 then
  491. audio_manager:play_sound("Error")
  492. return
  493. end
  494. audio_manager:play_sound("Cursor")
  495. self.abilities = entries
  496. UiContainer.current_submenu = "abilities"
  497. ui_manager:get_widget("MateriaMenu.Container.Abilities"):set_visible(true)
  498. self.ability_cursor.count = #entries
  499. self.ability_cursor:reset()
  500. self:populate_abilities()
  501. end,
  502. --- Gets the spells, summons or enemy skills the character can use in battle.
  503. --
  504. -- @param kind "magic", "summon" or "eskill".
  505. -- @return List of {attack = ID of the attack, times = uses per battle of a summon}.
  506. build_abilities = function(self, kind)
  507. local abilities = Characters.get_abilities(self.char_id)
  508. local entries = {}
  509. if kind == "magic" then
  510. -- The spells of a materia go together.
  511. for _, group in ipairs(abilities.magic_groups) do
  512. for _, spell in ipairs(group) do entries[#entries + 1] = {attack = spell} end
  513. end
  514. elseif kind == "summon" then
  515. for _, summon in ipairs(abilities.summon) do
  516. entries[#entries + 1] = {attack = summon.id, times = summon.times}
  517. end
  518. else
  519. for _, skill in ipairs(abilities.e_skill) do entries[#entries + 1] = {attack = skill} end
  520. end
  521. return entries
  522. end,
  523. --- Fills the rows of the abilities list.
  524. populate_abilities = function(self)
  525. local window = "MateriaMenu.Container.Abilities."
  526. for row = 1, 10 do
  527. local entry = self.abilities[self.ability_cursor.first + row - 1]
  528. local data = entry ~= nil and Game.Attacks[entry.attack] or nil
  529. if data == nil then
  530. ui_manager:get_widget(window .. "Name" .. row):set_text("")
  531. ui_manager:get_widget(window .. "Mp" .. row):set_text("")
  532. else
  533. local name = data.name
  534. if entry.times ~= nil and entry.times < 255 then name = name .. " x" .. tostring(entry.times) end
  535. ui_manager:get_widget(window .. "Name" .. row):set_text(name)
  536. ui_manager:get_widget(window .. "Mp" .. row):set_text(UiContainer.pad_value(data.mp or 0, 3))
  537. end
  538. end
  539. self:populate_ability_help()
  540. end,
  541. --- Shows the description of the highlighted ability.
  542. populate_ability_help = function(self)
  543. local entry = self.abilities[self.ability_cursor:index()]
  544. local data = entry ~= nil and Game.Attacks[entry.attack] or nil
  545. ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text(data ~= nil and data.description or "")
  546. end,
  547. submenu_list = function(self)
  548. UiContainer.current_submenu = "list"
  549. ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_visible(true)
  550. self.populate_details(self, true)
  551. end,
  552. --- Populate the equipment section.
  553. --
  554. -- It fills the header with weapn and armor names, slots, and current materia.
  555. populate_equip = function(self)
  556. local weapon = Game.Items[Characters[self.char_id].weapon.id]
  557. local armor = Game.Items[Characters[self.char_id].armor.id]
  558. ui_manager:get_widget("MateriaMenu.Container.Character.WpnLbl"):set_text(weapon.name)
  559. ui_manager:get_widget("MateriaMenu.Container.Character.ArmLbl"):set_text(armor.name)
  560. for i = 1, 8 do
  561. -- Show/hide slots
  562. -- TODO: Change image for no-growth slots?
  563. if #(weapon.slots) >= i then
  564. ui_manager:get_widget("MateriaMenu.Container.Character.WeaponSlots.Slot" .. tostring(i)):set_visible(true)
  565. else
  566. ui_manager:get_widget("MateriaMenu.Container.Character.WeaponSlots.Slot" .. tostring(i)):set_visible(false)
  567. end
  568. if #(armor.slots) >= i then
  569. ui_manager:get_widget("MateriaMenu.Container.Character.ArmorSlots.Slot" .. tostring(i)):set_visible(true)
  570. else
  571. ui_manager:get_widget("MateriaMenu.Container.Character.ArmorSlots.Slot" .. tostring(i)):set_visible(false)
  572. end
  573. -- Show/hide linkers
  574. if i % 2 ~= 0 then
  575. if #(weapon.slots) > i and weapon.slots[i] == 1 then
  576. ui_manager:get_widget("MateriaMenu.Container.Character.WeaponSlots.Link" .. tostring(math.floor(i / 2) + 1)):set_visible(true)
  577. else
  578. ui_manager:get_widget("MateriaMenu.Container.Character.WeaponSlots.Link" .. tostring(math.floor(i / 2) + 1)):set_visible(false)
  579. end
  580. if #(armor.slots) > i and armor.slots[i] == 1 then
  581. ui_manager:get_widget("MateriaMenu.Container.Character.ArmorSlots.Link" .. tostring(math.floor(i / 2) + 1)):set_visible(true)
  582. else
  583. ui_manager:get_widget("MateriaMenu.Container.Character.ArmorSlots.Link" .. tostring(math.floor(i / 2) + 1)):set_visible(false)
  584. end
  585. end
  586. -- Show/hide/set materia
  587. if Characters[self.char_id].weapon.materia[i - 1] ~= nil then
  588. local mat_type = Game.Materia[Characters[self.char_id].weapon.materia[i - 1].id].type
  589. ui_manager:get_widget("MateriaMenu.Container.Character.WeaponSlots.Mat" .. tostring(i)):set_image("icons/materia_" .. tostring(mat_type) .. ".png")
  590. ui_manager:get_widget("MateriaMenu.Container.Character.WeaponSlots.Mat" .. tostring(i)):set_visible(true)
  591. else
  592. ui_manager:get_widget("MateriaMenu.Container.Character.WeaponSlots.Mat" .. tostring(i)):set_visible(false)
  593. end
  594. if Characters[self.char_id].armor.materia[i - 1] ~= nil then
  595. local mat_type = Game.Materia[Characters[self.char_id].armor.materia[i - 1].id].type
  596. ui_manager:get_widget("MateriaMenu.Container.Character.ArmorSlots.Mat" .. tostring(i)):set_image("icons/materia_" .. tostring(mat_type) .. ".png")
  597. ui_manager:get_widget("MateriaMenu.Container.Character.ArmorSlots.Mat" .. tostring(i)):set_visible(true)
  598. else
  599. ui_manager:get_widget("MateriaMenu.Container.Character.ArmorSlots.Mat" .. tostring(i)):set_visible(false)
  600. end
  601. end
  602. end,
  603. --- Hiddes the materia details.
  604. --
  605. -- Must be called when the cursor goes into an empty slot
  606. hide_details = function(self)
  607. -- TODO
  608. ui_manager:get_widget("MateriaMenu.Container.Details.Empty"):set_visible(true)
  609. ui_manager:get_widget("MateriaMenu.Container.Details.Commands"):set_visible(false)
  610. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill"):set_visible(false)
  611. ui_manager:get_widget("MateriaMenu.Container.Details.Materia"):set_visible(false)
  612. ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text("")
  613. end,
  614. --- Show details of an enemy skill materia.
  615. --
  616. -- Must be called when the cursor goes over an enemy skill materia
  617. --
  618. -- @param materia Selected Enemy Skill materia.
  619. populate_e_skill_details = function(self, materia)
  620. if materia.id ~= Materia.ENEMY_SKILL_ID then
  621. print("ERROR: Tried to get enemy skill info form non-enemy skill materia with ID " .. tostring(materia.id))
  622. return 0
  623. elseif materia.skills == nil then
  624. print("ERROR: Enemy skill materia has no skill information")
  625. --return 0
  626. end
  627. ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text(Game.Materia[materia.id].description)
  628. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill.Name"):set_text(Game.Materia[materia.id].name)
  629. ui_manager:get_widget("MateriaMenu.Container.Details.Empty"):set_visible(false)
  630. ui_manager:get_widget("MateriaMenu.Container.Details.Commands"):set_visible(false)
  631. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill"):set_visible(true)
  632. ui_manager:get_widget("MateriaMenu.Container.Details.Materia"):set_visible(false)
  633. for s = 1, 24 do
  634. if materia.skills[s] ~= nil and materia.skills[s] == true then
  635. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill.Stars.Star" .. tostring(s)):set_image("icons/materia_star_3.png")
  636. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill.Skills.Skill" .. tostring(s)):set_text(Game.Attacks[71 + s].name)
  637. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill.Skills.Skill" .. tostring(s)):set_visible(true)
  638. else
  639. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill.Stars.Star" .. tostring(s)):set_image("icons/materia_star_empty_3.png")
  640. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill.Skills.Skill" .. tostring(s)):set_visible(false)
  641. end
  642. end
  643. end,
  644. --- Displays the command list in the details window
  645. populate_commands = function(self)
  646. ui_manager:get_widget("MateriaMenu.Container.Details.Empty"):set_visible(false)
  647. ui_manager:get_widget("MateriaMenu.Container.Details.Commands"):set_visible(true)
  648. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill"):set_visible(false)
  649. ui_manager:get_widget("MateriaMenu.Container.Details.Materia"):set_visible(false)
  650. ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text("")
  651. -- The commands the character has in battle.
  652. local commands = Characters.get_commands(self.char_id)
  653. self.commands = commands
  654. for i = 1, 12 do
  655. local widget = ui_manager:get_widget("MateriaMenu.Container.Details.Commands.Box.Command" .. i)
  656. local command = Game.Commands[commands[i]]
  657. if command ~= nil then
  658. widget:set_text(command.name)
  659. widget:set_visible(true)
  660. -- Commands with a list but nothing in it (no enemy skill learned...) are dimmed.
  661. local kind = self:command_list_kind(command)
  662. if kind ~= nil and #self:build_abilities(kind) == 0 then
  663. widget:set_colour(0.6, 0.6, 0.6)
  664. else
  665. widget:set_colour(1, 1, 1)
  666. end
  667. else
  668. widget:set_visible(false)
  669. end
  670. end
  671. end,
  672. --- Populated the details of the selected header item (check, materia, or none).
  673. --
  674. -- @param from_list If true, data will be read from the item higlighted in the list on the
  675. -- right. If false, data wil be red from the currently equipped items.
  676. populate_details = function(self, from_list)
  677. local id
  678. local ap
  679. local ap_next
  680. local level
  681. local max_level
  682. local master = false
  683. local materia -- Materia static information, as in Game.Materia[]
  684. local materia_selected -- Materia instance information, as in Materia[]
  685. if from_list == false then
  686. -- Get data from equiped materia
  687. if self.header_position_row == 1 then
  688. -- "Check" highlighted
  689. if self.header_position == 1 then
  690. self.populate_commands(self)
  691. return 0
  692. end
  693. -- Weapon materia
  694. local weapon_id = Characters[self.char_id].weapon.id
  695. local weapon = Game.Items[weapon_id]
  696. if #(weapon.slots) < self.header_position -1 or Characters[self.char_id].weapon.materia[self.header_position - 2] == nil then
  697. -- No slot or no materia in slot. Clear and exit
  698. self.hide_details(self)
  699. return 0
  700. end
  701. materia_selected = Characters[self.char_id].weapon.materia[self.header_position - 2]
  702. id = Characters[self.char_id].weapon.materia[self.header_position - 2].id
  703. ap = Characters[self.char_id].weapon.materia[self.header_position - 2].ap
  704. materia = Game.Materia[id]
  705. elseif self.header_position_row == 2 then
  706. -- "Order" highlighted
  707. if self.header_position == 1 then
  708. self.hide_details(self)
  709. return 0
  710. end
  711. -- Armor materia
  712. local armor_id = Characters[self.char_id].armor.id
  713. local armor = Game.Items[armor_id]
  714. if #(armor.slots) < self.header_position - 1 or Characters[self.char_id].armor.materia[self.header_position - 2] == nil then
  715. -- No slot or no materia in slot. Clear and exit
  716. self.hide_details(self)
  717. return 0
  718. end
  719. materia_selected = Characters[self.char_id].armor.materia[self.header_position - 2]
  720. id = Characters[self.char_id].armor.materia[self.header_position - 2].id
  721. ap = Characters[self.char_id].armor.materia[self.header_position - 2].ap
  722. materia = Game.Materia[id]
  723. else
  724. -- This should not happen, but just in case.
  725. self.hide_details(self)
  726. return 0
  727. end
  728. else
  729. --Get materia data from list.
  730. local mat_position = self.list_first_visible + self.list_position - 1
  731. if Materia[mat_position] == nil then
  732. self.hide_details(self)
  733. return 0
  734. end
  735. materia_selected = Materia[mat_position]
  736. id = Materia[mat_position].id
  737. ap = Materia[mat_position].ap
  738. materia = Game.Materia[Materia[mat_position].id]
  739. end
  740. -- Materia should be set, but just in case, one last check.
  741. if materia == nil then
  742. self.hide_details(self)
  743. return
  744. end
  745. -- Check for enemy skill. It is handled by another function.
  746. if id == Materia.ENEMY_SKILL_ID then
  747. self.populate_e_skill_details(self, materia_selected)
  748. return 0
  749. end
  750. ui_manager:get_widget("MateriaMenu.Container.Details.Empty"):set_visible(false)
  751. ui_manager:get_widget("MateriaMenu.Container.Details.Commands"):set_visible(false)
  752. ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill"):set_visible(false)
  753. ui_manager:get_widget("MateriaMenu.Container.Details.Materia"):set_visible(true)
  754. ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text(Game.Materia[id].description)
  755. -- Get level and max level, and check for master.
  756. level = 0
  757. for lv, lv_ap in ipairs(materia.levels_ap) do
  758. if lv_ap > ap then
  759. break
  760. end
  761. level = level + 1
  762. end
  763. max_level = #(materia.levels_ap)
  764. if level == max_level then
  765. master = true
  766. ap_next = 0
  767. else
  768. ap_next = materia.levels_ap[level + 1] - ap
  769. end
  770. -- Start displaying data
  771. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Icon"):set_image("icons/materia_" .. tostring(materia.type) .. ".png")
  772. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Name"):set_text(materia.name)
  773. for l = 1, 5 do
  774. if l > max_level then
  775. -- No such level, hide star.
  776. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Stars.Star" .. tostring(l)):set_visible(false)
  777. else
  778. if l <= level then
  779. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Stars.Star" .. tostring(l)):set_image("icons/materia_star_" .. tostring(materia.type) .. ".png")
  780. else
  781. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Stars.Star" .. tostring(l)):set_image("icons/materia_star_empty_" .. tostring(materia.type) .. ".png")
  782. end
  783. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Stars.Star" .. tostring(l)):set_visible(true)
  784. end
  785. end
  786. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.AP"):set_text(UiContainer.pad_value(ap, 6))
  787. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.APNext"):set_text(UiContainer.pad_value(ap_next, 6))
  788. --ui_manager:get_widget("MateriaMenu.Container.Details.Materia.AP"):set_text(UiContainer.pad_value(200000, 6))
  789. --ui_manager:get_widget("MateriaMenu.Container.Details.Materia.APNext"):set_text(UiContainer.pad_value(30000, 6))
  790. -- Hide all attacks
  791. for a = 1, 5 do
  792. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability" .. tostring(a)):set_visible(false)
  793. end
  794. -- Show attacks, vary by type
  795. if materia.type == Materia.TYPE.MAGIC then
  796. local attack_index = 1
  797. for attack_level, attack_id in pairs(materia.magic) do
  798. if level >= attack_level then
  799. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability" .. tostring(attack_index)):set_colour(1, 1, 1)
  800. else
  801. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability" .. tostring(attack_index)):set_colour(0.6, 0.6, 0.6)
  802. end
  803. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability" .. tostring(attack_index)):set_text(Game.Attacks[attack_id].name)
  804. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability" .. tostring(attack_index)):set_visible(true)
  805. attack_index = attack_index + 1
  806. end
  807. elseif materia.type == Materia.TYPE.SUMMON then
  808. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability1"):set_colour(1, 1, 1)
  809. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability1"):set_text(Game.Attacks[materia.summon].name)
  810. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability1"):set_visible(true)
  811. elseif materia.type == Materia.TYPE.COMMAND then
  812. local attack_index = 1
  813. for attack_level, attack_id in ipairs(materia.command) do
  814. if level >= attack_level then
  815. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability" .. tostring(attack_index)):set_colour(1, 1, 1)
  816. else
  817. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability" .. tostring(attack_index)):set_colour(0.6, 0.6, 0.6)
  818. end
  819. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability" .. tostring(attack_index)):set_text(Game.Commands[attack_id].name)
  820. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability" .. tostring(attack_index)):set_visible(true)
  821. attack_index = attack_index + 1
  822. end
  823. elseif materia.type == Materia.TYPE.INDEPENDENT then
  824. local bonus = 0
  825. for l = 1, max_level do
  826. if l <= level and materia.ability.param[l] ~= nil then
  827. bonus = materia.ability.param[l]
  828. end
  829. end
  830. if materia.ability ~= nil then
  831. local text = "TEXT"
  832. if materia.ability.id == Materia.ABILITY.MP_PLUS then
  833. text = "<include name=\"StatMP\"/> +<colour value=\"1 1 0\">" .. tostring(bonus) .. "</colour>%"
  834. elseif materia.ability.id == Materia.ABILITY.HP_PLUS then
  835. text = "<include name=\"StatHP\"/> +<colour value=\"1 1 0\">" .. tostring(bonus) .. "</colour>%"
  836. elseif materia.ability.id == Materia.ABILITY.STR_PLUS then
  837. text = "<include name=\"StatStr\"/> +<colour value=\"1 1 0\">" .. tostring(bonus) .. "</colour>%"
  838. elseif materia.ability.id == Materia.ABILITY.VIT_PLUS then
  839. text = "<include name=\"StatVit\"/> +<colour value=\"1 1 0\">" .. tostring(bonus) .. "</colour>%"
  840. elseif materia.ability.id == Materia.ABILITY.MAG_PLUS then
  841. text = "<include name=\"StatMag\"/> +<colour value=\"1 1 0\">" .. tostring(bonus) .. "</colour>%"
  842. elseif materia.ability.id == Materia.ABILITY.DEX_PLUS then
  843. text = "<include name=\"StatDex\"/> +<colour value=\"1 1 0\">" .. tostring(bonus) .. "</colour>%"
  844. elseif materia.ability.id == Materia.ABILITY.LCK_PLUS then
  845. text = "<include name=\"StatLck\"/> +<colour value=\"1 1 0\">" .. tostring(bonus) .. "</colour>%"
  846. elseif materia.ability.id == Materia.ABILITY.EXP_PLUS then
  847. text = "<include name=\"StatEXP\"/> +"
  848. elseif materia.ability.id == Materia.ABILITY.MONEY_PLUS then
  849. text = "<include name=\"Currency\"/> +"
  850. elseif materia.ability.id == Materia.ABILITY.ENCOUNTER_DOWN then
  851. text = "<include name=\"MateriaEncounters\"/> -"
  852. elseif materia.ability.id == Materia.ABILITY.ENCOUNTER_UP then
  853. text = "<include name=\"MateriaEncountersChocobo\"/> -"
  854. elseif materia.ability.id == Materia.ABILITY.PREEMPTIVE then
  855. text = materia.name
  856. elseif materia.ability.id == Materia.ABILITY.LONG_RANGE then
  857. text = materia.name
  858. elseif materia.ability.id == Materia.ABILITY.MEGA_ALL then
  859. text = materia.name
  860. elseif materia.ability.id == Materia.ABILITY.COUNTER_ATTACK then
  861. text = materia.name
  862. elseif materia.ability.id == Materia.ABILITY.COVER then
  863. text = materia.name .. " +<colour value=\"1 1 0\">" .. tostring(bonus) .. "</colour>%"
  864. elseif materia.ability.id == Materia.ABILITY.UNDERWATER then
  865. text = materia.name
  866. elseif materia.ability.id == Materia.ABILITY.HP_MP then
  867. text = materia.name
  868. end
  869. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability1"):set_colour(1, 1, 1)
  870. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability1"):set_text(text)
  871. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability1"):set_visible(true)
  872. end
  873. elseif materia.type == Materia.TYPE.SUPPORT then
  874. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability1"):set_colour(1, 1, 1)
  875. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability1"):set_text(materia.name)
  876. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Abilities.Ability1"):set_visible(true)
  877. end
  878. -- Hide all effects
  879. for a = 1, 7 do
  880. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(a)):set_visible(false)
  881. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(a)):set_visible(false)
  882. end
  883. -- Show effects, if any.
  884. local effect_index = 1
  885. if materia.stats.str ~= nil and materia.stats.str ~= 0 then
  886. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_text("<include name=\"StatStr\" />")
  887. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_visible(true)
  888. if materia.stats.str < 0 then
  889. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 0 0\">-" .. UiContainer.pad_value(-1 * materia.stats.str, 2) .. "</colour>")
  890. else
  891. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 1 0\">+" .. UiContainer.pad_value(materia.stats.str, 2) .. "</colour>")
  892. end
  893. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_visible(true)
  894. effect_index = effect_index + 1
  895. end
  896. if materia.stats.vit ~= nil and materia.stats.vit ~= 0 then
  897. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_text("<include name=\"StatVit\" />")
  898. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_visible(true)
  899. if materia.stats.vit < 0 then
  900. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 0 0\">-" .. UiContainer.pad_value(-1 * materia.stats.vit, 2) .. "</colour>")
  901. else
  902. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 1 0\">+" .. UiContainer.pad_value(materia.stats.vit, 2) .. "</colour>")
  903. end
  904. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_visible(true)
  905. effect_index = effect_index + 1
  906. end
  907. if materia.stats.mag ~= nil and materia.stats.mag ~= 0 then
  908. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_text("<include name=\"StatMag\" />")
  909. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_visible(true)
  910. if materia.stats.mag < 0 then
  911. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 0 0\">-" .. UiContainer.pad_value(-1 * materia.stats.mag, 2) .. "</colour>")
  912. else
  913. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 1 0\">+" .. UiContainer.pad_value(materia.stats.mag, 2) .. "</colour>")
  914. end
  915. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_visible(true)
  916. effect_index = effect_index + 1
  917. end
  918. if materia.stats.spr ~= nil and materia.stats.spr ~= 0 then
  919. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_text("<include name=\"StatSpr\" />")
  920. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_visible(true)
  921. if materia.stats.spr < 0 then
  922. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 0 0\">-" .. UiContainer.pad_value(-1 * materia.stats.spr, 2) .. "</colour>")
  923. else
  924. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 1 0\">+" .. UiContainer.pad_value(materia.stats.spr, 2) .. "</colour>")
  925. end
  926. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_visible(true)
  927. effect_index = effect_index + 1
  928. end
  929. if materia.stats.dex ~= nil and materia.stats.dex ~= 0 then
  930. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_text("<include name=\"StatDex\" />")
  931. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_visible(true)
  932. if materia.stats.dex < 0 then
  933. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 0 0\">-" .. UiContainer.pad_value(-1 * materia.stats.dex, 2) .. "</colour>")
  934. else
  935. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 1 0\">+" .. UiContainer.pad_value(materia.stats.dex, 2) .. "</colour>")
  936. end
  937. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_visible(true)
  938. effect_index = effect_index + 1
  939. end
  940. if materia.stats.lck ~= nil and materia.stats.lck ~= 0 then
  941. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_text("<include name=\"StatLck\" />")
  942. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_visible(true)
  943. if materia.stats.lck < 0 then
  944. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 0 0\">-" .. UiContainer.pad_value(-1 * materia.stats.lck, 2) .. "</colour>")
  945. else
  946. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 1 0\">+" .. UiContainer.pad_value(materia.stats.lck, 2) .. "</colour>")
  947. end
  948. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_visible(true)
  949. effect_index = effect_index + 1
  950. end
  951. if materia.stats.hp ~= nil and materia.stats.hp ~= 0 then
  952. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_text("<include name=\"StatHP\" />")
  953. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_visible(true)
  954. if materia.stats.hp < 0 then
  955. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 0 0\">-" .. UiContainer.pad_value(-1 * materia.stats.hp, 2) .. "%</colour>")
  956. else
  957. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 1 0\">+" .. UiContainer.pad_value(materia.stats.hp, 2) .. "%</colour>")
  958. end
  959. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_visible(true)
  960. effect_index = effect_index + 1
  961. end
  962. if materia.stats.mp ~= nil and materia.stats.mp ~= 0 then
  963. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_text("<include name=\"StatMP\" />")
  964. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Stat" .. tostring(effect_index)):set_visible(true)
  965. if materia.stats.mp < 0 then
  966. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 0 0\">-" .. UiContainer.pad_value(-1 * materia.stats.mp, 2) .. "%</colour>")
  967. else
  968. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_text("<colour value=\"1 1 0\">+" .. UiContainer.pad_value(materia.stats.mp, 2) .. "%</colour>")
  969. end
  970. ui_manager:get_widget("MateriaMenu.Container.Details.Materia.Effects.Val" .. tostring(effect_index)):set_visible(true)
  971. effect_index = effect_index + 1
  972. end
  973. end,
  974. --- Populates the materia list.
  975. populate_list = function(self)
  976. local pos = 1
  977. for i = self.list_first_visible, self.list_first_visible + self.list_position_total - 1 do
  978. if Materia[i] ~= nil then
  979. local type = Game.Materia[Materia[i].id].type
  980. local name = Game.Materia[Materia[i].id].name
  981. ui_manager:get_widget("MateriaMenu.Container.List.Icon" .. tostring(pos)):set_image("icons/materia_" .. tostring(type) .. ".png")
  982. ui_manager:get_widget("MateriaMenu.Container.List.Icon" .. tostring(pos)):set_visible(true)
  983. ui_manager:get_widget("MateriaMenu.Container.List.Name" .. tostring(pos)):set_text(name)
  984. ui_manager:get_widget("MateriaMenu.Container.List.Name" .. tostring(pos)):set_visible(true)
  985. else
  986. ui_manager:get_widget("MateriaMenu.Container.List.Icon" .. tostring(pos)):set_visible(false)
  987. ui_manager:get_widget("MateriaMenu.Container.List.Name" .. tostring(pos)):set_visible(false)
  988. end
  989. pos = pos + 1
  990. end
  991. -- Set scroll bar position
  992. local scroll = ui_manager:get_widget("MateriaMenu.Container.List.Scroll")
  993. -- first_item_in_window == 1 then Top -> 0%3
  994. -- first_item_in_window == inventory_size - first_item_in_window then top = 100%-21
  995. local percent = math.floor((self.list_first_visible - 1) * 100 / (self.list_size - self.list_position_total))
  996. local fixed = 3 + (-1 * math.floor(self.list_first_visible - 1) * 25 / (self.list_size - self.list_position_total))
  997. scroll:set_y(percent, fixed)
  998. end,
  999. --- Equips the materia selected in the list at the currently selected slot.
  1000. --
  1001. -- It can also be used to swap or remove materia.
  1002. equip_materia = function(self)
  1003. local equip
  1004. local equip_slot = self.header_position - 2 -- 0-8
  1005. local list_pos = self.list_first_visible + self.list_position - 1
  1006. local materia_equip = {id = nil, ap = 0}
  1007. local materia_list = {id = nil, ap = 0}
  1008. --- Get info about the seleceted materias (equipped and in the list)
  1009. if self.header_position_row == 1 then
  1010. equip = Inventory.ITEM_TYPE.WEAPON
  1011. if equip_slot + 1 > #(Game.Items[Characters[self.char_id].weapon.id].slots) then
  1012. print("BEEP No slot " .. tostring(equip_slot) .. " in current weapon.")
  1013. return 0
  1014. end
  1015. if Characters[self.char_id].weapon.materia[equip_slot] ~= nil then
  1016. materia_equip = Characters[self.char_id].weapon.materia[equip_slot]
  1017. end
  1018. elseif self.header_position_row == 2 then
  1019. equip = Inventory.ITEM_TYPE.ARMOR
  1020. if equip_slot + 1 > #(Game.Items[Characters[self.char_id].armor.id].slots) then
  1021. print("BEEP No slot " .. tostring(equip_slot) .. " in current armor.")
  1022. return 0
  1023. end
  1024. if Characters[self.char_id].armor.materia[equip_slot] ~= nil then
  1025. materia_equip = Characters[self.char_id].armor.materia[equip_slot]
  1026. end
  1027. end
  1028. if Materia[list_pos] ~= nil then
  1029. materia_list = Materia[list_pos]
  1030. end
  1031. --- Equipped materia to the list (can be null)
  1032. Materia[list_pos] = materia_equip
  1033. if Materia[list_pos].id == nil then
  1034. Materia[list_pos] = nil
  1035. end
  1036. --- Materia from list to weapon or armor
  1037. if equip == Inventory.ITEM_TYPE.WEAPON then
  1038. Characters[self.char_id].weapon.materia[equip_slot] = materia_list
  1039. if Characters[self.char_id].weapon.materia[equip_slot].id == nil then
  1040. Characters[self.char_id].weapon.materia[equip_slot] = nil
  1041. end
  1042. elseif equip == Inventory.ITEM_TYPE.ARMOR then
  1043. Characters[self.char_id].armor.materia[equip_slot] = materia_list
  1044. if Characters[self.char_id].armor.materia[equip_slot].id == nil then
  1045. Characters[self.char_id].armor.materia[equip_slot] = nil
  1046. end
  1047. end
  1048. -- The materia change the stats, and the current HP and MP can't be over the new maximums.
  1049. Characters.recalculate_stats(self.char_id)
  1050. -- Reload character data and list.
  1051. self.populate_equip(self)
  1052. self.populate_list(self)
  1053. self.submenu_none(self)
  1054. end,
  1055. --- Hides the item menu and goes back to the main menu.
  1056. hide = function(self)
  1057. ui_manager:get_widget("MateriaMenu"):set_visible(false)
  1058. UiContainer.current_menu = "main"
  1059. UiContainer.current_submenu = ""
  1060. UiContainer.resume_menu("main")
  1061. return 0;
  1062. end,
  1063. }