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 loadFromXML(self, XMLRoot): self.name = XMLRoot.find("name").text self.animation = ResourceManager.getAnimation(self.name).produce() self.layer = int(XMLRoot.find("layer").text) self.dp = Vector2(float(0), float(0)) if XMLRoot.find("dx") != None and XMLRoot.find("dy") != None: self.dp = Vector2(float(XMLRoot.find("dx").text), float(XMLRoot.find("dy").text)) self.origin = XMLRoot.find("origin") if self.origin != None: self.origin = Util.parseVector2(self.origin) if XMLRoot.find("width") != None and XMLRoot.find("height") != None: self.size = Vector2(float(XMLRoot.find("width").text), float(XMLRoot.find("height").text)) else: self.size = Vector2(-1, -1) if XMLRoot.find("tileWidth") == None or XMLRoot.find("tileHeight") == None: self.tileSize = None else: self.tileSize = Vector2(float(XMLRoot.find("tileWidth").text), float(XMLRoot.find("tileHeight").text)) self._sprite = Sprite(self.animation._anim.getTexture(), self.animation.getRect()) if type(self.origin) == Vector2: self._sprite.origin = self.origin # Default value for when there is no FacingComponent self._facingLeft = False # This needs to be the ordering. Facing interferes with entitySizeUpdate. self.reset() self.entitySizeUpdate()
def init(self, XMLRoot=None): """Initialisation is called before populating with XML data. The basic implementation simply dynamically assigns variable names and their values, converted to the most sane type found. Reimplement as necessary.""" if XMLRoot != None: for prop in XMLRoot: setattr(self, prop.tag, Util.convert(prop.text))
def getDrawBounds(self): rect = None for sprite in self.sprites: curRect = sprite._sprite.global_bounds if rect == None: rect = curRect else: rect = Util.rectUnion(rect, curRect) return rect
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 init(self, title, graphicsFile): """Given the graphics element of the configuration file, parses the properties therein and sets them as properties of the View class.""" XMLroot = ElementTree.parse(graphicsFile).getroot() for configNode in XMLroot: setattr(self, configNode.tag, Util.convert(configNode.text)) Input.init(self) TextManager.init() # Creates screen self._window = sfml.RenderWindow(sfml.VideoMode(self.WINDOW_WIDTH, self.WINDOW_HEIGHT), title) self._pane = NGUIPane(0, 0, self.WINDOW_WIDTH, self.WINDOW_HEIGHT) self._pane.name = "HumanView" self.mouseFocus = None
def loadFromXML(self, XMLRoot): self._name = XMLRoot.attrib["name"] self._loop = True if "repeat" in XMLRoot.attrib: self._loop = Util.convert(XMLRoot.attrib["repeat"]) self._texture = nEngine.graphics.ResourceManager.ResourceManager.getTexture(XMLRoot.attrib["texture"]) hasChildren = False for frameRoot in XMLRoot: frame = Frame() frame.loadFromXML(frameRoot) self._frames.append(frame) hasChildren = True if not hasChildren: frame = Frame() frame._rect = Rectangle((0,0),(self._texture.size)) self._frames.append(frame)
def init(self, XMLRoot): researchNode = XMLRoot.find("research") if researchNode != None: self._research = Util.convert(researchNode.text)
def init(self, XMLRoot): self.range = Util.convert(XMLRoot.find("range").text) self.rate = Util.convert(XMLRoot.find("rate").text) self.capacity = Util.convert(XMLRoot.find("capacity").text)
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)
def init(self, XMLRoot): self._facingLeft = Util.convert(XMLRoot.find("facingLeft"))