Example #1
0
class QPokerWidget(QWidget):
    def __init__(self, dataDir="", parent=None):
        QWidget.__init__(self, parent)
        self.renderer = QSvgRenderer(dataDir + "poker.svg")
        self.scene = QGraphicsScene()
        self.chat = QGraphicsSimpleTextItem()
        self.table = QGraphicsSvgItem(dataDir + "poker.svg")
        self.table.setSharedRenderer(self.renderer)
        self.table.setElementId("table")
        self.table.setMatrix(self.renderer.matrixForElement("transform_table"))
        self.scene.addItem(self.chat)
        self.scene.addItem(self.table)
        self.board = []
        for i in range(5):
            card = AnimatedGraphicsSvgItem(dataDir + "svg-cards.svg",
                                           self.table)
            card.setElementId("back")
            parent = self.renderer.matrixForElement("transform_table")
            child = self.renderer.matrixForElement("transform_card%i" % i)
            cardMatrix = child.translate(-parent.dx(), -parent.dy())
            card.setMatrix(cardMatrix)
            #card.setFlag(QGraphicsSvgItem.ItemIsMovable, True)
            card.scale(0.5, 0.5)
            card.hide()
            self.scene.addItem(card)
            self.board.append(card)
        self.seats = []
        self.names = []
        self.moneys = []
        self.bets = []
        for i in range(10):
            seat = SeatItem()

            def seatClickedEvent(seat=i):
                seatClickedCallback = self.seatClicked
                seatClickedCallback(seat)

            seat.event = seatClickedEvent
            seat.setSharedRenderer(self.renderer)
            seat.setElementId("seat")
            seat.setMatrix(
                self.renderer.matrixForElement("transform_seat%i" % i))
            self.scene.addItem(seat)
            self.seats.append(seat)
            name = QGraphicsSimpleTextItem(seat)
            name.setMatrix(self.renderer.matrixForElement("seat_name"))
            self.scene.addItem(name)
            self.names.append(name)
            money = QGraphicsSimpleTextItem(seat)
            money.setMatrix(self.renderer.matrixForElement("seat_money"))
            self.scene.addItem(money)
            self.moneys.append(money)
            bet = QGraphicsSimpleTextItem()
            bet.setMatrix(self.renderer.matrixForElement("transform_bet%i" %
                                                         i))
            self.scene.addItem(bet)
            self.bets.append(bet)
        self.pots = []
        for i in range(9):
            pot = QGraphicsSimpleTextItem()
            pot.setMatrix(self.renderer.matrixForElement("transform_pot%i" %
                                                         i))
            self.scene.addItem(pot)
            self.pots.append(pot)
        self.view = QGraphicsView(self)
        self.view.setScene(self.scene)
        self.view.resize(800, 600)
        self.fold = ActionItem()
        self.fold.setText("fold")
        self.fold.setPos(0, 550)
        self.scene.addItem(self.fold)
        self.fold.event = lambda: self.foldClicked()
        self.check = ActionItem()
        self.check.setText("check")
        self.check.setPos(50, 550)
        self.scene.addItem(self.check)
        self.check.event = lambda: self.checkClicked()
        self.call = ActionItem()
        self.call.setText("call")
        self.call.setPos(100, 550)
        self.scene.addItem(self.call)
        self.call.event = lambda: self.callClicked()
        self.bet = ActionItem()
        self.bet.setText("bet")
        self.bet.setPos(150, 550)
        self.scene.addItem(self.bet)
        self.bet.event = lambda: self.betClicked()

    def renderChat(self, message):
        self.chat.setText(message)

    def renderBoard(self, cards):
        for i in range(len(cards)):
            item = self.board[i]
            card = cards[i]
            item.setElementId(card2SvgElement(card))
            if item.isVisible() == False:
                item.show()
        for i in range(len(cards), len(self.board)):
            item = self.board[i]
            item.hide()
            item.setElementId('back')

    def renderStart(self):
        map(lambda svgItem: svgItem.setElementId('back'), self.board)
        map(lambda svgItem: svgItem.hide(), self.board)

    def renderPlayerArrive(self, seat, name):
        self.names[seat].setText(name)
        self.names[seat].show()

    def renderPlayerLeave(self, seat):
        self.names[seat].setText('')
        self.names[seat].hide()
        self.moneys[seat].hide()
        self.bets[seat].hide()

    def renderPlayerChips(self, seat, money, bet):
        if bet > 0:
            self.bets[seat].setText(str(bet))
            self.bets[seat].show()
        else:
            self.bets[seat].hide()
        self.moneys[seat].setText(str(money))
        self.moneys[seat].show()

    def renderPot(self, index, amount):
        self.pots[index].setText(str(amount))
        self.pots[index].show()

    def renderPotReset(self):
        for pot in self.pots:
            pot.setText("")
            pot.hide()

    def renderPosition(self, seatInPosition):
        for i in range(len(self.seats)):
            seat = self.seats[i]
            if i == seatInPosition:
                seat.setElementId("seat_inposition")
            else:
                seat.setElementId("seat")

    def renderPositionReset(self):
        for seat in self.seats:
            seat.setElementId("seat")

    def keyPressEvent(self, event):
        if event.text() == "q":
            self.view.scale(1.1, 1.1)
        elif event.text() == "a":
            self.view.scale(0.9, 0.9)

    seatClicked = lambda seat: None
    foldClicked = lambda: None
    checkClicked = lambda: None
    callClicked = lambda: None
    betClicked = lambda: None
Example #2
0
class PlayerWind(QGraphicsEllipseItem):
    """a round wind tile"""
    def __init__(self, name, tileset, roundsFinished=0, parent = None):
        """generate new wind tile"""
        if not len(WINDPIXMAPS):
            WINDPIXMAPS[('E', False)] = None # avoid recursion
            self.genWINDPIXMAPS()
        QGraphicsEllipseItem.__init__(self)
        if parent:
            self.setParentItem(parent)
        self.name = name
        self.face = QGraphicsSvgItem()
        self.face.setParentItem(self)
        self.prevailing = None
        self.setWind(name, roundsFinished)
        self.tileset = tileset
        self.__sizeFace()

    @staticmethod
    def genWINDPIXMAPS():
        """prepare wind tiles"""
        tileset = Tileset(Preferences.windTilesetName)
        for wind in WINDS:
            for prevailing in False, True:
                pwind = PlayerWind(wind, tileset, prevailing)
                pMap = QPixmap(40, 40)
                pMap.fill(Qt.transparent)
                painter = QPainter(pMap)
                painter.setRenderHint(QPainter.Antialiasing)
                painter.scale(0.40, 0.40)
                pwind.paint(painter, QStyleOptionGraphicsItem())
                for child in pwind.childItems():
                    if isinstance(child, QGraphicsSvgItem):
                        painter.save()
                        painter.translate(child.mapToParent(0.0, 0.0))
                        child.paint(painter, QStyleOptionGraphicsItem())
                        painter.restore()
                WINDPIXMAPS[(wind, prevailing)] = pMap

    def __sizeFace(self):
        """size the chinese character depending on the wind tileset"""
        self.resetTransform()
        size = self.tileset.faceSize
        self.setFlag(QGraphicsItem.ItemClipsChildrenToShape)
        diameter = size.height() * 1.1
        scaleFactor = 0.9
        facePos = {'traditional':(10, 10), 'default':(15, 10),
                   'classic':(19, 1), 'jade':(19, 1)}
        self.setRect(0, 0, diameter, diameter)
        self.setScale(scaleFactor)
        faceX, faceY = facePos[self.tileset.desktopFileName]
        self.face.setPos(faceX, faceY)
        self.face.setSharedRenderer(self.tileset.renderer())

    def setWind(self, name, roundsFinished):
        """change the wind"""
        self.name = name
        if isinstance(roundsFinished, bool):
            self.prevailing = roundsFinished
        else:
            self.prevailing = name == WINDS[roundsFinished % 4]
        self.setBrush(ROUNDWINDCOLOR if self.prevailing else QColor('white'))
        windtilenr = {'N':1, 'S':2, 'E':3, 'W':4}
        self.face.setElementId('WIND_%d' % windtilenr[name])
Example #3
0
class PlayerWind(QGraphicsEllipseItem):
    """a round wind tile"""
    def __init__(self, name, tileset, roundsFinished=0, parent = None):
        """generate new wind tile"""
        if not len(WINDPIXMAPS):
            WINDPIXMAPS[('E', False)] = None # avoid recursion
            self.genWINDPIXMAPS()
        QGraphicsEllipseItem.__init__(self)
        if parent:
            self.setParentItem(parent)
        self.name = name
        self.face = QGraphicsSvgItem()
        self.face.setParentItem(self)
        self.prevailing = None
        self.setWind(name, roundsFinished)
        self.tileset = tileset
        self.__sizeFace()

    @staticmethod
    def genWINDPIXMAPS():
        """prepare wind tiles"""
        tileset = Tileset(common.PREF.windTilesetName)
        for wind in WINDS:
            for prevailing in False, True:
                pwind = PlayerWind(wind, tileset, prevailing)
                pMap = QPixmap(40, 40)
                pMap.fill(Qt.transparent)
                painter = QPainter(pMap)
                painter.setRenderHint(QPainter.Antialiasing)
                painter.scale(0.40, 0.40)
                pwind.paint(painter, QStyleOptionGraphicsItem())
                for child in pwind.childItems():
                    if isinstance(child, QGraphicsSvgItem):
                        painter.save()
                        painter.translate(child.mapToParent(0.0, 0.0))
                        child.paint(painter, QStyleOptionGraphicsItem())
                        painter.restore()
                WINDPIXMAPS[(wind, prevailing)] = pMap

    def __sizeFace(self):
        """size the chinese character depending on the wind tileset"""
        self.resetTransform()
        size = self.tileset.faceSize
        self.setFlag(QGraphicsItem.ItemClipsChildrenToShape)
        diameter = size.height() * 1.1
        scaleFactor = 0.9
        facePos = {'traditional':(10, 10), 'default':(15, 10),
                   'classic':(19, 1), 'jade':(19, 1)}
        self.setRect(0, 0, diameter, diameter)
        self.setScale(scaleFactor)
        faceX, faceY = facePos[self.tileset.desktopFileName]
        self.face.setPos(faceX, faceY)
        self.face.setSharedRenderer(self.tileset.renderer())

    def setWind(self, name, roundsFinished):
        """change the wind"""
        self.name = name
        if isinstance(roundsFinished, bool):
            self.prevailing = roundsFinished
        else:
            self.prevailing = name == WINDS[roundsFinished % 4]
        self.setBrush(ROUNDWINDCOLOR if self.prevailing else QColor('white'))
        windtilenr = {'N':1, 'S':2, 'E':3, 'W':4}
        self.face.setElementId('WIND_%d' % windtilenr[name])
Example #4
0
class QPokerWidget(QWidget):
    def __init__(self, dataDir = "", parent = None):
        QWidget.__init__(self, parent)
        self.renderer = QSvgRenderer(dataDir + "poker.svg")
        self.scene = QGraphicsScene()
        self.chat = QGraphicsSimpleTextItem()
        self.table = QGraphicsSvgItem(dataDir + "poker.svg")
        self.table.setSharedRenderer(self.renderer)
        self.table.setElementId("table")
        self.table.setMatrix(self.renderer.matrixForElement("transform_table"))
        self.scene.addItem(self.chat)
        self.scene.addItem(self.table)
        self.board = []
        for i in range(5):
            card = AnimatedGraphicsSvgItem(dataDir + "svg-cards.svg", self.table)
            card.setElementId("back")
            parent = self.renderer.matrixForElement("transform_table")
            child = self.renderer.matrixForElement("transform_card%i" % i)
            cardMatrix = child.translate(-parent.dx(), -parent.dy())
            card.setMatrix(cardMatrix)
            #card.setFlag(QGraphicsSvgItem.ItemIsMovable, True)
            card.scale(0.5, 0.5)
            card.hide()
            self.scene.addItem(card)
            self.board.append(card)
        self.seats = []
        self.names = []
        self.moneys = []
        self.bets = []
        for i in range(10):
            seat = SeatItem()
            def seatClickedEvent(seat = i):                
                seatClickedCallback = self.seatClicked
                seatClickedCallback(seat)
            seat.event = seatClickedEvent
            seat.setSharedRenderer(self.renderer)
            seat.setElementId("seat")
            seat.setMatrix(self.renderer.matrixForElement("transform_seat%i" % i))
            self.scene.addItem(seat)
            self.seats.append(seat)
            name = QGraphicsSimpleTextItem(seat)
            name.setMatrix(self.renderer.matrixForElement("seat_name"))
            self.scene.addItem(name)
            self.names.append(name)
            money = QGraphicsSimpleTextItem(seat)
            money.setMatrix(self.renderer.matrixForElement("seat_money"))
            self.scene.addItem(money)
            self.moneys.append(money)
            bet = QGraphicsSimpleTextItem()            
            bet.setMatrix(self.renderer.matrixForElement("transform_bet%i" % i))
            self.scene.addItem(bet)
            self.bets.append(bet)
        self.pots = []
        for i in range(9):
            pot = QGraphicsSimpleTextItem()
            pot.setMatrix(self.renderer.matrixForElement("transform_pot%i" %i))
            self.scene.addItem(pot)
            self.pots.append(pot)
        self.view = QGraphicsView(self)
        self.view.setScene(self.scene)
        self.view.resize(800, 600)
        self.fold = ActionItem()
        self.fold.setText("fold")
        self.fold.setPos(0, 550)
        self.scene.addItem(self.fold)
        self.fold.event = lambda: self.foldClicked()
        self.check = ActionItem()
        self.check.setText("check")
        self.check.setPos(50, 550)
        self.scene.addItem(self.check)
        self.check.event = lambda: self.checkClicked()
        self.call = ActionItem()
        self.call.setText("call")
        self.call.setPos(100, 550)
        self.scene.addItem(self.call)
        self.call.event = lambda: self.callClicked()
        self.bet = ActionItem()
        self.bet.setText("bet")
        self.bet.setPos(150, 550)
        self.scene.addItem(self.bet)
        self.bet.event = lambda: self.betClicked()
    def renderChat(self, message):
        self.chat.setText(message)
    def renderBoard(self, cards):
        for i in range(len(cards)):
            item = self.board[i]
            card = cards[i]
            item.setElementId(card2SvgElement(card))
            if item.isVisible() == False:
                item.show()
        for i in range(len(cards), len(self.board)):
            item = self.board[i]
            item.hide()
            item.setElementId('back')
    def renderStart(self):
        map(lambda svgItem: svgItem.setElementId('back'), self.board)
        map(lambda svgItem: svgItem.hide(), self.board)
    def renderPlayerArrive(self, seat, name):
        self.names[seat].setText(name)
        self.names[seat].show()
    def renderPlayerLeave(self, seat):
        self.names[seat].setText('')
        self.names[seat].hide()
        self.moneys[seat].hide()
        self.bets[seat].hide()
    def renderPlayerChips(self, seat, money, bet):
        if bet > 0:
            self.bets[seat].setText(str(bet))
            self.bets[seat].show()
        else:
            self.bets[seat].hide()
        self.moneys[seat].setText(str(money))
        self.moneys[seat].show()
    def renderPot(self, index, amount):
        self.pots[index].setText(str(amount))
        self.pots[index].show()
    def renderPotReset(self):
        for pot in self.pots:
            pot.setText("")
            pot.hide()
    def renderPosition(self, seatInPosition):
        for i in range(len(self.seats)):
            seat = self.seats[i]
            if i == seatInPosition:
                seat.setElementId("seat_inposition")
            else:
                seat.setElementId("seat")
    def renderPositionReset(self):
        for seat in self.seats:
            seat.setElementId("seat")
    def keyPressEvent(self, event):
        if event.text() == "q":
            self.view.scale(1.1, 1.1)
        elif event.text() == "a":
            self.view.scale(0.9, 0.9)
    seatClicked = lambda seat: None
    foldClicked = lambda: None
    checkClicked = lambda: None
    callClicked = lambda: None
    betClicked = lambda: None