Skip to content

Commit

Permalink
GraphEditor - Inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Daethalus committed May 18, 2024
1 parent 2183959 commit 689364d
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 42 deletions.
88 changes: 70 additions & 18 deletions Editor/Source/Fyrion/Editor/Editor/GraphEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ namespace Fyrion
{
ResourceObject nodeObject = Repository::Read(node);

Array<RID> inputValues = nodeObject.GetSubObjectSetAsArray(GraphNodeAsset::InputValues);
HashMap<String, RID> inputCache;

for (RID input : inputValues)
{
ResourceObject valueObject = Repository::Read(input);
inputCache.Insert(valueObject[GraphNodeValue::Input].Value<String>(), input);
}

GraphEditorNode* graphEditorNode = m_nodes.Emplace(node, MakeUnique<GraphEditorNode>(GraphEditorNode{
.rid = node,
Expand All @@ -41,7 +49,7 @@ namespace Fyrion
.node = node,
.name = param.GetName()
}, MakeUnique<GraphEditorNodePin>(GraphEditorNodePin{
.node = node,
.node = graphEditorNode,
.label = FormatName(param.GetName()),
.name = param.GetName(),
.typeId = param.GetFieldInfo().typeInfo.typeId,
Expand All @@ -60,12 +68,13 @@ namespace Fyrion
.node = node,
.name = param.GetName(),
}, MakeUnique<GraphEditorNodePin>(GraphEditorNodePin{
.node = node,
.node = graphEditorNode,
.label = FormatName(param.GetName()),
.name = param.GetName(),
.typeId = param.GetFieldInfo().typeInfo.typeId,
.kind = GraphEditorPinKind::Output
})).first->second.Get();

graphEditorNode->outputs.EmplaceBack(pin);
}
}
Expand All @@ -87,7 +96,7 @@ namespace Fyrion
.node = node,
.name = field->GetName(),
}, MakeUnique<GraphEditorNodePin>(GraphEditorNodePin{
.node = node,
.node = graphEditorNode,
.label = FormatName(field->GetName()),
.name = field->GetName(),
.typeId = field->GetFieldInfo().typeInfo.typeId,
Expand All @@ -100,6 +109,35 @@ namespace Fyrion
}
else
{
graphEditorNode->addOutputPin = MakeUnique<GraphEditorNodePin>(GraphEditorNodePin{
.node = graphEditorNode,
.kind = GraphEditorPinKind::AddOutputPin
});

for (RID inputValue : inputValues)
{
ResourceObject valueObject = Repository::Read(inputValue);

String name = valueObject[GraphNodeValue::Input].Value<String>();
RID valueSuboject = valueObject.GetSubObject(GraphNodeValue::Value);

GraphEditorNodePin* pin = m_pins.Emplace(GraphEditorNodePinLookup{
.node = node,
.name = name,
}, MakeUnique<GraphEditorNodePin>(GraphEditorNodePin{
.node = graphEditorNode,
.label = FormatName(name),
.name = name,
// .typeId = Repository::GetResourceTypeHandler(valueSuboject)->GetTypeInfo().typeId,
.value = Repository::ReadData(valueSuboject),
.kind = GraphEditorPinKind::Output,
.publicValue = valueObject[GraphNodeValue::PublicValue].Value<bool>()
})).first->second.Get();

graphEditorNode->outputs.EmplaceBack(pin);
}


if (graphEditorNode->label.Empty())
{
graphEditorNode->label = "Input";
Expand All @@ -118,25 +156,15 @@ namespace Fyrion
return;
}

GraphEditorLink* editorLink = m_links.Emplace(link, MakeUnique<GraphEditorLink>(GraphEditorLink{
m_links.Emplace(link, MakeUnique<GraphEditorLink>(GraphEditorLink{
.rid = link,
.index = m_linksArray.Size(),
.inputPin = inputPin,
.outputPin = outputPin,
.linkType = outputPin->typeId
})).first->second.Get();

m_linksArray.EmplaceBack(editorLink);
}));

if (const auto& it = m_nodes.Find(inputPin->node))
{
it->second->links.Insert(link);
}

if (const auto& it = m_nodes.Find(outputPin->node))
{
it->second->links.Insert(link);
}
inputPin->links.Emplace(link);
outputPin->links.Emplace(link);
}

GraphEditorNodePin* GraphEditor::GetPin(GraphEditorNodePin* left, GraphEditorNodePin* right, GraphEditorPinKind disiredKind)
Expand Down Expand Up @@ -190,6 +218,23 @@ namespace Fyrion
}
}

void GraphEditor::AddInputNode(const Vec2& position)
{
RID nodeAsset = Repository::CreateResource<GraphNodeAsset>();

ResourceObject nodeObject = Repository::Write(nodeAsset);
nodeObject[GraphNodeAsset::Position] = position;
nodeObject.Commit();

ResourceObject graphAsset = Repository::Write(m_graph);
graphAsset.AddToSubObjectSet(ResourceGraphAsset::Nodes, nodeAsset);
graphAsset.Commit();

Repository::SetUUID(nodeAsset, UUID::RandomUUID());
AddNodeCache(nodeAsset);
m_assetTree.MarkDirty();
}

void GraphEditor::AddOutput(TypeHandler* outputType, const Vec2& position)
{
RID nodeAsset = Repository::CreateResource<GraphNodeAsset>();
Expand Down Expand Up @@ -265,6 +310,13 @@ namespace Fyrion
void GraphEditor::DeleteLink(RID link)
{
Repository::DestroyResource(link);

if (const auto& it = m_links.Find(link))
{
it->second->inputPin->links.Erase(link);
it->second->outputPin->links.Erase(link);
}

m_links.Erase(link);
m_assetTree.MarkDirty();
}
Expand Down Expand Up @@ -296,7 +348,7 @@ namespace Fyrion
void GraphEditor::DeletePin(GraphEditorNodePin* pin)
{
m_pins.Erase(GraphEditorNodePinLookup{
.node = pin->node,
.node = pin->node->rid,
.name = pin->name,
});
}
Expand Down
47 changes: 27 additions & 20 deletions Editor/Source/Fyrion/Editor/Editor/GraphEditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ namespace Fyrion
class AssetTree;
class TypeHandler;
class FunctionHandler;

struct GraphEditorLink;
struct GraphEditorNode;

enum class GraphEditorPinKind
{
Input,
Output,
AddOutputPin,
AddInputPin
};

struct GraphEditorNodePin
{
RID node;
GraphEditorNode* node;
String label;
String name;
TypeID typeId;
ConstPtr value;
GraphEditorPinKind kind;
HashSet<RID> links{};
bool publicValue = false;
};

struct GraphEditorNodePinLookup
Expand Down Expand Up @@ -54,29 +60,30 @@ namespace Fyrion

struct GraphEditorNode
{
RID rid{};
TypeHandler* typeHandler{};
FunctionHandler* functionHandler{};
Vec2 position{};
String label{};
bool initialized{};

Array<GraphEditorNodePin*> inputs{};
Array<GraphEditorNodePin*> outputs{};
HashSet<RID> links{};
RID rid{};
TypeHandler* typeHandler{};
FunctionHandler* functionHandler{};
Vec2 position{};
String label{};
bool initialized{};
UniquePtr<GraphEditorNodePin> addInputPin{};
UniquePtr<GraphEditorNodePin> addOutputPin{};

Array<GraphEditorNodePin*> inputs{};
Array<GraphEditorNodePin*> outputs{};
};

struct GraphEditorLink
{
RID rid{};
u64 index{};
GraphEditorNodePin* inputPin{};
GraphEditorNodePin* outputPin{};
TypeID linkType{};
};

using GraphNodeMap = HashMap<RID, UniquePtr<GraphEditorNode>>;
using GraphNodePinMap = HashMap<GraphEditorNodePinLookup, UniquePtr<GraphEditorNodePin>>;
using GraphNodeLinkMap = HashMap<RID, UniquePtr<GraphEditorLink>>;

class GraphEditor
{
Expand All @@ -87,12 +94,13 @@ namespace Fyrion

FY_NO_COPY_CONSTRUCTOR(GraphEditor)

void OpenGraph(RID rid);
bool IsGraphLoaded() const;
GraphNodeMap& GetNodes() { return m_nodes; }
GraphNodePinMap& GetPins() { return m_pins; }
Span<GraphEditorLink*> GetLinks() { return m_linksArray; }
void OpenGraph(RID rid);
bool IsGraphLoaded() const;
GraphNodeMap& GetNodes() { return m_nodes; }
GraphNodePinMap& GetPins() { return m_pins; }
GraphNodeLinkMap& GetLinks() { return m_links; }

void AddInputNode(const Vec2& position);
void AddOutput(TypeHandler* outputType, const Vec2& position);
void AddNode(FunctionHandler* functionHandler, const Vec2& position);
bool ValidateLink(GraphEditorNodePin* inputPin, GraphEditorNodePin* outputPin);
Expand All @@ -113,8 +121,7 @@ namespace Fyrion
GraphNodeMap m_nodes;
GraphNodePinMap m_pins;

HashMap<RID, UniquePtr<GraphEditorLink>> m_links;
Array<GraphEditorLink*> m_linksArray;
GraphNodeLinkMap m_links;

void AddNodeCache(RID node);
void AddLinkCache(RID link);
Expand Down
28 changes: 24 additions & 4 deletions Editor/Source/Fyrion/Editor/Window/GraphEditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ namespace Fyrion
for (GraphEditorNodePin* input : node->inputs)
{
builder.Input(ed::PinId(input));
DrawPinIcon(input->typeId, false);
DrawPinIcon(input->typeId, !input->links.Empty());
ImGui::Spring(0);
ImGui::TextUnformatted(input->label.CStr());
ImGui::Spring(0);
Expand All @@ -170,9 +170,19 @@ namespace Fyrion
builder.Output(ed::PinId(output));
ImGui::TextUnformatted(output->label.CStr());
ImGui::Spring(0);
DrawPinIcon(output->typeId, false);
DrawPinIcon(output->typeId, !output->links.Empty());
builder.EndOutput();
}

if (node->addOutputPin)
{
builder.Output(ed::PinId(node->addOutputPin.Get()));
ImGui::TextUnformatted("+");
ImGui::Spring(0);
DrawPinIcon(0, false);
builder.EndOutput();
}

builder.End();

ImVec2 pos = ed::GetNodePosition(node->rid.id);
Expand All @@ -182,9 +192,9 @@ namespace Fyrion
}
}

for (GraphEditorLink* link : m_graphEditor.GetLinks())
for (const auto& it : m_graphEditor.GetLinks())
{
ed::Link(link->rid.id, ed::PinId(link->inputPin), ed::PinId(link->outputPin), GetTypeColor(link->linkType), 3.f);
ed::Link(it.second->rid.id, ed::PinId(it.second->inputPin), ed::PinId(it.second->outputPin), GetTypeColor(it.second->linkType), 3.f);
}

if (ed::BeginCreate())
Expand Down Expand Up @@ -275,6 +285,16 @@ namespace Fyrion

void GraphEditorWindow::OnInitGraphEditor()
{
s_menuItemContext.AddMenuItem(MenuItemCreation{
.itemName = "Graph/Input",
.priority = 10,
.action = [](const MenuItemEventData& eventData)
{
GraphEditorWindow* graphEditorWindow = static_cast<GraphEditorWindow*>(eventData.drawData);
graphEditorWindow->m_graphEditor.AddInputNode(graphEditorWindow->m_clickMousePos);
}
});

Span<TypeHandler*> outputs = Registry::FindTypesByAttribute<ResourceGraphOutput>();
for (TypeHandler* outputType : outputs)
{
Expand Down
4 changes: 4 additions & 0 deletions Engine/Source/Fyrion/Graphics/GraphicsTypeRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ namespace Fyrion
Registry::Type<ShaderStageInfo>();
Registry::Type<Array<ShaderStageInfo>>();
Registry::Type<ShaderInfo>();
Registry::Type<Buffer>();


Registry::Type<BufferUsage>();
}
}

0 comments on commit 689364d

Please sign in to comment.