def test_contains_point(self): test_data = ( ((93, 761), new_base(pos=[100, 100, 0], size=[41, 41]), None), ((93, 761), new_base(pos=[200, 100, 0], size=[41, 41]), None), ((95, 682), new_base(pos=[100, 677, 0], size=[41, 41]), True), ) for point, ob, expected in test_data: result = actor_lib.contains_point(ob, point) self.assertEqual(result, expected)
def handle_doubleclick(self, first_click, second_click): # First check panels for i, p in self.panels.items(): if p.contains(first_click.pos) and p.contains(second_click.pos): if p.handle_doubleclick(first_click, second_click): return else: break first_real_mouse_pos = (first_click.pos[0] - self.draw_margin[0], first_click.pos[1] - self.draw_margin[1]) second_real_mouse_pos = (second_click.pos[0] - self.draw_margin[0], second_click.pos[1] - self.draw_margin[1]) # Now check actors for a in self.actors: if actor_lib.contains_point( a, first_real_mouse_pos) and actor_lib.contains_point( a, second_real_mouse_pos): self.double_left_click_actor(a) break
def handle_doubleclick(self, first_click, second_click): # First check panels for i, p in self.panels.items(): if p.contains(first_click.pos) and p.contains(second_click.pos): if p.handle_doubleclick(first_click, second_click): return else: break first_real_mouse_pos = ( first_click.pos[0] - self.draw_margin[0], first_click.pos[1] - self.draw_margin[1] ) second_real_mouse_pos = ( second_click.pos[0] - self.draw_margin[0], second_click.pos[1] - self.draw_margin[1] ) # Now check actors for a in self.actors: if actor_lib.contains_point(a, first_real_mouse_pos) and actor_lib.contains_point(a, second_real_mouse_pos): self.double_left_click_actor(a) break
def handle_mouseup(self, event, drag=False): # We are no longer dragging, lets get rid of this rect self.drag_rect = None # First check panels for i, p in self.panels.items(): if p.contains(event.pos): result = p.handle_mouseup(event, drag) if result == True:# Panel has handled it return elif result == False or result == None:# Panel has not handled it break else: # Panel has sent us back a new event # we re-run this function with the new event return self.handle_mouseup(result, drag) if self.mouseup_callback: callback_func, args = self.mouseup_callback, self.mouseup_callback_args # Set these to nothing now incase we want to make a new callback # in the current callback self.mouseup_callback = None self.mouseup_callback_args = [] return callback_func(event, drag, *args) mods = pygame.key.get_mods() real_mouse_pos = (event.pos[0] - self.draw_margin[0], event.pos[1] - self.draw_margin[1]) if event.button == 1:# Left click if not drag: if self.key_mod: actor_target = None for a in self.actors: if actor_lib.contains_point(a, real_mouse_pos): actor_target = weakref.ref(a)() break if KMOD_SHIFT & mods: for a in self.selected_actors: if a.team == self.player_team: self.queue_order(a, self.key_mod, pos=real_mouse_pos, target=actor_target) else: for a in self.selected_actors: if a.team == self.player_team: self.add_order(a, self.key_mod, pos=real_mouse_pos, target=actor_target) else: if not KMOD_SHIFT & mods: self.unselect_all_actors() for a in self.actors: if actor_lib.contains_point(a, real_mouse_pos): self.left_click_actor(a) break elif drag: self.key_mod = None elif event.button == 3:# Right click if len(self.selected_actors) == 0: return actor_target = None for a in self.actors: if actor_lib.contains_point(a, real_mouse_pos): actor_target = weakref.ref(a)() break # No actor clicked, this means we're moving if not actor_target: for a in self.selected_actors: if a.team == self.player_team: if KMOD_SHIFT & mods: self.queue_order(a, "move", pos=real_mouse_pos) else: self.add_order(a, "move", pos=real_mouse_pos) # An actor was clicked else: if actor_target.team != self.selected_actors[0].team: for a in self.selected_actors: if a.team == self.player_team: if KMOD_SHIFT & mods: self.queue_order(a, "attack", target=actor_target) else: self.add_order(a, "attack", target=actor_target) else: for a in self.selected_actors: if a.team == self.player_team: if KMOD_SHIFT & mods: self.queue_order(a, "aid", target=actor_target) else: self.add_order(a, "aid", target=actor_target) else: print("battle_screen.handle_mouseup: event.button = %s" % event.button)
def handle_mouseup(self, event, drag=False): # We are no longer dragging, lets get rid of this rect self.drag_rect = None # First check panels for i, p in self.panels.items(): if p.contains(event.pos): result = p.handle_mouseup(event, drag) if result == True: # Panel has handled it return elif result == False or result == None: # Panel has not handled it break else: # Panel has sent us back a new event # we re-run this function with the new event return self.handle_mouseup(result, drag) if self.mouseup_callback: callback_func, args = self.mouseup_callback, self.mouseup_callback_args # Set these to nothing now incase we want to make a new callback # in the current callback self.mouseup_callback = None self.mouseup_callback_args = [] return callback_func(event, drag, *args) mods = pygame.key.get_mods() real_mouse_pos = (event.pos[0] - self.draw_margin[0], event.pos[1] - self.draw_margin[1]) if event.button == 1: # Left click if not drag: if self.key_mod: actor_target = None for a in self.actors: if actor_lib.contains_point(a, real_mouse_pos): actor_target = weakref.ref(a)() break if KMOD_SHIFT & mods: for a in self.selected_actors: if a.team == self.player_team: self.queue_order(a, self.key_mod, pos=real_mouse_pos, target=actor_target) else: for a in self.selected_actors: if a.team == self.player_team: self.add_order(a, self.key_mod, pos=real_mouse_pos, target=actor_target) else: if not KMOD_SHIFT & mods: self.unselect_all_actors() for a in self.actors: if actor_lib.contains_point(a, real_mouse_pos): self.left_click_actor(a) break elif drag: self.key_mod = None elif event.button == 3: # Right click if len(self.selected_actors) == 0: return actor_target = None for a in self.actors: if actor_lib.contains_point(a, real_mouse_pos): actor_target = weakref.ref(a)() break # No actor clicked, this means we're moving if not actor_target: for a in self.selected_actors: if a.team == self.player_team: if KMOD_SHIFT & mods: self.queue_order(a, "move", pos=real_mouse_pos) else: self.add_order(a, "move", pos=real_mouse_pos) # An actor was clicked else: if actor_target.team != self.selected_actors[0].team: for a in self.selected_actors: if a.team == self.player_team: if KMOD_SHIFT & mods: self.queue_order(a, "attack", target=actor_target) else: self.add_order(a, "attack", target=actor_target) else: for a in self.selected_actors: if a.team == self.player_team: if KMOD_SHIFT & mods: self.queue_order(a, "aid", target=actor_target) else: self.add_order(a, "aid", target=actor_target) else: print("battle_screen.handle_mouseup: event.button = %s" % event.button)