object_selector_widget.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Flexlay - A Generic 2D Game Editor
  2. # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from typing import Optional
  17. import pickle
  18. from PyQt5.QtCore import Qt, QSize, QPoint, QByteArray, QMimeData, QEvent
  19. from PyQt5.QtGui import QDrag, QPainter, QPixmap, QMouseEvent, QResizeEvent, QPaintEvent
  20. from PyQt5.QtWidgets import QSizePolicy, QWidget
  21. from flexlay.color import Color
  22. from flexlay.graphic_context import GraphicContext
  23. from flexlay.math import Rectf, Point, Origin
  24. from flexlay.object_brush import ObjectBrush
  25. class SuperTuxBadGuyData:
  26. def __init__(self) -> None:
  27. pass
  28. class ObjectSelectorWidget(QWidget):
  29. def __init__(self, cell_w: int, cell_h: int, viewport: QWidget, parent: Optional[QWidget] = None) -> None:
  30. super().__init__(parent)
  31. self.viewport: QWidget = viewport
  32. self.cell_width: int = cell_w
  33. self.cell_height: int = cell_h
  34. self.brushes: list[ObjectBrush] = []
  35. self.has_focus = False
  36. self.index = 0
  37. self.mouse_pos: Optional[Point] = None
  38. self.click_pos: Optional[Point] = None
  39. self.mouse_over_tile = -1
  40. self.scale = 1.0
  41. self.drag_obj = -1
  42. self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
  43. self.setMouseTracking(True)
  44. def minimumSizeHint(self) -> QSize:
  45. columns = self.get_columns()
  46. min_rows = (len(self.brushes) + columns - 1) // columns
  47. return QSize(self.cell_width * self.get_columns(),
  48. self.cell_height * min_rows)
  49. def resizeEvent(self, event: QResizeEvent) -> None:
  50. pass
  51. def get_columns(self) -> int:
  52. return self.viewport.width() // self.cell_width
  53. def mousePressEvent(self, event: QMouseEvent) -> None:
  54. if event.button() == Qt.LeftButton:
  55. if self.mouse_over_tile != -1:
  56. self.drag_obj = self.mouse_over_tile
  57. if self.drag_obj != -1:
  58. drag = QDrag(self)
  59. mime_data = QMimeData()
  60. # GRUMBEL obj = SuperTuxBadGuyData()
  61. data = QByteArray(pickle.dumps(self.drag_obj))
  62. mime_data.setData("application/x-supertux-badguy", data)
  63. drag.setMimeData(mime_data)
  64. pixmap = QPixmap.fromImage(self.brushes[self.drag_obj].get_sprite().get_pixelbuffer().get_qimage())
  65. drag.setPixmap(pixmap)
  66. drag.setHotSpot(QPoint(self.brushes[self.drag_obj].get_sprite().width // 2,
  67. self.brushes[self.drag_obj].get_sprite().height // 2))
  68. drag.exec()
  69. self.drag_obj = -1
  70. def mouseReleaseEvent(self, event: QMouseEvent) -> None:
  71. if event.button() == Qt.LeftButton:
  72. if self.drag_obj != -1:
  73. # releaseMouse()
  74. self.drag_obj = -1
  75. def mouseMoveEvent(self, event: QMouseEvent) -> None:
  76. self.mouse_pos = Point.from_qt(event.pos())
  77. cell_w = self.width() / self.get_columns()
  78. x = int(event.x() // cell_w)
  79. y = int(event.y() // self.cell_height)
  80. self.mouse_over_tile = y * self.get_columns() + x
  81. if self.mouse_over_tile < 0 or self.mouse_over_tile >= len(self.brushes):
  82. self.mouse_over_tile = -1
  83. if self.mouse_over_tile > -1:
  84. self.setToolTip(self.brushes[self.mouse_over_tile].get_data())
  85. self.repaint()
  86. def paintEvent(self, event: QPaintEvent) -> None:
  87. painter = QPainter(self)
  88. gc = GraphicContext(painter)
  89. for i in range(len(self.brushes)):
  90. x = i % self.get_columns()
  91. y = i // self.get_columns()
  92. cell_w = self.width() / self.get_columns()
  93. rect = Rectf(x * cell_w, y * self.cell_height,
  94. (x + 1) * cell_w, (y + 1) * self.cell_height)
  95. if (x + y - 1) % 2 == 0:
  96. gc.fill_rect(rect, Color(224, 224, 224))
  97. else:
  98. gc.fill_rect(rect, Color(192, 192, 192))
  99. sprite = self.brushes[i].get_sprite()
  100. sprite.set_alignment(Origin.center, 0, 0)
  101. sprite.set_scale(min(1.0, self.cell_width / sprite.width),
  102. min(1.0, self.cell_height / sprite.height))
  103. sprite.draw(rect.left + rect.width / 2,
  104. rect.top + rect.height / 2,
  105. gc)
  106. # highlight the current selection
  107. if self.mouse_over_tile == i and self.has_focus:
  108. gc.fill_rect(rect, Color(0, 0, 255, 20))
  109. def enterEvent(self, event: QEvent) -> None:
  110. self.has_focus = True
  111. def leaveEvent(self, event: QEvent) -> None:
  112. self.has_focus = False
  113. self.repaint()
  114. def add_brush(self, brush: ObjectBrush) -> None:
  115. self.brushes.append(brush)
  116. # EOF #