当前单元高亮
This commit is contained in:
parent
9cee15a935
commit
6376ae1ed6
|
|
@ -14,6 +14,12 @@ MapPresent::MapPresent(QWidget* parent /*= nullptr*/)
|
||||||
_center_index.row = 0;
|
_center_index.row = 0;
|
||||||
_center_index.col = 0;
|
_center_index.col = 0;
|
||||||
|
|
||||||
|
auto delegate = new BasicUnitDelegate(this);
|
||||||
|
_present_delegate[delegate->unitType()] = delegate;
|
||||||
|
connect(this, &MapPresent::mouseOut, delegate, &BasicUnitDelegate::hotClear);
|
||||||
|
connect(this, &MapPresent::mouseHover, delegate, &BasicUnitDelegate::hotIndexSet);
|
||||||
|
connect(delegate, &BasicUnitDelegate::updateRequest, [=](){ this->update(); });
|
||||||
|
|
||||||
this->setMouseTracking(true);
|
this->setMouseTracking(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,7 +72,7 @@ QRectF MapPresent::outlineGet(const PresentIndex& idx) const
|
||||||
void MapPresent::paintEvent(QPaintEvent* ev)
|
void MapPresent::paintEvent(QPaintEvent* ev)
|
||||||
{
|
{
|
||||||
QWidget::paintEvent(ev);
|
QWidget::paintEvent(ev);
|
||||||
BasicUnitDelegate t;
|
UnitPresentDelegate &t = *_present_delegate[0];
|
||||||
|
|
||||||
QPainter p(this);
|
QPainter p(this);
|
||||||
for (auto opt : _visible_units) {
|
for (auto opt : _visible_units) {
|
||||||
|
|
@ -76,7 +82,7 @@ void MapPresent::paintEvent(QPaintEvent* ev)
|
||||||
p.translate(rect.topLeft());
|
p.translate(rect.topLeft());
|
||||||
rect.moveTo(0, 0);
|
rect.moveTo(0, 0);
|
||||||
|
|
||||||
auto end_pts = t.endPointsGet(rect);
|
auto end_pts = BasicUnitDelegate::endPointsGet(rect);
|
||||||
QPainterPath clicp_path;
|
QPainterPath clicp_path;
|
||||||
clicp_path.moveTo(end_pts.first());
|
clicp_path.moveTo(end_pts.first());
|
||||||
for (auto idx = 0; idx < end_pts.size(); ++idx) {
|
for (auto idx = 0; idx < end_pts.size(); ++idx) {
|
||||||
|
|
@ -205,6 +211,11 @@ bool PresentIndex::isValid() const
|
||||||
return row != INT_MAX && col != INT_MAX;
|
return row != INT_MAX && col != INT_MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool PresentIndex::operator==(const PresentIndex& other) const
|
||||||
|
{
|
||||||
|
return row == other.row && col == other.col;
|
||||||
|
}
|
||||||
|
|
||||||
UnitPresentDelegate::UnitPresentDelegate(QObject* parent /*= nullptr*/)
|
UnitPresentDelegate::UnitPresentDelegate(QObject* parent /*= nullptr*/)
|
||||||
:QObject(parent) {
|
:QObject(parent) {
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ struct MAPPRESENT_EXPORT PresentIndex {
|
||||||
|
|
||||||
PresentIndex& operator+=(const PresentIndex& other);
|
PresentIndex& operator+=(const PresentIndex& other);
|
||||||
bool operator!=(const PresentIndex& other) const;
|
bool operator!=(const PresentIndex& other) const;
|
||||||
|
bool operator==(const PresentIndex& other) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -64,7 +65,7 @@ signals:
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 更新通知/重绘请求
|
/// 更新通知/重绘请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void updateRequest();
|
void updateRequest(const PresentIndex &idx);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -94,6 +95,8 @@ private:
|
||||||
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;
|
||||||
|
|
||||||
|
// ===================================
|
||||||
|
QHash<int, UnitPresentDelegate*> _present_delegate;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
#include "UnitDelegate.h"
|
#include "UnitDelegate.h"
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
|
||||||
|
BasicUnitDelegate::BasicUnitDelegate(QObject* parent /*= nullptr*/)
|
||||||
|
:UnitPresentDelegate(parent) {
|
||||||
|
}
|
||||||
|
|
||||||
int BasicUnitDelegate::unitType() const
|
int BasicUnitDelegate::unitType() const
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -8,12 +12,16 @@ int BasicUnitDelegate::unitType() const
|
||||||
|
|
||||||
void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
|
void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
|
||||||
{
|
{
|
||||||
|
p->setRenderHint(QPainter::Antialiasing, true);
|
||||||
|
|
||||||
auto rect = option.outline;
|
auto rect = option.outline;
|
||||||
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;
|
||||||
|
|
||||||
QRadialGradient brush(center, alen);
|
QRadialGradient brush(center, alen);
|
||||||
|
brush.setColorAt(0, Qt::gray);
|
||||||
|
brush.setColorAt(1, Qt::white);
|
||||||
p->fillRect(rect, brush);
|
p->fillRect(rect, brush);
|
||||||
|
|
||||||
QList<QPointF> points = endPointsGet(rect);
|
QList<QPointF> points = endPointsGet(rect);
|
||||||
|
|
@ -22,8 +30,15 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
|
||||||
for (auto idx = 0; idx < 6; ++idx) {
|
for (auto idx = 0; idx < 6; ++idx) {
|
||||||
lines << QLineF(points[idx], points[idx + 1]);
|
lines << QLineF(points[idx], points[idx + 1]);
|
||||||
}
|
}
|
||||||
|
if (_hot_index == option.index) {
|
||||||
|
auto pen = p->pen();
|
||||||
|
pen.setColor(Qt::red);
|
||||||
|
pen.setWidth(10);
|
||||||
|
p->setPen(pen);
|
||||||
|
}
|
||||||
p->drawLines(lines);
|
p->drawLines(lines);
|
||||||
|
|
||||||
|
|
||||||
auto ft = p->font();
|
auto ft = p->font();
|
||||||
ft.setPixelSize(alen * 0.3);
|
ft.setPixelSize(alen * 0.3);
|
||||||
p->setFont(ft);
|
p->setFont(ft);
|
||||||
|
|
@ -51,3 +66,17 @@ QList<QPointF> BasicUnitDelegate::endPointsGet(const QRectF& rect)
|
||||||
|
|
||||||
return points;
|
return points;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BasicUnitDelegate::hotClear()
|
||||||
|
{
|
||||||
|
_hot_index = PresentIndex();
|
||||||
|
emit this->updateRequest(_hot_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BasicUnitDelegate::hotIndexSet(const PresentIndex& idx)
|
||||||
|
{
|
||||||
|
if (_hot_index != idx) {
|
||||||
|
_hot_index = idx;
|
||||||
|
emit this->updateRequest(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,15 @@
|
||||||
/// </summary>
|
/// </summary>
|
||||||
class BasicUnitDelegate : public UnitPresentDelegate {
|
class BasicUnitDelegate : public UnitPresentDelegate {
|
||||||
public:
|
public:
|
||||||
int unitType() const override;
|
BasicUnitDelegate(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
int unitType() const override;
|
||||||
void paint(QPainter* p, const PresentOption& option) override;
|
void paint(QPainter* p, const PresentOption& option) override;
|
||||||
QList<QPointF> endPointsGet(const QRectF& rect);
|
static QList<QPointF> endPointsGet(const QRectF& rect);
|
||||||
|
|
||||||
|
void hotClear();
|
||||||
|
void hotIndexSet(const PresentIndex &idx);
|
||||||
|
|
||||||
|
private:
|
||||||
|
PresentIndex _hot_index;
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue