This commit is contained in:
codeboss 2025-06-30 01:19:07 +08:00
parent 452e11c145
commit 5a917f3ff4
5 changed files with 77 additions and 41 deletions

View File

@ -98,7 +98,7 @@ std::shared_ptr<MessageLoader> MapKernal::messageLoader() const
void MapKernal::registData(std::shared_ptr<TopicData> init) void MapKernal::registData(std::shared_ptr<TopicData> init)
{ {
if (_variable_types.contains(init->topicString())) if (_variable_types.contains(init->topicString()))
throw new UniException(QString("重复注册类型数据{%1}").arg(init->topicString())); throw new UniException(QString(u8"重复注册类型数据{%1}").arg(init->topicString()));
_variable_types[init->topicString()] = init; _variable_types[init->topicString()] = init;
} }
@ -132,6 +132,14 @@ NodeKind LogicalNode::nodeKind() const
return _node_type; return _node_type;
} }
int LogicalNode::depth() const
{
auto node_temp = this->parent().lock();
if (!node_temp) return 0;
return this->parent().lock()->depth() + 1;
}
std::shared_ptr<LogicalNode> LogicalNode::bindMap() const std::shared_ptr<LogicalNode> LogicalNode::bindMap() const
{ {
auto node_temp = this->parent().lock(); auto node_temp = this->parent().lock();
@ -155,7 +163,7 @@ int LogicalNode::getID() const
QString LogicalNode::rtName() const QString LogicalNode::rtName() const
{ {
return QString("%1(%2)").arg(this->typeName()).arg(getID()); return QString(u8"%1(%2)").arg(this->typeName()).arg(getID());
} }
std::weak_ptr<LogicalNode> LogicalNode::parent() const std::weak_ptr<LogicalNode> LogicalNode::parent() const
@ -171,7 +179,7 @@ void BehaviorMapNode::setVariable(const QString& key, IO_TYPE t, std::shared_ptr
{ {
if (_variables.contains(key)) if (_variables.contains(key))
if (_variables[key].second->topicString() != ins->topicString()) if (_variables[key].second->topicString() != ins->topicString())
throw new UniException("不同数据类型数据绑定同一个名称!"); throw new UniException(u8"不同数据类型数据绑定同一个名称!");
_variables[key] = std::make_pair(t, ins); _variables[key] = std::make_pair(t, ins);
} }
@ -233,7 +241,7 @@ QHash<QString, std::shared_ptr<TopicData>> BehaviorMapNode::inputList() const
{ {
QHash<QString, std::shared_ptr<TopicData>> list; QHash<QString, std::shared_ptr<TopicData>> list;
for (auto key : this->_variables.keys()) { for (auto key : this->_variables.keys()) {
if (this->_variables[key].first == IO_TYPE::INPUT && key.contains("::")) if (this->_variables[key].first == IO_TYPE::INPUT && key.contains(u8"::"))
list[key] = _variables[key].second; list[key] = _variables[key].second;
} }
@ -244,7 +252,7 @@ QHash<QString, std::shared_ptr<TopicData>> BehaviorMapNode::outputList() const
{ {
QHash<QString, std::shared_ptr<TopicData>> list; QHash<QString, std::shared_ptr<TopicData>> list;
for (auto key : this->_variables.keys()) { for (auto key : this->_variables.keys()) {
if (this->_variables[key].first == IO_TYPE::OUTPUT && key.contains("::")) if (this->_variables[key].first == IO_TYPE::OUTPUT && key.contains(u8"::"))
list[key] = _variables[key].second; list[key] = _variables[key].second;
} }
@ -275,7 +283,7 @@ QList<std::shared_ptr<LogicalNode>> SequenceNode::getForwards() const
QString SequenceNode::typeName() const QString SequenceNode::typeName() const
{ {
return "顺序节点"; return u8"顺序节点";
} }
LogicalResult SequenceNode::execute() { LogicalResult SequenceNode::execute() {
@ -309,7 +317,7 @@ void SequenceNode::recoveryFrom(const QJsonObject& obj)
QStringList state_items; QStringList state_items;
STRLIST_PEAK(state_items); STRLIST_PEAK(state_items);
for (auto kv : state_items) { for (auto kv : state_items) {
auto kv_pair = kv.split(":"); auto kv_pair = kv.split(u8":");
_state_map[kv_pair[0].toInt()] = (LogicalResult)kv_pair[1].toInt(); _state_map[kv_pair[0].toInt()] = (LogicalResult)kv_pair[1].toInt();
} }
} }
@ -318,7 +326,7 @@ void SequenceNode::saveTo(QJsonObject& obj) const
{ {
QStringList state_items; QStringList state_items;
for (auto inv : _state_map.keys()) { for (auto inv : _state_map.keys()) {
state_items << QString("%1:%2").arg(inv).arg((int)_state_map[inv]); state_items << QString(u8"%1:%2").arg(inv).arg((int)_state_map[inv]);
} }
STRLIST_SAVE(state_items); STRLIST_SAVE(state_items);
@ -398,7 +406,7 @@ void SelectorNode::recoveryFrom(const QJsonObject& obj)
QStringList state_items; QStringList state_items;
STRLIST_PEAK(state_items); STRLIST_PEAK(state_items);
for (auto kv : state_items) { for (auto kv : state_items) {
auto kv_pair = kv.split(":"); auto kv_pair = kv.split(u8":");
_state_map[kv_pair[0].toInt()] = (LogicalResult)kv_pair[1].toInt(); _state_map[kv_pair[0].toInt()] = (LogicalResult)kv_pair[1].toInt();
} }
} }
@ -407,7 +415,7 @@ void SelectorNode::saveTo(QJsonObject& obj) const
{ {
QStringList state_items; QStringList state_items;
for (auto inv : _state_map.keys()) { for (auto inv : _state_map.keys()) {
state_items << QString("%1:%2").arg(inv).arg((int)_state_map[inv]); state_items << QString(u8"%1:%2").arg(inv).arg((int)_state_map[inv]);
} }
STRLIST_SAVE(state_items); STRLIST_SAVE(state_items);
@ -420,7 +428,7 @@ void SelectorNode::reset()
QString SelectorNode::typeName() const QString SelectorNode::typeName() const
{ {
return "选择节点"; return u8"选择节点";
} }
LogicalResult SelectorNode::execute() LogicalResult SelectorNode::execute()
@ -462,7 +470,7 @@ void ParallelNode::reset()
QString ParallelNode::typeName() const QString ParallelNode::typeName() const
{ {
return "并行节点"; return u8"并行节点";
} }
LogicalResult ParallelNode::execute() LogicalResult ParallelNode::execute()
@ -498,7 +506,7 @@ void ParallelNode::recoveryFrom(const QJsonObject& obj)
QStringList state_items; QStringList state_items;
STRLIST_PEAK(state_items); STRLIST_PEAK(state_items);
for (auto kv : state_items) { for (auto kv : state_items) {
auto kv_pair = kv.split(":"); auto kv_pair = kv.split(u8":");
_state_map[kv_pair[0].toInt()] = (LogicalResult)kv_pair[1].toInt(); _state_map[kv_pair[0].toInt()] = (LogicalResult)kv_pair[1].toInt();
} }
} }
@ -507,7 +515,7 @@ void ParallelNode::saveTo(QJsonObject& obj) const
{ {
QStringList state_items; QStringList state_items;
for (auto inv : _state_map.keys()) { for (auto inv : _state_map.keys()) {
state_items << QString("%1:%2").arg(inv).arg((int)_state_map[inv]); state_items << QString(u8"%1:%2").arg(inv).arg((int)_state_map[inv]);
} }
STRLIST_SAVE(state_items); STRLIST_SAVE(state_items);
@ -518,14 +526,14 @@ void CompareNode::reset() {
QString CompareNode::typeName() const QString CompareNode::typeName() const
{ {
return QString("%1<%2>").arg("比较节点", delegateName()); return QString(u8"%1<%2>").arg(u8"比较节点", delegateName());
} }
LogicalResult CompareNode::execute() LogicalResult CompareNode::execute()
{ {
if (this->_bind_delegate) { if (this->_bind_delegate) {
auto vl = _data_map["左值"]; auto vl = _data_map[u8"左值"];
auto vb = _data_map["右值"]; auto vb = _data_map[u8"右值"];
if (this->_bind_delegate->compare(vl, vb)) if (this->_bind_delegate->compare(vl, vb))
return LogicalResult::SUCCESS; return LogicalResult::SUCCESS;
} }
@ -556,15 +564,15 @@ std::shared_ptr<Serializable> CompareNode::newDefault() const
CompareNode::CompareNode() CompareNode::CompareNode()
: LogicalNode(NodeKind::COMPARENODE) { : LogicalNode(NodeKind::COMPARENODE) {
_data_map["左值"] = std::make_shared<GeneralData>(); _data_map[u8"左值"] = std::make_shared<GeneralData>();
_data_map["右值"] = std::make_shared<GeneralData>(); _data_map[u8"右值"] = std::make_shared<GeneralData>();
} }
QString CompareNode::delegateName() const QString CompareNode::delegateName() const
{ {
if (_bind_delegate) if (_bind_delegate)
return _bind_delegate->name(); return _bind_delegate->name();
return ""; return u8"";
} }
void CompareNode::bindDelegate(std::shared_ptr<CompareKernel> ins) void CompareNode::bindDelegate(std::shared_ptr<CompareKernel> ins)
@ -584,27 +592,27 @@ QHash<QString, std::shared_ptr<TopicData>> CompareNode::outputList() const
void CompareNode::recoveryFrom(const QJsonObject& obj) void CompareNode::recoveryFrom(const QJsonObject& obj)
{ {
auto vala = obj["ValL"]; auto vala = obj[u8"ValL"];
auto valb = obj["ValR"]; auto valb = obj[u8"ValR"];
_data_map["左值"]->recoveryFrom(vala.toObject()); _data_map[u8"左值"]->recoveryFrom(vala.toObject());
_data_map["右值"]->recoveryFrom(valb.toObject()); _data_map[u8"右值"]->recoveryFrom(valb.toObject());
} }
void CompareNode::saveTo(QJsonObject& obj) const void CompareNode::saveTo(QJsonObject& obj) const
{ {
QJsonObject vala, valb; QJsonObject vala, valb;
_data_map["左值"]->saveTo(vala); _data_map[u8"左值"]->saveTo(vala);
_data_map["右值"]->saveTo(valb); _data_map[u8"右值"]->saveTo(valb);
obj["ValL"] = vala; obj[u8"ValL"] = vala;
obj["ValR"] = valb; obj[u8"ValR"] = valb;
} }
#include <QJsonDocument> #include <QJsonDocument>
QString GeneralData::topicString() const QString GeneralData::topicString() const
{ {
return "GeneralData"; return u8"GeneralData";
} }
std::shared_ptr<Serializable> GeneralData::newDefault() const std::shared_ptr<Serializable> GeneralData::newDefault() const

View File

@ -249,6 +249,11 @@ public:
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
NodeKind nodeKind() const; NodeKind nodeKind() const;
/// <summary>
/// 节点深度
/// </summary>
/// <returns></returns>
int depth() const;
/// <summary> /// <summary>
/// 获取包含树图节点 /// 获取包含树图节点

View File

@ -1,31 +1,42 @@
#include "BehaviorEditor.h" #include "BehaviorEditor.h"
NodePresent::NodePresent(std::shared_ptr<LogicalNode> bind) NodePresent::NodePresent(QWidget* pwidget, double& width_bind, std::shared_ptr<LogicalNode> bind)
:_node_bind(bind) { : _widget_p(pwidget), _node_bind(bind), _column_width(width_bind) {
} }
QRectF NodePresent::contentMeasure() const QRectF NodePresent::contentMeasure() const
{ {
return QRectF(0, 0, 200, 40); auto metrics = this->_widget_p->fontMetrics();
return metrics.boundingRect(this->_node_bind->rtName());
} }
QRectF NodePresent::boundingRect() const QRectF NodePresent::boundingRect() const
{ {
return contentMeasure() + QMargins(0, 0, 20, 20); auto rect = contentMeasure();
rect.setWidth(std::max(_column_width, rect.width()));
_column_width = rect.width();
return rect + QMargins(0, 0, 30, 30);
} }
#include <QStyleOptionGraphicsItem>
void NodePresent::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) void NodePresent::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{ {
//painter->drawRect(boundingRect()); painter->drawRect(boundingRect());
painter->translate(QPointF(10, 10)); painter->fillRect(option->rect - QMargins(8,8,8,8), Qt::gray);
painter->translate(QPointF(15, 15));
auto rect = contentMeasure(); auto rect = contentMeasure();
painter->fillRect(rect, Qt::red); painter->drawText(rect, 0, this->_node_bind->rtName());
} }
BehaviorsPresent::BehaviorsPresent(QWidget* parent /*= nullptr*/) BehaviorsPresent::BehaviorsPresent(QWidget* parent /*= nullptr*/)
: QGraphicsView(parent) { : QGraphicsView(parent) {
this->setScene(&_bind_scene); this->setScene(&_bind_scene);
auto font = this->font();
font.setPixelSize(20);
this->setFont(font);
} }
void BehaviorsPresent::setRoot(std::shared_ptr<BehaviorMapNode> root) void BehaviorsPresent::setRoot(std::shared_ptr<BehaviorMapNode> root)
@ -54,8 +65,12 @@ void BehaviorsPresent::presentAllocate(std::shared_ptr<LogicalNode> ins)
default: default:
if (_present_peers.contains(ins)) if (_present_peers.contains(ins))
return; return;
_present_peers[ins] = new NodePresent(ins); for(auto idx=_column_aligns.size(); idx < ins->depth() + 1; ++idx)
_column_aligns.append(0);
auto &current_width = _column_aligns[ins->depth()];
_present_peers[ins] = new NodePresent(this, current_width, ins);
this->_bind_scene.addItem(_present_peers[ins]); this->_bind_scene.addItem(_present_peers[ins]);
break; break;
} }
@ -76,6 +91,7 @@ void BehaviorsPresent::presentRelease(std::shared_ptr<LogicalNode> ins)
default: default:
if (this->_present_peers.contains(ins)) if (this->_present_peers.contains(ins))
delete this->_present_peers[ins]; delete this->_present_peers[ins];
this->_present_peers.remove(ins);
break; break;
} }
} }

View File

@ -4,12 +4,19 @@
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QGraphicsView> #include <QGraphicsView>
uint qHash(const std::shared_ptr<LogicalNode> data, uint seed) noexcept;
/// <summary>
/// 节点显示代理
/// </summary>
class NodePresent : public QGraphicsItem { class NodePresent : public QGraphicsItem {
private: private:
QWidget *const _widget_p;
std::shared_ptr<LogicalNode> _node_bind; std::shared_ptr<LogicalNode> _node_bind;
double &_column_width;
public: public:
NodePresent(std::shared_ptr<LogicalNode> bind); NodePresent(QWidget *pwidget, double &width_bind, std::shared_ptr<LogicalNode> bind);
QRectF contentMeasure() const; QRectF contentMeasure() const;
@ -19,7 +26,6 @@ public:
}; };
uint qHash(const std::shared_ptr<LogicalNode> data, uint seed) noexcept;
/// <summary> /// <summary>
/// 行为树编辑展示 /// 行为树编辑展示
/// </summary> /// </summary>
@ -29,6 +35,7 @@ private:
QGraphicsScene _bind_scene; QGraphicsScene _bind_scene;
const double _space_h = 120; const double _space_h = 120;
QVector<double> _column_aligns;
QHash<std::shared_ptr<LogicalNode>, NodePresent*> _present_peers; QHash<std::shared_ptr<LogicalNode>, NodePresent*> _present_peers;
/// <summary> /// <summary>

View File

@ -39,7 +39,7 @@ int main(int argc, char *argv[])
item->setID(10000); item->setID(10000);
parallel->insert(item); parallel->insert(item);
item = std::make_shared<CompareNode>(); item = std::make_shared<CompareNode>();
item->setID(10001); item->setID(100010000);
parallel->insert(item); parallel->insert(item);
w.setRoot(root); w.setRoot(root);