status_menu.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. if UiContainer == nil then UiContainer = {} end
  2. local ROOT = "StatusMenu.Container."
  3. --- Inside width of the bars of the header.
  4. local BAR_WIDTH = 54
  5. --- Colour of the names of the elements and statuses that apply, and of those that don't.
  6. local COLOUR_ACTIVE = { 1.0, 1.0, 1.0 }
  7. local COLOUR_INACTIVE = { 0.45, 0.45, 0.35 }
  8. --- Widgets of the first page with a number, and where its value comes from.
  9. local NUMBERS = {
  10. { "Page1.Attributes.Str", function(c) return c.stats.str.total end },
  11. { "Page1.Attributes.Dex", function(c) return c.stats.dex.total end },
  12. { "Page1.Attributes.Vit", function(c) return c.stats.vit.total end },
  13. { "Page1.Attributes.Mag", function(c) return c.stats.mag.total end },
  14. { "Page1.Attributes.Spr", function(c) return c.stats.spr.total end },
  15. { "Page1.Attributes.Lck", function(c) return c.stats.lck.total end },
  16. { "Page1.Attributes.Atk", function(c) return c.stats.atk end },
  17. { "Page1.Attributes.AtkPct", function(c) return c.stats.acc end },
  18. { "Page1.Attributes.Def", function(c) return c.stats.def end },
  19. { "Page1.Attributes.DefPct", function(c) return c.stats.eva end },
  20. { "Page1.Attributes.MAtk", function(c) return c.stats.matk end },
  21. { "Page1.Attributes.MDef", function(c) return c.stats.mdef end },
  22. { "Page1.Attributes.MDefPct", function(c) return c.stats.meva end },
  23. { "Character.Exp", function(c) return c.exp end, 8 },
  24. { "Character.Next", function(c) return c.exp_to_next end, 8 },
  25. { "Character.LimitLevel", function(c) return c.limit.current end, 2 },
  26. }
  27. --- Elements of the second page, in the order of the widgets.
  28. local ELEMENTS = {
  29. { Characters.ELEMENT.FIRE, "Fire" }, { Characters.ELEMENT.ICE, "Ice" },
  30. { Characters.ELEMENT.BOLT, "Lightning" }, { Characters.ELEMENT.EARTH, "Earth" },
  31. { Characters.ELEMENT.POISON, "Poison" }, { Characters.ELEMENT.GRAVITY, "Gravity" },
  32. { Characters.ELEMENT.WATER, "Water" }, { Characters.ELEMENT.WIND, "Wind" },
  33. { Characters.ELEMENT.HOLY, "Holy" },
  34. }
  35. --- Statuses of the third page, in the order of the widgets.
  36. local STATUSES = {
  37. { Characters.STATUS.DEATH, "Death" }, { Characters.STATUS.NEAR_DEATH, "Near-death" },
  38. { Characters.STATUS.SLEEP, "Sleep" }, { Characters.STATUS.POISON, "Poison" },
  39. { Characters.STATUS.SADNESS, "Sadness" }, { Characters.STATUS.FURY, "Fury" },
  40. { Characters.STATUS.CONFUSION, "Confusion" }, { Characters.STATUS.SILENCE, "Silence" },
  41. { Characters.STATUS.HASTE, "Haste" }, { Characters.STATUS.SLOW, "Slow" },
  42. { Characters.STATUS.STOP, "Stop" }, { Characters.STATUS.FROG, "Frog" },
  43. { Characters.STATUS.SMALL, "Small" }, { Characters.STATUS.SLOW_NUMB, "Slow-numb" },
  44. { Characters.STATUS.PETRIFY, "Petrify" }, { Characters.STATUS.REGEN, "Regen" },
  45. { Characters.STATUS.BARRIER, "Barrier" }, { Characters.STATUS.MBARRIER, "MBarrier" },
  46. { Characters.STATUS.REFLECT, "Reflect" }, { Characters.STATUS.SHIELD, "Shield" },
  47. { Characters.STATUS.D_SENTENCE, "Death-sentence" }, { Characters.STATUS.MANIPULATE, "Manipulate" },
  48. { Characters.STATUS.BERSERK, "Berserk" }, { Characters.STATUS.PEERLESS, "Peerless" },
  49. { Characters.STATUS.PARALYZED, "Paralyzed" }, { Characters.STATUS.DARKNESS, "Darkness" },
  50. }
  51. --- Sets a name, white if it applies, gray otherwise.
  52. local function set_name(path, name, active)
  53. local widget = ui_manager:get_widget(path)
  54. local colour = active and COLOUR_ACTIVE or COLOUR_INACTIVE
  55. -- The text is set after the colour to apply it.
  56. widget:set_colour(colour[1], colour[2], colour[3])
  57. widget:set_text(name)
  58. end
  59. --- The status menu. Purely informative.
  60. --
  61. -- It has three pages that share the character data: attributes and equipment, elemental effects,
  62. -- and status effects. OK goes to the next page, looping, and Cancel closes the menu.
  63. UiContainer.StatusMenu = UiContainer.Menu.new{
  64. id = "status",
  65. parent = "main",
  66. --- ID of the character being shown.
  67. char_id = -1,
  68. --- Page being shown (1-3).
  69. page = 1,
  70. --- Total number of pages.
  71. page_total = 3,
  72. states = {
  73. [""] = {
  74. confirm = function(self)
  75. self:show_page(self.page % self.page_total + 1)
  76. end,
  77. cancel = function(self)
  78. script:request_end_sync(Script.UI, "StatusMenu", "hide", 0)
  79. end,
  80. },
  81. },
  82. --- Shows a page and hides the others.
  83. show_page = function(self, page)
  84. self.page = page
  85. for p = 1, self.page_total do
  86. ui_manager:get_widget(ROOT .. "Page" .. tostring(p)):set_visible(p == page)
  87. end
  88. end,
  89. --- Shows the data shared by every page.
  90. populate_header = function(self, character)
  91. UiContainer.populate_character_data(ROOT .. "Character", character)
  92. ui_manager:get_widget(ROOT .. "Character.Portrait"):set_image("characters/" .. tostring(self.char_id) .. ".png")
  93. UiContainer.set_bar(ROOT .. "Character.NextLevelLinePart", UiContainer.exp_progress(character), BAR_WIDTH)
  94. UiContainer.set_limit_bar(ROOT .. "Character.LimitLinePart", character, BAR_WIDTH)
  95. end,
  96. --- Shows the slots and materia of a weapon or armor.
  97. --
  98. -- @param prefix Prefix of the widgets: "Wpn" or "Arm".
  99. -- @param item Definition of the weapon or armor.
  100. -- @param materia Materia equipped on it. Slots 0-7.
  101. populate_slots = function(self, prefix, item, materia)
  102. local path = ROOT .. "Page1.Equipment." .. prefix .. ".Slots."
  103. for slot = 1, 8 do
  104. ui_manager:get_widget(path .. "Slot" .. tostring(slot)):set_visible(#(item.slots) >= slot)
  105. local equipped = materia[slot - 1]
  106. local widget = ui_manager:get_widget(path .. "Mat" .. tostring(slot))
  107. if equipped ~= nil and Game.Materia[equipped.id] ~= nil then
  108. widget:set_image("icons/materia_" .. tostring(Game.Materia[equipped.id].type) .. ".png")
  109. widget:set_visible(true)
  110. else
  111. widget:set_visible(false)
  112. end
  113. -- At every odd slot, if there is a next slot and it's linked, show the linker.
  114. if slot % 2 ~= 0 then
  115. local linked = #(item.slots) > slot and item.slots[slot] == 1
  116. ui_manager:get_widget(path .. "Link" .. tostring(math.floor(slot / 2) + 1)):set_visible(linked)
  117. end
  118. end
  119. end,
  120. --- Shows the attributes and the equipment.
  121. populate_page_1 = function(self, character)
  122. for _, entry in ipairs(NUMBERS) do
  123. local value = entry[2](character)
  124. ui_manager:get_widget(ROOT .. entry[1]):set_text(UiContainer.pad_value(value or 0, entry[3] or 4))
  125. end
  126. local equipment = ROOT .. "Page1.Equipment."
  127. local weapon = Game.Items[character.weapon.id]
  128. local armor = Game.Items[character.armor.id]
  129. ui_manager:get_widget(equipment .. "Wpn.Lbl"):set_text(weapon.name)
  130. ui_manager:get_widget(equipment .. "Arm.Lbl"):set_text(armor.name)
  131. if character.accessory ~= nil then
  132. ui_manager:get_widget(equipment .. "Acc.Lbl"):set_text(Game.Items[character.accessory].name)
  133. else
  134. ui_manager:get_widget(equipment .. "Acc.Lbl"):set_text("")
  135. end
  136. ui_manager:get_widget(equipment .. "Wpn.Icon"):set_image("icons/item_weapon_" .. tostring(self.char_id) .. ".png")
  137. self:populate_slots("Wpn", weapon, character.weapon.materia)
  138. self:populate_slots("Arm", armor, character.armor.materia)
  139. end,
  140. --- Shows the elements the attacks have, and the elements that are halved, nullified or
  141. -- absorbed.
  142. populate_page_2 = function(self, effects)
  143. local mode = Characters.ELEMENT_MODE
  144. local groups = {
  145. { "Attack", function(element) return effects.elements.attack[element] == true end },
  146. { "Halve", function(element) return effects.elements.defense[element] == mode.HALF end },
  147. { "Invalid", function(element) return effects.elements.defense[element] == mode.NULLIFY end },
  148. { "Absorb", function(element) return effects.elements.defense[element] == mode.ABSORB end },
  149. }
  150. for _, group in ipairs(groups) do
  151. for index, element in ipairs(ELEMENTS) do
  152. set_name(ROOT .. "Page2.Table." .. group[1] .. ".Name" .. tostring(index), element[2], group[2](element[1]))
  153. end
  154. end
  155. end,
  156. --- Shows the statuses the attacks inflict, and the ones the character is protected from.
  157. populate_page_3 = function(self, effects)
  158. local groups = {
  159. { "Attack", effects.status.attack },
  160. { "Defend", effects.status.defense },
  161. }
  162. for _, group in ipairs(groups) do
  163. for index, status in ipairs(STATUSES) do
  164. set_name(ROOT .. "Page3.Table." .. group[1] .. ".Name" .. tostring(index), status[2], group[2][status[1]] == true)
  165. end
  166. end
  167. end,
  168. --- Shows the data of the current character in every page.
  169. populate = function(self)
  170. local character = Characters[self.char_id]
  171. local effects = Characters.get_effects(self.char_id)
  172. self:populate_header(character)
  173. self:populate_page_1(character)
  174. self:populate_page_2(effects)
  175. self:populate_page_3(effects)
  176. end,
  177. --- Opens the status menu for UiContainer.current_character_id.
  178. show = function(self)
  179. self.char_id = UiContainer.current_character_id
  180. self:activate()
  181. self:populate()
  182. self:show_page(1)
  183. ui_manager:get_widget("StatusMenu"):set_visible(true)
  184. return 0
  185. end,
  186. --- Hides the status menu and goes back to the main menu.
  187. hide = function(self)
  188. ui_manager:get_widget("StatusMenu"):set_visible(false)
  189. self:deactivate()
  190. return 0
  191. end,
  192. }