diff --git a/ComponentBasic/BehaviorPerform.cpp b/ComponentBasic/BehaviorPerform.cpp index 18620ec..2e92f75 100644 --- a/ComponentBasic/BehaviorPerform.cpp +++ b/ComponentBasic/BehaviorPerform.cpp @@ -236,6 +236,31 @@ void BehaviorMapNode::setVariable(const QString& key, IO_TYPE t, std::shared_ptr _variables[key] = std::make_pair(t, ins); } +QList BehaviorMapNode::inputVariableKeys() const +{ + QList keys; + for(auto key : this->_variables.keys()) + if(this->_variables[key].first == IO_TYPE::INPUT) + keys << key; + + return keys; +} + +QList BehaviorMapNode::outputVariableKeys() const +{ + QList 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 BehaviorMapNode::getVariable(const QString& key) const { if (!_variables.contains(key)) diff --git a/ComponentBasic/BehaviorPerform.h b/ComponentBasic/BehaviorPerform.h index 13c7ba5..03dfb95 100644 --- a/ComponentBasic/BehaviorPerform.h +++ b/ComponentBasic/BehaviorPerform.h @@ -479,6 +479,9 @@ public: /// 节点初始化时会按照配置注册默认值变量 /// virtual void setVariable(const QString& key, IO_TYPE t, std::shared_ptr ins); + virtual QList inputVariableKeys() const; + virtual QList outputVariableKeys() const; + virtual void removeVariable(const QString &key); /// /// 获取指定名称的数据变量 /// diff --git a/SimsWorld/BehaviorEditor.cpp b/SimsWorld/BehaviorEditor.cpp index e2bde4a..2b686e7 100644 --- a/SimsWorld/BehaviorEditor.cpp +++ b/SimsWorld/BehaviorEditor.cpp @@ -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 ins) } } -void BehaviorsPresent::presentRelease(std::shared_ptr 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 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:: return origin_offset + QPointF(0, node_outline.height()); } +#include 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, NodePrese :_present_set_bind(present_set), _head_node(head_anchor) { } +std::shared_ptr BranchPresent::headNode() const +{ + return _head_node->logicalBind(); +} + QRectF BranchPresent::startOutline() const { auto parent_node = _head_node->logicalBind()->parent().lock(); diff --git a/SimsWorld/BehaviorEditor.h b/SimsWorld/BehaviorEditor.h index cb81550..c0ea807 100644 --- a/SimsWorld/BehaviorEditor.h +++ b/SimsWorld/BehaviorEditor.h @@ -59,6 +59,8 @@ private: public: BranchPresent(const QHash, NodePresent*> &present_set, NodePresent* head_anchor); + std::shared_ptr headNode() const; + QRectF startOutline() const; QRectF endOutline() const; void resetArrow(const QPointF& start, const QPointF& end); @@ -104,11 +106,6 @@ public: /// /// NodePresent* presentAllocate(std::shared_ptr ins); - /// - /// 递归释放显示节点 - /// - /// - void presentRelease(std::shared_ptr ins); /// /// 内容重新布局 @@ -158,8 +155,10 @@ private: QTextBrowser* const _logs_present; std::shared_ptr _map_root; + QUrl _current_fileurl; void open_behavior_map(); + void new_behavior_map(); public: BehaviorEditor(QWidget* parent = nullptr);