添加鼠标指向基元索引检测
This commit is contained in:
parent
901b660a64
commit
7edb3aba43
|
|
@ -1,20 +1,45 @@
|
|||
#include "MapPresent.h"
|
||||
#include "UnitDelegate.h"
|
||||
#include <QVector2D>
|
||||
#include <cmath>
|
||||
#include <QVector3D>
|
||||
#include <QPainter>
|
||||
|
||||
MapPresent::MapPresent(QWidget* parent /*= nullptr*/)
|
||||
:QWidget(parent)
|
||||
{
|
||||
|
||||
_center_index.row = 0;
|
||||
_center_index.col = 0;
|
||||
}
|
||||
|
||||
#include <QVector2D>
|
||||
#include <cmath>
|
||||
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 <QVector3D>
|
||||
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 <QPainter>
|
||||
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<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);
|
||||
|
||||
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<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;
|
||||
return row != INT_MAX && col != INT_MAX;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,15 +6,17 @@
|
|||
/// <summary>
|
||||
/// 绘制索引
|
||||
/// </summary>
|
||||
struct PresentIndex {
|
||||
struct MAPPRESENT_EXPORT PresentIndex {
|
||||
/// <summary>
|
||||
/// 行号
|
||||
/// </summary>
|
||||
int row = 0;
|
||||
int row = INT_MAX;
|
||||
/// <summary>
|
||||
/// 列号
|
||||
/// </summary>
|
||||
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 {
|
|||
/// <summary>
|
||||
/// 当前所绘制索引
|
||||
/// </summary>
|
||||
PresentIndex selfIndex;
|
||||
PresentIndex index;
|
||||
/// <summary>
|
||||
/// 当前绘制外缘
|
||||
/// </summary>
|
||||
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<QPointF> endPointsGet(const QRectF &rect);
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 基础地图绘制
|
||||
/// </summary>
|
||||
|
|
@ -84,6 +78,15 @@ private:
|
|||
/// </summary>
|
||||
PresentIndex _center_index;
|
||||
|
||||
// ===================================
|
||||
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;
|
||||
|
||||
|
||||
|
||||
public:
|
||||
MapPresent(QWidget* parent = nullptr);
|
||||
|
||||
|
|
@ -100,17 +103,13 @@ public:
|
|||
/// <returns></returns>
|
||||
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<PresentOption> _visible_units;
|
||||
void visibleUnitsTidy();
|
||||
|
||||
QList<PresentIndex> siblingsGet(const PresentIndex ¢er, uint16_t dist=1) const;
|
||||
QList<PresentIndex> itemFills(const PresentIndex &a, const PresentIndex &b) const;
|
||||
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -94,7 +94,9 @@
|
|||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="UnitDelegate.cpp" />
|
||||
<ClInclude Include="mappresent_global.h" />
|
||||
<ClInclude Include="UnitDelegate.h" />
|
||||
<QtMoc Include="MapPresent.h" />
|
||||
<ClCompile Include="MapPresent.cpp" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -29,10 +29,18 @@
|
|||
<ClCompile Include="MapPresent.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClInclude Include="UnitDelegate.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<QtMoc Include="MapPresent.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</QtMoc>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="UnitDelegate.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
#include "UnitDelegate.h"
|
||||
#include <QPainter>
|
||||
|
||||
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<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);
|
||||
|
||||
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<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;
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
#include "MapPresent.h"
|
||||
|
||||
/// <summary>
|
||||
/// 샘뇟왕겜데禱삥齡官辜
|
||||
/// </summary>
|
||||
class BasicUnitDelegate : public UnitPresentDelegate {
|
||||
public:
|
||||
int unitType() const override;
|
||||
|
||||
void paint(QPainter* p, const PresentOption& option) override;
|
||||
QList<QPointF> endPointsGet(const QRectF& rect);
|
||||
};
|
||||
Loading…
Reference in New Issue