完善行为树地图编辑接口

This commit is contained in:
codeboss 2025-07-05 00:32:32 +08:00
parent de23ab621a
commit 45544cdb0b
4 changed files with 98 additions and 30 deletions

View File

@ -236,6 +236,31 @@ void BehaviorMapNode::setVariable(const QString& key, IO_TYPE t, std::shared_ptr
_variables[key] = std::make_pair(t, ins);
}
QList<QString> BehaviorMapNode::inputVariableKeys() const
{
QList<QString> keys;
for(auto key : this->_variables.keys())
if(this->_variables[key].first == IO_TYPE::INPUT)
keys << key;
return keys;
}
QList<QString> BehaviorMapNode::outputVariableKeys() const
{
QList<QString> keys;
for (auto key : this->_variables.keys())
if (this->_variables[key].first == IO_TYPE::OUTPUT)
keys << key;
return keys;
}
void BehaviorMapNode::removeVariable(const QString& key)
{
this->_variables.remove(key);
}
std::shared_ptr<TopicData> BehaviorMapNode::getVariable(const QString& key) const
{
if (!_variables.contains(key))

View File

@ -479,6 +479,9 @@ public:
/// 节点初始化时会按照配置注册默认值变量
/// </summary>
virtual void setVariable(const QString& key, IO_TYPE t, std::shared_ptr<TopicData> ins);
virtual QList<QString> inputVariableKeys() const;
virtual QList<QString> outputVariableKeys() const;
virtual void removeVariable(const QString &key);
/// <summary>
/// 获取指定名称的数据变量
/// </summary>

View File

@ -166,7 +166,7 @@ QRectF NodePresent::boundingRect() const
{
auto rect = contentMeasure();
auto depth = this->_node_bind->depth();
if(_columns_width_seqs.size() <= depth)
if (_columns_width_seqs.size() <= depth)
_columns_width_seqs.resize(depth + 1);
auto width_col = _columns_width_seqs[depth];
@ -280,25 +280,34 @@ NodePresent* BehaviorsPresent::presentAllocate(std::shared_ptr<LogicalNode> ins)
}
}
void BehaviorsPresent::presentRelease(std::shared_ptr<LogicalNode> ins)
{
switch (ins->nodeKind())
{
case NodeKind::MAPNODE:
case NodeKind::PARALLELNODE:
case NodeKind::SELECTORNODE:
case NodeKind::SEQUENCENODE: {
for (auto item : ins->children()) {
presentRelease(item);
}
}break;
default:
if (this->_present_peers.contains(ins))
delete this->_present_peers[ins];
this->_present_peers.remove(ins);
break;
}
}
//void BehaviorsPresent::presentRelease(std::shared_ptr<LogicalNode> ins)
//{
// switch (ins->nodeKind())
// {
// case NodeKind::MAPNODE:
// case NodeKind::PARALLELNODE:
// case NodeKind::SELECTORNODE:
// case NodeKind::SEQUENCENODE: {
// for (auto item : ins->children()) {
// presentRelease(item);
// }
// }
// default:
// if (this->_present_peers.contains(ins))
// delete this->_present_peers[ins];
// this->_present_peers.remove(ins);
//
// // 清除BranchPresent
// BranchPresent *_target = nullptr;
// for(auto node : this->_branch_list)
// if (node->headNode() == ins) {
// _target = node;
// delete node;
// }
// this->_branch_list.removeAll(_target);
// break;
// }
//}
const double NodePresent::padding = 8;
void BehaviorsPresent::relayout()
@ -402,14 +411,41 @@ QPointF BehaviorsPresent::nodeRelayout(QHash<std::shared_ptr<LogicalNode>, std::
return origin_offset + QPointF(0, node_outline.height());
}
#include <QJsonDocument>
void BehaviorEditor::open_behavior_map()
{
auto url = QFileDialog::getOpenFileUrl(this, u8"´ò¿ªÐÐΪÊ÷Îļþ", QUrl(), "*.behw");
this->_logical_present->setRoot(_map_root);
if (!url.isValid())
_current_fileurl = QFileDialog::getOpenFileUrl(this, u8"打开行为树文件", QUrl(), "*.behw");
if (!_current_fileurl.isValid())
return;
QFile data_file(_current_fileurl.toLocalFile());
data_file.open(QIODevice::ReadOnly);
auto json = QJsonDocument::fromJson(data_file.readAll());
// 载入数据,回复节点内容
_map_root->recoveryFrom(json.object());
this->_logical_present->setRoot(_map_root);
}
void BehaviorEditor::new_behavior_map()
{
_current_fileurl = QFileDialog::getSaveFileUrl(this, u8"创建行为树文件", QUrl(), "*.behw");
if (!_current_fileurl.isValid())
return;
// 清空所有子节点内容
auto childs = this->_map_root->children();
if (childs.size())
this->_map_root->remove(childs.first());
// 清空行为树节点变量列表
auto variable_key_set = this->_map_root->inputVariableKeys();
variable_key_set << this->_map_root->outputVariableKeys();
for(auto key : variable_key_set)
this->_map_root->removeVariable(key);
// 重置地图节点
this->_logical_present->setRoot(_map_root);
}
BehaviorEditor::BehaviorEditor(QWidget* parent /*= nullptr*/)
@ -505,6 +541,11 @@ BranchPresent::BranchPresent(const QHash<std::shared_ptr<LogicalNode>, NodePrese
:_present_set_bind(present_set), _head_node(head_anchor) {
}
std::shared_ptr<LogicalNode> BranchPresent::headNode() const
{
return _head_node->logicalBind();
}
QRectF BranchPresent::startOutline() const
{
auto parent_node = _head_node->logicalBind()->parent().lock();

View File

@ -59,6 +59,8 @@ private:
public:
BranchPresent(const QHash<std::shared_ptr<LogicalNode>, NodePresent*> &present_set, NodePresent* head_anchor);
std::shared_ptr<LogicalNode> headNode() const;
QRectF startOutline() const;
QRectF endOutline() const;
void resetArrow(const QPointF& start, const QPointF& end);
@ -104,11 +106,6 @@ public:
/// <param name="_present_peers"></param>
/// <param name="ins"></param>
NodePresent* presentAllocate(std::shared_ptr<LogicalNode> ins);
/// <summary>
/// 递归释放显示节点
/// </summary>
/// <param name="ins"></param>
void presentRelease(std::shared_ptr<LogicalNode> ins);
/// <summary>
/// ÄÚÈÝÖØÐ²¼¾Ö
@ -158,8 +155,10 @@ private:
QTextBrowser* const _logs_present;
std::shared_ptr<BehaviorMapNode> _map_root;
QUrl _current_fileurl;
void open_behavior_map();
void new_behavior_map();
public:
BehaviorEditor(QWidget* parent = nullptr);