Пример #1
0
 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
Пример #2
0
class HumanView():

  def __init__(self, game):
    self._gm = game

  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 getPane(self):
    return self._pane

  def setCamera(self, camera):
    self._window.view = camera
  
  def resetCamera(self):
    self._window.view = sfml.View()
    self._window.view.reset(sfml.Rectangle((0,0), self._window.size))
  
  
  def scaleToWindow(self, sprite):
    """Scales the sprite to fit right into a background."""
    (w, h) = sprite.local_bounds.size
    surfaceAspectRatio = w / h
    windowAspectRatio = self.WINDOW_WIDTH / self.WINDOW_HEIGHT
    if surfaceAspectRatio > windowAspectRatio:
      scaleRatio = self.WINDOW_HEIGHT / h
    else:
      scaleRatio = self.WINDOW_WIDTH / w
    
    sprite.scale(scaleRatio)
    
    return sprite
  
  
  def draw(self):
    self.clear()
    self._pane.draw(self)
    self._window.display()

  def drawSprite(self, sprite):
    """Draws a surface on the screen. Should probably not be used directly."""
    self._window.draw(sprite)
  
  def drawLine(self, colour, startPos, endPos, width=1):
    #TODO: Line width not implemented yet
    lines = sfml.VertexArray(sfml.PrimitiveType.LINES_STRIP, 2)
    lines[0].position = startPos
    lines[0].color = colour
    lines[1].position = endPos
    lines[1].color = colour
    self._window.draw(lines)
    
  def clear(self, colour = sfml.Color.BLACK):
    self._window.clear(colour)
  
  # Input
  
  def onMouseEvent(self, event):
    print("[WARNING] HumanView.onMouseEvent called. This has never happened before!")
  
  def onKeyUpEvent(self, event):
    self._pane._onKeyUpEvent(event)
  
  def onKeyDownEvent(self, event):
    self._pane._onKeyDownEvent(event)
  
  def onMouseWheelEvent(self, event):
    self._pane._onMouseWheelEvent(event)
  
  def onMouseUpEvent(self, event):
    self._pane._onMouseUpEvent(event)
  
  def onMouseDownEvent(self, event):
    self._pane._onMouseDownEvent(event)
  
  def onMouseMoveEvent(self, event):
    self._pane._onMouseMoveEvent(event)
    newFocus = self._pane._getMouseFocus(event.position)
    if newFocus != self.mouseFocus:
      if self.mouseFocus != None:
        self.mouseFocus._onMouseDefocusEvent(event)
      self.mouseFocus = newFocus
      if self.mouseFocus != None:
        self.mouseFocus._onMouseFocusEvent(event)