Return to repo list

heart-of-gold

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

commit 95bfb56e45f0b02f1849be72f21aa80ecf9b2e0d
parent adf55165c2690a98545ec379fc465ea271b84505
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed, 28 Apr 2021 21:39:51 -0500

can now scroll by dragging right-click

Diffstat:
Msrc/interface.py | 103++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 70 insertions(+), 33 deletions(-)

diff --git a/src/interface.py b/src/interface.py @@ -32,10 +32,15 @@ class GameInterface(subsystem.GameSubsystem): self.bus = bus self.camera = camera self.drag_piece = False - self.drag_from_pos = (0, 0) - self.double_clicking = False - self.double_click_timer = 0 - self.double_click_mousepos = None + self.drag_camera = False + self.drag_piece_from_pos = (0, 0) + self.drag_camera_from_pos = (0, 0) + self.left_double_clicking = False + self.left_double_click_timer = 0 + self.left_double_click_mousepos = None + self.right_double_clicking = False + self.right_double_click_timer = 0 + self.right_double_click_mousepos = None self.old_mousepos = None self.camera_motions = [0, 0] self.key_bools = [ False for k in range(0, 350) ] @@ -111,20 +116,20 @@ class GameInterface(subsystem.GameSubsystem): """ React to a mousebutton being clicked. """ - # If we clicked the left mouse button... + # First, get important mouse positional info, namely unoffset mouse position and camera-offset mouse position + mouseraw = pygame.mouse.get_pos() + mousepos = (mouseraw[0] - self.camera.camera_surface_offset[0], mouseraw[1] - self.camera.camera_surface_offset[1]) + + # Handle left-click if event.button == 1: - # First, get important mouse positional info, namely unoffset mouse position and camera-offset mouse position - mouseraw = pygame.mouse.get_pos() - mousepos = (mouseraw[0] - self.camera.camera_surface_offset[0], mouseraw[1] - self.camera.camera_surface_offset[1]) - # Next, set up for double click - if self.double_click_timer == 0 and not self.double_clicking: - self.double_click_timer = 15 - self.double_click_mousepos = mousepos - elif not self.double_clicking and mousepos == self.double_click_mousepos: - self.double_clicking = True - self.double_click_timer = 0 + if self.left_double_click_timer == 0 and not self.left_double_clicking: + self.left_double_click_timer = 15 + self.left_double_click_mousepos = mousepos + elif not self.left_double_clicking and mousepos == self.left_double_click_mousepos: + self.left_double_clicking = True + self.left_double_click_timer = 0 # State checker # Main menu mode behavior @@ -151,17 +156,17 @@ class GameInterface(subsystem.GameSubsystem): # Normal in-battle turn control # See if we clicked a UI button if self.game.control_mode == CTRL_MODES.Turn_Normal: - self.bus.perform_trigger_turn_manager_buttons_at_pos(mouseraw, self.double_clicking) + self.bus.perform_trigger_turn_manager_buttons_at_pos(mouseraw, self.left_double_clicking) self.bus.perform_select_piece_with_tile_cursor() sp = self.bus.fetch("piece_manager", "selected_piece") if sp != None: # Drag a piece for facing - if not self.double_clicking and self.bus.fetch("turn_manager", "active_piece") == sp: + if not self.left_double_clicking and self.bus.fetch("turn_manager", "active_piece") == sp: self.drag_piece = True - self.drag_from_pos = mouseraw + self.drag_piece_from_pos = mouseraw # Double-click a piece for accessing status - elif self.double_clicking: - self.double_clicking = False + elif self.left_double_clicking: + self.left_double_clicking = False self.bus.perform_turn_manager_display_stats(sp) self.game.control_mode = CTRL_MODES.Turn_Display_Stats @@ -209,24 +214,44 @@ class GameInterface(subsystem.GameSubsystem): if self.game.control_mode == CTRL_MODES.Still_Scene_Normal: self.bus.perform_click_current_scene() - # Keepover - self.old_mousepos = mousepos + # Handle right-click + elif event.button == 3: + + # Battle mode behavior + if self.game.state_mode == STATE_MODES.Battle_Mode: + + # Handle dragging camera + if not self.drag_camera: + self.drag_camera = True + self.drag_camera_from_pos = mouseraw + + # Keepover + self.old_mousepos = mousepos def handle_mouse_release(self, event): """ React to a mousebutton being released. """ - # If we released the left mouse button... - if event.button == 1: + # First, get important mouse positional info, namely unoffset mouse position and camera-offset mouse position + mouseraw = pygame.mouse.get_pos() + mousepos = (mouseraw[0] - self.camera.camera_surface_offset[0], mouseraw[1] - self.camera.camera_surface_offset[1]) - # First, get important mouse positional info, namely unoffset mouse position and camera-offset mouse position - mouseraw = pygame.mouse.get_pos() - mousepos = (mouseraw[0] - self.camera.camera_surface_offset[0], mouseraw[1] - self.camera.camera_surface_offset[1]) + # Handle left-click + if event.button == 1: + # Toggle off dragging piece if self.game.state_mode == STATE_MODES.Battle_Mode: if self.drag_piece: self.drag_piece = False + # Handle rigtht-click + elif event.button == 3: + + # Toggle off dragging camera + if self.game.state_mode == STATE_MODES.Battle_Mode: + if self.drag_camera: + self.drag_camera = False + def update_interface(self): """ Update interface elements (such as the cursor) once @@ -238,11 +263,10 @@ class GameInterface(subsystem.GameSubsystem): # UNIVERSAL UPDATES # Doubleclick countdown - if self.double_click_timer > 0: - self.double_click_timer -= 1 + if self.left_double_click_timer > 0: + self.left_double_click_timer -= 1 else: - self.double_clicking = False - + self.left_double_clicking = False # React to keys self.react_to_keys() @@ -261,7 +285,7 @@ class GameInterface(subsystem.GameSubsystem): # Drag facing if self.drag_piece: ap = self.bus.fetch("turn_manager", "active_piece") - diff = (self.drag_from_pos[0] - mouseraw[0], self.drag_from_pos[1] - mouseraw[1]) + diff = (self.drag_piece_from_pos[0] - mouseraw[0], self.drag_piece_from_pos[1] - mouseraw[1]) if diff != (0, 0): if abs(diff[0]) > abs(diff[1]): if diff[0] >= 0: @@ -277,8 +301,21 @@ class GameInterface(subsystem.GameSubsystem): # Position cursor in scrollable modes and/or scroll if self.game.control_mode in SCROLLABLE_TURN_MODES: - if self.camera_motions != (0, 0): + # Move camera with keys + if self.camera_motions != [0, 0]: self.camera.move_offset(self.camera_motions, SCROLL_SPEED) + # Drag camera + elif self.drag_camera: + sg = lambda x: int((x > 0) - (x < 0)) + camdiff = [self.drag_camera_from_pos[0] - mouseraw[0], self.drag_camera_from_pos[1] - mouseraw[1]] + for k in (0, 1): + if abs(camdiff[k]) < 33: + camdiff[k] = 0 + else: + camdiff[k] = sg(camdiff[k]) + if camdiff != (0, 0): + self.camera.move_offset(tuple(camdiff), SCROLL_SPEED) + # Position cursor if tilepos != None: self.bus.perform_piece_manager_position_tile_cursor((tilepos[0], tilepos[1]))