Пример #1
0
class PaintTools:
    def __init__(self, main):
        self._paint_tool_icons_loc = "resources/paint_tool_icons.png"
        self._paint_tool_icons = None
        self.loadIcons(self._paint_tool_icons_loc)

        # Current active brush being used in the map editor.
        self._active_brush = Brushes.NONE
        self._icon_size = 64

        # Pencil Brush
        self._pencil_brush = QAction("Pencil Brush", main)
        self._pencil_brush.setCheckable(True)
        pencil_icon = self.getIcon(0, 0)
        self._pencil_brush.setIcon(pencil_icon)
        self._pencil_brush.triggered.connect(
            self.changeActiveBrush(Brushes.PENCIL, self._pencil_brush))

        # Tile Drag Brush
        self._tile_drag_brush = QAction("Tile Drag Brush", main)
        self._tile_drag_brush.setCheckable(True)
        tile_drag_icon = self.getIcon(64, 0)
        self._tile_drag_brush.setIcon(tile_drag_icon)
        self._tile_drag_brush.triggered.connect(
            self.changeActiveBrush(Brushes.TILE_DRAG, self._tile_drag_brush))

        # Eraser Brush
        self._eraser_brush = QAction("Eraser Brush", main)
        self._eraser_brush.setCheckable(True)
        eraser_icon = self.getIcon(self._icon_size * 6, 0)
        self._eraser_brush.setIcon(eraser_icon)
        self._eraser_brush.triggered.connect(
            self.changeActiveBrush(Brushes.ERASER, self._eraser_brush))
        # The last brush used
        self._last_brush_used = None

    # Changes the active brush depending on which brush tool button was pressed.
    def changeActiveBrush(self, brush, tool_button):
        def activeBrush():
            if tool_button.isChecked():
                if self._last_brush_used != None and self._last_brush_used != tool_button:
                    self._last_brush_used.setChecked(False)
                self._active_brush = brush
                self._last_brush_used = tool_button
            else:
                self._active_brush = Brushes.NONE

        return activeBrush

    def loadIcons(self, file_path, scale=2):
        self._paint_tool_icons = QPixmap(file_path)
        #self._paint_tool_icons = self._paint_tool_icons.scaled(QSize(self._paint_tool_icons.size().width()*scale, self._paint_tool_icons.size().height()*scale))

    def getIcon(self, x, y, w=64, h=64):
        clip_space = QRect(x, y, w, h)
        clipped = self._paint_tool_icons.copy(clip_space)
        icon = QIcon()
        icon.addPixmap(clipped)
        return icon

    def currentActiveBrush(self):
        return self._active_brush
Пример #2
0
class Zone(QPushButton):
    """
    Visible object that represents a zone in the game board. It allows the user
    to respond to requests made by the Game Master.
    pinkPixmap is the non-visible, interactive part of the widget (the button).
    whitePixmap is the visible, non-interactive part (the canvas).
    """
    zoneActivated = Signal(str)
    zoneSelected = Signal(str)

    @property
    def selectable(self):
        return self._selectable

    @selectable.setter
    def selectable(self, value):
        self._selectable = value
        if self._selectable:
            self.label.setPixmap(self.pinkPixmap)
        else:
            self.label.setPixmap(self.greyPixmap)

    def __init__(self,
                 parent=None,
                 name="Empty",
                 position=(0, 0),
                 size=(10, 10),
                 scale=1.0,
                 *args):
        super(Zone, self).__init__(parent)

        self.name = name
        self._selectable = False
        self.hold = False
        self.selected = False

        scaledX = position[0] * scale
        scaledY = position[1] * scale
        scaledW = size[0] * scale
        scaledH = size[1] * scale
        self.move(scaledX, scaledY)
        self.setMinimumSize(scaledW, scaledH)
        # self.setMaximumSize(300, 350)
        self.pinkPixmap = QPixmap('assets/zonesBitmaps/' + name + '.png')
        self.pinkPixmap = self.pinkPixmap.scaledToWidth(scaledW)
        self.whitePixmap = self.pinkPixmap.copy()
        self.whitePixmap.fill()
        self.greyPixmap = self.pinkPixmap.copy()
        self.greyPixmap.fill(QColor(100, 100, 100))
        # Done: Turn 'selectable' into a property that sets the unhover pixmap
        self.label = QLabel(self)
        self.label.setPixmap(self.greyPixmap)
        self.label.setScaledContents(True)

        self.setMask(self.pinkPixmap.mask())  # THIS DOES THE MAGIC

        # self.connect(self.button, SIGNAL('clicked()'), self.onClick)
        # self.button.clicked.connect(self.onClick())
        # self.connect(self.button, SIGNAL('hoverIn()'), self.onEnter)
        # self.connect(self.button, SIGNAL('hoverOut()'), self.onLeave)

    def enterEvent(self, ev):
        # self.emit(SIGNAL('hoverIn()'))
        if self.selectable:
            self.label.setPixmap(self.whitePixmap)
        # print('Inside')

    def leaveEvent(self, ev):
        # self.emit(SIGNAL('hoverOut()'))
        if self.selectable:
            self.label.setPixmap(self.pinkPixmap)

        # print('Outside')

    def mousePressEvent(self, ev):
        if self.selectable:
            if not self.selected:
                self.zoneActivated.emit(self.name + ' was clicked')
                print(self.name + ' clicked')
                self.zoneSelected.emit(self.name)
                if self.hold:
                    self.selected = True
            else:
                self.selected = False