def onMouseUpEvent(self, dispatcher, event): if event.button != Mouse.LEFT: return False if not self.clicked: return False self.clicked = False if self.dragRect != Rectangle(): # Dragging units = self.world.getUnitsWithinRectangle(self.dragRect, self.faction) if not Input.isKeyPressed(Keyboard.L_SHIFT) and not Input.isKeyPressed(Keyboard.L_ALT): self.SUController.deselectAll() if Input.isKeyPressed(Keyboard.L_ALT): self.SUController.removeUnits(units) else: self.SUController.addUnits(units) self.dragRect = Rectangle() return True # If the mouse moved too much, don't do anything if Util.vectorToDistance(event.position - self.clickedPos) > SelectionController.SELECT_DISTANCE: return False # If clicked is true and a mouseUp event was caught here, it means we # we haven't stacked another controller on top of this one. So it's safe # to pick a single entity. worldPos = self.gameView.viewToWorld(event.position) unit = self.world.getClosestUnit(worldPos, self.faction) dist = Util.vectorToDistance(unit.getComponent(PositionComponent).p - worldPos) # Stop if closest unit was too far away from mouse if self.gameView.scaleWorldToView(Vector2(dist, 0)).x > SelectionController.SELECT_DISTANCE: if not Input.isKeyPressed(Keyboard.L_SHIFT): self.SUController.deselectAll() return True # Definitely changing selection if not Input.isKeyPressed(Keyboard.L_SHIFT) and not Input.isKeyPressed(Keyboard.L_ALT): self.SUController.deselectAll() if self.doubleClickClock.elapsed_time.milliseconds > SelectionController.DOUBLE_CLICK_INTERVAL: # SINGLE CLICK if Input.isKeyPressed(Keyboard.L_ALT): self.SUController.removeUnit(unit) else: self.SUController.addUnit(unit) else: # DOUBLE CLICK rect = self.gameView.camera.viewRect units = self.world.getUnitsWithinRectangle(rect, self.faction) units = [u for u in units if u.name == unit.name] if Input.isKeyPressed(Keyboard.L_ALT): self.SUController.removeUnits(units) else: self.SUController.addUnits(units) self.doubleClickClock.restart() return True
def onMouseUpEvent(self, _, event): if event.button != Mouse.RIGHT: return False self._moving = False if Util.vectorToDistance(self._origViewPosition - event.position) < CameraController.DRAG_MIN_DISTANCE: return False # Consume input only if moved move-dragged return self._moved
def getClosestUnitFromList(self, p, unitList): units = [u for u in unitList if u.getComponent(PositionComponent) != None] distFun = lambda other: Util.vectorToDistance(other.getComponent(PositionComponent).p - p) return min(units, key=distFun)