save_menu.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. if UiContainer == nil then UiContainer = {} end
  2. --- The item menu.
  3. UiContainer.SaveMenu = {
  4. --- Current operation ("save/load")
  5. operation = "save",
  6. --- Current cursor position in the top bar (1-3).
  7. position = 1,
  8. --- Max cursor position in the top bar.
  9. position_total = 3,
  10. --- Maximum scroll position.
  11. max_slots = 15,
  12. --- First visible save slot.
  13. first_visible = 1,
  14. --- Run when the menu is creaded.
  15. --
  16. -- It does nothing.
  17. on_start = function(self)
  18. return 0
  19. end,
  20. --- Handles button events.
  21. --
  22. -- For the current submenu, handles directional keys, enter and escape events.
  23. -- @param button Pressed button string key. "Up", "Left", "Enter" and "Escape" are handled.
  24. -- @param event Trigger event. Normally, "Press".
  25. on_button = function(self, button, event)
  26. if UiContainer.current_menu == "save" then
  27. if UiContainer.current_submenu == "" then
  28. if button == "Escape" and event == "Press" then
  29. audio_manager:play_sound("Back")
  30. script:request_end_sync(Script.UI, "SaveMenu", "hide", 0)
  31. elseif button == "Enter" then
  32. self.action(self, self.first_visible - 1 + self.position)
  33. elseif button == "Down" then
  34. if self.position < self.position_total and self.first_visible + self.position < self.max_slots then
  35. -- Move cursor down
  36. self.position = self.position + 1
  37. ui_manager:get_widget("SaveMenu.Container.Cursor"):set_default_animation("Position" .. self.position)
  38. audio_manager:play_sound("Cursor")
  39. elseif self.position == self.position_total and self.first_visible + self.position < self.max_slots then
  40. self.first_visible = self.first_visible + 1
  41. self.populate_slots(self)
  42. audio_manager:play_sound("Cursor")
  43. else
  44. audio_manager:play_sound("Error")
  45. end
  46. elseif button == "Up" then
  47. if self.position > 1 then
  48. -- Move cursor up
  49. self.position = self.position - 1
  50. ui_manager:get_widget("SaveMenu.Container.Cursor"):set_default_animation("Position" .. self.position)
  51. audio_manager:play_sound("Cursor")
  52. elseif self.position == 1 and self.first_visible > 1 then
  53. self.first_visible = self.first_visible - 1
  54. self.populate_slots(self)
  55. audio_manager:play_sound("Cursor")
  56. else
  57. audio_manager:play_sound("Error")
  58. end
  59. end
  60. elseif UiContainer.current_submenu == "confirm" then
  61. end
  62. end
  63. return 0
  64. end,
  65. --- Opens the save menu.
  66. --
  67. -- Populates and updates displayed data before opening.
  68. show = function(self)
  69. if self.operation ~= "save" and self.operation ~= "load" then
  70. print("Error: Tried to open the save menu without setting an operation.")
  71. print("Do:")
  72. print(" UiContainer.SaveMenu.operation = \"save\"")
  73. print("or")
  74. print(" UiContainer.SaveMenu.operation = \"load\"")
  75. print("before calling show()")
  76. return 0
  77. end
  78. UiContainer.current_menu = "save"
  79. UiContainer.current_submenu = ""
  80. if self.operation == "load" then
  81. ui_manager:get_widget("SaveMenu.Container.TopBar.DescSave"):set_visible(false)
  82. ui_manager:get_widget("SaveMenu.Container.TopBar.DescLoad"):set_visible(true)
  83. ui_manager:get_widget("SaveMenu.Container.Title.TitleSave"):set_visible(false)
  84. ui_manager:get_widget("SaveMenu.Container.Title.TitleLoad"):set_visible(true)
  85. else
  86. ui_manager:get_widget("SaveMenu.Container.TopBar.DescSave"):set_visible(true)
  87. ui_manager:get_widget("SaveMenu.Container.TopBar.DescLoad"):set_visible(false)
  88. ui_manager:get_widget("SaveMenu.Container.Title.TitleSave"):set_visible(true)
  89. ui_manager:get_widget("SaveMenu.Container.Title.TitleLoad"):set_visible(false)
  90. end
  91. ui_manager:get_widget("SaveMenu"):set_visible(true)
  92. UiContainer.current_menu = "save"
  93. self.position = 1
  94. self.first_visible = 1
  95. ui_manager:get_widget("SaveMenu.Container.Cursor"):set_default_animation("Position" .. self.position)
  96. self.populate_slots(self)
  97. return 0;
  98. end,
  99. populate_slots = function(self)
  100. for s = 1, 3 do
  101. local slot = self.first_visible + s - 2 -- 0-index
  102. if savemap_manager:is_slot_empty(slot) == true then
  103. ui_manager:get_widget("SaveMenu.Container.Slot" .. tostring(s) .. ".Slot"):set_visible(false)
  104. ui_manager:get_widget("SaveMenu.Container.Slot" .. tostring(s) .. ".Empty"):set_visible(true)
  105. else
  106. local slot_id = "SaveMenu.Container.Slot" .. tostring(s) .. ".Slot."
  107. ui_manager:get_widget(slot_id .. "TimeMoney.Money"):set_text(UiContainer.pad_value(savemap_manager:get_slot_money(slot), 7))
  108. local seconds = savemap_manager:get_slot_game_time(slot)
  109. local time = UiContainer.pad_value(math.floor(seconds / 3600), 3) .. ":" .. UiContainer.zero_pad_value(math.floor((seconds % 3600) / 60), 2)
  110. ui_manager:get_widget(slot_id .. "TimeMoney.Time"):set_text(time)
  111. ui_manager:get_widget(slot_id .. "Location.Name"):set_text(savemap_manager:get_slot_location_name(slot))
  112. local save_party = {
  113. [1] = savemap_manager:get_slot_party_member(slot, 0),
  114. [2] = savemap_manager:get_slot_party_member(slot, 1),
  115. [3] = savemap_manager:get_slot_party_member(slot, 2)
  116. }
  117. ui_manager:get_widget(slot_id .. "Name"):set_text(savemap_manager:get_slot_character_name(slot, save_party[1]))
  118. ui_manager:get_widget(slot_id .. "LvNumber"):set_text(UiContainer.pad_value(savemap_manager:get_slot_character_level(slot, save_party[1]), 3))
  119. for c = 1, 3 do
  120. if save_party[c] ~= -1 and Characters[save_party[c] + 1] ~= nil then
  121. ui_manager:get_widget(slot_id .. "Party.Portrait" .. tostring(c)):set_image("characters/" .. tostring(save_party[c]) .. ".png")
  122. ui_manager:get_widget(slot_id .. "Party.Portrait" .. tostring(c)):set_visible(true)
  123. else
  124. ui_manager:get_widget(slot_id .. "Party.Portrait" .. tostring(c)):set_visible(false)
  125. end
  126. end
  127. ui_manager:get_widget("SaveMenu.Container.Slot" .. tostring(s) .. ".Slot"):set_visible(true)
  128. ui_manager:get_widget("SaveMenu.Container.Slot" .. tostring(s) .. ".Empty"):set_visible(false)
  129. end
  130. end
  131. end,
  132. action = function(self, slot)
  133. if self.operation == "save" then
  134. if savemap_manager:is_slot_empty(slot - 1) == false and ControlKey ~= savemap_manager:get_slot_control_key(slot - 1) then
  135. -- TODO: Confirmation dialog
  136. print("OVERRIDE ANOTHER SAVE")
  137. save_game(slot - 1, true)
  138. audio_manager:play_sound("Cursor")
  139. self.populate_slots(self)
  140. else
  141. save_game(slot - 1, true)
  142. audio_manager:play_sound("Cursor")
  143. self.populate_slots(self)
  144. end
  145. elseif self.operation == "load" then
  146. audio_manager:play_sound("Window")
  147. script:request_end_sync(Script.UI, "SaveMenu", "hide", 0)
  148. script:request_end_sync(Script.UI, "BeginMenu", "hide", 0)
  149. load_game(slot - 1)
  150. end
  151. end,
  152. --- Hides the item menu and goes back to the main menu.
  153. hide = function(self)
  154. ui_manager:get_widget("SaveMenu"):set_visible(false)
  155. if self.operation == "load" then
  156. UiContainer.current_menu = "begin"
  157. else
  158. UiContainer.current_menu = "main"
  159. end
  160. UiContainer.current_submenu = ""
  161. savemap_manager:release()
  162. return 0;
  163. end,
  164. }