item_menu.lua 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. if UiContainer == nil then UiContainer = {} end
  2. --- The item menu.
  3. UiContainer.ItemMenu = {
  4. --- Current cursor position in the top bar (1-3).
  5. bar_position = 1,
  6. --- Max cursor position in the top bar.
  7. bar_position_total = 3,
  8. --- Current cursor position in the item list (1-13).
  9. --
  10. -- It doesn't represent the item id or the inventory position, but the highlighted row among
  11. -- the ten displayed.
  12. item_cursor_position = 1,
  13. --- Max cursor position in the item list.
  14. item_cursor_position_total = 13,
  15. --- Highlighted inventory position (1-320).
  16. item_cursor_item_selected = 1,
  17. --- Current cursor position in the character section (1-3).
  18. char_position = 1,
  19. --- Max cursor position in the character section.
  20. char_position_total = 3,
  21. --- First inventory entry currently visible in the item list.
  22. first_item_in_window = 0,
  23. --- Key items cursor vertical position (0-9)
  24. --
  25. -- It doesn't represent the item id or the inventory position, but the highlighted row among
  26. -- the ten displayed.
  27. key_items_cursor_y_position = 1,
  28. --- Key items cursor horizontal position (0-1)
  29. --
  30. -- It doesn't represent the item id or the inventory position, but the highlighted row among
  31. -- the ten displayed. 1 if the cursor is in the left column, 2 if it's on the right.
  32. key_items_cursor_x_position = 1,
  33. --- Max vertical cursor position in the key item list.
  34. key_items_cursor_y_position_total = 13,
  35. --- Total number of rows in the key items window.
  36. --
  37. -- Includes visible and scrolled/scrollable rows. Since there are 51 key items, there are
  38. -- 26 rows.
  39. key_items_total_rows = 26,
  40. --- First visible row of key items (0-16)
  41. --
  42. -- Changes as the key item list scrols vertically.
  43. first_key_item_row_in_window = 1,
  44. --- Current cursor position in the order section (1-8).
  45. order_cursor_position = 1,
  46. --- Max cursor position in the order section.
  47. order_cursor_position_total = 8,
  48. --- Item ID of the currently selected (not highlighted) item.
  49. item_in_use = nil,
  50. --- Indicates it the selected item affect the entire party.
  51. item_in_use_party = false,
  52. -- Slots in the party inventory
  53. inventory_size = 320,
  54. --- True while the items are being ordered by hand (Order > Custom).
  55. custom_mode = false,
  56. --- Inventory position of the item picked in the custom order, or nil.
  57. custom_held = nil,
  58. --- Run when the menu is creaded.
  59. --
  60. -- It does nothing.
  61. on_start = function(self)
  62. return 0
  63. end,
  64. --- Handles button events.
  65. --
  66. -- For the current submenu, handles directional keys, enter and escape events.
  67. -- @param button Pressed direction or action name.
  68. -- @param event Trigger event. Normally, "Press".
  69. on_button = function(self, button, event)
  70. if UiContainer.current_menu == "item" then
  71. if UiContainer.current_submenu == "bar_selection" then
  72. if button == Config.ACTIONS.CANCEL and event == "Press" then
  73. audio_manager:play_sound("Back")
  74. UiContainer.current_menu = "main"
  75. script:request_end_sync(Script.UI, "ItemMenu", "hide", 0)
  76. elseif button == Config.CONTROLS.RIGHT then
  77. audio_manager:play_sound("Cursor")
  78. self.bar_position = self.bar_position + 1
  79. if self.bar_position > self.bar_position_total then
  80. self.bar_position = 1
  81. end
  82. ui_manager:get_widget("ItemMenu.Container.ItemMenuTopBar.Cursor"):set_default_animation("Position" .. self.bar_position)
  83. -- If highlighted, show key items.
  84. if self.bar_position == 3 then
  85. ui_manager:get_widget("ItemMenu.Container.KeyItems"):set_visible(true)
  86. else
  87. ui_manager:get_widget("ItemMenu.Container.KeyItems"):set_visible(false)
  88. end
  89. elseif button == Config.CONTROLS.LEFT then
  90. audio_manager:play_sound("Cursor")
  91. self.bar_position = self.bar_position - 1
  92. if self.bar_position < 1 then
  93. self.bar_position = self.bar_position_total
  94. end
  95. ui_manager:get_widget("ItemMenu.Container.ItemMenuTopBar.Cursor"):set_default_animation("Position" .. self.bar_position)
  96. -- If highlighted, show key items.
  97. if self.bar_position == 3 then
  98. ui_manager:get_widget("ItemMenu.Container.KeyItems"):set_visible(true)
  99. else
  100. ui_manager:get_widget("ItemMenu.Container.KeyItems"):set_visible(false)
  101. end
  102. elseif button == Config.ACTIONS.CONFIRM then
  103. if self.bar_position == 1 then
  104. self.submenu_items(self)
  105. elseif self.bar_position == 2 then
  106. self.submenu_order(self)
  107. elseif self.bar_position == 3 then
  108. self.submenu_key_items(self)
  109. else
  110. return 0
  111. end
  112. audio_manager:play_sound("Cursor")
  113. else
  114. return 0
  115. end
  116. elseif UiContainer.current_submenu == "order_selection" then
  117. if button == Config.ACTIONS.CANCEL and event == "Press" then
  118. audio_manager:play_sound("Back")
  119. self.submenu_bar(self)
  120. elseif button == Config.CONTROLS.DOWN then
  121. audio_manager:play_sound("Cursor")
  122. self.order_cursor_position = self.order_cursor_position + 1
  123. if self.order_cursor_position > self.order_cursor_position_total then
  124. self.order_cursor_position = 1
  125. end
  126. ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_default_animation("Position" .. self.order_cursor_position)
  127. elseif button == Config.CONTROLS.UP then
  128. audio_manager:play_sound("Cursor")
  129. self.order_cursor_position = self.order_cursor_position - 1
  130. if self.order_cursor_position <= 0 then
  131. self.order_cursor_position = self.order_cursor_position_total
  132. end
  133. ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_default_animation("Position" .. self.order_cursor_position)
  134. elseif button == Config.ACTIONS.CONFIRM then
  135. audio_manager:play_sound("Cursor")
  136. if self.order_cursor_position == 1 then
  137. -- Custom: the items are moved by hand in the list.
  138. self.submenu_custom(self)
  139. return 0
  140. elseif self.order_cursor_position == 2 then
  141. Inventory.sort(Inventory.ORDER.FIELD)
  142. elseif self.order_cursor_position == 3 then
  143. Inventory.sort(Inventory.ORDER.BATTLE)
  144. elseif self.order_cursor_position == 4 then
  145. Inventory.sort(Inventory.ORDER.THROW)
  146. elseif self.order_cursor_position == 5 then
  147. Inventory.sort(Inventory.ORDER.TYPE)
  148. elseif self.order_cursor_position == 6 then
  149. Inventory.sort(Inventory.ORDER.NAME)
  150. elseif self.order_cursor_position == 7 then
  151. Inventory.sort(Inventory.ORDER.MOST)
  152. elseif self.order_cursor_position == 8 then
  153. Inventory.sort(Inventory.ORDER.LEAST)
  154. end
  155. self.populate_items(self)
  156. self.submenu_bar(self)
  157. end
  158. elseif UiContainer.current_submenu == "item_selection" then
  159. local cursor = ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor")
  160. local desc_label = ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText")
  161. if button == Config.CONTROLS.DOWN then
  162. if self.item_cursor_item_selected >= self.inventory_size then
  163. -- At the very end, do nothing
  164. audio_manager:play_sound("Error")
  165. return 0
  166. end
  167. audio_manager:play_sound("Cursor")
  168. self.item_cursor_item_selected = self.item_cursor_item_selected + 1
  169. if self.item_cursor_position == self.item_cursor_position_total then
  170. -- The cursor is at the end. Scroll page, but dont change cursor.
  171. self.first_item_in_window = self.first_item_in_window + 1
  172. self.populate_items(self)
  173. else
  174. -- The cursor is not at the end, move cursor down.
  175. self.item_cursor_position = self.item_cursor_position + 1
  176. end
  177. cursor:set_default_animation("Position" .. self.item_cursor_position)
  178. -- If an item is selected, show description
  179. self.show_item_description(self)
  180. elseif button == Config.CONTROLS.UP then
  181. if self.item_cursor_item_selected <= 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. self.item_cursor_item_selected = self.item_cursor_item_selected - 1
  188. if self.item_cursor_position == 1 then
  189. -- The cursor is at the end. Scroll page, but dont change cursor.
  190. self.first_item_in_window = self.first_item_in_window - 1
  191. self.populate_items(self)
  192. else
  193. -- The cursor is not at the end, move cursor down.
  194. self.item_cursor_position = self.item_cursor_position - 1
  195. end
  196. cursor:set_default_animation("Position" .. self.item_cursor_position)
  197. -- If an item is selected, show description
  198. self.show_item_description(self)
  199. elseif button == Config.ACTIONS.R1 then
  200. if self.first_item_in_window + self.item_cursor_position_total < self.inventory_size - self.item_cursor_position_total then
  201. audio_manager:play_sound("Cursor")
  202. self.item_cursor_item_selected = self.item_cursor_item_selected + self.item_cursor_position_total
  203. self.first_item_in_window = self.first_item_in_window + self.item_cursor_position_total
  204. self.populate_items(self)
  205. self.show_item_description(self)
  206. else
  207. audio_manager:play_sound("Error")
  208. end
  209. elseif button == Config.ACTIONS.L1 then
  210. if self.first_item_in_window - self.item_cursor_position_total >= 0 then
  211. audio_manager:play_sound("Cursor")
  212. self.item_cursor_item_selected = self.item_cursor_item_selected - self.item_cursor_position_total
  213. self.first_item_in_window = self.first_item_in_window - self.item_cursor_position_total
  214. self.populate_items(self)
  215. self.show_item_description(self)
  216. else
  217. audio_manager:play_sound("Error")
  218. end
  219. elseif button == Config.ACTIONS.CANCEL and event == "Press" then
  220. audio_manager:play_sound("Back")
  221. if self.custom_mode then
  222. if self.custom_held ~= nil then
  223. -- Drop the picked item.
  224. self.custom_held = nil
  225. else
  226. -- Back to the order options.
  227. self.submenu_order(self)
  228. end
  229. else
  230. self.submenu_bar(self)
  231. UiContainer.current_submenu = "bar_selection"
  232. ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(false)
  233. end
  234. elseif button == Config.ACTIONS.CONFIRM and event == "Press" and self.custom_mode then
  235. self.custom_confirm(self)
  236. elseif button == Config.ACTIONS.CONFIRM then
  237. local selected = Inventory[self.item_cursor_item_selected]
  238. if selected ~= nil and Game.Items[selected.item].menu == 1 then
  239. audio_manager:play_sound("Cursor")
  240. self.item_in_use = selected.item
  241. if (Game.Items[selected.item].target.default_multiple) == 1 then
  242. self.item_in_use_party = true
  243. self.submenu_chars(self, true)
  244. else
  245. self.item_in_use_party = false
  246. self.submenu_chars(self, false)
  247. end
  248. else
  249. audio_manager:play_sound("Error")
  250. end
  251. end
  252. elseif UiContainer.current_submenu == "char_selection" then
  253. if button == Config.CONTROLS.DOWN then
  254. if self.item_in_use_party == true then
  255. return 0
  256. else
  257. audio_manager:play_sound("Cursor")
  258. self.char_position = self.char_position + 1
  259. if self.char_position > self.char_position_total then
  260. self.char_position = 1
  261. end
  262. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_default_animation("Position" .. self.char_position)
  263. end
  264. elseif button == Config.CONTROLS.UP then
  265. if self.item_in_use_party == true then
  266. return 0
  267. else
  268. audio_manager:play_sound("Cursor")
  269. self.char_position = self.char_position - 1
  270. if self.char_position < 1 then
  271. self.char_position = self.char_position_total
  272. end
  273. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_default_animation("Position" .. self.char_position)
  274. end
  275. elseif button == Config.ACTIONS.CANCEL then
  276. audio_manager:play_sound("Back")
  277. self.submenu_items(self)
  278. elseif button == Config.ACTIONS.CONFIRM then
  279. local use_result = false
  280. if self.item_in_use_party == true then
  281. use_result = self.use_item(self, self.item_in_use, nil)
  282. else
  283. use_result = self.use_item(self, self.item_in_use, self.char_position)
  284. end
  285. if use_result == false then
  286. audio_manager:play_sound("Error")
  287. else
  288. -- TODO: Audio here? or in use_item?
  289. end
  290. end
  291. elseif UiContainer.current_submenu == "key_item_selection" then
  292. if button == Config.CONTROLS.DOWN then
  293. if self.key_items_cursor_y_position == self.key_items_cursor_y_position_total and self.first_key_item_row_in_window == self.key_items_total_rows - self.key_items_cursor_y_position then
  294. -- Last key item selected, do nothing
  295. audio_manager:play_sound("Error")
  296. return 0
  297. end
  298. audio_manager:play_sound("Cursor")
  299. if self.key_items_cursor_y_position == self.key_items_cursor_y_position_total then
  300. -- Scroll down by 1, don't move cursor
  301. self.first_key_item_row_in_window = self.first_key_item_row_in_window + 1
  302. self.populate_key_items(self)
  303. else
  304. -- Move cursor down by 1.
  305. self.key_items_cursor_y_position = self.key_items_cursor_y_position + 1
  306. end
  307. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_default_animation("Position" .. self.key_items_cursor_y_position .. "-" .. self.key_items_cursor_x_position)
  308. -- Show description of the selected item, if any.
  309. local k_selected_id = 2 * (self.first_key_item_row_in_window + self.key_items_cursor_y_position - 2) + self.key_items_cursor_x_position - 1
  310. if (Inventory.key[k_selected_id] == true) then
  311. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text(Game.Key_Items[k_selected_id].description)
  312. else
  313. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  314. end
  315. elseif button == Config.CONTROLS.UP then
  316. if self.key_items_cursor_y_position == 1 and self.first_key_item_row_in_window == 1 then
  317. -- First item, do nothing.
  318. audio_manager:play_sound("Error")
  319. return 0
  320. end
  321. audio_manager:play_sound("Cursor")
  322. if self.key_items_cursor_y_position == 1 then
  323. -- Scroll down by 1, don't move cursor
  324. self.first_key_item_row_in_window = self.first_key_item_row_in_window - 1
  325. self.populate_key_items(self)
  326. else
  327. -- Move cursor up by 1.
  328. self.key_items_cursor_y_position = self.key_items_cursor_y_position - 1
  329. end
  330. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_default_animation("Position" .. self.key_items_cursor_y_position .. "-" .. self.key_items_cursor_x_position)
  331. -- Show description of the selected item, if any.
  332. local k_selected_id = 2 * (self.first_key_item_row_in_window + self.key_items_cursor_y_position - 2) + self.key_items_cursor_x_position - 1
  333. if (Inventory.key[k_selected_id] == true) then
  334. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text(Game.Key_Items[k_selected_id].description)
  335. else
  336. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  337. end
  338. elseif button == Config.CONTROLS.RIGHT then
  339. local k_selected_id = 2 * (self.first_key_item_row_in_window + self.key_items_cursor_y_position - 2) + self.key_items_cursor_x_position - 1
  340. if k_selected_id >= 50 then
  341. -- Already in last item, do nothing
  342. audio_manager:play_sound("Error")
  343. return 0
  344. end
  345. audio_manager:play_sound("Cursor")
  346. k_selected_id = k_selected_id + 1
  347. if self.key_items_cursor_x_position == 1 then
  348. -- If on the left, move to the right. No scrolling.
  349. self.key_items_cursor_x_position = 2
  350. else
  351. -- If on the right, move to left and one row down...
  352. self.key_items_cursor_x_position = 1
  353. if self.key_items_cursor_y_position == self.key_items_cursor_y_position_total then
  354. -- ... by scrolling down by 1, not moving cursor.
  355. self.first_key_item_row_in_window = self.first_key_item_row_in_window + 1
  356. self.populate_key_items(self)
  357. else
  358. -- ... by moving the cursor down.
  359. self.key_items_cursor_y_position = self.key_items_cursor_y_position + 1
  360. end
  361. end
  362. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_default_animation("Position" .. self.key_items_cursor_y_position .. "-" .. self.key_items_cursor_x_position)
  363. -- Show description of the selected item, if any.
  364. if (Inventory.key[k_selected_id] == true) then
  365. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text(Game.Key_Items[k_selected_id].description)
  366. else
  367. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  368. end
  369. elseif button == Config.CONTROLS.LEFT then
  370. local k_selected_id = 2 * (self.first_key_item_row_in_window + self.key_items_cursor_y_position - 2) + self.key_items_cursor_x_position - 1
  371. if k_selected_id <= 0 then
  372. -- Already in first item, do nothing
  373. audio_manager:play_sound("Error")
  374. return 0
  375. end
  376. audio_manager:play_sound("Cursor")
  377. k_selected_id = k_selected_id - 1
  378. if self.key_items_cursor_x_position == 2 then
  379. -- If on the right, move to the left. No scrolling.
  380. self.key_items_cursor_x_position = 1
  381. else
  382. -- If on the left, move to right and one row up...
  383. self.key_items_cursor_x_position = 2
  384. if self.key_items_cursor_y_position == 1 then
  385. -- ... by scrolling up by 1, not moving cursor.
  386. self.first_key_item_row_in_window = self.first_key_item_row_in_window - 1
  387. self.populate_key_items(self)
  388. else
  389. -- ... by moving the cursor up.
  390. self.key_items_cursor_y_position = self.key_items_cursor_y_position - 1
  391. end
  392. end
  393. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_default_animation("Position" .. self.key_items_cursor_y_position .. "-" .. self.key_items_cursor_x_position)
  394. -- Show description of the selected item, if any.
  395. if (Inventory.key[k_selected_id] == true) then
  396. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text(Game.Key_Items[k_selected_id].description)
  397. else
  398. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  399. end
  400. elseif button == Config.ACTIONS.R1 then
  401. if self.first_key_item_row_in_window + self.key_items_cursor_y_position_total < self.key_items_total_rows - self.key_items_cursor_y_position_total then
  402. audio_manager:play_sound("Cursor")
  403. self.first_key_item_row_in_window = self.first_key_item_row_in_window + self.key_items_cursor_y_position_total
  404. self.populate_items(self)
  405. -- Show description of the selected item, if any.
  406. local k_selected_id = 2 * (self.first_key_item_row_in_window + self.key_items_cursor_y_position - 2) + self.key_items_cursor_x_position - 1
  407. if (Inventory.key[k_selected_id] == true) then
  408. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text(Game.Key_Items[k_selected_id].description)
  409. else
  410. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  411. end
  412. else
  413. audio_manager:play_sound("Error")
  414. end
  415. elseif button == Config.ACTIONS.L1 then
  416. if self.first_key_item_row_in_window + self.key_items_cursor_y_position_total > 1 then
  417. audio_manager:play_sound("Cursor")
  418. self.first_key_item_row_in_window = self.first_key_item_row_in_window - self.key_items_cursor_y_position_total
  419. self.populate_items(self)
  420. -- Show description of the selected item, if any.
  421. local k_selected_id = 2 * (self.first_key_item_row_in_window + self.key_items_cursor_y_position - 2) + self.key_items_cursor_x_position - 1
  422. if (Inventory.key[k_selected_id] == true) then
  423. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text(Game.Key_Items[k_selected_id].description)
  424. else
  425. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  426. end
  427. else
  428. audio_manager:play_sound("Cursor")
  429. end
  430. elseif button == Config.ACTIONS.CANCEL then
  431. audio_manager:play_sound("Back")
  432. self.submenu_bar(self)
  433. end
  434. end
  435. -- The blinking cursor of the custom order follows the list when it scrolls.
  436. if self.custom_mode then self.update_held(self) end
  437. end
  438. return 0
  439. end,
  440. --- Opens the main menu.
  441. --
  442. -- Populates and updates displayed data before opening.
  443. show = function(self)
  444. ui_manager:get_widget("ItemMenu"):set_visible(true)
  445. UiContainer.current_menu = "item"
  446. self.char_position_total = 0
  447. for c = 1, 3 do
  448. if Party[c] ~= nil then
  449. self.char_position_total = self.char_position_total + 1
  450. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(c), Characters[Party[c]])
  451. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(c) .. ".Portrait"):set_image("characters/" .. tostring(Party[c]) .. ".png")
  452. else
  453. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(c), nil)
  454. end
  455. end
  456. self.populate_items(self)
  457. self.populate_key_items(self)
  458. self.submenu_items(self)
  459. return 0;
  460. end,
  461. --- Initializes the selection of the menu section in the top bar.
  462. submenu_bar = function(self)
  463. UiContainer.current_submenu = "bar_selection"
  464. self.custom_mode = false
  465. self.custom_held = nil
  466. self.update_held(self)
  467. ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(false)
  468. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_visible(false)
  469. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.CursorMultiple"):set_visible(false)
  470. ui_manager:get_widget("ItemMenu.Container.ItemMenuTopBar.Cursor"):set_visible(true)
  471. ui_manager:get_widget("ItemMenu.Container.ItemMenuTopBar.Cursor"):set_default_animation("Position" .. self.bar_position)
  472. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  473. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_visible(false)
  474. ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_visible(false)
  475. ui_manager:get_widget("ItemMenu.Container.ItemOrder"):set_visible(false)
  476. end,
  477. --- Initializes the sorting mode window.
  478. submenu_order = function(self)
  479. UiContainer.current_submenu = "order_selection"
  480. self.custom_mode = false
  481. self.custom_held = nil
  482. self.update_held(self)
  483. self.order_cursor_position = 1
  484. ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(false)
  485. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_visible(false)
  486. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.CursorMultiple"):set_visible(false)
  487. ui_manager:get_widget("ItemMenu.Container.ItemMenuTopBar.Cursor"):set_visible(false)
  488. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  489. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_visible(false)
  490. ui_manager:get_widget("ItemMenu.Container.ItemOrder"):set_visible(true)
  491. ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_visible(true)
  492. ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_default_animation("Position" .. self.order_cursor_position)
  493. end,
  494. --- Starts the custom order: the cursor goes over the list of items, and they can be swapped.
  495. submenu_custom = function(self)
  496. self.custom_mode = true
  497. self.custom_held = nil
  498. ui_manager:get_widget("ItemMenu.Container.ItemOrder"):set_visible(false)
  499. ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_visible(false)
  500. self.submenu_items(self)
  501. end,
  502. --- Confirm in the custom order: picks the highlighted item, or swaps it with the picked one.
  503. --
  504. -- Items can only be swapped with other items, so the list never gets empty places in the
  505. -- middle.
  506. custom_confirm = function(self)
  507. local index = self.item_cursor_item_selected
  508. if self.custom_held == nil then
  509. if Inventory[index] == nil then
  510. audio_manager:play_sound("Error")
  511. return
  512. end
  513. audio_manager:play_sound("Cursor")
  514. self.custom_held = index
  515. elseif self.custom_held == index then
  516. audio_manager:play_sound("Back")
  517. self.custom_held = nil
  518. elseif Inventory[index] == nil then
  519. audio_manager:play_sound("Error")
  520. else
  521. audio_manager:play_sound("Cursor")
  522. Inventory[self.custom_held], Inventory[index] = Inventory[index], Inventory[self.custom_held]
  523. self.custom_held = nil
  524. self.populate_items(self)
  525. self.show_item_description(self)
  526. end
  527. end,
  528. --- Places the blinking cursor of the item picked in the custom order, or hides it.
  529. update_held = function(self)
  530. local held = ui_manager:get_widget("ItemMenu.Container.ItemList.Held")
  531. local row = nil
  532. if self.custom_held ~= nil then row = self.custom_held - self.first_item_in_window end
  533. if row == nil or row < 1 or row > self.item_cursor_position_total then
  534. held:set_visible(false)
  535. return
  536. end
  537. held:set_visible(true)
  538. held:set_default_animation("Blink" .. tostring(row))
  539. held:play_animation("Blink" .. tostring(row))
  540. end,
  541. --- Shows the description of the highlighted inventory item, or clears it if the slot is empty.
  542. show_item_description = function(self)
  543. local label = ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText")
  544. local entry = Inventory[self.item_cursor_item_selected]
  545. if entry ~= nil then
  546. label:set_text(Game.Items[entry.item].description)
  547. else
  548. label:set_text("")
  549. end
  550. end,
  551. --- Initializes the item selection selection mode.
  552. submenu_items = function(self)
  553. UiContainer.current_submenu = "item_selection"
  554. ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(true)
  555. ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_default_animation("Position" .. self.item_cursor_position)
  556. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_visible(false)
  557. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.CursorMultiple"):set_visible(false)
  558. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_visible(false)
  559. ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_visible(false)
  560. --ui_manager:get_widget("ItemMenu.Container.ItemMenuTopBar.Cursor"):set_visible(false)
  561. -- Show description of the very first item, if any.
  562. -- TODO: Selected, not first
  563. -- Show description of the selected item, if any.
  564. local k_selected_id = (self.first_key_item_row_in_window + self.key_items_cursor_y_position) * 2 + self.key_items_cursor_x_position
  565. if (Inventory.key[k_selected_id] == true) then
  566. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text(Game.Key_Items[k_selected_id].description)
  567. else
  568. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  569. end
  570. self.show_item_description(self)
  571. end,
  572. --- Initializes the character selection mode.
  573. submenu_chars = function(self, multiple)
  574. UiContainer.current_submenu = "char_selection"
  575. --ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(false)
  576. --ui_manager:get_widget("ItemMenu.Container.ItemMenuTopBar.Cursor"):set_visible(false)
  577. if (multiple == true) then
  578. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_visible(false)
  579. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.CursorMultiple"):set_visible(true)
  580. else
  581. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.CursorMultiple"):set_visible(false)
  582. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_visible(true)
  583. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_default_animation("Position" .. self.char_position)
  584. end
  585. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_visible(false)
  586. ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_visible(false)
  587. end,
  588. --- Initializes the key item selection mode.
  589. submenu_key_items = function(self)
  590. UiContainer.current_submenu = "key_item_selection"
  591. ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(true)
  592. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_visible(false)
  593. ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.CursorMultiple"):set_visible(false)
  594. ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_visible(false)
  595. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_visible(true)
  596. ui_manager:get_widget("ItemMenu.Container.KeyItems.Cursor"):set_default_animation("Position" .. self.key_items_cursor_y_position .. "-" .. self.key_items_cursor_x_position)
  597. -- Show description of the selected item, if any.
  598. local k_selected_id = 2 * (self.first_key_item_row_in_window + self.key_items_cursor_y_position - 2) + self.key_items_cursor_x_position - 1
  599. if (Inventory.key[k_selected_id] == true) then
  600. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text(Game.Key_Items[k_selected_id].description)
  601. else
  602. ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText"):set_text("")
  603. end
  604. end,
  605. --- Uses an item from the menu.
  606. --
  607. -- Checks the item conditions. If they are valid, uses the item and discounts 1 from the
  608. -- inventory.
  609. --
  610. -- @param item The ID of the item being used.
  611. -- @param character_pos Position in the party of the character on which to use the item (1-3).
  612. -- If nil, the item affects the whole party.
  613. use_item = function(self, item, character_pos)
  614. char_id = -1
  615. if character_pos then
  616. -- Normal item, use in one character.
  617. char_id = Party[character_pos]
  618. end
  619. -- TODO: When HP MAX is fixed, use it instead of base.
  620. if Game.Items[item].dmg_formula == 2 then
  621. -- HP or MP heal: Potion, Hi-Potion, X-Potion, Ether, Turbo Ether, Phoenix Down
  622. if Game.Items[item].dmg_modifier == 6 then
  623. -- Partial restore: Potion, Hi-Potion, Ether
  624. if Game.Items[item].restore == 0 then
  625. -- Partial HP restore: Potion, Hi potion. Based on power.
  626. if char_id == -1 then
  627. return false
  628. end
  629. if Characters[char_id].stats.hp.current < Characters[char_id].stats.hp.base then
  630. Characters[char_id].stats.hp.current = Characters[char_id].stats.hp.current + (25 * Game.Items[item].power)
  631. if Characters[char_id].stats.hp.current > Characters[char_id].stats.hp.base then
  632. Characters[char_id].stats.hp.current = Characters[char_id].stats.hp.base
  633. end
  634. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  635. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  636. else
  637. return false
  638. end
  639. elseif Game.Items[item].restore == 1 then
  640. -- Partial MP restore: Ether. Based on power.
  641. if Characters[char_id].stats.mp.current < Characters[char_id].stats.mp.base then
  642. Characters[char_id].stats.mp.current = Characters[char_id].stats.mp.current + (25 * Game.Items[item].power)
  643. if Characters[char_id].stats.mp.current > Characters[char_id].stats.mp.base then
  644. Characters[char_id].stats.mp.current = Characters[char_id].stats.mp.base
  645. end
  646. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  647. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  648. else
  649. return false
  650. end
  651. end
  652. elseif Game.Items[item].dmg_modifier == 4 then
  653. -- Full restore: X-Potion, Turbo Ether or partial resurrect: Phoenix Down.
  654. if Game.Items[item].restore == 0 then
  655. -- Full HP restore: X-Potion. Power ignored.
  656. if char_id == -1 then
  657. return false
  658. end
  659. if Characters[char_id].stats.hp.current < Characters[char_id].stats.hp.base then
  660. Characters[char_id].stats.hp.current = Characters[char_id].stats.hp.base
  661. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  662. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  663. else
  664. return false
  665. end
  666. elseif Game.Items[item].restore == 1 then
  667. -- Full MP restore: Turbo ether. Power ignored.
  668. if Characters[char_id].stats.mp.current < Characters[char_id].stats.mp.base then
  669. Characters[char_id].stats.mp.current = Characters[char_id].stats.mp.base
  670. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  671. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  672. else
  673. return false
  674. end
  675. else
  676. -- Resurrect: Phoenix down. Power / 32 is the percent of max HP.
  677. if Characters[char_id].stats.hp.current == 0 then
  678. Characters[char_id].stats.hp.current = math.floor(Game.Items[item].power * Characters[char_id].stats.hp.current / 32)
  679. if Characters[char_id].stats.hp.current > Characters[char_id].stats.hp.base then
  680. Characters[char_id].stats.hp.current = Characters[char_id].stats.hp.base
  681. end
  682. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  683. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  684. else
  685. return false
  686. end
  687. end
  688. end
  689. elseif Game.Items[item].dmg_formula == 0 and Game.Items[item].dmg_modifier == 0 then
  690. -- Full HP and MP restore: Elixir, Megalixir
  691. if Game.Items[item].target.default_multiple == 0 then
  692. --Full HP and MP restore for single target: Elixir.
  693. if Characters[char_id].stats.hp.current < Characters[char_id].stats.hp.base or Characters[char_id].stats.mp.current < Characters[char_id].stats.mp.base then
  694. Characters[char_id].stats.hp.current = Characters[char_id].stats.hp.base
  695. Characters[char_id].stats.mp.current = Characters[char_id].stats.mp.base
  696. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  697. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  698. else
  699. return false
  700. end
  701. else
  702. --Full HP and MP restore for party: MegaElixir.
  703. local useable = false;
  704. for c = 1, 3 do
  705. if Characters[c] ~= nil then
  706. if Characters[c].stats.hp.current < Characters[c].stats.hp.base then
  707. useable = true
  708. elseif Characters[c].stats.mp.current < Characters[c].stats.mp.base then
  709. useable = true
  710. end
  711. end
  712. end
  713. if useable == true then
  714. for pos = 1, 3 do
  715. if Characters[Party[pos]] ~= nil then
  716. Characters[Party[pos]].stats.hp.current = Characters[Party[pos]].stats.hp.base
  717. Characters[Party[pos]].stats.mp.current = Characters[Party[pos]].stats.mp.base
  718. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(pos), Characters[Party[pos]])
  719. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(pos), Characters[Party[pos]])
  720. end
  721. end
  722. else
  723. return false
  724. end
  725. end
  726. elseif Game.Items[item].dmg_formula == 4 then
  727. -- Status change: Hyper, Tranquilizer
  728. if #(Game.Items[item].status) > 0 then
  729. for _, status in ipairs(Game.Items[item].status) do
  730. if status.status == 5 then
  731. -- Fury. Cure saddness or induce fury.
  732. local sadness_key = -1
  733. local fury_key = -1
  734. for st_key, st_val in ipairs(Characters[char_id].status) do
  735. if st_val == 4 then
  736. sadness_key = st_key
  737. elseif st_val == 5 then
  738. fury_key = st_key
  739. end
  740. end
  741. -- If fury already, donothing. If sad, cure. Else, apply fury.
  742. if fury_key ~= -1 then
  743. return false
  744. elseif sadness_key ~= -1 then
  745. table.remove(Characters[char_id].status, sadness_key)
  746. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  747. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  748. else
  749. table.insert(Characters[char_id].status, 5)
  750. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  751. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  752. end
  753. elseif status.status == 4 then
  754. -- Saddness. Cure fury or induce sadness.
  755. local sadness_key = -1
  756. local fury_key = -1
  757. for st_key, st_val in ipairs(Characters[char_id].status) do
  758. if st_val == 4 then
  759. sadness_key = st_key
  760. elseif st_val == 5 then
  761. fury_key = st_key
  762. end
  763. end
  764. -- If fury already, donothing. If sad, cure. Else, apply fury.
  765. if sadness_key ~= -1 then
  766. return false
  767. elseif fury_key ~= -1 then
  768. table.remove(Characters[char_id].status, fury_key)
  769. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  770. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  771. else
  772. table.insert(Characters[char_id].status, 4)
  773. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(character_pos), Characters[char_id])
  774. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(character_pos), Characters[char_id])
  775. end
  776. end
  777. end
  778. end
  779. elseif Game.Items[item].id == 70 then
  780. -- Tent. Can't be differenciated by other means other than ID. Full HP and MP restore for party.
  781. local useable = false;
  782. for c = 1, 3 do
  783. if Characters[c] ~= nil then
  784. if Characters[c].stats.hp.current < Characters[c].stats.hp.base then
  785. useable = true
  786. elseif Characters[c].stats.mp.current < Characters[c].stats.mp.base then
  787. useable = true
  788. end
  789. end
  790. end
  791. if useable == true then
  792. for pos = 1, 3 do
  793. if Characters[Party[pos]] ~= nil then
  794. Characters[Party[pos]].stats.hp.current = Characters[Party[pos]].stats.hp.base
  795. Characters[Party[pos]].stats.mp.current = Characters[Party[pos]].stats.mp.base
  796. UiContainer.populate_character_data("ItemMenu.Container.ItemMenuCharacters.Character" .. tostring(pos), Characters[Party[pos]])
  797. UiContainer.populate_character_data("MainMenu.Container.Characters.Character" .. tostring(pos), Characters[Party[pos]])
  798. end
  799. end
  800. else
  801. return false
  802. end
  803. elseif Game.Items[item].id == 71 then
  804. -- Power source. Can't be differenciated by other means other than ID. STR bonus + 1.
  805. if not Characters.add_stat_bonus(char_id, "str") then
  806. return false
  807. end
  808. elseif Game.Items[item].id == 72 then
  809. -- Guard source. Can't be differenciated by other means other than ID. VIT bonus + 1.
  810. if not Characters.add_stat_bonus(char_id, "vit") then
  811. return false
  812. end
  813. elseif Game.Items[item].id == 73 then
  814. -- Magic source. Can't be differenciated by other means other than ID. MAG bonus + 1.
  815. if not Characters.add_stat_bonus(char_id, "mag") then
  816. return false
  817. end
  818. elseif Game.Items[item].id == 74 then
  819. -- Mind source. Can't be differenciated by other means other than ID. SPR bonus + 1.
  820. if not Characters.add_stat_bonus(char_id, "spr") then
  821. return false
  822. end
  823. elseif Game.Items[item].id == 75 then
  824. -- Speed source. Can't be differenciated by other means other than ID. DEX bonus + 1.
  825. if not Characters.add_stat_bonus(char_id, "dex") then
  826. return false
  827. end
  828. elseif Game.Items[item].id == 76 then
  829. -- Luck source. Can't be differenciated by other means other than ID. LCK bonus + 1.
  830. if not Characters.add_stat_bonus(char_id, "lck") then
  831. return false
  832. end
  833. elseif Game.Items[item].id == 87 then
  834. -- Omnislash. Can't be differenciated by other means other than ID.
  835. -- Functionality hardcoded here.
  836. if (char_id ~= 0) then
  837. -- TODO: Show some message
  838. return false
  839. end
  840. for l = 1, 3 do
  841. if Characters[0].limit.learned[l][1] == 0 or Characters[0].limit.learned[l][2] == 0 then
  842. -- Some limit not learned yet. Show some message.
  843. return false
  844. end
  845. end
  846. if Characters[0].limit.learned[4][1] == 1 then
  847. -- Already learned
  848. return false
  849. end
  850. Characters[0].limit.learned[4][1] = 1
  851. elseif Game.Items[item].id == 88 then
  852. -- Catastrophe. Can't be differenciated by other means other than ID.
  853. -- Functionality hardcoded here.
  854. if (char_id ~= 1) then
  855. -- TODO: Show some message
  856. return false
  857. end
  858. for l = 1, 3 do
  859. if Characters[1].limit.learned[l][1] == 0 or Characters[1].limit.learned[l][2] == 0 then
  860. -- Some limit not learned yet. Show some message.
  861. return false
  862. end
  863. end
  864. if Characters[1].limit.learned[4][1] == 1 then
  865. -- Already learned
  866. return false
  867. end
  868. Characters[1].limit.learned[4][1] = 1
  869. elseif Game.Items[item].id == 89 then
  870. -- Final Heaven. Can't be differenciated by other means other than ID.
  871. -- Functionality hardcoded here.
  872. if (char_id ~= 2) then
  873. -- TODO: Show some message
  874. return false
  875. end
  876. for l = 1, 3 do
  877. if Characters[2].limit.learned[l][1] == 2 or Characters[2].limit.learned[l][2] == 0 then
  878. -- Some limit not learned yet. Show some message.
  879. return false
  880. end
  881. end
  882. if Characters[2].limit.learned[4][1] == 1 then
  883. -- Already learned
  884. return false
  885. end
  886. Characters[2].limit.learned[4][1] = 1
  887. elseif Game.Items[item].id == 90 then
  888. -- Great Gospel. Can't be differenciated by other means other than ID.
  889. -- Functionality hardcoded here.
  890. if (char_id ~= 3) then
  891. -- TODO: Show some message
  892. return false
  893. end
  894. for l = 1, 3 do
  895. if Characters[3].limit.learned[l][1] == 0 or Characters[3].limit.learned[l][2] == 0 then
  896. -- Some limit not learned yet. Show some message.
  897. return false
  898. end
  899. end
  900. if Characters[3].limit.learned[4][1] == 1 then
  901. -- Already learned
  902. return false
  903. end
  904. Characters[3].limit.learned[4][1] = 1
  905. elseif Game.Items[item].id == 91 then
  906. -- Cosmo Memory. Can't be differenciated by other means other than ID.
  907. -- Functionality hardcoded here.
  908. if (char_id ~= 4) then
  909. -- TODO: Show some message
  910. return false
  911. end
  912. for l = 1, 3 do
  913. if Characters[4].limit.learned[l][1] == 0 or Characters[4].limit.learned[l][2] == 0 then
  914. -- Some limit not learned yet. Show some message.
  915. return false
  916. end
  917. end
  918. if Characters[4].limit.learned[4][1] == 1 then
  919. -- Already learned
  920. return false
  921. end
  922. Characters[4].limit.learned[4][1] = 1
  923. elseif Game.Items[item].id == 92 then
  924. -- All Creation. Can't be differenciated by other means other than ID.
  925. -- Functionality hardcoded here.
  926. if (char_id ~= 5) then
  927. -- TODO: Show some message
  928. return false
  929. end
  930. for l = 1, 3 do
  931. if Characters[5].limit.learned[l][1] == 0 or Characters[5].limit.learned[l][2] == 0 then
  932. -- Some limit not learned yet. Show some message.
  933. return false
  934. end
  935. end
  936. if Characters[5].limit.learned[4][1] == 1 then
  937. -- Already learned
  938. return false
  939. end
  940. Characters[5].limit.learned[4][1] = 1
  941. elseif Game.Items[item].id == 93 then
  942. -- Chaos. Can't be differenciated by other means other than ID.
  943. -- Functionality hardcoded here.
  944. if (char_id ~= 7) then
  945. -- TODO: Show some message
  946. return false
  947. end
  948. for l = 1, 3 do
  949. if Characters[7].limit.learned[l][1] == 0 then
  950. -- Some limit not learned yet. Show some message.
  951. return false
  952. end
  953. end
  954. if Characters[7].limit.learned[4][1] == 1 then
  955. -- Already learned
  956. return false
  957. end
  958. Characters[0].limit.learned[4][1] = 1
  959. elseif Game.Items[item].id == 94 then
  960. -- Highwind. Can't be differenciated by other means other than ID.
  961. -- Functionality hardcoded here.
  962. if (char_id ~= 8) then
  963. -- TODO: Show some message
  964. return false
  965. end
  966. for l = 1, 3 do
  967. if Characters[8].limit.learned[l][1] == 0 or Characters[8].limit.learned[l][2] == 0 then
  968. -- Some limit not learned yet. Show some message.
  969. return false
  970. end
  971. end
  972. if Characters[8].limit.learned[4][1] == 1 then
  973. -- Already learned
  974. return false
  975. end
  976. Characters[8].limit.learned[4][1] = 1
  977. elseif Game.Items[item].id == 98 then
  978. -- Save Crystal. Can't be differenciated by other means other than ID.
  979. -- TODO: Implement.
  980. return false
  981. end
  982. -- If this point has been reached, an item has been succesfully used.
  983. -- Discount it from the inventory.
  984. Inventory.remove_item(item, 1)
  985. self.populate_items(self)
  986. -- If it was the last item, go back to the list
  987. if Inventory.get_item_quantity(item) == 0 then
  988. self.submenu_items(self)
  989. end
  990. return true
  991. end,
  992. --- Populates the visible inventory entries.
  993. --
  994. -- 13 entries visible at a time.
  995. populate_items = function(self)
  996. local first_item_in_window = self.first_item_in_window
  997. for i = 1, self.item_cursor_position_total do
  998. local w_icon = ui_manager:get_widget("ItemMenu.Container.ItemList.Icon" .. tostring(i))
  999. local w_name = ui_manager:get_widget("ItemMenu.Container.ItemList.Name" .. tostring(i))
  1000. local w_colon = ui_manager:get_widget("ItemMenu.Container.ItemList.Colon" .. tostring(i))
  1001. local w_qty = ui_manager:get_widget("ItemMenu.Container.ItemList.Qty" .. tostring(i))
  1002. local inventory_index = i + first_item_in_window
  1003. if Inventory[inventory_index] == nil then
  1004. w_icon:set_visible(false)
  1005. w_name:set_visible(false)
  1006. w_colon:set_visible(false)
  1007. w_qty:set_visible(false)
  1008. else
  1009. local useable = Game.Items[Inventory[inventory_index].item].menu
  1010. local type = Game.Items[Inventory[inventory_index].item].type
  1011. -- TODO: Get icon.
  1012. w_icon:set_visible(true)
  1013. w_name:set_visible(true)
  1014. w_colon:set_visible(true)
  1015. w_qty:set_visible(true)
  1016. -- TODO: Gray for unuseable
  1017. if (useable == 1) then
  1018. w_name:set_colour(1.0, 1.0, 1.0)
  1019. w_colon:set_colour(1.0, 1.0, 1.0)
  1020. w_qty:set_colour(1.0, 1.0, 1.0)
  1021. else
  1022. w_name:set_colour(0.6, 0.6, 0.6)
  1023. w_colon:set_colour(0.6, 0.6, 0.6)
  1024. w_qty:set_colour(0.6, 0.6, 0.6)
  1025. end
  1026. -- Item icon
  1027. if type == Inventory.ITEM_TYPE.ITEM then
  1028. w_icon:set_image("icons/item_item.png")
  1029. elseif type == Inventory.ITEM_TYPE.ARMOR then
  1030. w_icon:set_image("icons/item_armor.png")
  1031. elseif type == Inventory.ITEM_TYPE.ACCESSORY then
  1032. w_icon:set_image("icons/item_accessory.png")
  1033. elseif type == Inventory.ITEM_TYPE.WEAPON then
  1034. local users = Game.Items[Inventory[inventory_index].item].users
  1035. for _, v in ipairs(users) do
  1036. if v >= 0 and v <= 8 then
  1037. w_icon:set_image("icons/item_weapon_" .. v .. ".png")
  1038. break;
  1039. end
  1040. end
  1041. end
  1042. w_name:set_text(Game.Items[Inventory[inventory_index].item].name)
  1043. w_qty:set_text(tostring(Inventory[inventory_index].quantity))
  1044. w_colon:set_text(":") -- Refresh text to apply colour.
  1045. end
  1046. end
  1047. -- Set scroll bar position
  1048. local scroll = ui_manager:get_widget("ItemMenu.Container.ItemList.Scroll")
  1049. -- first_item_in_window == 1 then Top -> 0%3
  1050. -- first_item_in_window == inventory_size - first_item_in_window then top = 100%-21
  1051. local percent = math.floor(self.first_item_in_window * 100 / (self.inventory_size - self.item_cursor_position_total))
  1052. local fixed = -1 * math.floor(self.first_item_in_window * 17 / (self.inventory_size - self.item_cursor_position_total)) + 3
  1053. scroll:set_y(percent, fixed)
  1054. return
  1055. end,
  1056. --- Populates the visible key items entries.
  1057. --
  1058. -- 26 entries visible at a time (13 rows).
  1059. populate_key_items = function(self)
  1060. local first_key_item_row_in_window = self.first_key_item_row_in_window
  1061. for i = 1, self.key_items_cursor_y_position_total do
  1062. local label_left = ui_manager:get_widget("ItemMenu.Container.KeyItems.KeyItem" .. tostring(i) .. "-1")
  1063. local label_right = ui_manager:get_widget("ItemMenu.Container.KeyItems.KeyItem" .. tostring(i) .. "-2")
  1064. local left_item_index = (first_key_item_row_in_window + i - 2) * 2
  1065. local right_item_index = left_item_index + 1
  1066. if Inventory.has_key_item(left_item_index) == true then -- Left column
  1067. label_left:set_text(Game.Key_Items[left_item_index].name)
  1068. else
  1069. label_left:set_text("")
  1070. end
  1071. if Inventory.has_key_item(right_item_index) == true then -- Right column
  1072. label_right:set_text(Game.Key_Items[right_item_index].name)
  1073. else
  1074. label_right:set_text("")
  1075. end
  1076. end
  1077. -- Set scroll bar position
  1078. local scroll = ui_manager:get_widget("ItemMenu.Container.KeyItems.Scroll")
  1079. -- first_key_item_row_in_window == 1 then Top -> 0%3
  1080. -- first_key_item_row_in_window == key_items_total_rows - first_key_item_row_in_window = 100%-21
  1081. local percent = math.ceil((self.first_key_item_row_in_window - 1) * 100 / (self.key_items_total_rows - self.key_items_cursor_y_position_total))
  1082. local fixed = -1 * math.floor((self.first_key_item_row_in_window - 1) * 13 / (self.key_items_total_rows - self.key_items_cursor_y_position_total)) + 3
  1083. scroll:set_y(percent, fixed)
  1084. return
  1085. end,
  1086. --- Hides the item menu and goes back to the main menu.
  1087. hide = function(self)
  1088. ui_manager:get_widget("ItemMenu"):set_visible(false)
  1089. UiContainer.current_menu = "main"
  1090. UiContainer.current_submenu = ""
  1091. UiContainer.resume_menu("main")
  1092. return 0;
  1093. end,
  1094. }