ActWorld/MapPresent/UnitDelegate.cpp

88 lines
2.2 KiB
C++
Raw Permalink Normal View History

2025-10-11 15:03:22 +00:00
#include "UnitDelegate.h"
#include <QPainter>
2025-10-12 05:20:15 +00:00
BasicUnitDelegate::BasicUnitDelegate(QObject* parent /*= nullptr*/)
:UnitPresentDelegate(parent) {
}
2025-10-11 15:03:22 +00:00
int BasicUnitDelegate::unitType() const
{
return 0;
}
void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
{
2025-10-12 05:20:15 +00:00
p->setRenderHint(QPainter::Antialiasing, true);
2025-10-11 15:03:22 +00:00
auto rect = option.outline;
auto center = rect.center();
auto alen = rect.width() / 2;
const float xspan = alen * sqrt(3) / 2;
QRadialGradient brush(center, alen);
2025-10-12 05:20:15 +00:00
brush.setColorAt(0, Qt::gray);
brush.setColorAt(1, Qt::white);
2025-10-12 12:21:45 +00:00
p->setBrush(brush);
2025-10-12 13:11:54 +00:00
p->drawRect(rect);
2025-10-11 15:03:22 +00:00
2025-10-12 05:20:15 +00:00
if (_hot_index == option.index) {
auto pen = p->pen();
pen.setColor(Qt::red);
pen.setWidth(10);
p->setPen(pen);
2025-10-12 13:11:54 +00:00
auto pathx = clipPathGet(rect);
p->drawPath(pathx);
2025-10-12 05:20:15 +00:00
}
2025-10-11 15:03:22 +00:00
auto ft = p->font();
ft.setPixelSize(alen * 0.3);
p->setFont(ft);
2025-10-12 15:04:18 +00:00
p->setPen(Qt::black);
2025-10-11 15:03:22 +00:00
auto text_rect = QRectF(center.x() - xspan, center.y() - alen / 2, xspan * 2, alen);
auto text_str = QString("(%1,%2)").arg(option.index.row).arg(option.index.col);
p->drawText(text_rect, Qt::AlignCenter, text_str);
}
2025-10-12 12:21:45 +00:00
QPainterPath BasicUnitDelegate::clipPathGet(const QRectF& rect)
2025-10-11 15:03:22 +00:00
{
auto center = rect.center();
auto alen = rect.width() / 2;
const float xspan = alen * sqrt(3) / 2;
QList<QPointF> points;
points << QPointF(center.x() - xspan, center.y() - alen / 2);
points << QPointF(center.x(), 0);
points << QPointF(center.x() + xspan, center.y() - alen / 2);
points << QPointF(center.x() + xspan, center.y() + alen / 2);
points << QPointF(center.x(), rect.height());
points << QPointF(center.x() - xspan, center.y() + alen / 2);
points << QPointF(center.x() - xspan, center.y() - alen / 2);
2025-10-12 12:21:45 +00:00
QPainterPath clicp_path;
clicp_path.moveTo(points.first());
for (auto idx = 0; idx < points.size(); ++idx) {
clicp_path.lineTo(points[idx]);
}
return clicp_path;
2025-10-11 15:03:22 +00:00
}
2025-10-12 05:20:15 +00:00
void BasicUnitDelegate::hotClear()
{
2025-10-12 15:04:18 +00:00
auto prev_idx = _hot_index;
2025-10-12 05:20:15 +00:00
_hot_index = PresentIndex();
2025-10-12 15:04:18 +00:00
2025-10-12 16:25:29 +00:00
emit this->updateRequest({ prev_idx });
2025-10-12 05:20:15 +00:00
}
void BasicUnitDelegate::hotIndexSet(const PresentIndex& idx)
{
if (_hot_index != idx) {
2025-10-12 15:04:18 +00:00
auto prev_idx = _hot_index;
2025-10-12 05:20:15 +00:00
_hot_index = idx;
2025-10-12 15:04:18 +00:00
2025-10-12 16:25:29 +00:00
emit this->updateRequest({ prev_idx });
emit this->updateRequest({ idx });
2025-10-12 05:20:15 +00:00
}
}