diff --git a/MapPresent/MapPresent.cpp b/MapPresent/MapPresent.cpp index 3587aaa..c734f2a 100644 --- a/MapPresent/MapPresent.cpp +++ b/MapPresent/MapPresent.cpp @@ -1,20 +1,45 @@ #include "MapPresent.h" +#include "UnitDelegate.h" +#include +#include +#include +#include MapPresent::MapPresent(QWidget* parent /*= nullptr*/) :QWidget(parent) { - + _center_index.row = 0; + _center_index.col = 0; } -#include -#include PresentIndex MapPresent::indexGet(const QPointF& pos) const { + decltype(_visible_units) templist; + for (auto& unit : _visible_units) { + if (unit.outline.contains(pos)) { + templist << unit; + } + } + + while (templist.size() > 1) { + auto opta = templist[0]; + auto optb = templist[1]; + auto dista = QVector3D(opta.outline.center()).distanceToPoint(QVector3D(pos)); + auto distb = QVector3D(optb.outline.center()).distanceToPoint(QVector3D(pos)); + if (dista < distb) { + templist.removeAt(1); + } + else { + templist.removeAt(0); + } + } + + if (templist.size()) + return templist[0].index; return PresentIndex(); } -#include QRectF MapPresent::outlineGet(const PresentIndex& idx) const { auto widget_rect = this->rect(); @@ -33,7 +58,6 @@ QRectF MapPresent::outlineGet(const PresentIndex& idx) const return QRectF(target_center.toPointF(), QSizeF(ap_len * 2, ap_len * 2)); } -#include void MapPresent::paintEvent(QPaintEvent* ev) { QWidget::paintEvent(ev); @@ -43,7 +67,7 @@ void MapPresent::paintEvent(QPaintEvent* ev) for (auto opt : _visible_units) { p.save(); - auto rect = opt.selfOutline; + auto rect = opt.outline; p.translate(rect.topLeft()); rect.moveTo(0, 0); @@ -55,7 +79,7 @@ void MapPresent::paintEvent(QPaintEvent* ev) } p.setClipPath(clicp_path); - opt.selfOutline = rect; + opt.outline = rect; t.paint(&p, opt); p.restore(); @@ -71,8 +95,8 @@ void MapPresent::resizeEvent(QResizeEvent* event) PresentOption AssumeOpt(PresentIndex index, QRectF rf) { PresentOption opt; - opt.selfIndex = index; - opt.selfOutline = rf; + opt.index = index; + opt.outline = rf; return opt; } @@ -146,53 +170,7 @@ bool PresentIndex::operator!=(const PresentIndex& other) const return row != other.row || col != other.col; } -int BasicUnitDelegate::unitType() const +bool PresentIndex::isValid() 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; - - QRadialGradient brush(center, alen); - p->fillRect(rect, brush); - - QList points = endPointsGet(rect); - - QVector 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); - - 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); -} - -QList BasicUnitDelegate::endPointsGet(const QRectF& rect) -{ - auto center = rect.center(); - auto alen = rect.width() / 2; - const float xspan = alen * sqrt(3) / 2; - - QList 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; + return row != INT_MAX && col != INT_MAX; } diff --git a/MapPresent/MapPresent.h b/MapPresent/MapPresent.h index 3218c26..e650756 100644 --- a/MapPresent/MapPresent.h +++ b/MapPresent/MapPresent.h @@ -6,15 +6,17 @@ /// /// 绘制索引 /// -struct PresentIndex { +struct MAPPRESENT_EXPORT PresentIndex { /// /// 行号 /// - int row = 0; + int row = INT_MAX; /// /// 列号 /// - int col = 0; + int col = INT_MAX; + + bool isValid() const; PresentIndex& operator+=(const PresentIndex& other); bool operator!=(const PresentIndex& other) const; @@ -27,11 +29,11 @@ struct PresentOption { /// /// 当前所绘制索引 /// - PresentIndex selfIndex; + PresentIndex index; /// /// 当前绘制外缘 /// - QRectF selfOutline; + QRectF outline; bool isMouseOver = false; bool isSelected = false; @@ -56,14 +58,6 @@ public: virtual void paint(QPainter* p, const PresentOption& option) = 0; }; -class BasicUnitDelegate : public UnitPresentDelegate { -public: - int unitType() const override; - - void paint(QPainter* p, const PresentOption& option) override; - QList endPointsGet(const QRectF &rect); -}; - /// /// 基础地图绘制 /// @@ -84,6 +78,15 @@ private: /// PresentIndex _center_index; + // =================================== + QList _visible_units; + void visibleUnitsTidy(); + + QList siblingsGet(const PresentIndex& center, uint16_t dist = 1) const; + QList itemFills(const PresentIndex& a, const PresentIndex& b) const; + + + public: MapPresent(QWidget* parent = nullptr); @@ -100,17 +103,13 @@ public: /// QRectF outlineGet(const PresentIndex& idx) const; +signals: + void mouseEnter(); + void mouseOut(); + void mouseHover(); protected: void paintEvent(QPaintEvent* ev) override; void resizeEvent(QResizeEvent* event) override; -private: - QList _visible_units; - void visibleUnitsTidy(); - - QList siblingsGet(const PresentIndex ¢er, uint16_t dist=1) const; - QList itemFills(const PresentIndex &a, const PresentIndex &b) const; - - }; diff --git a/MapPresent/MapPresent.vcxproj b/MapPresent/MapPresent.vcxproj index 45a9b62..afbd2f1 100644 --- a/MapPresent/MapPresent.vcxproj +++ b/MapPresent/MapPresent.vcxproj @@ -94,7 +94,9 @@ + + diff --git a/MapPresent/MapPresent.vcxproj.filters b/MapPresent/MapPresent.vcxproj.filters index bead507..43ddc33 100644 --- a/MapPresent/MapPresent.vcxproj.filters +++ b/MapPresent/MapPresent.vcxproj.filters @@ -29,10 +29,18 @@ Source Files + + Header Files + Header Files + + + Source Files + + \ No newline at end of file diff --git a/MapPresent/UnitDelegate.cpp b/MapPresent/UnitDelegate.cpp new file mode 100644 index 0000000..8f48217 --- /dev/null +++ b/MapPresent/UnitDelegate.cpp @@ -0,0 +1,53 @@ +#include "UnitDelegate.h" +#include + +int BasicUnitDelegate::unitType() const +{ + return 0; +} + +void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option) +{ + auto rect = option.outline; + auto center = rect.center(); + auto alen = rect.width() / 2; + const float xspan = alen * sqrt(3) / 2; + + QRadialGradient brush(center, alen); + p->fillRect(rect, brush); + + QList points = endPointsGet(rect); + + QVector 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); + + 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.index.row).arg(option.index.col); + p->drawText(text_rect, Qt::AlignCenter, text_str); +} + +QList BasicUnitDelegate::endPointsGet(const QRectF& rect) +{ + auto center = rect.center(); + auto alen = rect.width() / 2; + const float xspan = alen * sqrt(3) / 2; + + QList 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; +} diff --git a/MapPresent/UnitDelegate.h b/MapPresent/UnitDelegate.h new file mode 100644 index 0000000..e7a5e39 --- /dev/null +++ b/MapPresent/UnitDelegate.h @@ -0,0 +1,13 @@ +#pragma once +#include "MapPresent.h" + +/// +/// 基础空白单元绘制委托 +/// +class BasicUnitDelegate : public UnitPresentDelegate { +public: + int unitType() const override; + + void paint(QPainter* p, const PresentOption& option) override; + QList endPointsGet(const QRectF& rect); +}; \ No newline at end of file