From 56d63b7ff6e3633a8ca9d8cac8c8ca9ef14eaffa Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Sun, 12 May 2019 20:42:06 -0700 Subject: [PATCH] another big refactor --- src/GraphModel.cpp | 353 ++++++++++++++++++ src/GraphModel.h | 125 +++++++ src/Imogen.h | 277 -------------- src/{NodeGraph.cpp => NodeEditor.cpp} | 38 +- src/{NodeGraph.h => NodeEditor.h} | 66 +--- ...GraphControler.cpp => ParameterEditor.cpp} | 93 ++--- ...NodeGraphControler.h => ParameterEditor.h} | 22 +- src/main.cpp | 2 +- 8 files changed, 552 insertions(+), 424 deletions(-) create mode 100644 src/GraphModel.cpp create mode 100644 src/GraphModel.h rename src/{NodeGraph.cpp => NodeEditor.cpp} (98%) rename src/{NodeGraph.h => NodeEditor.h} (73%) rename src/{NodeGraphControler.cpp => ParameterEditor.cpp} (91%) rename src/{NodeGraphControler.h => ParameterEditor.h} (94%) diff --git a/src/GraphModel.cpp b/src/GraphModel.cpp new file mode 100644 index 00000000..26766886 --- /dev/null +++ b/src/GraphModel.cpp @@ -0,0 +1,353 @@ +// https://github.com/CedricGuillemet/Imogen +// +// The MIT License(MIT) +// +// Copyright(c) 2019 Cedric Guillemet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +#include "GraphModel.h" +#include + + +struct UndoRedo +{ + UndoRedo(); + virtual ~UndoRedo(); + + virtual void Undo() + { + if (mSubUndoRedo.empty()) + return; + for (int i = int(mSubUndoRedo.size()) - 1; i >= 0; i--) + { + mSubUndoRedo[i]->Undo(); + } + } + virtual void Redo() + { + for (auto& undoRedo : mSubUndoRedo) + { + undoRedo->Redo(); + } + } + template + void AddSubUndoRedo(const T& subUndoRedo) + { + mSubUndoRedo.push_back(std::make_shared(subUndoRedo)); + } + void Discard() + { + mbDiscarded = true; + } + bool IsDiscarded() const + { + return mbDiscarded; + } + +protected: + std::vector> mSubUndoRedo; + bool mbDiscarded; +}; + +struct UndoRedoHandler +{ + UndoRedoHandler() : mbProcessing(false), mCurrent(NULL) + { + } + ~UndoRedoHandler() + { + Clear(); + } + + void Undo() + { + if (mUndos.empty()) + return; + mbProcessing = true; + mUndos.back()->Undo(); + mRedos.push_back(mUndos.back()); + mUndos.pop_back(); + mbProcessing = false; + } + + void Redo() + { + if (mRedos.empty()) + return; + mbProcessing = true; + mRedos.back()->Redo(); + mUndos.push_back(mRedos.back()); + mRedos.pop_back(); + mbProcessing = false; + } + + template + void AddUndo(const T& undoRedo) + { + if (undoRedo.IsDiscarded()) + return; + if (mCurrent && &undoRedo != mCurrent) + mCurrent->AddSubUndoRedo(undoRedo); + else + mUndos.push_back(std::make_shared(undoRedo)); + mbProcessing = true; + mRedos.clear(); + mbProcessing = false; + } + + void Clear() + { + mbProcessing = true; + mUndos.clear(); + mRedos.clear(); + mbProcessing = false; + } + + bool mbProcessing; + UndoRedo* mCurrent; + // private: + + std::vector> mUndos; + std::vector> mRedos; +}; + +inline UndoRedo::UndoRedo() : mbDiscarded(false) +{ + if (!gUndoRedoHandler.mCurrent) + { + gUndoRedoHandler.mCurrent = this; + } +} + +inline UndoRedo::~UndoRedo() +{ + if (gUndoRedoHandler.mCurrent == this) + { + gUndoRedoHandler.mCurrent = NULL; + } +} + +template +struct URChange : public UndoRedo +{ + URChange(int index, + std::function GetElements, + std::function Changed = [](int index) {}) + : GetElements(GetElements), mIndex(index), Changed(Changed) + { + if (gUndoRedoHandler.mbProcessing) + return; + + mPreDo = *GetElements(mIndex); + } + virtual ~URChange() + { + if (gUndoRedoHandler.mbProcessing || mbDiscarded) + return; + + if (*GetElements(mIndex) != mPreDo) + { + mPostDo = *GetElements(mIndex); + gUndoRedoHandler.AddUndo(*this); + } + else + { + // TODO: should not be here unless asking for too much useless undo + } + } + virtual void Undo() + { + *GetElements(mIndex) = mPreDo; + Changed(mIndex); + UndoRedo::Undo(); + } + virtual void Redo() + { + UndoRedo::Redo(); + *GetElements(mIndex) = mPostDo; + Changed(mIndex); + } + + T mPreDo; + T mPostDo; + int mIndex; + + std::function GetElements; + std::function Changed; +}; + + +struct URDummy : public UndoRedo +{ + URDummy() : UndoRedo() + { + if (gUndoRedoHandler.mbProcessing) + return; + } + virtual ~URDummy() + { + if (gUndoRedoHandler.mbProcessing) + return; + + gUndoRedoHandler.AddUndo(*this); + } + virtual void Undo() + { + UndoRedo::Undo(); + } + virtual void Redo() + { + UndoRedo::Redo(); + } +}; + + +template +struct URDel : public UndoRedo +{ + URDel(int index, + std::function*()> GetElements, + std::function OnDelete = [](int index) {}, + std::function OnNew = [](int index) {}) + : GetElements(GetElements), mIndex(index), OnDelete(OnDelete), OnNew(OnNew) + { + if (gUndoRedoHandler.mbProcessing) + return; + + mDeletedElement = (*GetElements())[mIndex]; + } + virtual ~URDel() + { + if (gUndoRedoHandler.mbProcessing || mbDiscarded) + return; + // add to handler + gUndoRedoHandler.AddUndo(*this); + } + virtual void Undo() + { + GetElements()->insert(GetElements()->begin() + mIndex, mDeletedElement); + OnNew(mIndex); + UndoRedo::Undo(); + } + virtual void Redo() + { + UndoRedo::Redo(); + OnDelete(mIndex); + GetElements()->erase(GetElements()->begin() + mIndex); + } + + T mDeletedElement; + int mIndex; + + std::function*()> GetElements; + std::function OnDelete; + std::function OnNew; +}; + +template +struct URAdd : public UndoRedo +{ + URAdd(int index, + std::function*()> GetElements, + std::function OnDelete = [](int index) {}, + std::function OnNew = [](int index) {}) + : GetElements(GetElements), mIndex(index), OnDelete(OnDelete), OnNew(OnNew) + { + } + virtual ~URAdd() + { + if (gUndoRedoHandler.mbProcessing || mbDiscarded) + return; + + mAddedElement = (*GetElements())[mIndex]; + // add to handler + gUndoRedoHandler.AddUndo(*this); + } + virtual void Undo() + { + OnDelete(mIndex); + GetElements()->erase(GetElements()->begin() + mIndex); + UndoRedo::Undo(); + } + virtual void Redo() + { + UndoRedo::Redo(); + GetElements()->insert(GetElements()->begin() + mIndex, mAddedElement); + OnNew(mIndex); + } + + T mAddedElement; + int mIndex; + + std::function*()> GetElements; + std::function OnDelete; + std::function OnNew; +}; + + +GraphModel::GraphModel() : mbTransaction(false), mUndoRedoHandler(new UndoRedoHandler) +{ + +} + +GraphModel::~GraphModel() +{ + delete mUndoRedoHandler; +} + +void GraphModel::BeginTransaction(bool undoable) +{ + assert(!mbTransaction); + + mbTransaction = true; +} + +void GraphModel::EndTransaction() +{ + assert(mbTransaction); + mbTransaction = false; +} + +void GraphModel::Undo() +{ + assert(!mbTransaction); + mUndoRedoHandler->Undo(); +} + +void GraphModel::Redo() +{ + assert(!mbTransaction); + mUndoRedoHandler->Redo(); +} + + +bool GraphModel::IsIOUsed(int nodeIndex, int slotIndex, bool forOutput) const +{ + for (auto& link : mLinks) + { + if ((link.InputIdx == nodeIndex && link.InputSlot == slotIndex && forOutput) || + (link.OutputIdx == nodeIndex && link.OutputSlot == slotIndex && !forOutput)) + { + return true; + } + } + return false; +} diff --git a/src/GraphModel.h b/src/GraphModel.h new file mode 100644 index 00000000..1a1c2e3f --- /dev/null +++ b/src/GraphModel.h @@ -0,0 +1,125 @@ +// https://github.com/CedricGuillemet/Imogen +// +// The MIT License(MIT) +// +// Copyright(c) 2019 Cedric Guillemet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +#pragma once + +struct UndoRedoHandler; + + +struct NodeRug +{ + ImVec2 mPos, mSize; + uint32_t mColor; + std::string mText; + + bool operator!=(const NodeRug& other) const + { + if (mPos != other.mPos) + return true; + if (mSize != other.mSize) + return false; + if (mColor != other.mColor) + return true; + if (mText != other.mText) + return true; + return false; + } +}; + + +struct Node +{ + int mType; + ImVec2 mPos; + bool mbSelected; + + bool operator!=(const Node& other) const + { + if (mType != other.mType) + return true; + if (mPos.x != other.mPos.x) + return true; + if (mPos.y != other.mPos.y) + return true; + + return false; + } +}; + +struct NodeLink +{ + int mInputIdx, mInputSlot, mOutputIdx, mOutputSlot; + + bool operator==(const NodeLink& other) const + { + return mInputIdx == other.mInputIdx && mInputSlot == other.mInputSlot && mOutputIdx == other.mOutputIdx && + mOutputSlot == other.mOutputSlot; + } +}; + +class GraphModel +{ +public: + GraphModel(); + ~GraphModel(); + + // Transaction + void Clear(); + void BeginTransaction(bool undoable); + void EndTransaction(); + + // undo/redo + + void Undo(); + void Redo(); + + // setters + void AddNode(size_t type); + void DelNode(size_t nodeIndex); + void AddLink(int inputNodeIndex, int inputSlotIndex, int outputNodeIndex, int outputSlotIndex); + void DelLink(int index, int slot); + + + + // getters + + // linked to runtime...not sure + bool NodeIsCubemap(size_t nodeIndex) const; + bool NodeIs2D(size_t nodeIndex) const; + bool NodeIsCompute(size_t nodeIndex) const; + bool NodeHasUI(size_t nodeIndex) const; + + + + bool IsIOUsed(int nodeIndex, int slotIndex, bool forOutput) const; + + // clipboard + void CopyNodes(const std::vector nodes); + void CutNodes(const std::vector nodes); + void PasteNodes(); + +private: + bool mbTransaction; + UndoRedoHandler* mUndoRedoHandler; +}; \ No newline at end of file diff --git a/src/Imogen.h b/src/Imogen.h index 6ad1c930..46d0c50c 100644 --- a/src/Imogen.h +++ b/src/Imogen.h @@ -153,280 +153,3 @@ struct Imogen std::vector> mHotkeyFunctions; }; - -struct UndoRedo -{ - UndoRedo(); - virtual ~UndoRedo(); - - virtual void Undo() - { - if (mSubUndoRedo.empty()) - return; - for (int i = int(mSubUndoRedo.size()) - 1; i >= 0; i--) - { - mSubUndoRedo[i]->Undo(); - } - } - virtual void Redo() - { - for (auto& undoRedo : mSubUndoRedo) - { - undoRedo->Redo(); - } - } - template - void AddSubUndoRedo(const T& subUndoRedo) - { - mSubUndoRedo.push_back(std::make_shared(subUndoRedo)); - } - void Discard() - { - mbDiscarded = true; - } - bool IsDiscarded() const - { - return mbDiscarded; - } - -protected: - std::vector> mSubUndoRedo; - bool mbDiscarded; -}; - -struct UndoRedoHandler -{ - UndoRedoHandler() : mbProcessing(false), mCurrent(NULL) - { - } - ~UndoRedoHandler() - { - Clear(); - } - - void Undo() - { - if (mUndos.empty()) - return; - mbProcessing = true; - mUndos.back()->Undo(); - mRedos.push_back(mUndos.back()); - mUndos.pop_back(); - mbProcessing = false; - } - - void Redo() - { - if (mRedos.empty()) - return; - mbProcessing = true; - mRedos.back()->Redo(); - mUndos.push_back(mRedos.back()); - mRedos.pop_back(); - mbProcessing = false; - } - - template - void AddUndo(const T& undoRedo) - { - if (undoRedo.IsDiscarded()) - return; - if (mCurrent && &undoRedo != mCurrent) - mCurrent->AddSubUndoRedo(undoRedo); - else - mUndos.push_back(std::make_shared(undoRedo)); - mbProcessing = true; - mRedos.clear(); - mbProcessing = false; - } - - void Clear() - { - mbProcessing = true; - mUndos.clear(); - mRedos.clear(); - mbProcessing = false; - } - - bool mbProcessing; - UndoRedo* mCurrent; - // private: - - std::vector> mUndos; - std::vector> mRedos; -}; - -extern UndoRedoHandler gUndoRedoHandler; - -inline UndoRedo::UndoRedo() : mbDiscarded(false) -{ - if (!gUndoRedoHandler.mCurrent) - { - gUndoRedoHandler.mCurrent = this; - } -} - -inline UndoRedo::~UndoRedo() -{ - if (gUndoRedoHandler.mCurrent == this) - { - gUndoRedoHandler.mCurrent = NULL; - } -} - -template -struct URChange : public UndoRedo -{ - URChange(int index, - std::function GetElements, - std::function Changed = [](int index) {}) - : GetElements(GetElements), mIndex(index), Changed(Changed) - { - if (gUndoRedoHandler.mbProcessing) - return; - - mPreDo = *GetElements(mIndex); - } - virtual ~URChange() - { - if (gUndoRedoHandler.mbProcessing || mbDiscarded) - return; - - if (*GetElements(mIndex) != mPreDo) - { - mPostDo = *GetElements(mIndex); - gUndoRedoHandler.AddUndo(*this); - } - else - { - // TODO: should not be here unless asking for too much useless undo - } - } - virtual void Undo() - { - *GetElements(mIndex) = mPreDo; - Changed(mIndex); - UndoRedo::Undo(); - } - virtual void Redo() - { - UndoRedo::Redo(); - *GetElements(mIndex) = mPostDo; - Changed(mIndex); - } - - T mPreDo; - T mPostDo; - int mIndex; - - std::function GetElements; - std::function Changed; -}; - - -struct URDummy : public UndoRedo -{ - URDummy() : UndoRedo() - { - if (gUndoRedoHandler.mbProcessing) - return; - } - virtual ~URDummy() - { - if (gUndoRedoHandler.mbProcessing) - return; - - gUndoRedoHandler.AddUndo(*this); - } - virtual void Undo() - { - UndoRedo::Undo(); - } - virtual void Redo() - { - UndoRedo::Redo(); - } -}; - - -template -struct URDel : public UndoRedo -{ - URDel(int index, - std::function*()> GetElements, - std::function OnDelete = [](int index) {}, - std::function OnNew = [](int index) {}) - : GetElements(GetElements), mIndex(index), OnDelete(OnDelete), OnNew(OnNew) - { - if (gUndoRedoHandler.mbProcessing) - return; - - mDeletedElement = (*GetElements())[mIndex]; - } - virtual ~URDel() - { - if (gUndoRedoHandler.mbProcessing || mbDiscarded) - return; - // add to handler - gUndoRedoHandler.AddUndo(*this); - } - virtual void Undo() - { - GetElements()->insert(GetElements()->begin() + mIndex, mDeletedElement); - OnNew(mIndex); - UndoRedo::Undo(); - } - virtual void Redo() - { - UndoRedo::Redo(); - OnDelete(mIndex); - GetElements()->erase(GetElements()->begin() + mIndex); - } - - T mDeletedElement; - int mIndex; - - std::function*()> GetElements; - std::function OnDelete; - std::function OnNew; -}; - -template -struct URAdd : public UndoRedo -{ - URAdd(int index, - std::function*()> GetElements, - std::function OnDelete = [](int index) {}, - std::function OnNew = [](int index) {}) - : GetElements(GetElements), mIndex(index), OnDelete(OnDelete), OnNew(OnNew) - { - } - virtual ~URAdd() - { - if (gUndoRedoHandler.mbProcessing || mbDiscarded) - return; - - mAddedElement = (*GetElements())[mIndex]; - // add to handler - gUndoRedoHandler.AddUndo(*this); - } - virtual void Undo() - { - OnDelete(mIndex); - GetElements()->erase(GetElements()->begin() + mIndex); - UndoRedo::Undo(); - } - virtual void Redo() - { - UndoRedo::Redo(); - GetElements()->insert(GetElements()->begin() + mIndex, mAddedElement); - OnNew(mIndex); - } - - T mAddedElement; - int mIndex; - - std::function*()> GetElements; - std::function OnDelete; - std::function OnNew; -}; diff --git a/src/NodeGraph.cpp b/src/NodeEditor.cpp similarity index 98% rename from src/NodeGraph.cpp rename to src/NodeEditor.cpp index 81c00fcf..f37afdb8 100644 --- a/src/NodeGraph.cpp +++ b/src/NodeEditor.cpp @@ -26,13 +26,13 @@ #include "imgui.h" #include "imgui_internal.h" #include -#include "NodeGraph.h" +#include "NodeEditor.h" +#include "GraphModel.h" #include #include #include #include "EvaluationStages.h" #include "imgui_stdlib.h" -#include "NodeGraphControler.h" #include #include "imgui_markdown/imgui_markdown.h" #include "UI.h" @@ -44,7 +44,7 @@ static inline float Distance(ImVec2& a, ImVec2& b) { return sqrtf((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } - +/* Node::Node(int type, const ImVec2& pos) { mType = type; @@ -54,7 +54,7 @@ Node::Node(int type, const ImVec2& pos) OutputsCount = gMetaNodes[type].mOutputs.size(); mbSelected = false; } - +*/ struct NodeOrder { size_t mNodeIndex; @@ -390,7 +390,7 @@ bool RecurseIsLinked(int from, int to) return false; } -void NodeGraphUpdateEvaluationOrder(NodeGraphControlerBase* controler) +void NodeGraphUpdateEvaluationOrder(GraphModel* model) { mOrders = ComputeEvaluationOrder(links, nodes.size()); std::sort(mOrders.begin(), mOrders.end()); @@ -403,7 +403,7 @@ void NodeGraphUpdateEvaluationOrder(NodeGraphControlerBase* controler) } } -size_t NodeGraphAddNode(NodeGraphControlerBase* controler, +size_t NodeGraphAddNode(GraphModel* model, int type, const std::vector* parameters, int posx, @@ -423,7 +423,7 @@ size_t NodeGraphAddNode(NodeGraphControlerBase* controler, return index; } -void NodeGraphAddLink(NodeGraphControlerBase* controler, int InputIdx, int InputSlot, int OutputIdx, int OutputSlot) +void NodeGraphAddLink(GraphModel* model, int InputIdx, int InputSlot, int OutputIdx, int OutputSlot) { if (InputIdx >= nodes.size() || OutputIdx >= nodes.size()) { @@ -471,7 +471,7 @@ void NodeGraphAddRug( rugs.push_back({ImVec2(float(posX), float(posY)), ImVec2(float(sizeX), float(sizeY)), color, comment}); } -static void DeleteSelectedNodes(NodeGraphControlerBase* controler) +static void DeleteSelectedNodes(GraphModel* model) { URDummy urDummy; for (int selection = int(nodes.size()) - 1; selection >= 0; selection--) @@ -555,7 +555,7 @@ static void DeleteSelectedNodes(NodeGraphControlerBase* controler) } } -static void ContextMenu(ImVec2 offset, int nodeHovered, NodeGraphControlerBase* controler) +static void ContextMenu(ImVec2 offset, int nodeHovered, GraphModel* model) { ImGuiIO& io = ImGui::GetIO(); size_t metaNodeCount = gMetaNodes.size(); @@ -898,7 +898,7 @@ bool HandleConnections(ImDrawList* drawList, int nodeIndex, const ImVec2 offset, const float factor, - NodeGraphControlerBase* controler, + GraphModel* model, bool bDrawOnly) { static int editingNodeIndex; @@ -1068,7 +1068,7 @@ static bool DrawNode(ImDrawList* drawList, int nodeIndex, const ImVec2 offset, const float factor, - NodeGraphControlerBase* controler, + GraphModel* model, bool overInput) { ImGuiIO& io = ImGui::GetIO(); @@ -1230,7 +1230,7 @@ static bool DrawNode(ImDrawList* drawList, return nodeHovered; } -void ComputeDelegateSelection(NodeGraphControlerBase* controler) +void ComputeDelegateSelection(GraphModel* model) { // only one selection allowed for delegate controler->mSelectedNodeIndex = -1; @@ -1259,7 +1259,7 @@ void NodeGraphSelectNode(int selectedNodeIndex) } } -void NodeGraph(NodeGraphControlerBase* controler, bool enabled) +void NodeGraph(GraphModel* model, bool enabled) { ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0.f); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0.f, 0.f)); @@ -1586,15 +1586,3 @@ ImRect GetFinalNodeDisplayRect() return DisplayRectMargin(rect); } -bool IsIOUsed(int nodeIndex, int slotIndex, bool forOutput) -{ - for (auto& link : links) - { - if ((link.InputIdx == nodeIndex && link.InputSlot == slotIndex && forOutput) || - (link.OutputIdx == nodeIndex && link.OutputSlot == slotIndex && !forOutput)) - { - return true; - } - } - return false; -} \ No newline at end of file diff --git a/src/NodeGraph.h b/src/NodeEditor.h similarity index 73% rename from src/NodeGraph.h rename to src/NodeEditor.h index 99584eeb..866826b1 100644 --- a/src/NodeGraph.h +++ b/src/NodeEditor.h @@ -29,6 +29,7 @@ #include "imgui.h" #include "imgui_internal.h" +/* struct NodeGraphControlerBase { NodeGraphControlerBase() : mSelectedNodeIndex(-1), mCategories(nullptr) @@ -39,8 +40,7 @@ struct NodeGraphControlerBase const std::vector* mCategories; virtual void UpdateEvaluationList(const std::vector nodeOrderList) = 0; - virtual void AddLink(int InputIdx, int InputSlot, int OutputIdx, int OutputSlot) = 0; - virtual void DelLink(int index, int slot) = 0; + virtual unsigned int GetNodeTexture(size_t index) = 0; // A new node has been added in the graph. Do a push_back on your node array // add node for batch(loading graph) @@ -53,27 +53,22 @@ struct NodeGraphControlerBase virtual void SetParamBlock(size_t index, const std::vector& paramBlock) = 0; virtual void SetTimeSlot(size_t index, int frameStart, int frameEnd) = 0; - virtual bool NodeHasUI(size_t nodeIndex) const = 0; + virtual int NodeIsProcesing(size_t nodeIndex) const = 0; virtual float NodeProgress(size_t nodeIndex) const = 0; - virtual bool NodeIsCubemap(size_t nodeIndex) const = 0; - virtual bool NodeIs2D(size_t nodeIndex) const = 0; - virtual bool NodeIsCompute(size_t nodeIndex) const = 0; + virtual void DrawNodeImage(ImDrawList* drawList, const ImRect& rc, const ImVec2 marge, const size_t nodeIndex) = 0; // return false if background must be rendered by node graph virtual bool RenderBackground() = 0; - // clipboard - virtual void CopyNodes(const std::vector nodes) = 0; - virtual void CutNodes(const std::vector nodes) = 0; - virtual void PasteNodes() = 0; - virtual bool IsIOPinned(size_t nodeIndex, size_t io, bool forOutput) const = 0; -}; +}; +*/ +/* struct Node { int mType; - ImVec2 Pos, Size; + ImVec2 mPos, mSize; size_t InputsCount, OutputsCount; bool mbSelected; Node() : mbSelected(false) @@ -117,25 +112,7 @@ struct Node } }; -struct NodeLink -{ - int InputIdx, InputSlot, OutputIdx, OutputSlot; - NodeLink() - { - } - NodeLink(int input_idx, int input_slot, int output_idx, int output_slot) - { - InputIdx = input_idx; - InputSlot = input_slot; - OutputIdx = output_idx; - OutputSlot = output_slot; - } - bool operator==(const NodeLink& other) const - { - return InputIdx == other.InputIdx && InputSlot == other.InputSlot && OutputIdx == other.OutputIdx && - OutputSlot == other.OutputSlot; - } -}; +*/ inline bool operator!=(const ImVec2 r1, const ImVec2 r2) { @@ -146,28 +123,11 @@ inline bool operator!=(const ImVec2 r1, const ImVec2 r2) return false; } -struct NodeRug -{ - ImVec2 mPos, mSize; - uint32_t mColor; - std::string mText; - bool operator!=(const NodeRug& other) const - { - if (mPos != other.mPos) - return true; - if (mSize != other.mSize) - return false; - if (mColor != other.mColor) - return true; - if (mText != other.mText) - return true; - return false; - } -}; -void NodeGraph(NodeGraphControlerBase* delegate, bool enabled); -void NodeGraphClear(); // delegate is not called +class GraphModel; +void NodeGraph(GraphModel* model, bool enabled); +/*void NodeGraphClear(); // delegate is not called const std::vector& NodeGraphGetLinks(); const std::vector& NodeGraphRugs(); ImVec2 NodeGraphGetNodePos(size_t index); @@ -186,4 +146,4 @@ void NodeGraphUpdateEvaluationOrder(NodeGraphControlerBase* delegate); void NodeGraphUpdateScrolling(); void NodeGraphSelectNode(int selectedNodeIndex); void NodeGraphLayout(); -bool IsIOUsed(int nodeIndex, int slotIndex, bool forOutput); \ No newline at end of file +*/ \ No newline at end of file diff --git a/src/NodeGraphControler.cpp b/src/ParameterEditor.cpp similarity index 91% rename from src/NodeGraphControler.cpp rename to src/ParameterEditor.cpp index d5c62fa7..d300a6db 100644 --- a/src/NodeGraphControler.cpp +++ b/src/ParameterEditor.cpp @@ -23,7 +23,7 @@ // SOFTWARE. // -#include "NodeGraphControler.h" +#include "ParameterEditor.h" #include "EvaluationStages.h" #include "Library.h" #include "nfd.h" @@ -32,13 +32,13 @@ #include "UI.h" #include "Utils.h" -NodeGraphControler::NodeGraphControler() +ParameterEditor::ParameterEditor() : mbMouseDragging(false), mEditingContext(mEvaluationStages, false, 1024, 1024), mUndoRedoParamSetMouse(nullptr) { mCategories = &MetaNode::mCategories; } -void NodeGraphControler::Clear() +void ParameterEditor::Clear() { mSelectedNodeIndex = -1; mBackgroundNode = -1; @@ -47,7 +47,7 @@ void NodeGraphControler::Clear() mEditingContext.Clear(); } -void NodeGraphControler::SetParamBlock(size_t index, const std::vector& parameters) +void ParameterEditor::SetParamBlock(size_t index, const std::vector& parameters) { auto& stage = mEvaluationStages.mStages[index]; stage.mParameters = parameters; @@ -56,7 +56,7 @@ void NodeGraphControler::SetParamBlock(size_t index, const std::vector undoRedoAddNode(int(mEvaluationStages.mStages.size()), [&]() { return &mEvaluationStages.mStages; }, @@ -81,7 +81,7 @@ void NodeGraphControler::UserAddNode(size_t type) AddSingleNode(type); } -void NodeGraphControler::UserDeleteNode(size_t index) +void ParameterEditor::UserDeleteNode(size_t index) { URDummy urdummy; mEvaluationStages.RemoveAnimation(index); @@ -98,7 +98,7 @@ void NodeGraphControler::UserDeleteNode(size_t index) } } -void NodeGraphControler::HandlePin(uint32_t parameterPair) +void ParameterEditor::HandlePin(uint32_t parameterPair) { auto pinIter = std::find( mEvaluationStages.mPinnedParameters.begin(), mEvaluationStages.mPinnedParameters.end(), parameterPair); @@ -121,7 +121,7 @@ void NodeGraphControler::HandlePin(uint32_t parameterPair) } } -bool NodeGraphControler::EditSingleParameter(unsigned int nodeIndex, +bool ParameterEditor::EditSingleParameter(unsigned int nodeIndex, unsigned int parameterIndex, void* paramBuffer, const MetaParameter& param) @@ -317,14 +317,14 @@ bool NodeGraphControler::EditSingleParameter(unsigned int nodeIndex, return dirty; } -void NodeGraphControler::UpdateDirtyParameter(int index) +void ParameterEditor::UpdateDirtyParameter(int index) { auto& stage = mEvaluationStages.mStages[index]; mEvaluationStages.SetEvaluationParameters(index, stage.mParameters); mEditingContext.SetTargetDirty(index, Dirty::Parameter); } -void NodeGraphControler::PinnedEdit() +void ParameterEditor::PinnedEdit() { int dirtyNode = -1; for (const auto pin : mEvaluationStages.mPinnedParameters) @@ -352,7 +352,7 @@ void NodeGraphControler::PinnedEdit() } } -void NodeGraphControler::EditNodeParameters() +void ParameterEditor::EditNodeParameters() { size_t index = mSelectedNodeIndex; @@ -437,7 +437,7 @@ void NodeGraphControler::EditNodeParameters() } } -void NodeGraphControler::HandlePinIO(size_t nodeIndex, size_t slotIndex, bool forOutput) +void ParameterEditor::HandlePinIO(size_t nodeIndex, size_t slotIndex, bool forOutput) { if (IsIOUsed(nodeIndex, slotIndex, forOutput)) { @@ -450,28 +450,12 @@ void NodeGraphControler::HandlePinIO(size_t nodeIndex, size_t slotIndex, bool fo ImGui::PopID(); } -void NodeGraphControler::NodeEdit() +void ParameterEditor::NodeEdit() { ImGuiIO& io = ImGui::GetIO(); if (mSelectedNodeIndex == -1) { - /* - for (const auto pin : mEvaluationStages.mPinnedParameters) - { - unsigned int nodeIndex = (pin >> 16) & 0xFFFF; - unsigned int parameterIndex = pin & 0xFFFF; - if (parameterIndex != 0xDEAD) - continue; - - ImGui::PushID(1717171 + nodeIndex); - uint32_t parameterPair = (uint32_t(nodeIndex) << 16) + 0xDEAD; - HandlePin(parameterPair); - ImGui::SameLine(); - Imogen::RenderPreviewNode(nodeIndex, *this); - ImGui::PopID(); - } - */ auto& io = mEvaluationStages.mPinnedIO; for (size_t nodeIndex = 0; nodeIndex < io.size(); nodeIndex++) { @@ -513,20 +497,20 @@ void NodeGraphControler::NodeEdit() } } -void NodeGraphControler::SetTimeSlot(size_t index, int frameStart, int frameEnd) +void ParameterEditor::SetTimeSlot(size_t index, int frameStart, int frameEnd) { auto& stage = mEvaluationStages.mStages[index]; stage.mStartFrame = frameStart; stage.mEndFrame = frameEnd; } -void NodeGraphControler::SetTimeDuration(size_t index, int duration) +void ParameterEditor::SetTimeDuration(size_t index, int duration) { auto& stage = mEvaluationStages.mStages[index]; stage.mEndFrame = stage.mStartFrame + duration; } -void NodeGraphControler::InvalidateParameters() +void ParameterEditor::InvalidateParameters() { for (size_t i = 0; i < mEvaluationStages.mStages.size(); i++) { @@ -535,7 +519,7 @@ void NodeGraphControler::InvalidateParameters() } } -void NodeGraphControler::SetKeyboardMouse(float rx, +void ParameterEditor::SetKeyboardMouse(float rx, float ry, float dx, float dy, @@ -682,8 +666,8 @@ void NodeGraphControler::SetKeyboardMouse(float rx, mEditingContext.SetTargetDirty(mSelectedNodeIndex, Dirty::Mouse); } } - -bool NodeGraphControler::NodeIs2D(size_t nodeIndex) const +/* +bool ParameterEditor::NodeIs2D(size_t nodeIndex) const { auto target = mEditingContext.GetRenderTarget(nodeIndex); if (target) @@ -691,17 +675,12 @@ bool NodeGraphControler::NodeIs2D(size_t nodeIndex) const return false; } -bool NodeGraphControler::NodeIsCompute(size_t nodeIndex) const +bool ParameterEditor::NodeIsCompute(size_t nodeIndex) const { - /*auto buffer = mEditingContext.GetComputeBuffer(nodeIndex); - if (buffer) - return true; - return false; - */ return (gEvaluators.GetMask(mEvaluationStages.mStages[nodeIndex].mType) & EvaluationGLSLCompute) != 0; } -bool NodeGraphControler::NodeIsCubemap(size_t nodeIndex) const +bool ParameterEditor::NodeIsCubemap(size_t nodeIndex) const { auto target = mEditingContext.GetRenderTarget(nodeIndex); if (target) @@ -709,26 +688,26 @@ bool NodeGraphControler::NodeIsCubemap(size_t nodeIndex) const return false; } -ImVec2 NodeGraphControler::GetEvaluationSize(size_t nodeIndex) const +ImVec2 ParameterEditor::GetEvaluationSize(size_t nodeIndex) const { int imageWidth(1), imageHeight(1); EvaluationAPI::GetEvaluationSize(&mEditingContext, int(nodeIndex), &imageWidth, &imageHeight); return ImVec2(float(imageWidth), float(imageHeight)); } -void NodeGraphControler::CopyNodes(const std::vector nodes) +void ParameterEditor::CopyNodes(const std::vector nodes) { mStagesClipboard.clear(); for (auto nodeIndex : nodes) mStagesClipboard.push_back(mEvaluationStages.mStages[nodeIndex]); } -void NodeGraphControler::CutNodes(const std::vector nodes) +void ParameterEditor::CutNodes(const std::vector nodes) { mStagesClipboard.clear(); } -void NodeGraphControler::PasteNodes() +void ParameterEditor::PasteNodes() { for (auto& sourceNode : mStagesClipboard) { @@ -755,7 +734,7 @@ void NodeGraphControler::PasteNodes() } // animation -AnimTrack* NodeGraphControler::GetAnimTrack(uint32_t nodeIndex, uint32_t parameterIndex) +AnimTrack* ParameterEditor::GetAnimTrack(uint32_t nodeIndex, uint32_t parameterIndex) { for (auto& animTrack : mEvaluationStages.mAnimTrack) { @@ -765,7 +744,7 @@ AnimTrack* NodeGraphControler::GetAnimTrack(uint32_t nodeIndex, uint32_t paramet return NULL; } -void NodeGraphControler::MakeKey(int frame, uint32_t nodeIndex, uint32_t parameterIndex) +void ParameterEditor::MakeKey(int frame, uint32_t nodeIndex, uint32_t parameterIndex) { if (nodeIndex == -1) { @@ -792,12 +771,12 @@ void NodeGraphControler::MakeKey(int frame, uint32_t nodeIndex, uint32_t paramet size_t parameterOffset = GetParameterOffset(uint32_t(stage.mType), parameterIndex); animTrack->mAnimation->SetValue(frame, &stage.mParameters[parameterOffset]); } - -void NodeGraphControler::GetKeyedParameters(int frame, uint32_t nodeIndex, std::vector& keyed) +*/ +void ParameterEditor::GetKeyedParameters(int frame, uint32_t nodeIndex, std::vector& keyed) { } -void NodeGraphControler::DrawNodeImage(ImDrawList* drawList, +void ParameterEditor::DrawNodeImage(ImDrawList* drawList, const ImRect& rc, const ImVec2 marge, const size_t nodeIndex) @@ -822,8 +801,8 @@ void NodeGraphControler::DrawNodeImage(ImDrawList* drawList, ImVec2(1, 0)); } } - -bool NodeGraphControler::RenderBackground() +/* +bool ParameterEditor::RenderBackground() { if (mBackgroundNode != -1) { @@ -832,8 +811,8 @@ bool NodeGraphControler::RenderBackground() } return false; } - -void NodeGraphControler::SetParameter(int nodeIndex, +*/ +void ParameterEditor::SetParameter(int nodeIndex, const std::string& parameterName, const std::string& parameterValue) { diff --git a/src/NodeGraphControler.h b/src/ParameterEditor.h similarity index 94% rename from src/NodeGraphControler.h rename to src/ParameterEditor.h index c7b24112..dbde6026 100644 --- a/src/NodeGraphControler.h +++ b/src/ParameterEditor.h @@ -32,13 +32,13 @@ #include "Library.h" #include "EvaluationContext.h" -struct NodeGraphControler : public NodeGraphControlerBase +struct ParameterEditor { - NodeGraphControler(); + ParameterEditor(); void Clear(); - virtual void AddSingleNode(size_t type); + /*virtual void AddSingleNode(size_t type); virtual void UserAddNode(size_t type); virtual void AddLink(int inputIdx, int inputSlot, int outputIdx, int outputSlot) { @@ -57,7 +57,7 @@ struct NodeGraphControler : public NodeGraphControlerBase } virtual void UserDeleteNode(size_t index); virtual void SetParamBlock(size_t index, const std::vector& parameters); - + */ virtual unsigned int GetNodeTexture(size_t index) { return mEditingContext.GetEvaluationTexture(index); @@ -79,7 +79,7 @@ struct NodeGraphControler : public NodeGraphControlerBase bool bCtrl, bool bAlt, bool bShift); - + /* bool NodeHasUI(size_t nodeIndex) const { if (mEvaluationStages.mStages.size() <= nodeIndex) @@ -121,21 +121,21 @@ struct NodeGraphControler : public NodeGraphControlerBase return mEvaluationStages.GetAnimTrack(); } - + void MakeKey(int frame, uint32_t nodeIndex, uint32_t parameterIndex); void GetKeyedParameters(int frame, uint32_t nodeIndex, std::vector& keyed); AnimTrack* GetAnimTrack(uint32_t nodeIndex, uint32_t parameterIndex); - + */ void PinnedEdit(); EvaluationContext mEditingContext; EvaluationStages mEvaluationStages; - std::vector mStagesClipboard; - int mBackgroundNode; + //std::vector mStagesClipboard; + //int mBackgroundNode; bool mbMouseDragging; - URChange>* mUndoRedoParamSetMouse; + //URChange>* mUndoRedoParamSetMouse; EvaluationStage* Get(ASyncId id) { @@ -148,7 +148,7 @@ struct NodeGraphControler : public NodeGraphControlerBase unsigned int parameterIndex, void* paramBuffer, const MetaParameter& param); - void NodeIsAdded(int index); + //void NodeIsAdded(int index); void UpdateDirtyParameter(int index); void EditNodeParameters(); void HandlePin(uint32_t parameterPair); diff --git a/src/main.cpp b/src/main.cpp index 8a1a491c..aff75773 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,7 +100,7 @@ void APIENTRY openglCallbackFunction(GLenum /*source*/, Library library; enki::TaskScheduler g_TS; -UndoRedoHandler gUndoRedoHandler; + Builder* builder; SDL_Window* window; SDL_GLContext glThreadContext;