def __show_load_game(self): ret = self.show_select_savegame(mode='mp_load') if ret is None: # user aborted return path, gamename, gamepassword = ret # get name from path paths, names = SavegameManager.get_multiplayersaves() mapname = names[paths.index(path)] self.__create_game(load=(mapname, gamename, gamepassword))
def show_select_savegame(self, mode, sanity_checker=None, sanity_criteria=None): """Shows menu to select a savegame. @param mode: 'save', 'load' or 'mp_load' @param sanity_checker: only allow manually entered names that pass this test @param sanity_criteria: explain which names are allowed to the user @return: Path to savegamefile or None""" assert mode in ('save', 'load', 'mp_load', 'mp_save') map_files, map_file_display = None, None mp = False args = mode, sanity_checker, sanity_criteria # for reshow if mode.startswith('mp'): mode = mode[3:] mp = True # below this line, mp_load == load, mp_save == save if mode == 'load': if not mp: map_files, map_file_display = SavegameManager.get_saves() else: map_files, map_file_display = SavegameManager.get_multiplayersaves() if len(map_files) == 0: self.show_popup(_("No saved games"), _("There are no saved games to load.")) return else: # don't show autosave and quicksave on save if not mp: map_files, map_file_display = SavegameManager.get_regular_saves() else: map_files, map_file_display = SavegameManager.get_multiplayersaves() # Prepare widget old_current = self._switch_current_widget('select_savegame') self.current.findChild(name='headline').text = _('Save game') if mode == 'save' else _('Load game') self.current.findChild(name=OkButton.DEFAULT_NAME).helptext = _('Save game') if mode == 'save' else _('Load game') name_box = self.current.findChild(name="gamename_box") password_box = self.current.findChild(name="gamepassword_box") if mp and mode == 'load': # have gamename name_box.parent.showChild(name_box) password_box.parent.showChild(password_box) gamename_textfield = self.current.findChild(name="gamename") gamepassword_textfield = self.current.findChild(name="gamepassword") gamepassword_textfield.text = u"" def clear_gamedetails_textfields(): gamename_textfield.text = u"" gamepassword_textfield.text = u"" gamename_textfield.capture(clear_gamedetails_textfields, 'mouseReleased', 'default') else: if name_box not in name_box.parent.hidden_children: name_box.parent.hideChild(name_box) if password_box not in name_box.parent.hidden_children: password_box.parent.hideChild(password_box) self.current.show() if not hasattr(self, 'filename_hbox'): self.filename_hbox = self.current.findChild(name='enter_filename') self.filename_hbox_parent = self.filename_hbox._getParent() if mode == 'save': # only show enter_filename on save self.filename_hbox_parent.showChild(self.filename_hbox) elif self.filename_hbox not in self.filename_hbox_parent.hidden_children: self.filename_hbox_parent.hideChild(self.filename_hbox) def tmp_selected_changed(): """Fills in the name of the savegame in the textbox when selected in the list""" if mode == 'save': # set textbox only if we are in save mode if self.current.collectData('savegamelist') == -1: # set blank if nothing is selected self.current.findChild(name="savegamefile").text = u"" else: self.current.distributeData({'savegamefile' : \ map_file_display[self.current.collectData('savegamelist')]}) self.current.distributeInitialData({'savegamelist' : map_file_display}) self.current.distributeData({'savegamelist' : -1}) # Don't select anything by default cb = Callback.ChainedCallbacks(Gui._create_show_savegame_details(self.current, map_files, 'savegamelist'), \ tmp_selected_changed) cb() # Refresh data on start self.current.findChild(name="savegamelist").mapEvents({ 'savegamelist/action' : cb }) self.current.findChild(name="savegamelist").capture(cb, event_name="keyPressed") bind = { OkButton.DEFAULT_NAME : True, CancelButton.DEFAULT_NAME : False, DeleteButton.DEFAULT_NAME : 'delete' } if mode == 'save': bind['savegamefile'] = True retval = self.show_dialog(self.current, bind) if not retval: # cancelled self.current = old_current return if retval == 'delete': # delete button was pressed. Apply delete and reshow dialog, delegating the return value delete_retval = self._delete_savegame(map_files) if delete_retval: self.current.distributeData({'savegamelist' : -1}) cb() self.current = old_current return self.show_select_savegame(*args) selected_savegame = None if mode == 'save': # return from textfield selected_savegame = self.current.collectData('savegamefile') if selected_savegame == "": self.show_error_popup(windowtitle=_("No filename given"), description=_("Please enter a valid filename.")) self.current = old_current return self.show_select_savegame(*args) # reshow dialog elif selected_savegame in map_file_display: # savegamename already exists #xgettext:python-format message = _("A savegame with the name '{name}' already exists.").format( name=selected_savegame) + u"\n" + _('Overwrite it?') if not self.show_popup(_("Confirmation for overwriting"), message, show_cancel_button=True): self.current = old_current return self.show_select_savegame(*args) # reshow dialog elif sanity_checker and sanity_criteria: if not sanity_checker(selected_savegame): self.show_error_popup(windowtitle=_("Invalid filename given"), description=sanity_criteria) self.current = old_current return self.show_select_savegame(*args) # reshow dialog else: # return selected item from list selected_savegame = self.current.collectData('savegamelist') selected_savegame = None if selected_savegame == -1 else map_files[selected_savegame] if selected_savegame is None: # ok button has been pressed, but no savegame was selected self.show_popup(_("Select a savegame"), _("Please select a savegame or click on cancel.")) self.current = old_current return self.show_select_savegame(*args) # reshow dialog if mp and mode == 'load': # also name gamename_textfield = self.current.findChild(name="gamename") ret = selected_savegame, self.current.collectData('gamename'), self.current.collectData('gamepassword') else: ret = selected_savegame self.current = old_current # reuse old widget return ret
def show_select_savegame(self, mode, sanity_checker=None, sanity_criteria=None): """Shows menu to select a savegame. @param mode: Valid options are 'save', 'load', 'mp_load', 'mp_save' @param sanity_checker: only allow manually entered names that pass this test @param sanity_criteria: explain which names are allowed to the user @return: Path to savegamefile or None""" assert mode in ("save", "load", "mp_load", "mp_save") map_files, map_file_display = None, None args = mode, sanity_checker, sanity_criteria # for reshow mp = mode.startswith("mp_") if mp: mode = mode[3:] # below this line, mp_load == load, mp_save == save if mode == "load": if not mp: map_files, map_file_display = SavegameManager.get_saves() else: map_files, map_file_display = SavegameManager.get_multiplayersaves() if not map_files: self.show_popup(_("No saved games"), _("There are no saved games to load.")) return else: # don't show autosave and quicksave on save if not mp: map_files, map_file_display = SavegameManager.get_regular_saves() else: map_files, map_file_display = SavegameManager.get_multiplayersaves() # Prepare widget old_current = self._switch_current_widget("select_savegame") if mode == "save": helptext = _("Save game") elif mode == "load": helptext = _("Load game") # else: not a valid mode, so we can as well crash on the following self.current.findChild(name="headline").text = helptext self.current.findChild(name=OkButton.DEFAULT_NAME).helptext = helptext name_box = self.current.findChild(name="gamename_box") password_box = self.current.findChild(name="gamepassword_box") if mp and mode == "load": # have gamename name_box.parent.showChild(name_box) password_box.parent.showChild(password_box) gamename_textfield = self.current.findChild(name="gamename") gamepassword_textfield = self.current.findChild(name="gamepassword") gamepassword_textfield.text = u"" def clear_gamedetails_textfields(): gamename_textfield.text = u"" gamepassword_textfield.text = u"" gamename_textfield.capture(clear_gamedetails_textfields, "mouseReleased", "default") else: if name_box not in name_box.parent.hidden_children: name_box.parent.hideChild(name_box) if password_box not in name_box.parent.hidden_children: password_box.parent.hideChild(password_box) self.current.show() if not hasattr(self, "filename_hbox"): self.filename_hbox = self.current.findChild(name="enter_filename") self.filename_hbox_parent = self.filename_hbox.parent if mode == "save": # only show enter_filename on save self.filename_hbox_parent.showChild(self.filename_hbox) elif self.filename_hbox not in self.filename_hbox_parent.hidden_children: self.filename_hbox_parent.hideChild(self.filename_hbox) def tmp_selected_changed(): """Fills in the name of the savegame in the textbox when selected in the list""" if mode != "save": # set textbox only if we are in save mode return if self.current.collectData("savegamelist") == -1: # set blank if nothing is selected self.current.findChild(name="savegamefile").text = u"" else: savegamefile = map_file_display[self.current.collectData("savegamelist")] self.current.distributeData({"savegamefile": savegamefile}) self.current.distributeInitialData({"savegamelist": map_file_display}) # Select first item when loading, nothing when saving selected_item = -1 if mode == "save" else 0 self.current.distributeData({"savegamelist": selected_item}) cb_details = Gui._create_show_savegame_details(self.current, map_files, "savegamelist") cb = Callback.ChainedCallbacks(cb_details, tmp_selected_changed) cb() # Refresh data on start self.current.mapEvents({"savegamelist/action": cb}) self.current.findChild(name="savegamelist").capture(cb, event_name="keyPressed") bind = {OkButton.DEFAULT_NAME: True, CancelButton.DEFAULT_NAME: False, DeleteButton.DEFAULT_NAME: "delete"} if mode == "save": bind["savegamefile"] = True retval = self.show_dialog(self.current, bind) if not retval: # cancelled self.current = old_current return if retval == "delete": # delete button was pressed. Apply delete and reshow dialog, delegating the return value delete_retval = self._delete_savegame(map_files) if delete_retval: self.current.distributeData({"savegamelist": -1}) cb() self.current = old_current return self.show_select_savegame(*args) selected_savegame = None if mode == "save": # return from textfield selected_savegame = self.current.collectData("savegamefile") if selected_savegame == "": self.show_error_popup( windowtitle=_("No filename given"), description=_("Please enter a valid filename.") ) self.current = old_current return self.show_select_savegame(*args) # reshow dialog elif selected_savegame in map_file_display: # savegamename already exists # xgettext:python-format message = ( _("A savegame with the name '{name}' already exists.").format(name=selected_savegame) + u"\n" + _("Overwrite it?") ) # keep the pop-up non-modal because otherwise it is double-modal (#1876) if not self.show_popup( _("Confirmation for overwriting"), message, show_cancel_button=True, modal=False ): self.current = old_current return self.show_select_savegame(*args) # reshow dialog elif sanity_checker and sanity_criteria: if not sanity_checker(selected_savegame): self.show_error_popup(windowtitle=_("Invalid filename given"), description=sanity_criteria) self.current = old_current return self.show_select_savegame(*args) # reshow dialog else: # return selected item from list selected_savegame = self.current.collectData("savegamelist") assert selected_savegame != -1, "No savegame selected in savegamelist" selected_savegame = map_files[selected_savegame] if mp and mode == "load": # also name gamename_textfield = self.current.findChild(name="gamename") ret = selected_savegame, self.current.collectData("gamename"), self.current.collectData("gamepassword") else: ret = selected_savegame self.current = old_current # reuse old widget return ret
def show_select_savegame(self, mode, sanity_checker=None, sanity_criteria=None): """Shows menu to select a savegame. @param mode: Valid options are 'save', 'load', 'mp_load', 'mp_save' @param sanity_checker: only allow manually entered names that pass this test @param sanity_criteria: explain which names are allowed to the user @return: Path to savegamefile or None""" assert mode in ('save', 'load', 'mp_load', 'mp_save') map_files, map_file_display = None, None args = mode, sanity_checker, sanity_criteria # for reshow mp = mode.startswith('mp_') if mp: mode = mode[3:] # below this line, mp_load == load, mp_save == save if mode == 'load': if not mp: map_files, map_file_display = SavegameManager.get_saves() else: map_files, map_file_display = SavegameManager.get_multiplayersaves() if not map_files: self.show_popup(_("No saved games"), _("There are no saved games to load.")) return else: # don't show autosave and quicksave on save if not mp: map_files, map_file_display = SavegameManager.get_regular_saves() else: map_files, map_file_display = SavegameManager.get_multiplayersaves() # Prepare widget old_current = self._switch_current_widget('select_savegame') if mode == 'save': helptext = _('Save game') elif mode == 'load': helptext = _('Load game') # else: not a valid mode, so we can as well crash on the following self.current.findChild(name='headline').text = helptext self.current.findChild(name=OkButton.DEFAULT_NAME).helptext = helptext name_box = self.current.findChild(name="gamename_box") password_box = self.current.findChild(name="gamepassword_box") if mp and mode == 'load': # have gamename name_box.parent.showChild(name_box) password_box.parent.showChild(password_box) gamename_textfield = self.current.findChild(name="gamename") gamepassword_textfield = self.current.findChild(name="gamepassword") gamepassword_textfield.text = u"" def clear_gamedetails_textfields(): gamename_textfield.text = u"" gamepassword_textfield.text = u"" gamename_textfield.capture(clear_gamedetails_textfields, 'mouseReleased', 'default') else: if name_box not in name_box.parent.hidden_children: name_box.parent.hideChild(name_box) if password_box not in name_box.parent.hidden_children: password_box.parent.hideChild(password_box) self.current.show() if not hasattr(self, 'filename_hbox'): self.filename_hbox = self.current.findChild(name='enter_filename') self.filename_hbox_parent = self.filename_hbox.parent if mode == 'save': # only show enter_filename on save self.filename_hbox_parent.showChild(self.filename_hbox) elif self.filename_hbox not in self.filename_hbox_parent.hidden_children: self.filename_hbox_parent.hideChild(self.filename_hbox) def tmp_selected_changed(): """Fills in the name of the savegame in the textbox when selected in the list""" if mode != 'save': # set textbox only if we are in save mode return if self.current.collectData('savegamelist') == -1: # set blank if nothing is selected self.current.findChild(name="savegamefile").text = u"" else: savegamefile = map_file_display[self.current.collectData('savegamelist')] self.current.distributeData({'savegamefile': savegamefile}) self.current.distributeInitialData({'savegamelist': map_file_display}) # Select first item when loading, nothing when saving selected_item = -1 if mode == 'save' else 0 self.current.distributeData({'savegamelist': selected_item}) cb_details = Gui._create_show_savegame_details(self.current, map_files, 'savegamelist') cb = Callback.ChainedCallbacks(cb_details, tmp_selected_changed) cb() # Refresh data on start self.current.mapEvents({'savegamelist/action': cb}) self.current.findChild(name="savegamelist").capture(cb, event_name="keyPressed") bind = { OkButton.DEFAULT_NAME : True, CancelButton.DEFAULT_NAME : False, DeleteButton.DEFAULT_NAME : 'delete' } if mode == 'save': bind['savegamefile'] = True retval = self.show_dialog(self.current, bind) if not retval: # cancelled self.current = old_current return if retval == 'delete': # delete button was pressed. Apply delete and reshow dialog, delegating the return value delete_retval = self._delete_savegame(map_files) if delete_retval: self.current.distributeData({'savegamelist' : -1}) cb() self.current = old_current return self.show_select_savegame(*args) selected_savegame = None if mode == 'save': # return from textfield selected_savegame = self.current.collectData('savegamefile') if selected_savegame == "": self.show_error_popup(windowtitle=_("No filename given"), description=_("Please enter a valid filename.")) self.current = old_current return self.show_select_savegame(*args) # reshow dialog elif selected_savegame in map_file_display: # savegamename already exists #xgettext:python-format message = _("A savegame with the name '{name}' already exists.").format( name=selected_savegame) + u"\n" + _('Overwrite it?') # keep the pop-up non-modal because otherwise it is double-modal (#1876) if not self.show_popup(_("Confirmation for overwriting"), message, show_cancel_button=True, modal=False): self.current = old_current return self.show_select_savegame(*args) # reshow dialog elif sanity_checker and sanity_criteria: if not sanity_checker(selected_savegame): self.show_error_popup(windowtitle=_("Invalid filename given"), description=sanity_criteria) self.current = old_current return self.show_select_savegame(*args) # reshow dialog else: # return selected item from list selected_savegame = self.current.collectData('savegamelist') assert selected_savegame != -1, "No savegame selected in savegamelist" selected_savegame = map_files[selected_savegame] if mp and mode == 'load': # also name gamename_textfield = self.current.findChild(name="gamename") ret = selected_savegame, self.current.collectData('gamename'), self.current.collectData('gamepassword') else: ret = selected_savegame self.current = old_current # reuse old widget return ret