-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameTile.py
57 lines (43 loc) · 2.42 KB
/
GameTile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from typing import Optional
from PySide6.QtWidgets import *
from PySide6.QtCore import * # type: ignore
from PySide6.QtGui import * # type: ignore
class GameTile(QLabel):
clicked = Signal()
def __init__(self, image: QPixmap, imageHeight: int, expandedImageHeight: int, parent: Optional[QWidget] = None) -> None:
super().__init__(parent)
# Scale the image to 600x900 (or whatever x 900) first so the rounded corners are consistent
image = image.scaledToHeight(900, mode=Qt.TransformationMode.SmoothTransformation)
# I want all images to be the same size, but I also want to animate the width instead of the height,
# so I convert the height to a width when the tile is created
ratio = image.width() / image.height()
# GameTile seems to actually be slightly bigger than the image,
# so make image smaller so GameTile is correct size
self.baseImageWidth = int(imageHeight * ratio - 2)
self.expandedImageWidth = int(expandedImageHeight * ratio - 2)
self._imageWidth = self.baseImageWidth
# Image (with rounded corners)
radius = 30
self.imagePixmap = QPixmap(image.size())
self.imagePixmap.fill(Qt.GlobalColor.transparent)
painter = QPainter(self.imagePixmap)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
painter.setBrush(QBrush(image))
painter.setPen(Qt.PenStyle.NoPen)
painter.drawRoundedRect(image.rect(), radius, radius)
self.setPixmap(self.imagePixmap.scaledToWidth(self._imageWidth, Qt.TransformationMode.SmoothTransformation))
self.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
def mousePressEvent(self, _e: QMouseEvent) -> None:
self.clicked.emit()
@Property(int)
def imageWidth(self) -> int:
return self._imageWidth
@imageWidth.setter # type: ignore
def imageWidth(self, value: int) -> None:
if value == self.baseImageWidth or self.expandedImageWidth:
# If the animation is over, do a higher quality transformation
self.setPixmap(self.imagePixmap.scaledToWidth(value, Qt.TransformationMode.SmoothTransformation))
else:
# Do fast transformation while animating
self.setPixmap(self.imagePixmap.scaledToWidth(value, Qt.TransformationMode.FastTransformation))
self._imageWidth = value