Skip to content

Commit

Permalink
GraphEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
Daethalus committed May 12, 2024
1 parent b83bc77 commit fe08eca
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
63 changes: 56 additions & 7 deletions Editor/Source/Fyrion/Editor/Window/GraphEditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <imgui_canvas.h>
#include "builders.h"
#include "drawing.h"
#include "imgui_node_editor_internal.h"
#include "widgets.h"
#include "Fyrion/Engine.hpp"
#include "Fyrion/Assets/AssetTypes.hpp"
Expand All @@ -17,16 +18,22 @@
#include "Fyrion/Resource/Repository.hpp"
#include "Fyrion/Resource/ResourceGraph.hpp"


namespace ed = ax::NodeEditor;


namespace Fyrion
{

MenuItemContext GraphEditorWindow::s_menuItemContext = {};

namespace
{
bool operator==(const ImRect& a, const ImRect& b)
{
return a.Min.x == b.Min.x && a.Min.y == b.Min.y && a.Max.x == b.Max.x && a.Max.y == b.Max.y;
}


const int pinIconSize = 24;

inline ImColor GetNodeColor(const StringView& nodeName)
Expand Down Expand Up @@ -94,12 +101,18 @@ namespace Fyrion
void GraphEditorWindow::SetCurrentEditor()
{
ed::SetCurrentEditor((ed::EditorContext*)m_editorContext);
ImVec2 mousePos = ed::ScreenToCanvas(ImGui::GetMousePos());
m_mousePos = {mousePos.x, mousePos.y};
}

void GraphEditorWindow::Draw(u32 id, bool& open)
{
if (!m_graph) return;

static ImRect lastRect = {};

ed::Detail::EditorContext* editor = static_cast<ed::Detail::EditorContext*>(m_editorContext);

ax::NodeEditor::Utilities::BlueprintNodeBuilder builder{nullptr, 64, 64};

auto& style = ImGui::GetStyle();
Expand All @@ -108,22 +121,38 @@ namespace Fyrion
ImGui::SetNextWindowSize({800 * style.ScaleFactor, 400 * style.ScaleFactor}, ImGuiCond_Once);
ImGui::Begin(id, ICON_FA_DIAGRAM_PROJECT " Graph Editor", &open);

if (ImGui::IsMouseClicked(ImGuiMouseButton_Right) && ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows))
{
lastRect = editor->GetViewRect();
}

SetCurrentEditor();

if (m_graphTypeId == GetTypeID<ResourceGraphAsset>())
{
ResourceObject object = Repository::Read(m_graph);
m_nodesCache.Resize(object.GetSubObjectSetCount(ResourceGraphAsset::Nodes));
object.GetSubObjectSet(ResourceGraphAsset::Nodes, m_nodesCache);

m_linkCache.Resize(object.GetSubObjectSetCount(ResourceGraphAsset::Links));
object.GetSubObjectSet(ResourceGraphAsset::Nodes, m_linkCache);
}

ed::Begin("Node Graph Editor", ImVec2(0.0, 0.0f));

for(const RID& node: m_nodesCache)
{
ResourceObject nodeObject = Repository::Read(node);

if (!m_initialized.Has(node))
{
Vec2 pos = nodeObject[GraphNodeAsset::Position].Value<Vec2>();
ed::SetNodePosition(ed::NodeId(node.id), ImVec2(pos.x, pos.y));
m_initialized.Insert(node);
}

builder.Begin(ed::NodeId(node.id));

ResourceObject nodeObject = Repository::Read(node);
TypeHandler* typeHandler = nullptr;
FunctionHandler* functionHandler = nullptr;

Expand Down Expand Up @@ -181,9 +210,8 @@ namespace Fyrion
}
else if (functionHandler)
{

Span<ParamHandler> params = functionHandler->GetParams();
for(const ParamHandler& param : params)
for (const ParamHandler& param : params)
{
if (param.HasAttribute<GraphInput>())
{
Expand All @@ -196,7 +224,7 @@ namespace Fyrion
}
}

for(const ParamHandler& param : params)
for (const ParamHandler& param : params)
{
if (param.HasAttribute<GraphOutput>())
{
Expand All @@ -208,17 +236,36 @@ namespace Fyrion
}
}
}
builder.End();
}

for(const RID& link: m_linkCache)
{
ResourceObject linkObject = Repository::Read(link);

builder.End();
//ed::Link(link.id, link.inputId, link.outputId, GetTypeColor(link.type), 3.f);
}

if (ed::BeginCreate())
{
ed::PinId inputPinId, outputPinId;
if (ed::QueryNewLink(&inputPinId, &outputPinId))
{
if (inputPinId && outputPinId && inputPinId != outputPinId)
{

}
}
}
ed::EndCreate();

ed::End();

ed::SetCurrentEditor(nullptr);

if (ImGui::IsMouseReleased(ImGuiMouseButton_Right) && ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows))
if (ImGui::IsMouseReleased(ImGuiMouseButton_Right) && ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows) && editor->GetViewRect() == lastRect)
{
m_clickMousePos = m_mousePos;
ImGui::OpenPopup("add-node-popup");
}

Expand All @@ -244,6 +291,7 @@ namespace Fyrion
RID nodeAsset = Repository::CreateResource<GraphNodeAsset>();
ResourceObject nodeObject = Repository::Write(nodeAsset);
nodeObject[GraphNodeAsset::NodeOutput] = outputType->GetName();
nodeObject[GraphNodeAsset::Position] = m_clickMousePos;
nodeObject.Commit();

ResourceObject graphAsset = Repository::Write(m_graph);
Expand All @@ -256,6 +304,7 @@ namespace Fyrion
RID nodeAsset = Repository::CreateResource<GraphNodeAsset>();
ResourceObject nodeObject = Repository::Write(nodeAsset);
nodeObject[GraphNodeAsset::NodeFunction] = functionHandler->GetName();
nodeObject[GraphNodeAsset::Position] = m_clickMousePos;
nodeObject.Commit();

ResourceObject graphAsset = Repository::Write(m_graph);
Expand Down
8 changes: 7 additions & 1 deletion Editor/Source/Fyrion/Editor/Window/GraphEditorWindow.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include "Fyrion/Common.hpp"
#include "Fyrion/Core/HashSet.hpp"
#include "Fyrion/Core/Math.hpp"
#include "Fyrion/Core/Registry.hpp"
#include "Fyrion/Editor/EditorTypes.hpp"
#include "Fyrion/Editor/MenuItem.hpp"
Expand Down Expand Up @@ -34,8 +36,12 @@ namespace Fyrion
String m_graphName{};
VoidPtr m_editorContext{};
String m_searchNodeString{};
Vec2 m_mousePos{};
Vec2 m_clickMousePos{};

Array<RID> m_nodesCache{};
Array<RID> m_nodesCache{};
Array<RID> m_linkCache{};
HashSet<RID> m_initialized{};

void SetCurrentEditor();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Fyrion
{
void CreateGPUBuffer(const Array<u8>& data, const BufferUsage& usage, Buffer& buffer)
void CreateBuffer(const Array<u8>& data, const BufferUsage& usage, Buffer& buffer)
{
buffer = Graphics::CreateBuffer(BufferCreation{
.usage = usage,
Expand All @@ -31,10 +31,10 @@ namespace Fyrion
geometryRender.Attribute<ResourceGraphOutput>("Outputs/Geometry Render");


auto uploadGPUBuffer = Registry::Function<CreateGPUBuffer>("Fyrion::CreateGPUBuffer");
auto uploadGPUBuffer = Registry::Function<CreateBuffer>("Fyrion::CreateBuffer");
uploadGPUBuffer.Param<0>("data").Attribute<GraphInput>();
uploadGPUBuffer.Param<1>("usage").Attribute<GraphInput>();
uploadGPUBuffer.Param<2>("buffer").Attribute<GraphOutput>();
uploadGPUBuffer.Attribute<ResourceGraphNode>("Render/Create GPU Buffer");
uploadGPUBuffer.Attribute<ResourceGraphNode>("Render/Create Buffer");
}
}

0 comments on commit fe08eca

Please sign in to comment.