shop_menu.lua 24 KB

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