六边形基元平铺MapWidget

This commit is contained in:
codeboss 2025-10-11 22:40:51 +08:00
parent cb9d386647
commit 901b660a64
2 changed files with 63 additions and 26 deletions

View File

@ -37,39 +37,66 @@ QRectF MapPresent::outlineGet(const PresentIndex& idx) const
void MapPresent::paintEvent(QPaintEvent* ev) void MapPresent::paintEvent(QPaintEvent* ev)
{ {
QWidget::paintEvent(ev); QWidget::paintEvent(ev);
QPainter p(this);
BasicUnitDelegate t; BasicUnitDelegate t;
p.save(); QPainter p(this);
p.setPen(Qt::red); for (auto opt : _visible_units) {
auto rect = outlineGet(_center_index); p.save();
p.translate(rect.topLeft());
rect.moveTo(0, 0); auto rect = opt.selfOutline;
p.translate(rect.topLeft());
rect.moveTo(0, 0);
auto end_pts = t.endPointsGet(rect);
QPainterPath clicp_path;
clicp_path.moveTo(end_pts.first());
for (auto idx = 0; idx < end_pts.size(); ++idx) {
clicp_path.lineTo(end_pts[idx]);
}
p.setClipPath(clicp_path);
opt.selfOutline = rect;
t.paint(&p, opt);
p.restore();
}
}
void MapPresent::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
visibleUnitsTidy();
}
PresentOption AssumeOpt(PresentIndex index, QRectF rf) {
PresentOption opt; PresentOption opt;
opt.selfIndex = _center_index; opt.selfIndex = index;
opt.selfOutline = rect; opt.selfOutline = rf;
t.paint(&p, opt); return opt;
p.restore(); }
for (auto idx = 1; idx < 10; ++idx) { void MapPresent::visibleUnitsTidy() {
auto siblings = siblingsGet(_center_index, idx); _visible_units.clear();
p.setPen(Qt::green);
for (auto item : siblings) {
auto rect = outlineGet(item);
p.save();
p.translate(rect.topLeft());
rect.moveTo(0, 0);
//p.drawRect(rect);
opt.selfIndex = item; QRectF curr_outline = this->rect();
opt.selfOutline = rect; _visible_units << AssumeOpt(_center_index, outlineGet(_center_index));
t.paint(&p, opt);
p.restore(); int visible_count = 1, deduce_x = 1;
while (visible_count) {
visible_count = 0;
auto siblings = siblingsGet(_center_index, deduce_x++);
for (auto unit : siblings) {
auto visible_rect = outlineGet(unit);
if (curr_outline.intersects(visible_rect)) {
_visible_units << AssumeOpt(unit, visible_rect);
visible_count++;
}
} }
} }
// TODO ¹¹½¨¿ìËÙ²âÊÔ×éÖ¯½á¹¹
} }
QList<PresentIndex> MapPresent::siblingsGet(const PresentIndex& center, uint16_t dist) const QList<PresentIndex> MapPresent::siblingsGet(const PresentIndex& center, uint16_t dist) const
@ -131,6 +158,9 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
auto alen = rect.width() / 2; auto alen = rect.width() / 2;
const float xspan = alen * sqrt(3) / 2; const float xspan = alen * sqrt(3) / 2;
QRadialGradient brush(center, alen);
p->fillRect(rect, brush);
QList<QPointF> points = endPointsGet(rect); QList<QPointF> points = endPointsGet(rect);
QVector<QLineF> lines; QVector<QLineF> lines;
@ -143,6 +173,7 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
ft.setPixelSize(alen * 0.3); ft.setPixelSize(alen * 0.3);
p->setFont(ft); p->setFont(ft);
p->setPen(Qt::white);
auto text_rect = QRectF(center.x() - xspan, center.y() - alen / 2, xspan * 2, alen); auto text_rect = QRectF(center.x() - xspan, center.y() - alen / 2, xspan * 2, alen);
auto text_str = QString("(%1,%2)").arg(option.selfIndex.row).arg(option.selfIndex.col); auto text_str = QString("(%1,%2)").arg(option.selfIndex.row).arg(option.selfIndex.col);
p->drawText(text_rect, Qt::AlignCenter, text_str); p->drawText(text_rect, Qt::AlignCenter, text_str);
@ -153,7 +184,7 @@ QList<QPointF> BasicUnitDelegate::endPointsGet(const QRectF& rect)
auto center = rect.center(); auto center = rect.center();
auto alen = rect.width() / 2; auto alen = rect.width() / 2;
const float xspan = alen * sqrt(3) / 2; const float xspan = alen * sqrt(3) / 2;
QList<QPointF> points; QList<QPointF> points;
points << QPointF(center.x() - xspan, center.y() - alen / 2); points << QPointF(center.x() - xspan, center.y() - alen / 2);
points << QPointF(center.x(), 0); points << QPointF(center.x(), 0);

View File

@ -103,8 +103,14 @@ public:
protected: protected:
void paintEvent(QPaintEvent* ev) override; void paintEvent(QPaintEvent* ev) override;
void resizeEvent(QResizeEvent* event) override;
private: private:
QList<PresentOption> _visible_units;
void visibleUnitsTidy();
QList<PresentIndex> siblingsGet(const PresentIndex &center, uint16_t dist=1) const; QList<PresentIndex> siblingsGet(const PresentIndex &center, uint16_t dist=1) const;
QList<PresentIndex> itemFills(const PresentIndex &a, const PresentIndex &b) const; QList<PresentIndex> itemFills(const PresentIndex &a, const PresentIndex &b) const;
}; };