ActWorld/MapPresent/MapPresent.cpp

168 lines
4.3 KiB
C++
Raw Normal View History

2025-10-10 16:56:47 +00:00
#include "MapPresent.h"
MapPresent::MapPresent(QWidget* parent /*= nullptr*/)
:QWidget(parent)
{
}
#include <QVector2D>
#include <cmath>
PresentIndex MapPresent::indexGet(const QPointF& pos) const
{
return PresentIndex();
}
#include <QVector3D>
QRectF MapPresent::outlineGet(const PresentIndex& idx) const
{
auto widget_rect = this->rect();
auto widget_center = widget_rect.center();
const float ap_len = _primitive_region_square_len / 2;
auto a2_vec = ap_len * sqrt(3) / 2;
auto n_yinc = QVector3D(a2_vec, ap_len * 3 / 2, 0);
auto n_xinc = QVector3D(a2_vec, -ap_len * 3 / 2, 0);
auto xinc = idx.row - _center_index.row;
auto yinc = idx.col - _center_index.col;
auto target_center = QVector3D(widget_center) + n_yinc * yinc + n_xinc * xinc;
target_center -= QVector3D(ap_len, ap_len, 0);
return QRectF(target_center.toPointF(), QSizeF(ap_len * 2, ap_len * 2));
}
#include <QPainter>
void MapPresent::paintEvent(QPaintEvent* ev)
{
QWidget::paintEvent(ev);
QPainter p(this);
BasicUnitDelegate t;
p.save();
p.setPen(Qt::red);
auto rect = outlineGet(_center_index);
p.translate(rect.topLeft());
rect.moveTo(0, 0);
PresentOption opt;
opt.selfIndex = _center_index;
opt.selfOutline = rect;
t.paint(&p, opt);
p.restore();
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);
opt.selfIndex = item;
opt.selfOutline = rect;
t.paint(&p, opt);
p.restore();
}
}
}
QList<PresentIndex> MapPresent::siblingsGet(const PresentIndex& center, uint16_t dist) const
{
QList<PresentIndex> e6_points;
e6_points << PresentIndex{ center.row, center.col - dist };
e6_points << PresentIndex{ center.row + dist, center.col };
e6_points << PresentIndex{ center.row + dist, center.col + dist };
e6_points << PresentIndex{ center.row, center.col + dist };
e6_points << PresentIndex{ center.row - dist, center.col };
e6_points << PresentIndex{ center.row - dist, center.col - dist };
e6_points << PresentIndex{ center.row, center.col - dist };
QList<PresentIndex> values;
for (auto idx = 0; idx < 6; ++idx) {
auto ap = e6_points[idx];
auto bp = e6_points[idx + 1];
values << itemFills(ap, bp);
}
return values;
}
QList<PresentIndex> MapPresent::itemFills(const PresentIndex& a, const PresentIndex& b) const
{
QList<PresentIndex> items;
auto sign_row = (a.row == b.row) ? 0 : ((b.row - a.row) / std::abs(a.row - b.row));
auto sign_col = (a.col == b.col) ? 0 : ((b.col - a.col) / std::abs(a.col - b.col));
auto temp_pt = a;
while (temp_pt != b) {
items << temp_pt;
temp_pt += PresentIndex{ sign_row, sign_col };
}
return items;
}
PresentIndex& PresentIndex::operator+=(const PresentIndex& other)
{
row += other.row;
col += other.col;
return *this;
}
bool PresentIndex::operator!=(const PresentIndex& other) const
{
return row != other.row || col != other.col;
}
int BasicUnitDelegate::unitType() const
{
return 0;
}
void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
{
auto rect = option.selfOutline;
auto center = rect.center();
auto alen = rect.width() / 2;
const float xspan = alen * sqrt(3) / 2;
QList<QPointF> points = endPointsGet(rect);
QVector<QLineF> lines;
for (auto idx = 0; idx < 6; ++idx) {
lines << QLineF(points[idx], points[idx + 1]);
}
p->drawLines(lines);
auto ft = p->font();
ft.setPixelSize(alen * 0.3);
p->setFont(ft);
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);
}
QList<QPointF> BasicUnitDelegate::endPointsGet(const QRectF& rect)
{
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);
return points;
}