def mouseDragged(self, sender, event): if event.isConsumedByWidgets(): return if not self._controller._layer: if self._debug: print 'No layers active. Cancelling map action' return realCoords = self._getRealCoords(sender, event) if event.getButton() == fife.MouseEvent.MIDDLE: self._scrollX = (self._dragx-realCoords[0])/10.0 self._scrollY = (self._dragy-realCoords[1])/10.0 else: if self._mode != MapEditor.SELECTING: self._controller.deselectSelection() if self._mode == MapEditor.SELECTING: if event.getButton() == fife.MouseEvent.LEFT: if self._eventlistener.shiftPressed: self._controller.deselectCell(realCoords[0], realCoords[1]) else: self._controller.selectCell(realCoords[0], realCoords[1]) elif self._mode == MapEditor.INSERTING: position = self._controller._camera.toMapCoordinates(fife.ScreenPoint(realCoords[0], realCoords[1]), False) position = self._controller._layer.getCellGrid().toLayerCoordinates(position) self._controller.selectCell(realCoords[0], realCoords[1]) self._controller.placeInstance(position, self._object) elif self._mode == MapEditor.REMOVING: self._controller.selectCell(realCoords[0], realCoords[1]) self._controller.removeInstances(self._controller.getInstancesFromSelection()) elif self._mode == MapEditor.MOVING: position = self._controller._camera.toMapCoordinates(fife.ScreenPoint(realCoords[0], realCoords[1]), False) positionExact = self._controller._layer.getCellGrid().toExactLayerCoordinates(position) position = self._controller._layer.getCellGrid().toLayerCoordinates(position) if self._eventlistener.shiftPressed: self._controller.moveInstances(self._selected_instances, positionExact-self._last_drag_pos_exact, True) else: self._controller.moveInstances(self._selected_instances, position-self._last_drag_pos, False) self._last_drag_pos = position self._last_drag_pos_exact = positionExact # Update selection self._controller.deselectSelection() for i in self._selected_instances: pos = i.getLocation().getMapCoordinates() pos = self._controller._camera.toScreenCoordinates(pos) self._controller.selectCell(pos.x, pos.y) elif self._mode == MapEditor.OBJECTPICKER: pass
def mousePressed(self, event): if event.isConsumedByWidgets(): return clickpoint = fife.ScreenPoint(event.getX(), event.getY()) self._test.movePlayer(clickpoint)
def mousePressed(self, evt): if evt.getButton( ) == fife.MouseEvent.RIGHT and not evt.isConsumedByWidgets(): target_mapcoord = self.session.view.cam.toMapCoordinates( fife.ScreenPoint(evt.getX(), evt.getY()), False) target = self._get_attackable_instance(evt) if target: if not self.session.world.diplomacy.are_enemies( self.session.world.player, target.owner): AddEnemyPair(self.session.world.player, target.owner).execute(self.session) for i in self.session.selected_instances: if hasattr(i, 'attack'): Attack(i, target).execute(self.session) else: for i in self.session.selected_instances: if i.movable: Act(i, target_mapcoord.x, target_mapcoord.y).execute(self.session) evt.consume() else: self.deselect_at_end = False super().mousePressed(evt)
def mouseDragged(self, event): if self.middle_click_point: self.application.view.moveCamera( self.middle_click_point.x - event.getX(), self.middle_click_point.y - event.getY()) self.middle_click_point = fife.ScreenPoint(event.getX(), event.getY())
def mousePressed(self, event): # pylint: disable=W0221 self.middle_container.activate() for callback_data in self.callbacks["mouse_pressed"]: func = callback_data["func"] click_point = fife.ScreenPoint(event.getX(), event.getY()) func(click_point, event.getButton()) self.old_mouse_pos = fife.DoublePoint(event.getX(), event.getY())
def _get_world_location_from_event(self, evt): """Returns the coordinates of an event at the map. @return Point with int coordinates""" screenpoint = fife.ScreenPoint(evt.getX(), evt.getY()) mapcoord = self.session.view.cam.toMapCoordinates(screenpoint, False) # undocumented legacy formula to correct coords, probably return Point(int(round(math.floor(mapcoord.x + mapcoord.x) / 2.0 + 0.25)), \ int(round(math.floor(mapcoord.y + mapcoord.y) / 2.0 + 0.25)))
def mouseMoved(self, evt): renderer = fife.InstanceRenderer.getInstance(self.cameras['main']) renderer.removeAllOutlines() pt = fife.ScreenPoint(evt.getX(), evt.getY()) instances = self.getInstancesAt(pt); for i in instances: if i.getObject().getId() in ('girl', 'beekeeper'): renderer.addOutlined(i, 173, 255, 47, 2)
def mouseMoved(self, event): self._instance_renderer.removeOutlined(self._player) pt = fife.ScreenPoint(event.getX(), event.getY()) instances = self._camera.getMatchingInstances(pt, self._actorlayer) for i in instances: if i.getId() == "player": self._instance_renderer.addOutlined(i, 173, 255, 47, 2, 250) break
def mousePressed(self, evt): if fife.MouseEvent.RIGHT == evt.getButton(): self.on_escape() elif fife.MouseEvent.LEFT == evt.getButton(): coords = self.session.view.cam.toMapCoordinates(fife.ScreenPoint(evt.getX(), evt.getY()), False) self.coords = (int(round(coords.x)), int(round(coords.y))) self._mark(self.coords) else: return self.tear_tool_active = False evt.consume()
def mouseDragged(self, event): if self.middle_click_point: if event.isControlPressed(): self.application.view.fineRotate( (event.getX() - self.middle_click_point.x) / 2) else: self.application.view.moveCamera( (self.middle_click_point.x - event.getX()) / 40.0, (self.middle_click_point.y - event.getY()) / 40.0) self.middle_click_point = fife.ScreenPoint(event.getX(), event.getY())
def mousePressed(self, evt): if evt.isConsumedByWidgets(): super(SelectionTool, self).mousePressed(evt) return elif evt.getButton() == fife.MouseEvent.LEFT: selectable = [] instances = self.session.view.cam.getMatchingInstances(\ fife.ScreenPoint(evt.getX(), evt.getY()), self.session.view.layers[LAYERS.OBJECTS]) for i in instances: # Check id, can be '' if instance is created and clicked on before # actual game representation class is created (network play) id = i.getId() if id == '': continue instance = WorldObject.get_object_by_id(int(id)) if instance.is_selectable: selectable.append(instance) if len(selectable) > 1: selectable = selectable[0:0] self.select_old = frozenset( self.session.selected_instances) if evt.isControlPressed( ) else frozenset() selectable = set(self.select_old ^ frozenset(selectable)) for instance in self.session.selected_instances - selectable: instance.deselect() for instance in selectable - self.session.selected_instances: instance.select() self.session.selected_instances = selectable self.select_begin = (evt.getX(), evt.getY()) self.session.ingame_gui.hide_menu() elif evt.getButton() == fife.MouseEvent.RIGHT: target_mapcoord = self.session.view.cam.toMapCoordinates(\ fife.ScreenPoint(evt.getX(), evt.getY()), False) for i in self.session.selected_instances: if i.movable: Act(i, target_mapcoord.x, target_mapcoord.y).execute(self.session) else: super(SelectionTool, self).mousePressed(evt) return evt.consume()
def mousePressed(self, event): if event.isConsumedByWidgets(): return if event.getButton() == fife.MouseEvent.LEFT: self.select_begin = (event.getX(), event.getY()) #self._test.rect_test(event.getX(), event.getY()) clickpoint = fife.ScreenPoint(event.getX(), event.getY()) self._test.movePlayer(clickpoint) event.consume()
def mousePressed(self, event): # Mouse press was consumed by a PyChan widget so lets ignore it. # Dont worry too much about this yet. This will come into play once # we have a GUI in place. if event.isConsumedByWidgets(): return clickpoint = fife.ScreenPoint(event.getX(), event.getY()) # Tell the application to move the player instance to the screen # coordinate where the user clicked. self._application.movePlayer(clickpoint)
def mousePressed(self, event): # Mouse press was consumed by a PyChan widget so lets ignore it. if event.isConsumedByWidgets(): return # Save the click point in screen coordinates clickpoint = fife.ScreenPoint(event.getX(), event.getY()) if self._application.maploaded: # Tell the application to move the player instance to the screen # coordinate where the user clicked. self._application.movePlayer(clickpoint)
def mouseReleased(self, evt): """Tear selected instances and set selection tool as cursor""" if fife.MouseEvent.LEFT == evt.getButton(): coords = self.session.view.cam.toMapCoordinates(fife.ScreenPoint(evt.getX(), evt.getY()), False) if self.coords is None: self.coords = (int(round(coords.x)), int(round(coords.y))) self._mark(self.coords, (int(round(coords.x)), int(round(coords.y)))) for i in self.selected: Tear(i).execute(self.session) self.tear_tool_active = False self.session.cursor = SelectionTool(self.session) evt.consume()
def _prepare_zoom_to_cursor(self, zoom): """Change the camera's position to accommodation zooming to the specified setting.""" def middle(click_coord, scale, length): mid = length / 2.0 return int(round(mid - (click_coord - mid) * (scale - 1))) scale = self.cam.getZoom() / zoom x, y = horizons.globals.fife.cursor.getPosition() new_x = middle(x, scale, horizons.globals.fife.engine_settings.getScreenWidth()) new_y = middle(y, scale, horizons.globals.fife.engine_settings.getScreenHeight()) screen_point = fife.ScreenPoint(new_x, new_y) map_point = self.cam.toMapCoordinates(screen_point, False) self.center(map_point.x, map_point.y)
def mouseMoved(self, event): # pylint: disable=C0103,W0221 """Called when the mouse was moved. Args: event: The mouse event """ for callback_data in self.callbacks["mouse_moved"]: func = callback_data["func"] click_point = fife.ScreenPoint(event.getX(), event.getY()) func(click_point) GameSceneListener.mouseMoved(self, event) controller = self.gamecontroller if controller is not None: controller.application.highlight_selected_object()
def mousePressed(self, evt): if evt.isConsumedByWidgets(): return clickpoint = fife.ScreenPoint(evt.getX(), evt.getY()) if (evt.getButton() == fife.MouseEvent.LEFT): self.hide_instancemenu() self.hero.run( self.getLocationAt(clickpoint) ) if (evt.getButton() == fife.MouseEvent.RIGHT): instances = self.getInstancesAt(clickpoint) print "selected instances on agent layer: ", [i.getObject().getId() for i in instances] if instances: self.show_instancemenu(clickpoint, instances[0])
def mousePressed(self, evt): if evt.isConsumedByWidgets(): return clickpoint = fife.ScreenPoint(evt.getX(), evt.getY()) if (evt.getButton() == fife.MouseEvent.LEFT): loc = self.getLocationAt(clickpoint).getMapCoordinates() self.survivor.take_action(loc) if (evt.getButton() == fife.MouseEvent.RIGHT): instances = self.getInstancesAt(clickpoint) print "selected instances on agent layer: ", [ i.getObject().getId() for i in instances ]
def mouseDragged(self, evt): if evt.getButton() == fife.MouseEvent.LEFT and hasattr( self, 'select_begin'): x, y = self.select_begin xx, yy = evt.getX(), evt.getY() do_multi = ((x - xx)**2 + (y - yy)**2) >= 10 # from 3px (3*3 + 1) self.session.view.renderer['GenericRenderer'].removeAll( self.__class__._SELECTION_RECTANGLE_NAME) if do_multi: # draw a rectangle xmin, xmax = min(x, xx), max(x, xx) ymin, ymax = min(y, yy), max(y, yy) a = fife.Point(xmin, ymin) b = fife.Point(xmax, ymin) c = fife.Point(xmax, ymax) d = fife.Point(xmin, ymax) self._draw_rect_line(a, b) self._draw_rect_line(b, c) self._draw_rect_line(d, c) self._draw_rect_line(d, a) area = fife.Rect(xmin, ymin, xmax - xmin, ymax - ymin) else: area = fife.ScreenPoint(xx, yy) instances = self.session.view.cam.getMatchingInstances( area, self.session.view.layers[LAYERS.OBJECTS], False) # False for accurate # get selection components instances = (self.fife_instance_to_uh_instance(i) for i in instances) instances = [i for i in instances if i is not None] # We only consider selectable items when dragging a selection box. instances = self.filter_selectable(instances) # If there is at least one player unit, we don't select any enemies. # This applies to both buildings and ships. if any( (self.is_owned_by_player(instance) for instance in instances)): instances = self.filter_owner(instances) self._update_selection(instances, do_multi) elif evt.getButton() == fife.MouseEvent.RIGHT: pass else: super(SelectionTool, self).mouseDragged(evt) return evt.consume()
def mousePressed(self, event): return clickpoint = fife.ScreenPoint(event.getX(), event.getY()) if (event.getButton() == fife.MouseEvent.MIDDLE): self.middle_click_point = clickpoint self.application.camera.detach() elif (event.getButton() == fife.MouseEvent.RIGHT): if self.application.paused: return elif (event.getButton() == fife.MouseEvent.LEFT): if self.application.paused: return
def mouseDragged(self, event): instancerenderer = fife.InstanceRenderer.getInstance( self._test._camera) instancerenderer.removeAllColored() instancerenderer.removeAllOutlines() if event.getButton() == fife.MouseEvent.LEFT and hasattr( self, 'select_begin'): do_multi = ((self.select_begin[0] - event.getX())**2 + (self.select_begin[1] - event.getY())** 2) >= 10 # from 3px (3*3 + 1) genericrenderer = fife.GenericRenderer.getInstance( self._test._camera) genericrenderer.removeAll("selection") if do_multi: # draw a rectangle a = fife.Point(min(self.select_begin[0], event.getX()), \ min(self.select_begin[1], event.getY())) b = fife.Point(max(self.select_begin[0], event.getX()), \ min(self.select_begin[1], event.getY())) c = fife.Point(max(self.select_begin[0], event.getX()), \ max(self.select_begin[1], event.getY())) d = fife.Point(min(self.select_begin[0], event.getX()), \ max(self.select_begin[1], event.getY())) genericrenderer.addLine("selection", \ fife.RendererNode(a), fife.RendererNode(b), 200, 200, 200) genericrenderer.addLine("selection", \ fife.RendererNode(b), fife.RendererNode(c), 200, 200, 200) genericrenderer.addLine("selection", \ fife.RendererNode(d), fife.RendererNode(c), 200, 200, 200) genericrenderer.addLine("selection", \ fife.RendererNode(a), fife.RendererNode(d), 200, 200, 200) instances = self._test._camera.getMatchingInstances(\ fife.Rect(min(self.select_begin[0], event.getX()), \ min(self.select_begin[1], event.getY()), \ abs(event.getX() - self.select_begin[0]), \ abs(event.getY() - self.select_begin[1])) if do_multi else fife.ScreenPoint(event.getX(), event.getY()), self._test._actorlayer, 0) # False for accurate for instance in instances: instancerenderer.addColored(instance, 250, 50, 250) instancerenderer.addOutlined(instance, 255, 255, 0, 2)
def screenToMapCoordinates(self, screenx, screeny): """ Convert screen coordinates to map coordinates, including z """ if not self._camera: if self.debug: print 'No camera bind yet in screenToMapCoordinates' return if not self._layer: if self.debug: print 'No layer assigned in screenToMapCoordinates' return screencoords = fife.ScreenPoint(screenx, screeny) z_offset = self._camera.getZOffset(self._layer) if self._layer == self._camera.getLocation().getLayer(): instance_z = float( -self._camera.getLocation().getExactLayerCoordinates().z) layer_z = float(self._layer.getCellGrid().getZShift() - ( self._camera.getLocation().getMapCoordinates().z - self._camera.getLocation().getExactLayerCoordinates().z)) z_offset.x *= int(instance_z + layer_z * self._layer.getCellGrid().getXScale()) z_offset.y *= int(instance_z + layer_z * self._layer.getCellGrid().getYScale()) else: instance_z = float(-self._camera.getLocation(). getExactLayerCoordinates(self._layer).z) layer_z = float(self._layer.getCellGrid().getZShift() - (self._camera.getLocation().getMapCoordinates().z - self._camera.getLocation( ).getExactLayerCoordinates(self._layer).z)) z_offset.x *= int(instance_z + layer_z * self._layer.getCellGrid().getXScale()) z_offset.y *= int(instance_z + layer_z * self._layer.getCellGrid().getYScale()) if (z_offset.z <= 0): screencoords.y += int(z_offset.y) else: screencoords.y -= int(z_offset.y) mapCoords = self._camera.toMapCoordinates(screencoords, False) mapCoords.z = self._layer.getCellGrid().getZShift() return mapCoords
def get_hover_instances(self, where, layers=None): """ Utility method, returns the instances under the cursor @param where: anything supporting getX/getY @param layers: list of layer ids to search for. Default to OBJECTS """ if self.consumed_by_widgets: return [] if layers is None: layers = [LAYERS.OBJECTS] all_instances = [] for layer in layers: x = where.getX() y = where.getY() instances = self.session.view.cam.getMatchingInstances( fife.ScreenPoint(x, y), self.session.view.layers[layer], False) # False for accurate # if no instances found, try again and search within a 8px radius if not instances: selection_radius = 8 radius = fife.Rect(x - selection_radius, y - selection_radius, selection_radius * 2, selection_radius * 2) instances = self.session.view.cam.getMatchingInstances( radius, self.session.view.layers[layer]) all_instances.extend(instances) hover_instances = [] for i in all_instances: id = i.getId() # Check id, can be '' if instance is created and clicked on before # actual game representation class is created (network play) if id == '': continue instance = WorldObject.get_object_by_id(int(id)) hover_instances.append(instance) return hover_instances
def moveCamera(self, camera_move_x, camera_move_y): scr_coord = self.camera.getOrigin() + fife.ScreenPoint( camera_move_x, camera_move_y) coord = self.camera.toMapCoordinates(scr_coord, False) coord.z = 0 # limit the camera to the current map's borders map_size = self.application.maplayer.getCellCache().getSize() if coord.x < map_size.x: coord.x = map_size.x if coord.y < map_size.y: coord.y = map_size.y if coord.x > map_size.w: coord.x = map_size.w if coord.y > map_size.h: coord.y = map_size.h loc = self.camera.getLocation() loc.setMapCoordinates(coord) self.camera.setLocation(loc)
def deselectCell(self, screenx, screeny): """ Deselects a cell at a position on screen """ if not self._camera: if self.debug: print 'No camera bind yet, cannot select any cell' return if not self._layer: if self.debug: print 'No layer assigned in selectCell' return mapCoords = self._camera.toMapCoordinates(fife.ScreenPoint(screenx, screeny), False) position = self._layer.getCellGrid().toLayerCoordinates(mapCoords) loc = fife.Location(self._layer) loc.setLayerCoordinates(position) for i in self._selection: if loc == i: self._selection.remove( loc ) fife.CellSelectionRenderer.getInstance(self._camera).deselectLocation(loc) return
def screen_coords_to_map_coords(self, click, layer): """Converts the screen coordinates to coordinates on the active map Args: click: Screen coordinates as fife.ScreenPoint or position tuple layer: The name of the layer the converted position should be on Returns: Converted coordinates as fife.Location """ active_map = self.current_map if active_map is None: return None if not isinstance(click, fife.ScreenPoint): click = fife.ScreenPoint(click[0], click[1]) coord = active_map.camera.toMapCoordinates(click, False) coord.z = 0 location = fife.Location(active_map.get_layer(layer)) location.setMapCoordinates(coord) return location
def moveCamera(self, camera_move_x, camera_move_y): scr_coord = self.camera.getOrigin() + fife.ScreenPoint( camera_move_x, camera_move_y) coord = self.camera.toMapCoordinates(scr_coord, False) coord.z = 0 #cur_rot = self.camera.getRotation() #cell_dimensions = self.camera.getCellImageDimensions() #new_coord2 = self.camera.getLocation().getMapCoordinates() #new_coord2.x += ((math.cos(cur_rot / 180 * math.pi) * camera_move_x / cell_dimensions.x # - math.sin(cur_rot / 180 * math.pi) * camera_move_y / cell_dimensions.y) # / self.target_zoom * math.sqrt(2)) #new_coord2.y += ((math.cos(cur_rot / 180 * math.pi) * camera_move_y / cell_dimensions.y # + math.sin(cur_rot / 180 * math.pi) * camera_move_x / cell_dimensions.x) # / self.target_zoom * math.sqrt(2)) #x_error = abs(new_coord2.x - new_coord.x) #y_error = abs(new_coord2.y - new_coord.y) #if x_error > self.x_error: # print(self.x_error, self.y_error) # self.x_error = x_error #if y_error > self.y_error: # print(self.x_error, self.y_error) # self.y_error = y_error # limit the camera to the current map's borders map_size = self.application.maplayer.getCellCache().getSize() if coord.x < map_size.x: coord.x = map_size.x if coord.y < map_size.y: coord.y = map_size.y if coord.x > map_size.w: coord.x = map_size.w if coord.y > map_size.h: coord.y = map_size.h loc = self.camera.getLocation() loc.setMapCoordinates(coord) self.camera.setLocation(loc)
def mousePressed(self, event): clickpoint = fife.ScreenPoint(event.getX(), event.getY()) if (event.getButton() == fife.MouseEvent.MIDDLE): self.middle_click_point = clickpoint self.application.camera.detach() if (event.getButton() == fife.MouseEvent.RIGHT): instances = self.application.view.getInstancesAt(clickpoint) if instances: if instances[0].getObject().getId() == 'tile': self.application.gui.new_character.addCharacter( self.application.world.visual.findTile(instances[0])) if (event.getButton() == fife.MouseEvent.LEFT): instances = self.application.view.getInstancesAt(clickpoint) if instances: if not self.application.selected_action: if self.application.current_character and instances[ 0].getObject().getId() == 'tile': tile = self.application.world.visual.findTile( instances[0]) if tile.movement_cost <= self.application.current_character.cur_AP and tile.movement_cost > 0: # walk current character to clicked tile self.application.battle_controller.run(tile) if self.application.selected_action: # execute selected combat action if instances[0].getObject().getId() in [ "boy", "tile", "mushroom" ]: target = self.application.world.visual.findObject( instances[0]) if self.application.world.isValidTarget( self.application.current_character, target, self.application.selected_action. targeting_rules): self.application.battle_controller.executeAction( self.application.selected_action, target)
def mouseMoved(self, evt): if not self.session.world.inited: return mousepoint = fife.ScreenPoint(evt.getX(), evt.getY()) # Status menu update current = self.session.view.cam.toMapCoordinates(mousepoint, False) if abs((current.x - self.lastmoved.x)**2 + (current.y - self.lastmoved.y)**2) >= 4**2: self.lastmoved = current island = self.session.world.get_island( Point(int(round(current.x)), int(round(current.y)))) if island: settlement = island.get_settlement( Point(int(round(current.x)), int(round(current.y)))) if settlement: self.session.ingame_gui.resourceinfo_set(settlement) else: self.session.ingame_gui.resourceinfo_set(None) else: self.session.ingame_gui.resourceinfo_set(None) # Mouse scrolling x, y = 0, 0 if mousepoint.x < 5: x -= 5 - mousepoint.x elif mousepoint.x >= (self.session.view.cam.getViewPort().right() - 5): x += 6 + mousepoint.x - self.session.view.cam.getViewPort().right() if mousepoint.y < 5: y -= 5 - mousepoint.y elif mousepoint.y >= (self.session.view.cam.getViewPort().bottom() - 5): y += 6 + mousepoint.y - self.session.view.cam.getViewPort().bottom( ) x *= 10 y *= 10 self.session.view.autoscroll(x, y)