Package horizons :: Package gui :: Package modules :: Module editorstartmenu
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.modules.editorstartmenu

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  import horizons.main 
 23  from horizons.gui.util import load_uh_widget 
 24  from horizons.gui.windows import Window 
 25  from horizons.i18n import gettext as T 
 26  from horizons.savegamemanager import SavegameManager 
 27  from horizons.util.python.callback import Callback 
 28  from horizons.util.startgameoptions import StartGameOptions 
 29   
 30   
31 -class EditorStartMenu(Window):
32 - def __init__(self, windows):
33 super().__init__(windows) 34 35 self._gui = load_uh_widget('editor_start_menu.xml') 36 self._gui.position_technique = "center:center" 37 self._right_side = None 38 self._select_mode('create_new_map')
39
40 - def show(self):
41 self._right_side.show() 42 self._gui.show() 43 44 events = {} 45 events['okay'] = self.act 46 events['cancel'] = self._windows.close 47 events['create_new_map'] = Callback(self._select_mode, 'create_new_map') 48 events['load_existing_map'] = Callback(self._select_mode, 'load_existing_map') 49 events['load_saved_game_map'] = Callback(self._select_mode, 'load_saved_game_map') 50 self._gui.mapEvents(events)
51
52 - def _select_mode(self, mode):
53 modes = { 54 'create_new_map': EditorCreateMapWidget, 55 'load_existing_map': EditorSelectMapWidget, 56 'load_saved_game_map': EditorSelectSavedGameWidget, 57 } 58 if modes[mode] is None: 59 return 60 61 if isinstance(self._right_side, modes[mode]): 62 return 63 64 self._gui.hide() 65 self._gui.findChild(name=mode).marked = True 66 self._right_side = modes[mode](self._windows, self._gui.findChild(name='right_side')) 67 self._right_side.show() 68 self._gui.show()
69
70 - def on_return(self):
71 self.act()
72
73 - def hide(self):
74 self._gui.hide()
75
76 - def act(self):
77 self._right_side.act()
78 79
80 -class EditorCreateMapWidget:
81 sizes = [50, 100, 150, 200, 250] 82
83 - def __init__(self, windows, parent_widget):
84 self._windows = windows 85 self._parent_widget = parent_widget 86 self._gui = load_uh_widget('editor_create_map.xml') 87 self._gui.findChild(name='size_150').marked = True 88 89 for size in self.sizes: 90 option_name = 'size_{:d}'.format(size) 91 # size of empty map in map editor 92 self._gui.findChild(name=option_name).text = T('{size}x{size} tiles').format(size=size)
93
94 - def show(self):
95 self._parent_widget.removeAllChildren() 96 self._parent_widget.addChild(self._gui)
97
98 - def act(self):
99 for size in self.sizes: 100 option_name = 'size_{:d}'.format(size) 101 if self._gui.findChild(name=option_name).marked: 102 self._windows.close() 103 104 # the empty list is interpreted as the empty list of random map island strings 105 options = StartGameOptions.create_editor_load([]) 106 options.map_padding = size // 2 107 horizons.main.start_singleplayer(options) 108 return
109 110
111 -class EditorSelectMapWidget:
112 - def __init__(self, windows, parent_widget):
113 self._windows = windows 114 self._parent_widget = parent_widget 115 self._gui = load_uh_widget('editor_select_map.xml') 116 self._map_data = None
117
118 - def show(self):
119 self._map_data = SavegameManager.get_maps() 120 self._gui.distributeInitialData({'map_list': self._map_data[1]}) 121 self._parent_widget.removeAllChildren() 122 self._parent_widget.addChild(self._gui)
123
124 - def act(self):
125 selected_map_index = self._gui.collectData('map_list') 126 if selected_map_index == -1: 127 # No map selected yet => select first available one 128 self._gui.distributeData({'map_list': 0}) 129 130 self._windows.close() 131 horizons.main.edit_map(self._map_data[0][selected_map_index])
132 133
134 -class EditorSelectSavedGameWidget:
135 - def __init__(self, windows, parent_widget):
136 self._windows = windows 137 self._parent_widget = parent_widget 138 self._gui = load_uh_widget('editor_select_saved_game.xml') 139 self._saved_game_data = None
140
141 - def show(self):
142 self._saved_game_data = SavegameManager.get_saves() 143 self._gui.distributeInitialData({'saved_game_list': self._saved_game_data[1]}) 144 self._parent_widget.removeAllChildren() 145 self._parent_widget.addChild(self._gui)
146
147 - def act(self):
148 if not self._saved_game_data[0]: 149 # there are no saved games: do nothing 150 # TODO: play a related sound effect and/or do something else to make the user understand what is wrong 151 return 152 153 selection_index = self._gui.collectData('saved_game_list') 154 if selection_index == -1: 155 # No map selected yet => select first available one 156 self._gui.distributeData({'saved_game_list': 0}) 157 158 self._windows.close() 159 horizons.main.edit_game_map(self._saved_game_data[0][selection_index])
160