六边形基元平铺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)
{
QWidget::paintEvent(ev);
QPainter p(this);
BasicUnitDelegate t;
QPainter p(this);
for (auto opt : _visible_units) {
p.save();
p.setPen(Qt::red);
auto rect = outlineGet(_center_index);
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;
opt.selfIndex = _center_index;
opt.selfOutline = rect;
t.paint(&p, opt);
p.restore();
opt.selfIndex = index;
opt.selfOutline = rf;
return opt;
}
for (auto idx = 1; idx < 10; ++idx) {
auto siblings = siblingsGet(_center_index, idx);
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);
void MapPresent::visibleUnitsTidy() {
_visible_units.clear();
opt.selfIndex = item;
opt.selfOutline = rect;
t.paint(&p, opt);
QRectF curr_outline = this->rect();
_visible_units << AssumeOpt(_center_index, outlineGet(_center_index));
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
@ -131,6 +158,9 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
auto alen = rect.width() / 2;
const float xspan = alen * sqrt(3) / 2;
QRadialGradient brush(center, alen);
p->fillRect(rect, brush);
QList<QPointF> points = endPointsGet(rect);
QVector<QLineF> lines;
@ -143,6 +173,7 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
ft.setPixelSize(alen * 0.3);
p->setFont(ft);
p->setPen(Qt::white);
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);
p->drawText(text_rect, Qt::AlignCenter, text_str);

View File

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