save_menu.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. if UiContainer == nil then UiContainer = {} end
  2. --- The save/load menu.
  3. UiContainer.SaveMenu = UiContainer.Menu.new{
  4. id = "save",
  5. --- Current operation ("save/load")
  6. operation = "save",
  7. --- Maximum number of save slots.
  8. max_slots = 15,
  9. --- Cursor over the three visible slots, scrolling over all of them.
  10. cursor = UiContainer.ListCursor.new{
  11. widget = "SaveMenu.Container.Cursor",
  12. rows = 3,
  13. count = 15,
  14. on_scroll = function() UiContainer.SaveMenu:populate_slots() end,
  15. },
  16. --- Cursors over Yes and No of the confirmation windows.
  17. confirm_cursor = {
  18. same = UiContainer.Cursor.new{widget = "SaveMenu.Container.ConfirmSame.Cursor", total = 2},
  19. other = UiContainer.Cursor.new{widget = "SaveMenu.Container.ConfirmOther.Cursor", total = 2},
  20. },
  21. --- Slot (1-based) waiting for the confirmation, and which window is open ("same" or "other").
  22. pending_slot = nil,
  23. confirm_kind = "same",
  24. states = {
  25. --- Confirmation to override a slot. Yes is the default unless the slot belongs to another
  26. -- playthrough.
  27. confirm_save = {
  28. cursor = function(self) return self.confirm_cursor[self.confirm_kind] end,
  29. axis = "vertical",
  30. confirm_sound = false,
  31. confirm = function(self, cursor)
  32. if cursor:index() == 1 then
  33. self:save_slot(self.pending_slot)
  34. else
  35. self:close_confirm()
  36. UiContainer.play_sound("Back")
  37. end
  38. end,
  39. cancel = function(self) self:close_confirm() end,
  40. },
  41. --- "Game saved". Confirm or cancel close it.
  42. saved = {
  43. confirm_sound = false,
  44. cancel_sound = false,
  45. confirm = function(self)
  46. self:close_saved()
  47. UiContainer.play_sound("Back")
  48. end,
  49. cancel = function(self)
  50. self:close_saved()
  51. UiContainer.play_sound("Back")
  52. end,
  53. },
  54. [""] = {
  55. cursor = function(self) return self.cursor end,
  56. axis = "vertical",
  57. cancel = function(self)
  58. script:request_end_sync(Script.UI, "SaveMenu", "hide", 0)
  59. end,
  60. -- The action plays its own sound.
  61. confirm_sound = false,
  62. confirm = function(self, cursor)
  63. self:action(cursor:index())
  64. end,
  65. },
  66. },
  67. --- Is the menu opened by a field script (a save point of a scene) instead of by the main menu?
  68. standalone = false,
  69. --- Opens the save menu.
  70. --
  71. -- Populates and updates displayed data before opening.
  72. show = function(self)
  73. if self.operation ~= "save" and self.operation ~= "load" then
  74. print("Error: Tried to open the save menu without setting an operation.")
  75. print("Do:")
  76. print(" UiContainer.SaveMenu.operation = \"save\"")
  77. print("or")
  78. print(" UiContainer.SaveMenu.operation = \"load\"")
  79. print("before calling show()")
  80. return 0
  81. end
  82. self:activate()
  83. if self.standalone then
  84. entity_manager:set_paused(true)
  85. MenuSettings.pause_available = false
  86. self.menu_was_available = MenuSettings.available
  87. MenuSettings.available = false
  88. end
  89. local loading = (self.operation == "load")
  90. local container = "SaveMenu.Container."
  91. ui_manager:get_widget(container .. "TopBar.DescSave"):set_visible(not loading)
  92. ui_manager:get_widget(container .. "TopBar.DescLoad"):set_visible(loading)
  93. ui_manager:get_widget(container .. "Title.TitleSave"):set_visible(not loading)
  94. ui_manager:get_widget(container .. "Title.TitleLoad"):set_visible(loading)
  95. ui_manager:get_widget("SaveMenu.Container.ConfirmSame"):set_visible(false)
  96. ui_manager:get_widget("SaveMenu.Container.ConfirmOther"):set_visible(false)
  97. ui_manager:get_widget("SaveMenu.Container.Saved"):set_visible(false)
  98. ui_manager:get_widget("SaveMenu"):set_visible(true)
  99. self.cursor.count = self.max_slots
  100. self.cursor:reset()
  101. self:populate_slots()
  102. return 0;
  103. end,
  104. populate_slots = function(self)
  105. for s = 1, 3 do
  106. local slot = self.cursor.first + s - 2 -- 0-index
  107. if savemap_manager:is_slot_empty(slot) == true then
  108. ui_manager:get_widget("SaveMenu.Container.Slot" .. tostring(s) .. ".Slot"):set_visible(false)
  109. ui_manager:get_widget("SaveMenu.Container.Slot" .. tostring(s) .. ".Empty"):set_visible(true)
  110. else
  111. local slot_id = "SaveMenu.Container.Slot" .. tostring(s) .. ".Slot."
  112. ui_manager:get_widget(slot_id .. "TimeMoney.Money"):set_text(UiContainer.format_money(savemap_manager:get_slot_money(slot), 7))
  113. local seconds = savemap_manager:get_slot_game_time(slot)
  114. local time = UiContainer.format_time(seconds)
  115. ui_manager:get_widget(slot_id .. "TimeMoney.Time"):set_text(time)
  116. ui_manager:get_widget(slot_id .. "Location.Name"):set_text(savemap_manager:get_slot_location_name(slot))
  117. local save_party = {
  118. [1] = savemap_manager:get_slot_party_member(slot, 0),
  119. [2] = savemap_manager:get_slot_party_member(slot, 1),
  120. [3] = savemap_manager:get_slot_party_member(slot, 2)
  121. }
  122. ui_manager:get_widget(slot_id .. "Name"):set_text(savemap_manager:get_slot_character_name(slot, save_party[1]))
  123. ui_manager:get_widget(slot_id .. "LvNumber"):set_text(UiContainer.pad_value(savemap_manager:get_slot_character_level(slot, save_party[1]), 3))
  124. for c = 1, 3 do
  125. if save_party[c] ~= -1 and Characters[save_party[c] + 1] ~= nil then
  126. ui_manager:get_widget(slot_id .. "Party.Portrait" .. tostring(c)):set_image("characters/" .. tostring(save_party[c]) .. ".png")
  127. ui_manager:get_widget(slot_id .. "Party.Portrait" .. tostring(c)):set_visible(true)
  128. else
  129. ui_manager:get_widget(slot_id .. "Party.Portrait" .. tostring(c)):set_visible(false)
  130. end
  131. end
  132. ui_manager:get_widget("SaveMenu.Container.Slot" .. tostring(s) .. ".Slot"):set_visible(true)
  133. ui_manager:get_widget("SaveMenu.Container.Slot" .. tostring(s) .. ".Empty"):set_visible(false)
  134. end
  135. end
  136. end,
  137. action = function(self, slot)
  138. if self.operation == "save" then
  139. if savemap_manager:is_slot_empty(slot - 1) == true then
  140. -- Nothing to override.
  141. self:save_slot(slot)
  142. return
  143. end
  144. -- Ask first. A slot of another playthrough is not overridden unless the player says so.
  145. self.pending_slot = slot
  146. local other = (ControlKey ~= savemap_manager:get_slot_control_key(slot - 1))
  147. self.confirm_kind = other and "other" or "same"
  148. ui_manager:get_widget("SaveMenu.Container.ConfirmSame"):set_visible(not other)
  149. ui_manager:get_widget("SaveMenu.Container.ConfirmOther"):set_visible(other)
  150. self.confirm_cursor[self.confirm_kind]:set(other and 2 or 1)
  151. self:set_state("confirm_save")
  152. audio_manager:play_sound("Cursor")
  153. elseif self.operation == "load" then
  154. audio_manager:play_sound("Window")
  155. script:request_end_sync(Script.UI, "SaveMenu", "hide", 0)
  156. script:request_end_sync(Script.UI, "BeginMenu", "hide", 0)
  157. load_game(slot - 1)
  158. end
  159. end,
  160. --- Closes the confirmation window without saving.
  161. close_confirm = function(self)
  162. ui_manager:get_widget("SaveMenu.Container.ConfirmSame"):set_visible(false)
  163. ui_manager:get_widget("SaveMenu.Container.ConfirmOther"):set_visible(false)
  164. self.pending_slot = nil
  165. self:set_state("")
  166. end,
  167. --- Saves the game in a slot and shows the "Game saved" window.
  168. --
  169. -- @param slot Slot to save in (1-based).
  170. save_slot = function(self, slot)
  171. ui_manager:get_widget("SaveMenu.Container.ConfirmSame"):set_visible(false)
  172. ui_manager:get_widget("SaveMenu.Container.ConfirmOther"):set_visible(false)
  173. self.pending_slot = nil
  174. save_game(slot - 1, true)
  175. audio_manager:play_sound("Cursor")
  176. self:populate_slots()
  177. ui_manager:get_widget("SaveMenu.Container.Saved"):set_visible(true)
  178. self:set_state("saved")
  179. end,
  180. --- Closes the "Game saved" window.
  181. close_saved = function(self)
  182. ui_manager:get_widget("SaveMenu.Container.Saved"):set_visible(false)
  183. self:set_state("")
  184. end,
  185. --- Hides the item menu and goes back to the main menu.
  186. hide = function(self)
  187. ui_manager:get_widget("SaveMenu"):set_visible(false)
  188. if self.standalone then
  189. self.standalone = false
  190. self:deactivate("")
  191. MenuSettings.available = self.menu_was_available
  192. MenuSettings.pause_available = true
  193. entity_manager:set_paused(false)
  194. else
  195. self:deactivate(self.operation == "load" and "begin" or "main")
  196. end
  197. savemap_manager:release()
  198. return 0;
  199. end,
  200. }