shop_menu.lua 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. if UiContainer == nil then UiContainer = {} end
  2. --- The shop menu.
  3. --
  4. -- Opened by the field scripts (open_shop). The shop to open is set in `shop_id` before showing the
  5. -- menu. The inventory of each shop is in Game.Shops (gamedata/shops.lua). Prices are the ones of
  6. -- the items and materia, the same in every shop. Items are sold for half of their price, materia for its AP (see materia_sell_price).
  7. --
  8. -- States:
  9. -- * "" (command): Buy, Sell or Exit.
  10. -- * "buy": list of the things the shop sells.
  11. -- * "buy_quantity": how many of the highlighted item to buy.
  12. -- * "sell": list of the things of the party that can be sold.
  13. -- * "sell_quantity": how many of the highlighted item to sell.
  14. -- * "sell_materia": stars, AP and price of the highlighted materia, to confirm the sale.
  15. UiContainer.ShopMenu = UiContainer.Menu.new{
  16. id = "shop",
  17. --- ID of the shop to open (index of Game.Shops).
  18. shop_id = 0,
  19. --- Visible rows of the list.
  20. ROWS = 8,
  21. --- Characters shown in the party window (the 9 that can be enabled).
  22. MAX_CHARACTERS = 9,
  23. --- Most items of a kind that the party can carry.
  24. MAX_QUANTITY = 99,
  25. --- Names of the kinds of shop (the type of the shop in Game.Shops).
  26. TITLES = {
  27. [1] = "Weapon Shop",
  28. [2] = "Item Shop",
  29. [3] = "Materia Shop",
  30. [4] = "Drug Store",
  31. [5] = "Tool Shop",
  32. [6] = "Accessory Shop",
  33. [7] = "Item Shop",
  34. [8] = "Item Shop",
  35. },
  36. --- Entries of the list being shown. Each one is {materia = bool, id = ID, price = gil, ...}.
  37. entries = {},
  38. --- Quantity chosen in the quantity states.
  39. quantity = 1,
  40. --- Most that can be chosen in the quantity states.
  41. max_quantity = 1,
  42. --- Cursor over Buy, Sell and Exit.
  43. command_cursor = UiContainer.Cursor.new{
  44. widget = "ShopMenu.Container.TopBar.Commands.Cursor",
  45. total = 3,
  46. },
  47. --- Cursor over the visible rows of the list, scrolling over the entries.
  48. list_cursor = UiContainer.ListCursor.new{
  49. widget = "ShopMenu.Container.List.Cursor",
  50. rows = 8,
  51. on_scroll = function() UiContainer.ShopMenu:populate_list() end,
  52. on_move = function() UiContainer.ShopMenu:populate_details() end,
  53. },
  54. states = {
  55. -- Buy, Sell, Exit.
  56. [""] = {
  57. cursor = function(self) return self.command_cursor end,
  58. axis = "horizontal",
  59. confirm_sound = false,
  60. confirm = function(self, cursor)
  61. local command = cursor:index()
  62. if command == 1 then
  63. UiContainer.play_sound("Cursor")
  64. self:enter_list("buy")
  65. elseif command == 2 then
  66. if not self:enter_list("sell") then return false end
  67. UiContainer.play_sound("Cursor")
  68. else
  69. UiContainer.play_sound("Back")
  70. script:request_end_sync(Script.UI, "ShopMenu", "hide", 0)
  71. end
  72. end,
  73. cancel = function(self)
  74. script:request_end_sync(Script.UI, "ShopMenu", "hide", 0)
  75. end,
  76. },
  77. buy = {
  78. cursor = function(self) return self.list_cursor end,
  79. axis = "vertical",
  80. confirm_sound = false,
  81. confirm = function(self, cursor)
  82. return self:start_buy(self.entries[cursor:index()])
  83. end,
  84. cancel = function(self) self:enter_command() end,
  85. },
  86. buy_quantity = {
  87. confirm_sound = false,
  88. confirm = function(self) return self:buy(self.entries[self.list_cursor:index()]) end,
  89. cancel = function(self) self:set_state("buy") self:update_prompt() end,
  90. },
  91. sell = {
  92. cursor = function(self) return self.list_cursor end,
  93. axis = "vertical",
  94. confirm_sound = false,
  95. confirm = function(self, cursor)
  96. return self:start_sell(self.entries[cursor:index()])
  97. end,
  98. cancel = function(self) self:enter_command() end,
  99. },
  100. -- Details of a materia before selling it: confirm sells it, cancel goes back to the list.
  101. sell_materia = {
  102. confirm_sound = false,
  103. confirm = function(self) return self:sell(self.entries[self.list_cursor:index()]) end,
  104. cancel = function(self) self:set_state("sell") self:update_prompt() end,
  105. },
  106. sell_quantity = {
  107. confirm_sound = false,
  108. confirm = function(self) return self:sell(self.entries[self.list_cursor:index()]) end,
  109. cancel = function(self) self:set_state("sell") self:update_prompt() end,
  110. },
  111. },
  112. -----------------------------------------------------------------------------------------------
  113. -- Opening and closing
  114. -----------------------------------------------------------------------------------------------
  115. --- Opens the shop set in `shop_id`.
  116. show = function(self)
  117. local shop = Game.Shops ~= nil and Game.Shops[self.shop_id] or nil
  118. if shop == nil then
  119. print("Tried to open invalid shop " .. tostring(self.shop_id))
  120. return 0
  121. end
  122. self.shop = shop
  123. self.materia_shop = (shop.type == 3)
  124. self:activate()
  125. -- Like the other menus opened by the scenes: the game is paused and the main menu is not
  126. -- available meanwhile.
  127. entity_manager:set_paused(true)
  128. MenuSettings.pause_available = false
  129. self.menu_was_available = MenuSettings.available
  130. MenuSettings.available = false
  131. ui_manager:get_widget("ShopMenu"):set_visible(true)
  132. ui_manager:get_widget("ShopMenu.Background"):set_visible(true)
  133. ui_manager:get_widget("ShopMenu.Container.Title.Title"):set_text(self.TITLES[shop.type] or "Shop")
  134. self:populate_party(nil)
  135. self.command_cursor:set(1)
  136. self:enter_command()
  137. return 0
  138. end,
  139. --- Closes the shop.
  140. hide = function(self)
  141. ui_manager:get_widget("ShopMenu.Background"):set_visible(false)
  142. ui_manager:get_widget("ShopMenu"):set_visible(false)
  143. self:deactivate()
  144. MenuSettings.available = self.menu_was_available
  145. MenuSettings.pause_available = true
  146. entity_manager:set_paused(false)
  147. return 0
  148. end,
  149. -----------------------------------------------------------------------------------------------
  150. -- States
  151. -----------------------------------------------------------------------------------------------
  152. --- Goes back to the Buy / Sell / Exit command.
  153. enter_command = function(self)
  154. self:set_state("")
  155. self.entries = {}
  156. self.list_cursor.count = 0
  157. self:update_prompt()
  158. self:populate_list()
  159. self:populate_details()
  160. end,
  161. --- Enters the list of things to buy or sell.
  162. --
  163. -- @param mode "buy" or "sell".
  164. -- @return False if there is nothing to list.
  165. enter_list = function(self, mode)
  166. local entries
  167. if mode == "buy" then
  168. entries = self:buy_entries()
  169. else
  170. entries = self:sell_entries()
  171. end
  172. if #entries == 0 then return false end
  173. self.entries = entries
  174. self.list_cursor.count = #entries
  175. self.list_cursor.first = 1
  176. self.list_cursor:set(1)
  177. self:set_state(mode)
  178. self:update_prompt()
  179. self:populate_list()
  180. self:populate_details()
  181. return true
  182. end,
  183. --- Shows the prompt (or the commands) that match the current state.
  184. update_prompt = function(self)
  185. local state = self:get_state()
  186. local bar = "ShopMenu.Container.TopBar."
  187. ui_manager:get_widget(bar .. "Commands"):set_visible(state == "")
  188. ui_manager:get_widget(bar .. "PromptBuy"):set_visible(state == "buy")
  189. ui_manager:get_widget(bar .. "PromptSell"):set_visible(state == "sell")
  190. ui_manager:get_widget(bar .. "PromptQuantity"):set_visible(
  191. state == "buy_quantity" or state == "sell_quantity")
  192. ui_manager:get_widget("ShopMenu.Container.MateriaSale"):set_visible(state == "sell_materia")
  193. ui_manager:get_widget("ShopMenu.Container.Quantity"):set_visible(
  194. state == "buy_quantity" or state == "sell_quantity")
  195. ui_manager:get_widget("ShopMenu.Container.List.Cursor"):set_visible(state ~= "")
  196. end,
  197. -----------------------------------------------------------------------------------------------
  198. -- Entries
  199. -----------------------------------------------------------------------------------------------
  200. --- Gets the data of the thing an entry refers to.
  201. entry_data = function(self, entry)
  202. if entry.materia then return Game.Materia[entry.id] end
  203. return Game.Items[entry.id]
  204. end,
  205. --- Entries of the things that the shop sells.
  206. buy_entries = function(self)
  207. local entries = {}
  208. for _, listed in ipairs(self.shop.items) do
  209. local entry = {materia = listed[1], id = listed[2]}
  210. local data = self:entry_data(entry)
  211. if data ~= nil then
  212. entry.price = data.price
  213. table.insert(entries, entry)
  214. end
  215. end
  216. return entries
  217. end,
  218. --- Entries of the things that the party can sell.
  219. --
  220. -- Materia shops buy materia. The others buy items, weapons, armors and accessories.
  221. sell_entries = function(self)
  222. local entries = {}
  223. if self.materia_shop then
  224. for slot = 1, MAX_MATERIA_SLOTS do
  225. local owned = Materia[slot]
  226. if owned ~= nil and Game.Materia[owned.id] ~= nil then
  227. table.insert(entries, {
  228. materia = true, id = owned.id, slot = slot, ap = owned.ap or 0,
  229. price = self:materia_sell_price(owned.id, owned.ap or 0),
  230. sellable = true,
  231. })
  232. end
  233. end
  234. else
  235. for slot = 1, MAX_INVENTORY_SLOTS do
  236. local owned = Inventory[slot]
  237. if owned ~= nil and Game.Items[owned.item] ~= nil then
  238. local data = Game.Items[owned.item]
  239. table.insert(entries, {
  240. materia = false, id = owned.item, quantity = owned.quantity,
  241. price = math.floor(data.price / 2),
  242. sellable = (data.sell ~= 0),
  243. })
  244. end
  245. end
  246. end
  247. return entries
  248. end,
  249. --- Gil that a materia is sold for.
  250. --
  251. -- As in ff7.exe: a materia sells for the AP it has. A mastered one sells for 70 times its
  252. -- price, and the ones priced 1 (the master materia, which have no levels to master) for 1.
  253. --
  254. -- @param id ID of the materia.
  255. -- @param ap AP of the materia.
  256. materia_sell_price = function(self, id, ap)
  257. local data = Game.Materia[id]
  258. if data.price == 1 then return 1 end
  259. local levels = data.levels_ap or {}
  260. if #levels > 1 and ap >= levels[#levels] then return data.price * 70 end
  261. return ap
  262. end,
  263. --- Icon of an entry.
  264. entry_icon = function(self, entry)
  265. if entry.materia then
  266. return "icons/materia_" .. tostring(Game.Materia[entry.id].type) .. ".png"
  267. end
  268. local data = Game.Items[entry.id]
  269. if data.type == Inventory.ITEM_TYPE.ARMOR then
  270. return "icons/item_armor.png"
  271. elseif data.type == Inventory.ITEM_TYPE.ACCESSORY then
  272. return "icons/item_accessory.png"
  273. elseif data.type == Inventory.ITEM_TYPE.WEAPON then
  274. for _, user in ipairs(data.users or {}) do
  275. if user >= 0 and user <= 8 then
  276. return "icons/item_weapon_" .. tostring(user) .. ".png"
  277. end
  278. end
  279. end
  280. return "icons/item_item.png"
  281. end,
  282. -----------------------------------------------------------------------------------------------
  283. -- Counting
  284. -----------------------------------------------------------------------------------------------
  285. --- How many of a thing does the party carry (not equipped)?
  286. owned_count = function(self, entry)
  287. if not entry.materia then return Inventory.get_item_quantity(entry.id) end
  288. local count = 0
  289. for slot = 1, MAX_MATERIA_SLOTS do
  290. if Materia[slot] ~= nil and Materia[slot].id == entry.id then count = count + 1 end
  291. end
  292. return count
  293. end,
  294. --- How many of a thing are equipped by the characters?
  295. equipped_count = function(self, entry)
  296. local count = 0
  297. for char_id = 0, 8 do
  298. local character = Characters[char_id]
  299. if type(character) == "table" and character.weapon ~= nil then
  300. if entry.materia then
  301. for _, equipment in ipairs({character.weapon, character.armor}) do
  302. for slot = 0, 7 do
  303. local materia = equipment.materia ~= nil and equipment.materia[slot] or nil
  304. if materia ~= nil and materia.id == entry.id then count = count + 1 end
  305. end
  306. end
  307. else
  308. if character.weapon.id == entry.id then count = count + 1 end
  309. if character.armor ~= nil and character.armor.id == entry.id then count = count + 1 end
  310. if character.accessory == entry.id then count = count + 1 end
  311. end
  312. end
  313. end
  314. return count
  315. end,
  316. --- Is there room in the inventory for one more of a thing?
  317. has_room = function(self, entry)
  318. if entry.materia then
  319. for slot = 1, MAX_MATERIA_SLOTS do
  320. if Materia[slot] == nil then return true end
  321. end
  322. return false
  323. end
  324. if Inventory.get_item_quantity(entry.id) > 0 then
  325. return Inventory.get_item_quantity(entry.id) < self.MAX_QUANTITY
  326. end
  327. for slot = 1, MAX_INVENTORY_SLOTS do
  328. if Inventory[slot] == nil then return true end
  329. end
  330. return false
  331. end,
  332. -----------------------------------------------------------------------------------------------
  333. -- Buying
  334. -----------------------------------------------------------------------------------------------
  335. --- Starts buying the highlighted entry.
  336. --
  337. -- Materia is bought straight away. For anything else the quantity is asked.
  338. -- @return False if it can't be bought.
  339. start_buy = function(self, entry)
  340. if entry == nil or Inventory.money < entry.price or not self:has_room(entry) then
  341. return false
  342. end
  343. if entry.materia then
  344. return self:buy(entry)
  345. end
  346. local room = self.MAX_QUANTITY - Inventory.get_item_quantity(entry.id)
  347. local affordable = (entry.price > 0) and math.floor(Inventory.money / entry.price) or room
  348. self.max_quantity = math.max(1, math.min(room, affordable))
  349. self:enter_quantity("buy_quantity")
  350. UiContainer.play_sound("Cursor")
  351. end,
  352. --- Buys the chosen quantity of an entry.
  353. buy = function(self, entry)
  354. if entry == nil then return false end
  355. local quantity = entry.materia and 1 or self.quantity
  356. local total = entry.price * quantity
  357. if Inventory.money < total or not self:has_room(entry) then
  358. return false
  359. end
  360. Inventory.remove_money(total)
  361. if entry.materia then
  362. Materia.add(entry.id, 0)
  363. else
  364. Inventory.add_item(entry.id, quantity)
  365. end
  366. UiContainer.play_sound("Buy")
  367. self:set_state("buy")
  368. self:update_prompt()
  369. self:populate_details()
  370. end,
  371. -----------------------------------------------------------------------------------------------
  372. -- Selling
  373. -----------------------------------------------------------------------------------------------
  374. --- Starts selling the highlighted entry.
  375. --
  376. -- Materia is sold straight away. For anything else the quantity is asked.
  377. -- @return False if it can't be sold.
  378. start_sell = function(self, entry)
  379. -- Materia can be sold for 0 gil (it has no AP).
  380. if entry == nil or not entry.sellable then return false end
  381. if not entry.materia and entry.price <= 0 then return false end
  382. if entry.materia then
  383. self:set_state("sell_materia")
  384. self:update_prompt()
  385. self:populate_materia_sale(entry)
  386. UiContainer.play_sound("Cursor")
  387. return
  388. end
  389. self.max_quantity = entry.quantity
  390. self:enter_quantity("sell_quantity")
  391. UiContainer.play_sound("Cursor")
  392. end,
  393. --- Sells the chosen quantity of an entry.
  394. sell = function(self, entry)
  395. if entry == nil or not entry.sellable then return false end
  396. if entry.materia then
  397. Materia[entry.slot] = nil
  398. Inventory.add_money(entry.price)
  399. else
  400. Inventory.remove_item(entry.id, self.quantity)
  401. Inventory.add_money(entry.price * self.quantity)
  402. end
  403. UiContainer.play_sound("Buy")
  404. -- The list changes: build it again and keep the cursor where it was if it can.
  405. local first, position = self.list_cursor.first, self.list_cursor.position
  406. local entries = self:sell_entries()
  407. if #entries == 0 then
  408. self:enter_command()
  409. return
  410. end
  411. self.entries = entries
  412. self.list_cursor.count = #entries
  413. local index = math.min(first + position - 1, #entries)
  414. first = math.max(1, math.min(first, #entries - self.ROWS + 1))
  415. self.list_cursor.first = first
  416. self.list_cursor:set(index - first + 1)
  417. self:set_state("sell")
  418. self:update_prompt()
  419. self:populate_list()
  420. self:populate_details()
  421. end,
  422. -----------------------------------------------------------------------------------------------
  423. -- Quantity
  424. -----------------------------------------------------------------------------------------------
  425. --- Opens the window to choose how many to buy or sell.
  426. enter_quantity = function(self, state)
  427. self.quantity = 1
  428. self:set_state(state)
  429. self:update_prompt()
  430. self:populate_quantity()
  431. end,
  432. --- Changes the quantity.
  433. --
  434. -- @param delta Amount to add. The quantity is kept between 1 and the maximum.
  435. change_quantity = function(self, delta)
  436. local quantity = math.max(1, math.min(self.max_quantity, self.quantity + delta))
  437. if quantity == self.quantity then
  438. UiContainer.play_sound("Error")
  439. return
  440. end
  441. UiContainer.play_sound("Cursor")
  442. self.quantity = quantity
  443. self:populate_quantity()
  444. end,
  445. -----------------------------------------------------------------------------------------------
  446. -- Widgets
  447. -----------------------------------------------------------------------------------------------
  448. --- Fills the quantity window.
  449. populate_quantity = function(self)
  450. local entry = self.entries[self.list_cursor:index()]
  451. local window = "ShopMenu.Container.Quantity."
  452. ui_manager:get_widget(window .. "Name"):set_text(self:entry_data(entry).name)
  453. ui_manager:get_widget(window .. "Count"):set_text(UiContainer.pad_value(self.quantity, 2))
  454. ui_manager:get_widget(window .. "Total"):set_text(
  455. UiContainer.pad_value(entry.price * self.quantity, 8))
  456. self:populate_slots(entry)
  457. end,
  458. --- Shows the materia slots and links of a weapon or armor in the quantity window, as the
  459. -- equipment menu does. Other things have no slots.
  460. populate_slots = function(self, entry)
  461. local data = (not entry.materia) and Game.Items[entry.id] or nil
  462. local slots = {}
  463. local growth = 1
  464. if data ~= nil and (data.type == Inventory.ITEM_TYPE.WEAPON or data.type == Inventory.ITEM_TYPE.ARMOR) then
  465. slots = data.slots or {}
  466. growth = data.growth
  467. end
  468. local window = "ShopMenu.Container.Quantity."
  469. for s = 1, 8 do
  470. local slot = ui_manager:get_widget(window .. "Slot" .. s)
  471. slot:set_visible(#slots >= s)
  472. if growth == 0 then
  473. slot:set_image("icons/materia_slot_no_growth.png")
  474. else
  475. slot:set_image("icons/materia_slot.png")
  476. end
  477. if s % 2 ~= 0 then
  478. -- A link joins an odd slot with the next one.
  479. ui_manager:get_widget(window .. "Link" .. (math.floor(s / 2) + 1)):set_visible(
  480. #slots > s and slots[s] == 1)
  481. end
  482. end
  483. end,
  484. --- Fills the window that shows a materia before it is sold.
  485. populate_materia_sale = function(self, entry)
  486. local data = Game.Materia[entry.id]
  487. local window = "ShopMenu.Container.MateriaSale."
  488. local levels = data.levels_ap or {}
  489. local level = 0
  490. for _, level_ap in ipairs(levels) do
  491. if level_ap > entry.ap then break end
  492. level = level + 1
  493. end
  494. ui_manager:get_widget(window .. "Icon"):set_image("icons/materia_" .. tostring(data.type) .. ".png")
  495. ui_manager:get_widget(window .. "Name"):set_text(data.name)
  496. for star = 1, 5 do
  497. local widget = ui_manager:get_widget(window .. "Star" .. star)
  498. if star > #levels then
  499. widget:set_visible(false)
  500. else
  501. widget:set_image((star <= level and "icons/materia_star_" or "icons/materia_star_empty_")
  502. .. tostring(data.type) .. ".png")
  503. widget:set_visible(true)
  504. end
  505. end
  506. ui_manager:get_widget(window .. "Ap"):set_text(UiContainer.pad_value(entry.ap, 8))
  507. ui_manager:get_widget(window .. "Price"):set_text(UiContainer.pad_value(entry.price, 8))
  508. end,
  509. --- Fills the rows of the list.
  510. populate_list = function(self)
  511. local list = "ShopMenu.Container.List."
  512. for row = 1, self.ROWS do
  513. local entry = self.entries[self.list_cursor.first + row - 1]
  514. local icon = ui_manager:get_widget(list .. "Icon" .. row)
  515. local name = ui_manager:get_widget(list .. "Name" .. row)
  516. local price = ui_manager:get_widget(list .. "Price" .. row)
  517. if entry == nil then
  518. icon:set_visible(false)
  519. name:set_text("")
  520. price:set_text("")
  521. else
  522. icon:set_image(self:entry_icon(entry))
  523. icon:set_visible(true)
  524. name:set_text(self:entry_data(entry).name)
  525. price:set_text(UiContainer.pad_value(entry.price, 7))
  526. -- Things that can't be sold are shown dimmed.
  527. if entry.sellable == false or (self:get_state() == "sell" and not entry.materia and entry.price <= 0) then
  528. name:set_colour(0.6, 0.6, 0.6)
  529. else
  530. name:set_colour(1, 1, 1)
  531. end
  532. end
  533. end
  534. end,
  535. --- Fills the description, the gil, the amounts and the party for the highlighted entry.
  536. populate_details = function(self)
  537. local info = "ShopMenu.Container.Info."
  538. ui_manager:get_widget(info .. "Gil"):set_text(UiContainer.pad_value(Inventory.money, 8))
  539. local entry = self.entries[self.list_cursor:index()]
  540. if self:get_state() == "" or entry == nil then
  541. ui_manager:get_widget("ShopMenu.Container.Help.Help"):set_text("")
  542. ui_manager:get_widget(info .. "Owned"):set_text("")
  543. ui_manager:get_widget(info .. "Equipped"):set_text("")
  544. self:populate_party(nil)
  545. return
  546. end
  547. ui_manager:get_widget("ShopMenu.Container.Help.Help"):set_text(
  548. self:entry_data(entry).description or "")
  549. ui_manager:get_widget(info .. "Owned"):set_text(UiContainer.pad_value(self:owned_count(entry), 8))
  550. ui_manager:get_widget(info .. "Equipped"):set_text(UiContainer.pad_value(self:equipped_count(entry), 8))
  551. self:populate_party(entry)
  552. end,
  553. --- Fills the party window: portraits of every character enabled in the PHS, and how the
  554. -- stats change if the entry is equipped.
  555. --
  556. -- @param entry Highlighted entry, or nil to show only the portraits.
  557. populate_party = function(self, entry)
  558. local data = (entry ~= nil and not entry.materia) and Game.Items[entry.id] or nil
  559. local is_weapon = data ~= nil and data.type == Inventory.ITEM_TYPE.WEAPON
  560. local is_armor = data ~= nil and data.type == Inventory.ITEM_TYPE.ARMOR
  561. -- Each character has the spot of its ID, even if that leaves empty spots between them.
  562. for char_id = 0, self.MAX_CHARACTERS - 1 do
  563. local slot = char_id + 1
  564. local character = Characters[char_id]
  565. if not (type(character) == "table" and character.enabled == true) then
  566. ui_manager:get_widget("ShopMenu.Container.Party.Slot" .. slot):set_visible(false)
  567. else
  568. local widget = "ShopMenu.Container.Party.Slot" .. slot
  569. ui_manager:get_widget(widget):set_visible(true)
  570. ui_manager:get_widget(widget .. ".Portrait"):set_image("characters/" .. tostring(char_id) .. ".png")
  571. local can_use = false
  572. if is_weapon or is_armor then
  573. for _, user in ipairs(data.users or {}) do
  574. if user == char_id then can_use = true end
  575. end
  576. end
  577. local stats = character.stats or {}
  578. local attack, defense = stats.atk or 0, stats.def or 0
  579. local new_attack, new_defense = attack, defense
  580. if can_use and is_weapon then
  581. new_attack = attack - Game.Items[character.weapon.id].power + data.power
  582. elseif can_use and is_armor then
  583. new_defense = defense - Game.Items[character.armor.id].defense.physical
  584. + data.defense.physical
  585. end
  586. self:populate_stat(widget .. ".Atk", can_use, attack, new_attack)
  587. self:populate_stat(widget .. ".Def", can_use, defense, new_defense)
  588. end
  589. end
  590. end,
  591. --- Fills a stat row of a party member.
  592. --
  593. -- @param prefix Path of the row widgets, without the Lbl/Old/Arw/New suffix.
  594. -- @param visible Show the row?
  595. -- @param old Value now.
  596. -- @param new Value if equipped.
  597. populate_stat = function(self, prefix, visible, old, new)
  598. for _, part in ipairs({"Lbl", "Old", "Arw", "New"}) do
  599. ui_manager:get_widget(prefix .. part):set_visible(visible)
  600. end
  601. if not visible then return end
  602. ui_manager:get_widget(prefix .. "Old"):set_text(UiContainer.pad_value(old, 3))
  603. local value = ui_manager:get_widget(prefix .. "New")
  604. value:set_text(UiContainer.pad_value(new, 3))
  605. if new > old then
  606. value:set_colour(1, 1, 0)
  607. elseif new < old then
  608. value:set_colour(1, 0, 0)
  609. else
  610. value:set_colour(1, 1, 1)
  611. end
  612. end,
  613. }
  614. --- Directional buttons of the quantity states.
  615. --
  616. -- Up and down change the quantity by one, left and right by ten.
  617. local function quantity_buttons()
  618. local deltas = {
  619. [Config.CONTROLS.UP] = 1,
  620. [Config.CONTROLS.DOWN] = -1,
  621. [Config.CONTROLS.RIGHT] = 10,
  622. [Config.CONTROLS.LEFT] = -10,
  623. }
  624. local buttons = {}
  625. for button, delta in pairs(deltas) do
  626. buttons[button] = function(self, event)
  627. if event == "Press" or event == "Repeat" then self:change_quantity(delta) end
  628. end
  629. end
  630. return buttons
  631. end
  632. UiContainer.ShopMenu.states.buy_quantity.buttons = quantity_buttons()
  633. UiContainer.ShopMenu.states.sell_quantity.buttons = quantity_buttons()