class Background: """ Display a centered background image on top of a black screen. """ def __init__(self): available_images = glob.glob( 'content/gui/images/background/mainmenu/bg_*.png') self.bg_images = deque(available_images) latest_bg = horizons.globals.fife.get_uh_setting("LatestBackground") try: # If we know the current background from an earlier session, # show all other available ones before picking that one again. self.bg_images.remove(latest_bg) self.bg_images.append(latest_bg) except ValueError: pass (res_width, res_height) = horizons.globals.fife.get_fife_setting( 'ScreenResolution').split('x') self._black_box = Container() self._black_box.size = int(res_width), int(res_height) self._black_box.base_color = (0, 0, 0, 255) self._image = Icon(position_technique='center:center') self.rotate_image() def rotate_image(self): """Select next background image to use in the game menu. Triggered by the "Change background" main menu button. """ self.bg_images.rotate(1) self._image.image = self.bg_images[0] # Save current background choice to settings. # This keeps the background image consistent between sessions. horizons.globals.fife.set_uh_setting("LatestBackground", self.bg_images[0]) horizons.globals.fife.save_settings() def show(self): self._black_box.show() self._image.show() def hide(self): self._image.hide() self._black_box.hide() @property def visible(self): return self._image.isVisible()
class Background: """ Display a centered background image on top of a black screen. """ def __init__(self): available_images = glob.glob('content/gui/images/background/mainmenu/bg_*.png') self.bg_images = deque(available_images) latest_bg = horizons.globals.fife.get_uh_setting("LatestBackground") try: # If we know the current background from an earlier session, # show all other available ones before picking that one again. self.bg_images.remove(latest_bg) self.bg_images.append(latest_bg) except ValueError: pass (res_width, res_height) = horizons.globals.fife.get_fife_setting('ScreenResolution').split('x') self._black_box = Container() self._black_box.size = int(res_width), int(res_height) self._black_box.base_color = (0, 0, 0, 255) self._image = Icon(position_technique='center:center') self.rotate_image() def rotate_image(self): """Select next background image to use in the game menu. Triggered by the "Change background" main menu button. """ self.bg_images.rotate(1) self._image.image = self.bg_images[0] # Save current background choice to settings. # This keeps the background image consistent between sessions. horizons.globals.fife.set_uh_setting("LatestBackground", self.bg_images[0]) horizons.globals.fife.save_settings() def show(self): self._black_box.show() self._image.show() def hide(self): self._image.hide() self._black_box.hide() @property def visible(self): return self._image.isVisible()
class Gui: """This class handles all the out of game menu, like the main and pause menu, etc. """ log = logging.getLogger("gui") def __init__(self): self.mainlistener = MainListener(self) self.windows = WindowManager() # temporary aliases for compatibility with rest of the code self.open_popup = self.windows.open_popup self.open_error_popup = self.windows.open_error_popup # Main menu background image setup. available_images = glob.glob('content/gui/images/background/mainmenu/bg_*.png') self.bg_images = deque(available_images) latest_bg = horizons.globals.fife.get_uh_setting("LatestBackground") try: # If we know the current background from an earlier session, # show all other available ones before picking that one again. self.bg_images.remove(latest_bg) self.bg_images.append(latest_bg) except ValueError: pass self._background = Icon(position_technique='center:center') self.rotate_background() self._background.show() # Initialize menu dialogs and widgets that are accessed from `gui`. self.singleplayermenu = SingleplayerMenu(self.windows) self.multiplayermenu = MultiplayerMenu(self, self.windows) self.help_dialog = HelpDialog(self.windows) self.loadingscreen = LoadingScreen() self.settings_dialog = SettingsDialog(self.windows) self.mainmenu = MainMenu(self, self.windows) self.fps_display = FPSDisplay() def show_main(self): """Shows the main menu""" GuiAction.subscribe(self._on_gui_click_action) GuiHover.subscribe(self._on_gui_hover_action) GuiCancelAction.subscribe(self._on_gui_cancel_action) if not self._background.isVisible(): self._background.show() self.windows.open(self.mainmenu) def show_select_savegame(self, mode): window = SelectSavegameDialog(mode, self.windows) return self.windows.open(window) def load_game(self): saved_game = self.show_select_savegame(mode='load') if saved_game is None: return False # user aborted dialog options = StartGameOptions(saved_game) horizons.main.start_singleplayer(options) return True def on_help(self): self.windows.toggle(self.help_dialog) def show_credits(self): """Shows the credits dialog. """ window = CreditsPickbeltWidget(self.windows) self.windows.open(window) def on_escape(self): self.windows.on_escape() def on_return(self): self.windows.on_return() def close_all(self): GuiAction.discard(self._on_gui_click_action) GuiHover.discard(self._on_gui_hover_action) GuiCancelAction.discard(self._on_gui_cancel_action) self.windows.close_all() self._background.hide() def show_loading_screen(self): if not self._background.isVisible(): self._background.show() self.windows.open(self.loadingscreen) def rotate_background(self): """Select next background image to use in the game menu. Triggered by the "Change background" main menu button. """ # Note: bg_images is a deque. self.bg_images.rotate(1) self._background.image = self.bg_images[0] # Save current background choice to settings. # This keeps the background image consistent between sessions. horizons.globals.fife.set_uh_setting("LatestBackground", self.bg_images[0]) horizons.globals.fife.save_settings() def _on_gui_click_action(self, msg): """Make a sound when a button is clicked""" AmbientSoundComponent.play_special('click', gain=10) def _on_gui_cancel_action(self, msg): """Make a sound when a cancelButton is clicked""" AmbientSoundComponent.play_special('success', gain=10) def _on_gui_hover_action(self, msg): """Make a sound when the mouse hovers over a button""" AmbientSoundComponent.play_special('refresh', position=None, gain=1) def show_editor_start_menu(self): editor_start_menu = EditorStartMenu(self.windows) self.windows.open(editor_start_menu)
class Gui(object): """This class handles all the out of game menu, like the main and pause menu, etc. """ log = logging.getLogger("gui") def __init__(self): self.mainlistener = MainListener(self) self.windows = WindowManager() # temporary aliases for compatibility with rest of the code self.show_popup = self.windows.show_popup self.show_error_popup = self.windows.show_error_popup self._background = Icon(image=self._get_random_background(), position_technique='center:center') self._background.show() self.subscribe() self.singleplayermenu = SingleplayerMenu(self.windows) self.multiplayermenu = MultiplayerMenu(self, self.windows) self.help_dialog = HelpDialog(self.windows) self.loadingscreen = LoadingScreen() self.settings_dialog = SettingsDialog(self.windows) self.mainmenu = MainMenu(self, self.windows) self.fps_display = FPSDisplay() def subscribe(self): """Subscribe to the necessary messages.""" GuiAction.subscribe(self._on_gui_action) def unsubscribe(self): GuiAction.unsubscribe(self._on_gui_action) def show_main(self): """Shows the main menu """ if not self._background.isVisible(): self._background.show() self.windows.show(self.mainmenu) def show_select_savegame(self, mode): window = SelectSavegameDialog(mode, self.windows) return self.windows.show(window) def load_game(self): saved_game = self.show_select_savegame(mode='load') if saved_game is None: return False # user aborted dialog options = StartGameOptions(saved_game) horizons.main.start_singleplayer(options) return True def on_help(self): self.windows.toggle(self.help_dialog) def show_credits(self): """Shows the credits dialog. """ window = CreditsPickbeltWidget(self.windows) self.windows.show(window) def on_escape(self): self.windows.on_escape() def close_all(self): self.windows.close_all() self._background.hide() def show_loading_screen(self): if not self._background.isVisible(): self._background.show() self.windows.show(self.loadingscreen) def randomize_background(self): """Randomly select a background image to use. This function is triggered by change background button from main menu.""" self._background.image = self._get_random_background() def _get_random_background(self): """Randomly select a background image to use through out the game menu.""" available_images = glob.glob('content/gui/images/background/mainmenu/bg_*.png') #get latest background latest_background = horizons.globals.fife.get_uh_setting("LatestBackground") #if there is a latest background then remove it from available list if latest_background is not None: available_images.remove(latest_background) background_choice = random.choice(available_images) #save current background choice horizons.globals.fife.set_uh_setting("LatestBackground", background_choice) horizons.globals.fife.save_settings() return background_choice def _on_gui_action(self, msg): AmbientSoundComponent.play_special('click') def show_editor_start_menu(self): editor_start_menu = EditorStartMenu(self.windows) self.windows.show(editor_start_menu)
class Window(object): def __init__(self, windows=None): # Reference to the window manager. Use it to show new windows or close # this window. self._windows = windows self._modal_background = None def show(self, **kwargs): """Show the window. After this call, the window should be visible. If you decide to not show the window here (e.g. an error occurred), you'll need to call `self._windows.close()` to remove the window from the manager. """ raise NotImplementedError def hide(self): """Hide the window. After this call, the window should not be visible anymore. However, it remains in the stack of open windows and will be visible once it becomes the topmost window again. You should *never* call this directly in your code, other than in `close()` when you overwrote it in your subclass. """ raise NotImplementedError def close(self): """Closes the window. You should *never* call this directly in your code. Use `self._windows.close()` to ask the WindowManager to remove the window instead. """ self.hide() def on_escape(self): """Define what happens when ESC is pressed. By default, the window will be closed. """ self._windows.close() def on_return(self): """Define what happens when RETURN is pressed.""" pass def _show_modal_background(self): """ Loads transparent background that de facto prohibits access to other gui elements by eating all input events. """ height = horizons.globals.fife.engine_settings.getScreenHeight() width = horizons.globals.fife.engine_settings.getScreenWidth() image = horizons.globals.fife.imagemanager.loadBlank(width, height) image = fife.GuiImage(image) self._modal_background = Icon(image=image) self._modal_background.position = (0, 0) self._modal_background.show() def _hide_modal_background(self): self._modal_background.hide()
class Window(object): def __init__(self, windows=None): # Reference to the window manager. Use it to open new windows or close # this window. self._windows = windows self._modal_background = None def open(self, **kwargs): """Open the window. After this call, the window should be visible. If you decide to not show the window here (e.g. an error occurred), you'll need to call `self._windows.close()` to remove the window from the manager. You may override this method in a subclass if you need to do stuff when a window is first shown. """ return self.show(**kwargs) def show(self, **kwargs): """Show the window. After this call, the window should be visible. You should *never* call this directly in your code. """ raise NotImplementedError def hide(self): """Hide the window. After this call, the window should not be visible anymore. However, it remains in the stack of open windows and will be visible once it becomes the topmost window again. You should *never* call this directly in your code, other than in `close()` when you overwrote it in your subclass. """ raise NotImplementedError def close(self): """Closes the window. You should *never* call this directly in your code. Use `self._windows.close()` to ask the WindowManager to remove the window instead. You may override this method in a subclass if you need to do stuff when a window is closed. """ self.hide() def on_escape(self): """Define what happens when ESC is pressed. By default, the window will be closed. """ self._windows.close() def on_return(self): """Define what happens when RETURN is pressed.""" pass def _show_modal_background(self): """ Loads transparent background that de facto prohibits access to other gui elements by eating all input events. """ height = horizons.globals.fife.engine_settings.getScreenHeight() width = horizons.globals.fife.engine_settings.getScreenWidth() image = horizons.globals.fife.imagemanager.loadBlank(width, height) image = fife.GuiImage(image) self._modal_background = Icon(image=image) self._modal_background.position = (0, 0) self._modal_background.show() def _hide_modal_background(self): self._modal_background.hide()
class Gui(object): """This class handles all the out of game menu, like the main and pause menu, etc. """ log = logging.getLogger("gui") def __init__(self): self.mainlistener = MainListener(self) self.windows = WindowManager() # temporary aliases for compatibility with rest of the code self.show_popup = self.windows.show_popup self.show_error_popup = self.windows.show_error_popup self._background = Icon(image=self._get_random_background(), position_technique='center:center') self._background.show() self.singleplayermenu = SingleplayerMenu(self.windows) self.multiplayermenu = MultiplayerMenu(self, self.windows) self.help_dialog = HelpDialog(self.windows) self.loadingscreen = LoadingScreen() self.settings_dialog = SettingsDialog(self.windows) self.mainmenu = MainMenu(self, self.windows) self.fps_display = FPSDisplay() def show_main(self): """Shows the main menu """ GuiAction.subscribe(self._on_gui_action) if not self._background.isVisible(): self._background.show() self.windows.show(self.mainmenu) def show_select_savegame(self, mode): window = SelectSavegameDialog(mode, self.windows) return self.windows.show(window) def load_game(self): saved_game = self.show_select_savegame(mode='load') if saved_game is None: return False # user aborted dialog options = StartGameOptions(saved_game) horizons.main.start_singleplayer(options) return True def on_help(self): self.windows.toggle(self.help_dialog) def show_credits(self): """Shows the credits dialog. """ window = CreditsPickbeltWidget(self.windows) self.windows.show(window) def on_escape(self): self.windows.on_escape() def on_return(self): self.windows.on_return() def close_all(self): GuiAction.discard(self._on_gui_action) self.windows.close_all() self._background.hide() def show_loading_screen(self): if not self._background.isVisible(): self._background.show() self.windows.show(self.loadingscreen) def randomize_background(self): """Randomly select a background image to use. This function is triggered by change background button from main menu.""" self._background.image = self._get_random_background() def _get_random_background(self): """Randomly select a background image to use through out the game menu.""" available_images = glob.glob('content/gui/images/background/mainmenu/bg_*.png') #get latest background latest_background = horizons.globals.fife.get_uh_setting("LatestBackground") #if there is a latest background then remove it from available list if latest_background is not None: available_images.remove(latest_background) background_choice = random.choice(available_images) #save current background choice horizons.globals.fife.set_uh_setting("LatestBackground", background_choice) horizons.globals.fife.save_settings() return background_choice def _on_gui_action(self, msg): AmbientSoundComponent.play_special('click') def show_editor_start_menu(self): editor_start_menu = EditorStartMenu(self.windows) self.windows.show(editor_start_menu)
class Gui(object): """This class handles all the out of game menu, like the main and pause menu, etc. """ log = logging.getLogger("gui") def __init__(self): self.mainlistener = MainListener(self) self.windows = WindowManager() # temporary aliases for compatibility with rest of the code self.show_popup = self.windows.show_popup self.show_error_popup = self.windows.show_error_popup # Main menu background image setup. available_images = glob.glob('content/gui/images/background/mainmenu/bg_*.png') self.bg_images = deque(available_images) latest_bg = horizons.globals.fife.get_uh_setting("LatestBackground") try: # If we know the current background from an earlier session, # show all other available ones before picking that one again. self.bg_images.remove(latest_bg) self.bg_images.append(latest_bg) except ValueError: pass self._background = Icon(position_technique='center:center') self.rotate_background() self._background.show() self.singleplayermenu = SingleplayerMenu(self.windows) self.multiplayermenu = MultiplayerMenu(self, self.windows) self.help_dialog = HelpDialog(self.windows) self.loadingscreen = LoadingScreen() self.settings_dialog = SettingsDialog(self.windows) self.mainmenu = MainMenu(self, self.windows) self.fps_display = FPSDisplay() def show_main(self): """Shows the main menu """ GuiAction.subscribe(self._on_gui_action) if not self._background.isVisible(): self._background.show() self.windows.show(self.mainmenu) def show_select_savegame(self, mode): window = SelectSavegameDialog(mode, self.windows) return self.windows.show(window) def load_game(self): saved_game = self.show_select_savegame(mode='load') if saved_game is None: return False # user aborted dialog options = StartGameOptions(saved_game) horizons.main.start_singleplayer(options) return True def on_help(self): self.windows.toggle(self.help_dialog) def show_credits(self): """Shows the credits dialog. """ window = CreditsPickbeltWidget(self.windows) self.windows.show(window) def on_escape(self): self.windows.on_escape() def on_return(self): self.windows.on_return() def close_all(self): GuiAction.discard(self._on_gui_action) self.windows.close_all() self._background.hide() def show_loading_screen(self): if not self._background.isVisible(): self._background.show() self.windows.show(self.loadingscreen) def rotate_background(self): """Select next background image to use in the game menu. Triggered by the "Change background" main menu button. """ # Note: bg_images is a deque. self.bg_images.rotate() self._background.image = self.bg_images[0] # Save current background choice to settings. # This keeps the background image consistent between sessions. horizons.globals.fife.set_uh_setting("LatestBackground", self.bg_images[0]) horizons.globals.fife.save_settings() def _on_gui_action(self, msg): AmbientSoundComponent.play_special('click') def show_editor_start_menu(self): editor_start_menu = EditorStartMenu(self.windows) self.windows.show(editor_start_menu)