GraphModel

This commit is contained in:
2024-04-08 07:39:41 +05:00
parent 58b62eb3a8
commit 8eed3f7065
10 changed files with 252 additions and 80 deletions

View File

@@ -4,37 +4,28 @@
namespace GraphC::gui {
NodeAttribute::NodeAttribute(id_t _id, NodeAttributeType _type, std::string _title)
: id(_id), type(_type), title(_title) {}
NodeAttributeLink::NodeAttributeLink(id_t _id, id_t _in, id_t _out)
: id(_id), in_attr_id(_in), out_attr_id(_out) {}
Node::Node(id_t _id, std::string _title)
: id(_id), title(_title) {}
void Node::draw(){
ImNodes::BeginNode(id);
void drawNode(GraphModel::Node& node){
ImNodes::BeginNode(node.id);
ImNodes::BeginNodeTitleBar();
ImGui::TextUnformatted(title.c_str());
ImGui::TextUnformatted(node.title.c_str());
ImNodes::EndNodeTitleBar();
for(NodeAttribute& a : attributes)
for(GraphModel::Attribute& a : node.attributes)
{
switch (a.type)
{
case NodeAttributeType::Input:
case GraphModel::Attribute::Type::Input:
ImNodes::BeginInputAttribute(a.id);
ImGui::Text("%s", a.title.c_str());
ImNodes::EndInputAttribute();
break;
case NodeAttributeType::Output:
case GraphModel::Attribute::Type::Output:
ImNodes::BeginOutputAttribute(a.id);
ImGui::Text("%s", a.title.c_str());
ImGui::Indent(40);
ImNodes::EndOutputAttribute();
break;
case NodeAttributeType::Static:
case GraphModel::Attribute::Type::Static:
ImNodes::BeginStaticAttribute(a.id);
ImGui::Text("%s", a.title.c_str());
ImNodes::EndStaticAttribute();
@@ -48,17 +39,15 @@ void Node::draw(){
}
Node CreateExampleNode(id_t* next_id, std::string title){
Node a = Node((*next_id)++, title);
a.attributes.push_back(NodeAttribute((*next_id)++, NodeAttributeType::Input, "In"));
a.attributes.push_back(NodeAttribute((*next_id)++, NodeAttributeType::Output, "Out"));
a.attributes.push_back(NodeAttribute((*next_id)++, NodeAttributeType::Static, "Static"));
GraphModel::Node CreateExampleNode(GraphModel::id_t* next_id, std::string title){
GraphModel::Node a = GraphModel::Node((*next_id)++, title);
a.attributes.push_back(GraphModel::Attribute((*next_id)++, &a, GraphModel::Attribute::Type::Input, "In"));
a.attributes.push_back(GraphModel::Attribute((*next_id)++, &a, GraphModel::Attribute::Type::Output, "Out"));
a.attributes.push_back(GraphModel::Attribute((*next_id)++, &a, GraphModel::Attribute::Type::Static, "Static"));
return a;
}
NodeEditor::NodeEditor(std::string _title) : title(_title) {
nodes.push_back(CreateExampleNode(&next_id, "node A"));
nodes.push_back(CreateExampleNode(&next_id, "node B"));
}
void NodeEditor::show(){
@@ -78,34 +67,35 @@ void NodeEditor::draw(){
ImGui::SetWindowSizeMin(300,300);
ImNodes::BeginNodeEditor();
// draw nodes
for(Node& n : nodes){
n.draw();
for(auto&& p : graph->getNodes()){
drawNode(p.second);
}
// draw links
for(const NodeAttributeLink& l : links){
ImNodes::Link(l.id, l.in_attr_id, l.out_attr_id);
// draw edges
const auto& edges = graph->getEdges();
edges.empl = GraphModel::Edge();
for(auto&& p : edges){
ImNodes::Link(p.second.id, p.second.to_attr_id, p.second.from_attr_id);
}
ImNodes::EndNodeEditor();
// handle link creation
id_t in_attr_id;
id_t out_attr_id;
if (ImNodes::IsLinkCreated(&in_attr_id, &out_attr_id))
// handle edge creation
GraphModel::id_t to_attr_id;
GraphModel::id_t from_attr_id;
if (ImNodes::IsLinkCreated(&from_attr_id, &to_attr_id))
{
NodeAttributeLink link(next_id++, in_attr_id, out_attr_id);
links.push_back(link);
graph->createEdge(graph->getEdge(from_attr_id), graph->getEdge(to_attr_id));
}
// handle link destruction
id_t link_id;
if (ImNodes::IsLinkDestroyed(&link_id))
// handle edge destruction
GraphModel::id_t edge_id;
if (ImNodes::IsLinkDestroyed(&edge_id))
{
auto iter = std::find_if(links.begin(), links.end(),
[link_id](const NodeAttributeLink& link) -> bool {
return link.id == link_id;
auto iter = std::find_if(edges.begin(), edges.end(),
[edge_id](const GraphModel::Edge& edge) -> bool {
return edge.id == edge_id;
});
assert(iter != links.end());
links.erase(iter);
assert(iter != edges.end());
edges.erase(iter);
}
ImGui::End();

View File

@@ -1,51 +1,17 @@
#pragma once
#include "../../dependencies/imnodes/imnodes.h"
#include <vector>
#include <string>
#include "../GraphModel/GraphModel.hpp"
#include <memory>
namespace GraphC::gui {
typedef i32 id_t;
enum class NodeAttributeType {
Input, Output, Static
};
struct NodeAttribute {
id_t id;
NodeAttributeType type;
std::string title;
NodeAttribute(id_t _id, NodeAttributeType _type, std::string _title);
};
struct Node {
id_t id;
std::string title;
std::vector<NodeAttribute> attributes;
Node(id_t _id, std::string _title);
void draw();
};
struct NodeAttributeLink {
id_t id;
id_t in_attr_id;
id_t out_attr_id;
NodeAttributeLink(id_t _id, id_t _in, id_t _out);
};
class NodeEditor {
std::string title=nullptr;
bool editor_open=false;
ImNodesContext* editor_context=nullptr;
id_t next_id=1;
std::vector<Node> nodes;
std::vector<NodeAttributeLink> links;
std::shared_ptr<GraphModel::Graph> graph;
public:
NodeEditor(std::string _title);