Return to repo list

heart-of-gold

Tactical RPG written in python, using pygame.
Return to HMagellan.com

commit 802a753d1edda5437a27f75cfba61cd731b50e66
parent efe15cf819473781fa4163dcf61df17e62b9df84
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed, 12 May 2021 17:10:01 -0500

base pause buttons now click and quit

Diffstat:
Msrc/base.py | 35+++++++++++++++++++++++++++++++++--
Msrc/bus.py | 2++
Msrc/interface.py | 3+++
3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/src/base.py b/src/base.py @@ -36,6 +36,10 @@ class BaseManager(manager.Manager): # For pause screen self.pause_save_button = None self.pause_load_button = None + self.pause_quit_button = None + # Element groups + self.normal_base_buttons = None + self.pause_menu_buttons = None # Event stages self.current_event_stage = 0 @@ -48,10 +52,15 @@ class BaseManager(manager.Manager): sheets = self.bus.fetch("sheet_manager", "sheets") # First, establish the UI + self.normal_base_buttons = pygame.sprite.LayeredDirty() + self.pause_menu_buttons = pygame.sprite.LayeredDirty() self.pause_save_button = BasePauseSaveButton(self, sheets["mainmenu_buttons_1"], (0, 3)) self.pause_save_button.set_position((200, 250)) self.pause_load_button = BasePauseLoadButton(self, sheets["mainmenu_buttons_1"], (0, 1)) self.pause_load_button.set_position((200, 320)) + self.pause_quit_button = BasePauseQuitButton(self, sheets["mainmenu_buttons_1"], (0, 4)) + self.pause_quit_button.set_position((200, 390)) + self.pause_menu_buttons.add(self.pause_save_button, self.pause_load_button, self.pause_quit_button) # Then, load the actual base elements with open(os.path.join("data", "board", basename, basename + ".json")) as f: definition = json.load(f) @@ -126,6 +135,17 @@ class BaseManager(manager.Manager): self.game.control_mode = CTRL_MODES.Base_Dialog self.bus.perform_scene_manager_load_scene_from_file(ent.event_spec[-1]) + def handle_base_ui_click(self, pos): + """ + Handle when a base button or other pressable UI + element it clicked. This includes pause menu elements + in base mode. Takes a pos value. + """ + if self.game.control_mode in PAUSE_MODES: + for pb in self.pause_menu_buttons: + if pb.rect.collidepoint(pos): + pb.be_pressed() + def update_managed(self, surface = None): """ Update the base entities. @@ -134,8 +154,7 @@ class BaseManager(manager.Manager): if self.game.control_mode == CTRL_MODES.Base_Normal: self.tile_cursor.update(surface) elif self.game.control_mode in PAUSE_MODES: - self.pause_save_button.update(surface) - self.pause_load_button.update(surface) + self.pause_menu_buttons.update(surface) self.base_entities.update(surface, self.game.control_mode not in PAUSE_MODES) ################################################### @@ -181,8 +200,20 @@ class BasePauseButton(entity.Entity): self.manager = manager self.clickable = False + def be_pressed(self, *args): + """ + Generic handler method for being clicked. + Overwritten in children. + """ + pass + class BasePauseSaveButton(BasePauseButton): pass class BasePauseLoadButton(BasePauseButton): pass + +class BasePauseQuitButton(BasePauseButton): + + def be_pressed(self): + self.manager.game.quit_game() diff --git a/src/bus.py b/src/bus.py @@ -125,6 +125,8 @@ class ManagerBus(subsystem.GameSubsystem): self.game.base_manager.position_tile_cursor(tile_pos) def perform_click_base_entity_at_pos(self, pos): self.game.base_manager.handle_base_entity_click(pos) + def perform_click_base_ui_at_pos(self, pos): + self.game.base_manager.handle_base_ui_click(pos) def perform_scene_manager_load_scene_from_file(self, scenefile): self.game.scene_manager.load_still_scene_from_file(scenefile) def perform_scene_manager_load_scene_from_def(self, definition): diff --git a/src/interface.py b/src/interface.py @@ -168,6 +168,9 @@ class GameInterface(subsystem.GameSubsystem): # Base mode behavior if self.game.state_mode == STATE_MODES.Base_Mode: + # Check for clicking on base UI elements in all CTRL_MODES + self.bus.perform_click_base_ui_at_pos(mousepos) + # Check for clicking on base entities if self.game.control_mode == CTRL_MODES.Base_Normal: self.bus.perform_click_base_entity_at_pos(mousepos)