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

Source Code for Module horizons.gui.gui

  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 glob 
 23  import logging 
 24  from collections import deque 
 25   
 26  from fife.extensions.pychan.widgets import Container, Icon 
 27   
 28  import horizons.globals 
 29  import horizons.main 
 30  from horizons.component.ambientsoundcomponent import AmbientSoundComponent 
 31  from horizons.gui.keylisteners import MainListener 
 32  from horizons.gui.modules import ( 
 33          HelpDialog, LoadingScreen, MultiplayerMenu, SelectSavegameDialog, SettingsDialog, SingleplayerMenu) 
 34  from horizons.gui.modules.editorstartmenu import EditorStartMenu 
 35  from horizons.gui.util import load_uh_widget 
 36  from horizons.gui.widgets.fpsdisplay import FPSDisplay 
 37  from horizons.gui.widgets.pickbeltwidget import CreditsPickbeltWidget 
 38  from horizons.gui.windows import Window, WindowManager 
 39  from horizons.i18n import gettext as T 
 40  from horizons.messaging import GuiAction, GuiCancelAction, GuiHover 
 41  from horizons.util.startgameoptions import StartGameOptions 
 97   
98 99 -class Background:
100 """ 101 Display a centered background image on top of a black screen. 102 """
103 - def __init__(self):
104 available_images = glob.glob('content/gui/images/background/mainmenu/bg_*.png') 105 self.bg_images = deque(available_images) 106 107 latest_bg = horizons.globals.fife.get_uh_setting("LatestBackground") 108 try: 109 # If we know the current background from an earlier session, 110 # show all other available ones before picking that one again. 111 self.bg_images.remove(latest_bg) 112 self.bg_images.append(latest_bg) 113 except ValueError: 114 pass 115 116 (res_width, res_height) = horizons.globals.fife.get_fife_setting('ScreenResolution').split('x') 117 self._black_box = Container() 118 self._black_box.size = int(res_width), int(res_height) 119 self._black_box.base_color = (0, 0, 0, 255) 120 121 self._image = Icon(position_technique='center:center') 122 self.rotate_image()
123
124 - def rotate_image(self):
125 """Select next background image to use in the game menu. 126 127 Triggered by the "Change background" main menu button. 128 """ 129 self.bg_images.rotate(1) 130 self._image.image = self.bg_images[0] 131 # Save current background choice to settings. 132 # This keeps the background image consistent between sessions. 133 horizons.globals.fife.set_uh_setting("LatestBackground", self.bg_images[0]) 134 horizons.globals.fife.save_settings()
135
136 - def show(self):
137 self._black_box.show() 138 self._image.show()
139
140 - def hide(self):
141 self._image.hide() 142 self._black_box.hide()
143 144 @property
145 - def visible(self):
146 return self._image.isVisible()
147
148 149 -class Gui:
150 """This class handles all the out of game menu, like the main and pause menu, etc. 151 """ 152 log = logging.getLogger("gui") 153
154 - def __init__(self):
155 self.mainlistener = MainListener(self) 156 157 self.windows = WindowManager() 158 # temporary aliases for compatibility with rest of the code 159 self.open_popup = self.windows.open_popup 160 self.open_error_popup = self.windows.open_error_popup 161 162 self._background = Background() 163 self._background.show() 164 165 # Initialize menu dialogs and widgets that are accessed from `gui`. 166 self.singleplayermenu = SingleplayerMenu(self.windows) 167 self.multiplayermenu = MultiplayerMenu(self, self.windows) 168 self.help_dialog = HelpDialog(self.windows) 169 self.loadingscreen = LoadingScreen() 170 self.settings_dialog = SettingsDialog(self.windows) 171 self.mainmenu = MainMenu(self, self.windows) 172 self.fps_display = FPSDisplay()
173
174 - def show_main(self):
175 """Shows the main menu""" 176 GuiAction.subscribe(self._on_gui_click_action) 177 GuiHover.subscribe(self._on_gui_hover_action) 178 GuiCancelAction.subscribe(self._on_gui_cancel_action) 179 180 if not self._background.visible: 181 self._background.show() 182 183 self.windows.open(self.mainmenu)
184
185 - def show_select_savegame(self, mode):
186 window = SelectSavegameDialog(mode, self.windows) 187 return self.windows.open(window)
188
189 - def load_game(self):
190 saved_game = self.show_select_savegame(mode='load') 191 if saved_game is None: 192 return False # user aborted dialog 193 194 options = StartGameOptions(saved_game) 195 horizons.main.start_singleplayer(options) 196 return True
197
198 - def on_help(self):
199 self.windows.toggle(self.help_dialog)
200
201 - def show_credits(self):
202 """Shows the credits dialog. """ 203 window = CreditsPickbeltWidget(self.windows) 204 self.windows.open(window)
205
206 - def on_escape(self):
207 self.windows.on_escape()
208
209 - def on_return(self):
210 self.windows.on_return()
211
212 - def close_all(self):
218
219 - def show_loading_screen(self):
220 if not self._background.visible: 221 self._background.show() 222 self.windows.open(self.loadingscreen)
223
224 - def _on_gui_click_action(self, msg):
225 """Make a sound when a button is clicked""" 226 AmbientSoundComponent.play_special('click', gain=10)
227
228 - def _on_gui_cancel_action(self, msg):
229 """Make a sound when a cancelButton is clicked""" 230 AmbientSoundComponent.play_special('success', gain=10)
231
232 - def _on_gui_hover_action(self, msg):
233 """Make a sound when the mouse hovers over a button""" 234 AmbientSoundComponent.play_special('refresh', position=None, gain=1)
235
236 - def show_editor_start_menu(self):
237 editor_start_menu = EditorStartMenu(self.windows) 238 self.windows.open(editor_start_menu)
239