2021-01-30 22:39:06 +01:00
|
|
|
#include "views/view_data_processor.hpp"
|
|
|
|
|
|
|
|
#include <hex/providers/provider.hpp>
|
2021-05-18 18:06:47 +02:00
|
|
|
#include <helpers/project_file_handler.hpp>
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
#include <imnodes.h>
|
2021-05-17 23:17:58 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2021-03-03 22:26:17 +01:00
|
|
|
ViewDataProcessor::ViewDataProcessor() : View("hex.view.data_processor.name") {
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<EventSettingsChanged>(this, [] {
|
2021-02-16 23:42:35 +01:00
|
|
|
auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color");
|
|
|
|
|
2021-03-06 12:40:29 +01:00
|
|
|
if (theme.is_number()) {
|
|
|
|
switch (static_cast<int>(theme)) {
|
2021-02-16 23:42:35 +01:00
|
|
|
default:
|
|
|
|
case 0: /* Dark theme */
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::StyleColorsDark();
|
2021-02-16 23:42:35 +01:00
|
|
|
break;
|
|
|
|
case 1: /* Light theme */
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::StyleColorsLight();
|
2021-02-16 23:42:35 +01:00
|
|
|
break;
|
|
|
|
case 2: /* Classic theme */
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::StyleColorsClassic();
|
2021-02-16 23:42:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::GetStyle().Flags = ImNodesStyleFlags_NodeOutline | ImNodesStyleFlags_GridLines;
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
});
|
2021-02-04 01:14:05 +01:00
|
|
|
|
2021-05-18 18:06:47 +02:00
|
|
|
EventManager::subscribe<EventProjectFileStore>(this, [this] {
|
|
|
|
ProjectFile::setDataProcessorContent(this->saveNodes());
|
|
|
|
});
|
|
|
|
|
|
|
|
EventManager::subscribe<EventProjectFileLoad>(this, [this] {
|
|
|
|
this->loadNodes(ProjectFile::getDataProcessorContent());
|
|
|
|
});
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<EventFileLoaded>(this, [this](const std::string &path){
|
2021-02-04 01:14:05 +01:00
|
|
|
for (auto &node : this->m_nodes) {
|
|
|
|
node->setCurrentOverlay(nullptr);
|
|
|
|
}
|
|
|
|
this->m_dataOverlays.clear();
|
|
|
|
});
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewDataProcessor::~ViewDataProcessor() {
|
|
|
|
for (auto &node : this->m_nodes)
|
|
|
|
delete node;
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::unsubscribe<EventSettingsChanged>(this);
|
|
|
|
EventManager::unsubscribe<EventFileLoaded>(this);
|
2021-05-18 18:06:47 +02:00
|
|
|
EventManager::unsubscribe<EventProjectFileStore>(this);
|
|
|
|
EventManager::unsubscribe<EventProjectFileLoad>(this);
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ViewDataProcessor::eraseLink(u32 id) {
|
|
|
|
auto link = std::find_if(this->m_links.begin(), this->m_links.end(), [&id](auto link){ return link.getID() == id; });
|
|
|
|
|
|
|
|
if (link == this->m_links.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto &node : this->m_nodes) {
|
|
|
|
for (auto &attribute : node->getAttributes()) {
|
|
|
|
attribute.removeConnectedAttribute(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_links.erase(link);
|
2021-08-17 13:41:19 +02:00
|
|
|
|
|
|
|
ProjectFile::markDirty();
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewDataProcessor::eraseNodes(const std::vector<int> &ids) {
|
|
|
|
for (const int id : ids) {
|
|
|
|
auto node = std::find_if(this->m_nodes.begin(), this->m_nodes.end(), [&id](auto node){ return node->getID() == id; });
|
|
|
|
|
|
|
|
for (auto &attr : (*node)->getAttributes()) {
|
|
|
|
std::vector<u32> linksToRemove;
|
|
|
|
for (auto &[linkId, connectedAttr] : attr.getConnectedAttributes())
|
|
|
|
linksToRemove.push_back(linkId);
|
|
|
|
|
|
|
|
for (auto linkId : linksToRemove)
|
|
|
|
eraseLink(linkId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const int id : ids) {
|
|
|
|
auto node = std::find_if(this->m_nodes.begin(), this->m_nodes.end(), [&id](auto node){ return node->getID() == id; });
|
|
|
|
|
|
|
|
std::erase_if(this->m_endNodes, [&id](auto node){ return node->getID() == id; });
|
|
|
|
|
|
|
|
delete *node;
|
|
|
|
|
|
|
|
this->m_nodes.erase(node);
|
|
|
|
}
|
2021-08-17 13:41:19 +02:00
|
|
|
|
|
|
|
ProjectFile::markDirty();
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewDataProcessor::processNodes() {
|
|
|
|
if (this->m_dataOverlays.size() != this->m_endNodes.size()) {
|
|
|
|
for (auto overlay : this->m_dataOverlays)
|
|
|
|
SharedData::currentProvider->deleteOverlay(overlay);
|
|
|
|
this->m_dataOverlays.clear();
|
|
|
|
|
|
|
|
for (u32 i = 0; i < this->m_endNodes.size(); i++)
|
|
|
|
this->m_dataOverlays.push_back(SharedData::currentProvider->newOverlay());
|
2021-01-31 16:11:25 +01:00
|
|
|
|
|
|
|
u32 overlayIndex = 0;
|
|
|
|
for (auto endNode : this->m_endNodes) {
|
|
|
|
endNode->setCurrentOverlay(this->m_dataOverlays[overlayIndex]);
|
|
|
|
overlayIndex++;
|
|
|
|
}
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
this->m_currNodeError.reset();
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (auto &endNode : this->m_endNodes) {
|
|
|
|
endNode->resetOutputData();
|
2021-03-07 13:20:33 +01:00
|
|
|
|
|
|
|
for (auto &node : this->m_nodes)
|
|
|
|
node->resetProcessedInputs();
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
endNode->process();
|
|
|
|
}
|
|
|
|
} catch (dp::Node::NodeError &e) {
|
|
|
|
this->m_currNodeError = e;
|
2021-03-21 14:52:21 +01:00
|
|
|
|
|
|
|
for (auto overlay : this->m_dataOverlays)
|
|
|
|
SharedData::currentProvider->deleteOverlay(overlay);
|
|
|
|
this->m_dataOverlays.clear();
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
} catch (std::runtime_error &e) {
|
|
|
|
printf("Node implementation bug! %s\n", e.what());
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
2021-02-04 01:14:05 +01:00
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewDataProcessor::drawContent() {
|
2021-03-03 22:26:17 +01:00
|
|
|
if (ImGui::Begin(View::toWindowName("hex.view.data_processor.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
if (ImGui::IsMouseReleased(ImGuiMouseButton_Right) && ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows)) {
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::ClearNodeSelection();
|
|
|
|
ImNodes::ClearLinkSelection();
|
2021-02-04 00:22:14 +01:00
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
this->m_rightClickedCoords = ImGui::GetMousePos();
|
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
if (ImNodes::IsNodeHovered(&this->m_rightClickedId))
|
2021-01-30 22:39:06 +01:00
|
|
|
ImGui::OpenPopup("Node Menu");
|
2021-08-17 13:38:59 +02:00
|
|
|
else if (ImNodes::IsLinkHovered(&this->m_rightClickedId))
|
2021-01-30 22:39:06 +01:00
|
|
|
ImGui::OpenPopup("Link Menu");
|
|
|
|
else
|
|
|
|
ImGui::OpenPopup("Context Menu");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginPopup("Context Menu")) {
|
|
|
|
dp::Node *node = nullptr;
|
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
if (ImNodes::NumSelectedNodes() > 0 || ImNodes::NumSelectedLinks() > 0) {
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::MenuItem("hex.view.data_processor.name"_lang)) {
|
2021-01-30 22:39:06 +01:00
|
|
|
std::vector<int> ids;
|
2021-08-17 13:38:59 +02:00
|
|
|
ids.resize(ImNodes::NumSelectedNodes());
|
|
|
|
ImNodes::GetSelectedNodes(ids.data());
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
this->eraseNodes(ids);
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::ClearNodeSelection();
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
ids.resize(ImNodes::NumSelectedLinks());
|
|
|
|
ImNodes::GetSelectedLinks(ids.data());
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
for (auto id : ids)
|
|
|
|
this->eraseLink(id);
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::ClearLinkSelection();
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 14:46:47 +01:00
|
|
|
for (const auto &[unlocalizedCategory, unlocalizedName, function] : ContentRegistry::DataProcessorNode::getEntries()) {
|
|
|
|
if (unlocalizedCategory.empty() && unlocalizedName.empty()) {
|
2021-01-30 22:39:06 +01:00
|
|
|
ImGui::Separator();
|
2021-03-06 14:46:47 +01:00
|
|
|
} else if (unlocalizedCategory.empty()) {
|
|
|
|
if (ImGui::MenuItem(LangEntry(unlocalizedName))) {
|
2021-01-30 22:39:06 +01:00
|
|
|
node = function();
|
2021-08-17 13:41:19 +02:00
|
|
|
ProjectFile::markDirty();
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-06 14:46:47 +01:00
|
|
|
if (ImGui::BeginMenu(LangEntry(unlocalizedCategory))) {
|
|
|
|
if (ImGui::MenuItem(LangEntry(unlocalizedName))) {
|
2021-01-30 22:39:06 +01:00
|
|
|
node = function();
|
2021-08-17 13:41:19 +02:00
|
|
|
ProjectFile::markDirty();
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node != nullptr) {
|
|
|
|
this->m_nodes.push_back(node);
|
|
|
|
|
|
|
|
bool hasOutput = false;
|
2021-02-04 12:46:38 +01:00
|
|
|
bool hasInput = false;
|
2021-01-30 22:39:06 +01:00
|
|
|
for (auto &attr : node->getAttributes()) {
|
|
|
|
if (attr.getIOType() == dp::Attribute::IOType::Out)
|
|
|
|
hasOutput = true;
|
2021-02-04 12:46:38 +01:00
|
|
|
|
|
|
|
if (attr.getIOType() == dp::Attribute::IOType::In)
|
|
|
|
hasInput = true;
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 12:46:38 +01:00
|
|
|
if (hasInput && !hasOutput)
|
2021-01-30 22:39:06 +01:00
|
|
|
this->m_endNodes.push_back(node);
|
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::SetNodeScreenSpacePos(node->getID(), this->m_rightClickedCoords);
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginPopup("Node Menu")) {
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::MenuItem("hex.view.data_processor.menu.remove_node"_lang))
|
2021-01-30 22:39:06 +01:00
|
|
|
this->eraseNodes({ this->m_rightClickedId });
|
|
|
|
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginPopup("Link Menu")) {
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::MenuItem("hex.view.data_processor.menu.remove_link"_lang))
|
2021-01-30 22:39:06 +01:00
|
|
|
this->eraseLink(this->m_rightClickedId);
|
|
|
|
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
{
|
|
|
|
int nodeId;
|
2021-08-17 13:38:59 +02:00
|
|
|
if (ImNodes::IsNodeHovered(&nodeId) && this->m_currNodeError.has_value() && this->m_currNodeError->first->getID() == nodeId) {
|
2021-02-04 01:14:05 +01:00
|
|
|
ImGui::BeginTooltip();
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.common.error"_lang);
|
2021-02-04 01:14:05 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::TextUnformatted(this->m_currNodeError->second.c_str());
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::BeginNodeEditor();
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
for (auto& node : this->m_nodes) {
|
2021-02-04 01:14:05 +01:00
|
|
|
const bool hasError = this->m_currNodeError.has_value() && this->m_currNodeError->first == node;
|
|
|
|
|
|
|
|
if (hasError)
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::PushColorStyle(ImNodesCol_NodeOutline, 0xFF0000FF);
|
2021-02-04 01:14:05 +01:00
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::BeginNode(node->getID());
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::BeginNodeTitleBar();
|
2021-05-17 23:17:58 +02:00
|
|
|
ImGui::TextUnformatted(LangEntry(node->getUnlocalizedTitle()));
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::EndNodeTitleBar();
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
node->drawNode();
|
|
|
|
|
|
|
|
for (auto& attribute : node->getAttributes()) {
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodesPinShape pinShape;
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
switch (attribute.getType()) {
|
2021-08-17 13:38:59 +02:00
|
|
|
case dp::Attribute::Type::Integer: pinShape = ImNodesPinShape_Circle; break;
|
|
|
|
case dp::Attribute::Type::Float: pinShape = ImNodesPinShape_Triangle; break;
|
|
|
|
case dp::Attribute::Type::Buffer: pinShape = ImNodesPinShape_Quad; break;
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (attribute.getIOType() == dp::Attribute::IOType::In) {
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::BeginInputAttribute(attribute.getID(), pinShape);
|
2021-02-13 15:15:32 +01:00
|
|
|
ImGui::TextUnformatted(LangEntry(attribute.getUnlocalizedName()));
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::EndInputAttribute();
|
2021-01-30 22:39:06 +01:00
|
|
|
} else if (attribute.getIOType() == dp::Attribute::IOType::Out) {
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::BeginOutputAttribute(attribute.getID(), ImNodesPinShape(pinShape + 1));
|
2021-02-13 15:15:32 +01:00
|
|
|
ImGui::TextUnformatted(LangEntry(attribute.getUnlocalizedName()));
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::EndOutputAttribute();
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::EndNode();
|
2021-02-04 01:14:05 +01:00
|
|
|
|
|
|
|
if (hasError)
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::PopColorStyle();
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &link : this->m_links)
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::Link(link.getID(), link.getFromID(), link.getToID());
|
|
|
|
|
|
|
|
ImNodes::MiniMap(0.2F, ImNodesMiniMapLocation_BottomRight);
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::EndNodeEditor();
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2021-01-31 11:00:35 +01:00
|
|
|
{
|
|
|
|
int linkId;
|
2021-08-17 13:38:59 +02:00
|
|
|
if (ImNodes::IsLinkDestroyed(&linkId)) {
|
2021-01-31 11:00:35 +01:00
|
|
|
this->eraseLink(linkId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
{
|
|
|
|
int from, to;
|
2021-08-17 13:38:59 +02:00
|
|
|
if (ImNodes::IsLinkCreated(&from, &to)) {
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
dp::Attribute *fromAttr, *toAttr;
|
|
|
|
for (auto &node : this->m_nodes) {
|
|
|
|
for (auto &attribute : node->getAttributes()) {
|
|
|
|
if (attribute.getID() == from)
|
|
|
|
fromAttr = &attribute;
|
|
|
|
else if (attribute.getID() == to)
|
|
|
|
toAttr = &attribute;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fromAttr == nullptr || toAttr == nullptr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (fromAttr->getType() != toAttr->getType())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (fromAttr->getIOType() == toAttr->getIOType())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!toAttr->getConnectedAttributes().empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
auto newLink = this->m_links.emplace_back(from, to);
|
|
|
|
|
|
|
|
fromAttr->addConnectedAttribute(newLink.getID(), toAttr);
|
|
|
|
toAttr->addConnectedAttribute(newLink.getID(), fromAttr);
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-08-17 13:38:59 +02:00
|
|
|
const int selectedLinkCount = ImNodes::NumSelectedLinks();
|
2021-01-30 22:39:06 +01:00
|
|
|
if (selectedLinkCount > 0 && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Delete))) {
|
|
|
|
static std::vector<int> selectedLinks;
|
|
|
|
selectedLinks.resize(static_cast<size_t>(selectedLinkCount));
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::GetSelectedLinks(selectedLinks.data());
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
for (const int id : selectedLinks) {
|
|
|
|
eraseLink(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-08-17 13:38:59 +02:00
|
|
|
const int selectedNodeCount = ImNodes::NumSelectedNodes();
|
2021-01-30 22:39:06 +01:00
|
|
|
if (selectedNodeCount > 0 && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Delete))) {
|
|
|
|
static std::vector<int> selectedNodes;
|
|
|
|
selectedNodes.resize(static_cast<size_t>(selectedNodeCount));
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::GetSelectedNodes(selectedNodes.data());
|
2021-01-30 22:39:06 +01:00
|
|
|
|
|
|
|
this->eraseNodes(selectedNodes);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->processNodes();
|
|
|
|
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewDataProcessor::drawMenu() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-17 23:17:58 +02:00
|
|
|
std::string ViewDataProcessor::saveNodes() {
|
|
|
|
using json = nlohmann::json;
|
|
|
|
json output;
|
|
|
|
|
|
|
|
output["nodes"] = json::object();
|
|
|
|
for (auto &node : this->m_nodes) {
|
|
|
|
auto id = node->getID();
|
|
|
|
auto &currNodeOutput = output["nodes"][std::to_string(id)];
|
2021-08-17 13:38:59 +02:00
|
|
|
auto pos = ImNodes::GetNodeGridSpacePos(id);
|
2021-05-17 23:17:58 +02:00
|
|
|
|
|
|
|
currNodeOutput["type"] = node->getUnlocalizedName();
|
|
|
|
currNodeOutput["pos"] = { { "x", pos.x }, { "y", pos.y } };
|
|
|
|
currNodeOutput["attrs"] = json::array();
|
|
|
|
currNodeOutput["id"] = id;
|
|
|
|
|
2021-05-18 18:06:47 +02:00
|
|
|
currNodeOutput["data"] = node->store();
|
|
|
|
|
2021-05-17 23:17:58 +02:00
|
|
|
u32 attrIndex = 0;
|
|
|
|
for (auto &attr : node->getAttributes()) {
|
|
|
|
currNodeOutput["attrs"][attrIndex] = attr.getID();
|
|
|
|
attrIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output["links"] = json::object();
|
|
|
|
for (auto &link : this->m_links) {
|
|
|
|
auto id = link.getID();
|
|
|
|
auto &currOutput = output["links"][std::to_string(id)];
|
|
|
|
|
|
|
|
currOutput["id"] = id;
|
|
|
|
currOutput["from"] = link.getFromID();
|
|
|
|
currOutput["to"] = link.getToID();
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewDataProcessor::loadNodes(std::string_view data) {
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
json input = json::parse(data);
|
|
|
|
|
2021-05-18 18:06:47 +02:00
|
|
|
u32 maxNodeId = 0;
|
|
|
|
u32 maxAttrId = 0;
|
|
|
|
u32 maxLinkId = 0;
|
2021-05-17 23:17:58 +02:00
|
|
|
|
|
|
|
for (auto &node : this->m_nodes)
|
|
|
|
delete node;
|
|
|
|
|
|
|
|
this->m_nodes.clear();
|
|
|
|
this->m_endNodes.clear();
|
|
|
|
this->m_links.clear();
|
|
|
|
|
|
|
|
auto &nodeEntries = ContentRegistry::DataProcessorNode::getEntries();
|
|
|
|
for (auto &node : input["nodes"]) {
|
|
|
|
dp::Node *newNode = nullptr;
|
|
|
|
for (auto &entry : nodeEntries) {
|
|
|
|
if (entry.name == node["type"])
|
|
|
|
newNode = entry.creatorFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newNode == nullptr)
|
|
|
|
continue;
|
|
|
|
|
2021-05-18 18:06:47 +02:00
|
|
|
u32 nodeId = node["id"];
|
|
|
|
maxNodeId = std::max(nodeId, maxNodeId);
|
2021-05-17 23:17:58 +02:00
|
|
|
|
2021-05-18 18:06:47 +02:00
|
|
|
newNode->setID(nodeId);
|
2021-05-17 23:17:58 +02:00
|
|
|
|
|
|
|
bool hasOutput = false;
|
|
|
|
bool hasInput = false;
|
|
|
|
u32 attrIndex = 0;
|
|
|
|
for (auto &attr : newNode->getAttributes()) {
|
|
|
|
if (attr.getIOType() == dp::Attribute::IOType::Out)
|
|
|
|
hasOutput = true;
|
|
|
|
|
|
|
|
if (attr.getIOType() == dp::Attribute::IOType::In)
|
|
|
|
hasInput = true;
|
|
|
|
|
2021-05-18 18:06:47 +02:00
|
|
|
u32 attrId = node["attrs"][attrIndex];
|
|
|
|
maxAttrId = std::max(attrId, maxAttrId);
|
|
|
|
|
|
|
|
attr.setID(attrId);
|
2021-05-17 23:17:58 +02:00
|
|
|
attrIndex++;
|
|
|
|
}
|
|
|
|
|
2021-05-18 18:06:47 +02:00
|
|
|
if (!node["data"].is_null())
|
|
|
|
newNode->load(node["data"]);
|
|
|
|
|
2021-05-17 23:17:58 +02:00
|
|
|
if (hasInput && !hasOutput)
|
|
|
|
this->m_endNodes.push_back(newNode);
|
|
|
|
|
|
|
|
this->m_nodes.push_back(newNode);
|
2021-08-17 13:38:59 +02:00
|
|
|
ImNodes::SetNodeGridSpacePos(nodeId, ImVec2(node["pos"]["x"], node["pos"]["y"]));
|
2021-05-17 23:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &link : input["links"]) {
|
|
|
|
dp::Link newLink(link["from"], link["to"]);
|
|
|
|
|
2021-05-18 18:06:47 +02:00
|
|
|
u32 linkId = link["id"];
|
|
|
|
maxLinkId = std::max(linkId, maxLinkId);
|
|
|
|
|
|
|
|
newLink.setID(linkId);
|
2021-05-17 23:17:58 +02:00
|
|
|
this->m_links.push_back(newLink);
|
|
|
|
|
|
|
|
dp::Attribute *fromAttr, *toAttr;
|
|
|
|
for (auto &node : this->m_nodes) {
|
|
|
|
for (auto &attribute : node->getAttributes()) {
|
|
|
|
if (attribute.getID() == newLink.getFromID())
|
|
|
|
fromAttr = &attribute;
|
|
|
|
else if (attribute.getID() == newLink.getToID())
|
|
|
|
toAttr = &attribute;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fromAttr == nullptr || toAttr == nullptr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (fromAttr->getType() != toAttr->getType())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (fromAttr->getIOType() == toAttr->getIOType())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!toAttr->getConnectedAttributes().empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
fromAttr->addConnectedAttribute(newLink.getID(), toAttr);
|
|
|
|
toAttr->addConnectedAttribute(newLink.getID(), fromAttr);
|
|
|
|
}
|
2021-05-18 18:06:47 +02:00
|
|
|
|
|
|
|
SharedData::dataProcessorNodeIdCounter = maxNodeId + 1;
|
|
|
|
SharedData::dataProcessorAttrIdCounter = maxAttrId + 1;
|
|
|
|
SharedData::dataProcessorLinkIdCounter = maxLinkId + 1;
|
2021-05-17 23:17:58 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|