Esempio n. 1
0
class ScreenController:
    """ Manages a ScreenView and controllers for other views on the screen """

    def __init__(self):
        self.screenView = ScreenView()
        self.controllers = list()
        self.running = True
        self.mouseDragButton = None
        self.lastTileOverPosition = HexCoordSys.getTilePosition(0, 0)
        self.session = ClientSession()
        self.controllers.append(TileController("Tile", self.session))
        self.controllers.append(PieceController("Piece", self.session, self.controllers["Tile"].tilePositionModel))
        self.mainLoop()

    def mainLoop(self):
        logging.info("Starting main client loop")
        clock = pygame.time.Clock()
        while self.running:
            # Limit to 10 loops a second. The rendering code is quick so this avoids excess CPU utilization.
            clock.tick(10)
            for ctrl in self.controllers:
                ctrl.processAnyNewData()
            for event in pygame.event.get():
                self.processUpdate(event)
            # Clear screen and redraw everything every loop to avoid logic for passing display.update dirty rects
            self.screenView.clearSurface()
            for controller in self.controllers:
                controller.draw(self.screenView.getSurface())
            self.screenView.draw()

    def processUpdate(self, event):
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            self.running = False
        elif event.type == MOUSEMOTION:
            self.screenView.cursor.setPosition(event.pos[0], event.pos[1])
            oldTilePosition = self.lastTileOverPosition
            self.lastTileOverPosition = self.screenView.cursor.getTilePosition()
            # Drag Right/Info click to a new tile
            if self.mouseDragButton is 3 and oldTilePosition != self.lastTileOverPosition:
                self.controllers["Piece"].trialMoveSelectedEvent(self.lastTileOverPosition)
        elif event.type == MOUSEBUTTONDOWN:
            self.mouseDragButton = event.button
            if event.button == 1:  # Left/action click
                self.controllers["Piece"].selectEvent(self.lastTileOverPosition)
        elif event.type == MOUSEBUTTONUP:
            if self.mouseDragButton is 3:  # Release Right/Info click
                self.controllers["Piece"].moveSelectedEvent(self.lastTileOverPosition)
                self.controllers["Piece"].selectEvent(self.lastTileOverPosition)
                self.mouseDragButton = None
Esempio n. 2
0
 def __init__(self):
     self.screenView = ScreenView()
     self.controllers = list()
     self.running = True
     self.mouseDragButton = None
     self.lastTileOverPosition = HexCoordSys.getTilePosition(0, 0)
     self.session = ClientSession()
     self.controllers.append(TileController("Tile", self.session))
     self.controllers.append(PieceController("Piece", self.session, self.controllers["Tile"].tilePositionModel))
     self.mainLoop()