Skip to content

Commit

Permalink
ResourceGraph / DrawTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daethalus committed May 21, 2024
1 parent a729220 commit 5c1f747
Show file tree
Hide file tree
Showing 22 changed files with 508 additions and 195 deletions.
71 changes: 60 additions & 11 deletions Editor/Source/Fyrion/Editor/Editor/GraphEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Fyrion
{
ResourceObject valueObject = Repository::Read(value);

String name = valueObject[GraphNodeValue::Input].Value<String>();
String name = valueObject[GraphNodeValue::Name].Value<String>();
String typeName = valueObject[GraphNodeValue::Type].Value<String>();
String resourceTypeName = valueObject[GraphNodeValue::ResourceType].Value<String>();

Expand All @@ -38,20 +38,21 @@ namespace Fyrion
}



GraphEditorNodePinLookup lookup = GraphEditorNodePinLookup{
.node = node->rid,
.name = name,
};

GraphEditorNodePin* pin = m_pins.Emplace(lookup, MakeUnique<GraphEditorNodePin>(GraphEditorNodePin{
.node = node,
.label = FormatName(name),
.label = name,
.name = name,
.typeId = typeId,
.valueAsset = value,
.kind = GraphEditorPinKind::Output,
.publicValue = valueObject[GraphNodeValue::PublicValue].Value<bool>(),
.resourceType = resourceTypeId
.resourceType = resourceTypeId,
.editable = true
})).first->second.Get();

node->outputs.EmplaceBack(pin);
Expand All @@ -69,7 +70,7 @@ namespace Fyrion
ResourceObject valueObject = Repository::Read(input);
if (valueObject)
{
inputCache.Insert(valueObject[GraphNodeValue::Input].Value<String>(), MakePair(input, valueObject[GraphNodeValue::Value].Value<RID>()));
inputCache.Insert(valueObject[GraphNodeValue::Name].Value<String>(), MakePair(input, valueObject[GraphNodeValue::Value].Value<RID>()));
}
}

Expand All @@ -78,7 +79,7 @@ namespace Fyrion
.typeHandler = Registry::FindTypeByName(nodeObject[GraphNodeAsset::NodeOutput].Value<String>()),
.functionHandler = Registry::FindFunctionByName(nodeObject[GraphNodeAsset::NodeFunction].Value<String>()),
.position = nodeObject[GraphNodeAsset::Position].Value<Vec2>(),
.label = FormatName(nodeObject[GraphNodeAsset::Label].Value<String>()),
.label = nodeObject[GraphNodeAsset::Label].Value<String>(),
.initialized = false
})).first->second.Get();

Expand Down Expand Up @@ -365,7 +366,7 @@ namespace Fyrion


ResourceObject valueObject = Repository::Write(valueAsset);
valueObject[GraphNodeValue::Input] = input->name;
valueObject[GraphNodeValue::Name] = input->name;
valueObject[GraphNodeValue::PublicValue] = true;
valueObject[GraphNodeValue::Type] = Registry::FindTypeById(input->typeId)->GetName();
if (TypeHandler* resourceTypeHandler = Registry::FindTypeById(input->resourceType))
Expand Down Expand Up @@ -441,7 +442,7 @@ namespace Fyrion
Repository::SetUUID(input->valueAsset, UUID::RandomUUID());

ResourceObject valueObject = Repository::Write(input->valueAsset);
valueObject[GraphNodeValue::Input] = input->name;
valueObject[GraphNodeValue::Name] = input->name;
valueObject.SetSubObject(GraphNodeValue::Value, input->value);
valueObject.Commit();

Expand Down Expand Up @@ -473,14 +474,39 @@ namespace Fyrion
Repository::DestroyResource(node);
if (auto it = m_nodes.Find(node))
{
ForEach(it->second->inputs.begin(), it->second->inputs.end(), this, &GraphEditor::DeletePin);
ForEach(it->second->outputs.begin(), it->second->outputs.end(), this, &GraphEditor::DeletePin);
ForEach(it->second->inputs.begin(), it->second->inputs.end(), this, &GraphEditor::DeletePinCache);
ForEach(it->second->outputs.begin(), it->second->outputs.end(), this, &GraphEditor::DeletePinCache);

m_nodes.Erase(it);
}
m_assetTree.MarkDirty();
}

void GraphEditor::DeletePin(GraphEditorNodePin* pin)
{
for(const auto& it: pin->links)
{
m_links.Erase(it.first);
Repository::DestroyResource(it.first);
}

if (const auto it = std::ranges::find(pin->node->inputs, pin); it != pin->node->inputs.end())
{
pin->node->inputs.Erase(it);
}

if (const auto it = std::ranges::find(pin->node->outputs, pin); it != pin->node->outputs.end())
{
pin->node->outputs.Erase(it);
}

Repository::DestroyResource(pin->valueAsset);

DeletePinCache(pin);
m_assetTree.MarkDirty();
}


void GraphEditor::MoveNode(GraphEditorNode* node, Vec2 newPos)
{
node->position = newPos;
Expand All @@ -495,6 +521,29 @@ namespace Fyrion
void GraphEditor::RenameNode(GraphEditorNode* node, StringView newLabel)
{
node->label = newLabel;

ResourceObject nodeObject = Repository::Write(node->rid);
nodeObject[GraphNodeAsset::Label] = newLabel;
nodeObject.Commit();

m_assetTree.MarkDirty();
}

void GraphEditor::RenamePin(GraphEditorNodePin* pin, StringView newName)
{
// pin->name = newName;
// pin->label = newName;
}

void GraphEditor::ChangePinVisibility(GraphEditorNodePin* pin, bool publicValue)
{
pin->publicValue = publicValue;

ResourceObject valueObject = Repository::Write(pin->valueAsset);
valueObject[GraphNodeValue::PublicValue] = publicValue;
valueObject.Commit();

m_assetTree.MarkDirty();
}

GraphEditorNode* GraphEditor::GetNodeByRID(RID rid)
Expand All @@ -506,7 +555,7 @@ namespace Fyrion
return nullptr;
}

void GraphEditor::DeletePin(GraphEditorNodePin* pin)
void GraphEditor::DeletePinCache(GraphEditorNodePin* pin)
{
m_pins.Erase(GraphEditorNodePinLookup{
.node = pin->node->rid,
Expand Down
6 changes: 5 additions & 1 deletion Editor/Source/Fyrion/Editor/Editor/GraphEditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Fyrion
HashSet<RID> links{};
bool publicValue = false;
TypeID resourceType;
bool editable = false;
};

struct GraphEditorNodePinLookup
Expand Down Expand Up @@ -114,9 +115,12 @@ namespace Fyrion

void DeleteLink(RID link);
void DeleteNode(RID node);
void DeletePin(GraphEditorNodePin* pin);

void MoveNode(GraphEditorNode* node, Vec2 newPos);
void RenameNode(GraphEditorNode* node, StringView newLabel);
void RenamePin(GraphEditorNodePin* pin, StringView newName);
void ChangePinVisibility(GraphEditorNodePin* pin, bool publicValue);

GraphEditorNode* GetNodeByRID(RID rid);

Expand All @@ -140,7 +144,7 @@ namespace Fyrion
GraphEditorNodePin* GetPin(GraphEditorNodePin* left, GraphEditorNodePin* right, GraphEditorPinKind disiredKind);

GraphEditorNodePin* FindPin(RID node, const StringView& pin, GraphEditorPinKind graphEditorPinKind);
void DeletePin(GraphEditorNodePin* pin);
void DeletePinCache(GraphEditorNodePin* pin);

void AddOutputPinCache(RID value, GraphEditorNode* node);
};
Expand Down
2 changes: 1 addition & 1 deletion Editor/Source/Fyrion/Editor/Editor/SceneEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ namespace Fyrion
Editor::GetAssetTree().MarkDirty();
}

void SceneEditor::UpdateComponent(RID component, VoidPtr value)
void SceneEditor::UpdateComponent(RID component, ConstPtr value)
{
Repository::Commit(component, value);
Editor::GetAssetTree().MarkDirty();
Expand Down
2 changes: 1 addition & 1 deletion Editor/Source/Fyrion/Editor/Editor/SceneEditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Fyrion
void AddComponent(RID object, TypeHandler* typeHandler);
void RemoveComponent(RID object, RID component);
void ResetComponent(RID component);
void UpdateComponent(RID component, VoidPtr value);
void UpdateComponent(RID component, ConstPtr value);

private:
RID m_rootObject{};
Expand Down
8 changes: 8 additions & 0 deletions Editor/Source/Fyrion/Editor/Window/GraphEditorWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ namespace Fyrion
return m_lastGraphEditorSelected;
}

~GraphEditorWindow() override
{
if (m_lastGraphEditorSelected == &this->m_graphEditor)
{
m_lastGraphEditorSelected = nullptr;
}
}

private:
GraphEditor m_graphEditor;

Expand Down
99 changes: 60 additions & 39 deletions Editor/Source/Fyrion/Editor/Window/PropertiesWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "PropertiesWindow.hpp"

#include "GraphEditorWindow.hpp"
#include "imgui_internal.h"
#include "Fyrion/Assets/AssetTypes.hpp"
#include "Fyrion/Core/StringUtils.hpp"
#include "Fyrion/Editor/Editor.hpp"
Expand All @@ -18,6 +19,8 @@ namespace Fyrion

void PropertiesWindow::Draw(u32 id, bool& open)
{
m_idCount = id;

ImGui::Begin(id, ICON_FA_CIRCLE_INFO " Properties", &open, ImGuiWindowFlags_NoScrollbar);

//TODO temporary
Expand Down Expand Up @@ -77,7 +80,7 @@ namespace Fyrion

if (ImGui::InputText(hash, m_stringCache, nameFlags))
{
m_renamingCache = objectName;
m_renamingCache = m_stringCache;
m_renamingFocus = true;
m_renamingObject = rid;
}
Expand Down Expand Up @@ -163,11 +166,18 @@ namespace Fyrion
if (open)
{
ImGui::Indent();
ConstPtr ptr = Repository::ReadData(component);
if (VoidPtr newValue = ImGui::DrawType(HashValue(component), typeHandler, ptr, readOnly ? ImGuiDrawTypeFlags_ReadOnly : 0))
{
m_sceneEditor.UpdateComponent(component, newValue);
}
ImGui::DrawType(ImGui::DrawTypeDesc{
.itemId = component.id,
.rid = component,
.typeHandler = typeHandler,
.instance = Repository::ReadData(component),
.flags = readOnly ? ImGuiDrawTypeFlags_ReadOnly : 0u,
.userData = &m_sceneEditor,
.callback = [](ImGui::DrawTypeDesc& desc, ConstPtr newValue)
{
static_cast<SceneEditor*>(desc.userData)->UpdateComponent(desc.rid, newValue);
}
});
ImGui::Unindent();
}
}
Expand Down Expand Up @@ -258,16 +268,15 @@ namespace Fyrion
ImGui::SetNextItemWidth(-1);

m_stringCache = graphNode->label;
u32 hash = HashValue(node);

if (ImGui::InputText(hash, m_stringCache, nameFlags))
if (ImGui::InputText(PushId(), m_stringCache, nameFlags))
{
m_renamingCache = graphNode->label;
m_renamingCache = m_stringCache;
m_renamingFocus = true;
m_renamingObject = node;
}

if (!ImGui::IsItemActive() && m_renamingFocus)
if (!ImGui::IsItemActive() && m_renamingFocus && m_renamingObject == node)
{
graphEditor->RenameNode(graphNode, m_renamingCache);
m_renamingObject = {};
Expand All @@ -281,7 +290,7 @@ namespace Fyrion
ImGui::TableNextColumn();
ImGui::SetNextItemWidth(-1);
String uuid = ToString(Repository::GetUUID(node));
ImGui::InputText(hash + 10, uuid, ImGuiInputTextFlags_ReadOnly);
ImGui::InputText(PushId(), uuid, ImGuiInputTextFlags_ReadOnly);

ImGui::EndDisabled();
ImGui::EndTable();
Expand All @@ -292,13 +301,21 @@ namespace Fyrion

}

bool openPopup = false;

for (GraphEditorNodePin* pin : graphNode->outputs)
{
ImGui::BeginDisabled(!pin->editable);
TypeHandler* typeHandler = Registry::FindTypeById(pin->typeId);
if (typeHandler)
{
bool propClicked = false;
bool open = ImGui::CollapsingHeaderProps(reinterpret_cast<usize>(pin), pin->label.CStr(), &propClicked);
if (propClicked)
{
openPopup = true;
m_selectedNodePin = pin;
}

if (open)
{
Expand All @@ -317,52 +334,56 @@ namespace Fyrion
ImGui::SetNextItemWidth(-1);

m_stringCache = pin->label;
u32 hash = HashValue(reinterpret_cast<usize>(pin));

if (ImGui::InputText(hash, m_stringCache, nameFlags))
if (ImGui::InputText(PushId(), m_stringCache, nameFlags))
{
m_renamingCache = graphNode->label;
m_renamingCache = m_stringCache;
m_renamingFocus = true;
m_renamingObject = node;
m_renmaingPin = pin;
}

if (!ImGui::IsItemActive() && m_renamingFocus)
if (!ImGui::IsItemActive() && m_renamingFocus && m_renmaingPin == pin)
{
//graphEditor->RenameNode(graphNode, m_renamingCache);
m_renamingObject = {};
graphEditor->RenamePin(pin, m_renamingCache);
m_renmaingPin = {};
m_renamingFocus = false;
m_renamingCache.Clear();
}

ImGui::TableNextColumn();
ImGui::Text("Public Value");
ImGui::TableNextColumn();

ImGui::EndDisabled();
bool publicValue = pin->publicValue;

if (ImGui::Checkbox("###aaaaa", &publicValue))
{
graphEditor->ChangePinVisibility(pin, publicValue);
}

// for (FieldHandler* field : content.typeHandler->GetFields())
// {
// BeginDisabled(readOnly);
//
// TableNextColumn();
// AlignTextToFramePadding();
//
// String formattedName = FormatName(field->GetName());
// String idStr = "###string_" + formattedName;
//
// Text("%s", formattedName.CStr());
// TableNextColumn();
//
// if (const auto& it = fieldRenders.Find(field->GetFieldInfo().typeInfo.typeId))
// {
// it->second(field, field->GetFieldPointer(content.instance), &hasChanged);
// }
//
// EndDisabled();
// }
ImGui::EndDisabled();
ImGui::EndTable();
}
}
}
ImGui::EndDisabled();
}

if (openPopup)
{
ImGui::OpenPopup("open-node-settings");
}

bool popupOpenSettings = ImGui::BeginPopupMenu("open-node-settings", 0, false);
if (popupOpenSettings)
{
if (ImGui::MenuItem("Remove"))
{
graphEditor->DeletePin(m_selectedNodePin);
ImGui::CloseCurrentPopup();
}
}
ImGui::EndPopupMenu(popupOpenSettings);
}

void PropertiesWindow::RegisterType(NativeTypeHandler<PropertiesWindow>& type)
Expand Down
Loading

0 comments on commit 5c1f747

Please sign in to comment.