简化软件架构

This commit is contained in:
codeboss 2025-10-12 21:37:29 +08:00
parent 306a7ee38d
commit 1f1383af9c
2 changed files with 38 additions and 62 deletions

View File

@ -6,26 +6,8 @@
#include <QEvent>
#include <QPainter>
#include <QMouseEvent>
class OptionGroup {
private:
PresentOption _bind_opt;
std::shared_ptr<OptionGroup> _next_group;
public:
OptionGroup(const PresentOption& opt, std::shared_ptr<OptionGroup> next = nullptr)
:_bind_opt(opt), _next_group(next) {
}
std::shared_ptr<OptionGroup> nextGet() const {
return _next_group;
}
const PresentOption& optionGet() const {
return _bind_opt;
}
};
#include <QElapsedTimer>
#include <QtDebug>
MapPresent::MapPresent(QWidget* parent /*= nullptr*/)
@ -35,7 +17,7 @@ MapPresent::MapPresent(QWidget* parent /*= nullptr*/)
_center_index.col = 0;
auto delegate = new BasicUnitDelegate(this);
_present_delegate[delegate->unitType()] = delegate;
_type_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(); });
@ -46,15 +28,10 @@ MapPresent::MapPresent(QWidget* parent /*= nullptr*/)
PresentIndex MapPresent::indexGet(const QPointF& pos) const
{
QList<PresentOption> templist;
for (auto unit_g : _visible_unit_groups) {
while (unit_g) {
auto unit = unit_g->optionGet();
for (auto unit : _visible_units) {
if (unit.outline.contains(pos)) {
templist << unit;
}
unit_g = unit_g->nextGet();
}
}
@ -95,19 +72,15 @@ QRectF MapPresent::outlineGet(const PresentIndex& idx) const
return QRectF(target_center.toPointF(), QSizeF(ap_len * 2, ap_len * 2));
}
#include <QElapsedTimer>
#include <QtDebug>
void MapPresent::paintEvent(QPaintEvent* ev)
{
QWidget::paintEvent(ev);
UnitPresentDelegate& t = *_present_delegate[0];
UnitPresentDelegate& t = *_type_present_delegate[0];
QPainter p(this);
for (auto opt_g : _visible_unit_groups) {
while (opt_g) {
for (auto opt : _visible_units) {
p.save();
auto opt = opt_g->optionGet();
auto rect = opt.outline;
p.translate(rect.topLeft());
rect.moveTo(0, 0);
@ -119,9 +92,6 @@ void MapPresent::paintEvent(QPaintEvent* ev)
t.paint(&p, opt);
p.restore();
opt_g = opt_g->nextGet();
}
}
}
@ -165,30 +135,23 @@ PresentOption AssumeOpt(PresentIndex index, QRectF rf) {
}
void MapPresent::visible_units_tidy() {
_visible_unit_groups.clear();
_visible_units.clear();
QRectF curr_outline = this->rect();
_visible_unit_groups << std::make_shared<OptionGroup>(
AssumeOpt(_center_index, outlineGet(_center_index))
);
_visible_units << AssumeOpt(_center_index, outlineGet(_center_index));
int visible_count = 1, deduce_x = 1;
while (visible_count) {
visible_count = 0;
std::shared_ptr<OptionGroup> prev_g = nullptr;
auto siblings = siblingsGet(_center_index, deduce_x++);
for (auto unit : siblings) {
auto visible_rect = outlineGet(unit);
if (curr_outline.intersects(visible_rect)) {
prev_g = std::make_shared<OptionGroup>(AssumeOpt(unit, visible_rect), prev_g);
_visible_units << AssumeOpt(unit, visible_rect);
visible_count++;
}
}
if (prev_g) {
_visible_unit_groups << prev_g;
}
}
// TODO ¹¹½¨¿ìËÙ²âÊÔ×éÖ¯½á¹¹

View File

@ -69,9 +69,8 @@ signals:
void updateRequest(const PresentIndex &idx);
};
class OptionGroup;
/// <summary>
/// 價插華芞餅秶
/// 基础瓦片地图绘制
/// </summary>
class MAPPRESENT_EXPORT MapPresent : public QWidget
{
@ -91,13 +90,27 @@ private:
PresentIndex _center_index;
// ===================================
QList<std::shared_ptr<OptionGroup>> _visible_unit_groups;
/// <summary>
/// 试图核心索引
/// </summary>
QList<PresentOption> _visible_units;
/// <summary>
/// 可视化单元整理
/// </summary>
void visible_units_tidy();
/// <summary>
/// 可视化索引填充
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
QList<PresentIndex> item_supply(const PresentIndex& a, const PresentIndex& b) const;
// ===================================
QHash<int, UnitPresentDelegate*> _present_delegate;
/// <summary>
/// 类型化绘制委托
/// </summary>
QHash<int, UnitPresentDelegate*> _type_present_delegate;
public: