|
| 1 | +/************************************************************************** |
| 2 | + * TileGfxItem.cpp |
| 3 | + * |
| 4 | + * Harmony Engine - Editor Tool |
| 5 | + * Copyright (c) 2025 Jason Knobler |
| 6 | + * |
| 7 | + * Harmony Editor Tool License: |
| 8 | + * https://github.com/GameOverture/HarmonyEngine/blob/master/LICENSE |
| 9 | + *************************************************************************/ |
| 10 | +#include "Global.h" |
| 11 | +#include "TileSetGfxItem.h" |
| 12 | + |
| 13 | +#include <QPainter> |
| 14 | +#include <QGraphicsSceneMouseEvent> |
| 15 | + |
| 16 | +const int iTILE_PADDING = 0; |
| 17 | + |
| 18 | +TileSetGfxItem::TileSetGfxItem(QPointF ptCurPos, QSize vSize, const QPixmap& pixmapRef, const QPolygonF& outlinePolygon) : |
| 19 | + QGraphicsItem(nullptr), |
| 20 | + m_bSelected(true), |
| 21 | + m_pRectItem(nullptr), |
| 22 | + m_pPixmapItem(nullptr), |
| 23 | + m_pShapeItem(nullptr) |
| 24 | +{ |
| 25 | + m_pRectItem = new QGraphicsRectItem(0.0f, 0.0f, vSize.width() + 1, vSize.height() + 1); |
| 26 | + m_pRectItem->setPos(ptCurPos.x(), ptCurPos.y()); |
| 27 | + m_pRectItem->setPen(QPen(QBrush(HyGlobal::ConvertHyColor(HyColor::Green)), 1.0f, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin)); |
| 28 | + m_pRectItem->setParentItem(this); |
| 29 | + |
| 30 | + m_pPixmapItem = new QGraphicsPixmapItem(pixmapRef); |
| 31 | + m_pPixmapItem->setParentItem(this); |
| 32 | + |
| 33 | + m_pShapeItem = new QGraphicsPolygonItem(outlinePolygon); |
| 34 | + m_pShapeItem->setPen(QPen(QBrush(HyGlobal::ConvertHyColor(HyColor::Orange)), 1.0f, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin)); |
| 35 | + m_pShapeItem->setParentItem(this); |
| 36 | + |
| 37 | + //// Have this item pass through mouse events to the child m_pGfxRectItem |
| 38 | + //setAcceptHoverEvents(false); |
| 39 | + //setAcceptedMouseButtons(Qt::NoButton); |
| 40 | +} |
| 41 | + |
| 42 | +TileSetGfxItem::~TileSetGfxItem() |
| 43 | +{ |
| 44 | + delete m_pRectItem; |
| 45 | + delete m_pPixmapItem; |
| 46 | + delete m_pShapeItem; |
| 47 | +} |
| 48 | + |
| 49 | +bool TileSetGfxItem::IsSelected() const |
| 50 | +{ |
| 51 | + return m_bSelected; |
| 52 | +} |
| 53 | + |
| 54 | +void TileSetGfxItem::SetSelected(bool bSelected) |
| 55 | +{ |
| 56 | + m_bSelected = bSelected; |
| 57 | +} |
| 58 | + |
| 59 | +QPointF TileSetGfxItem::GetPos() const |
| 60 | +{ |
| 61 | + return m_pRectItem->pos(); |
| 62 | +} |
| 63 | + |
| 64 | +const QPixmap& TileSetGfxItem::GetPixmap() const |
| 65 | +{ |
| 66 | + return m_pPixmapItem->pixmap(); |
| 67 | +} |
| 68 | + |
| 69 | +/*virtual*/ QRectF TileSetGfxItem::boundingRect() const /*override*/ |
| 70 | +{ |
| 71 | + return m_pRectItem->boundingRect().adjusted(-iTILE_PADDING, -iTILE_PADDING, iTILE_PADDING, iTILE_PADDING); // Adjust for selection border |
| 72 | +} |
| 73 | + |
| 74 | +/*virtual*/ void TileSetGfxItem::paint(QPainter *pPainter, const QStyleOptionGraphicsItem *option, QWidget *widget) /*override*/ |
| 75 | +{ |
| 76 | + //QGraphicsPixmapItem::paint(pPainter, option, widget); |
| 77 | + //if(IsSelected()) |
| 78 | + //{ |
| 79 | + // pPainter->setPen(Qt::DashLine); |
| 80 | + // pPainter->drawRect(boundingRect()); |
| 81 | + //} |
| 82 | +} |
| 83 | + |
| 84 | +///*virtual*/ QVariant TileGfxItem::itemChange(GraphicsItemChange eChange, const QVariant &value) /*override*/ |
| 85 | +//{ |
| 86 | +// if(eChange == QGraphicsItem::ItemSelectedHasChanged) |
| 87 | +// { |
| 88 | +// if(IsSelected()) |
| 89 | +// setZValue(1.0); // Bring selected item to the front |
| 90 | +// else |
| 91 | +// setZValue(0.0); // Reset z-value when not selected |
| 92 | +// } |
| 93 | +// |
| 94 | +// return QGraphicsPixmapItem::itemChange(eChange, value); |
| 95 | +//} |
| 96 | + |
| 97 | +///*virtual*/ void TileGfxItem::mousePressEvent(QGraphicsSceneMouseEvent *pEvent) /*override*/ |
| 98 | +//{ |
| 99 | +// if(pEvent->button() == Qt::LeftButton) |
| 100 | +// { |
| 101 | +// setSelected(!IsSelected()); // Toggle selection on left click |
| 102 | +// pEvent->accept(); |
| 103 | +// } |
| 104 | +// else |
| 105 | +// { |
| 106 | +// QGraphicsPixmapItem::mousePressEvent(pEvent); // Pass other events to base class |
| 107 | +// } |
| 108 | +//} |
| 109 | +// |
| 110 | +///*virtual*/ void TileGfxItem::mouseMoveEvent(QGraphicsSceneMouseEvent *pEvent) /*override*/ |
| 111 | +//{ |
| 112 | +// if(IsSelected() && pEvent->buttons() & Qt::LeftButton) |
| 113 | +// { |
| 114 | +// setPos(mapToScene(pEvent->pos() - pEvent->buttonDownPos(Qt::LeftButton))); |
| 115 | +// pEvent->accept(); |
| 116 | +// } |
| 117 | +// else |
| 118 | +// { |
| 119 | +// QGraphicsPixmapItem::mouseMoveEvent(pEvent); // Pass other events to base class |
| 120 | +// } |
| 121 | +//} |
| 122 | +// |
| 123 | +///*virtual*/ void TileGfxItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *pEvent) /*override*/ |
| 124 | +//{ |
| 125 | +// if(pEvent->button() == Qt::LeftButton) |
| 126 | +// { |
| 127 | +// pEvent->accept(); |
| 128 | +// } |
| 129 | +// else |
| 130 | +// { |
| 131 | +// QGraphicsPixmapItem::mouseReleaseEvent(pEvent); // Pass other events to base class |
| 132 | +// } |
| 133 | +//} |
| 134 | +// |
| 135 | +///*virtual*/ void TileGfxItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *pEvent) /*override*/ |
| 136 | +//{ |
| 137 | +// QGraphicsPixmapItem::contextMenuEvent(pEvent); // Pass context menu events to base class |
| 138 | +//} |
0 commit comments