Skip to content

Commit beb1fe6

Browse files
big refactor attempt 2 WIP
1 parent b57efd0 commit beb1fe6

13 files changed

Lines changed: 819 additions & 483 deletions

src/EvaluationContext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "EvaluationContext.h"
3030
#include "Evaluators.h"
3131
#include "NodeGraphControler.h"
32+
#include "UndoRedo.h"
3233

3334
static const unsigned int wrap[] = {GL_REPEAT, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER, GL_MIRRORED_REPEAT};
3435
static const unsigned int filter[] = {GL_LINEAR, GL_NEAREST};

src/EvaluationStages.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <algorithm>
3131
#include <map>
3232
#include <GL/gl3w.h>
33+
#include "UndoRedo.h"
3334

3435
EvaluationStages::EvaluationStages() : mFrameMin(0), mFrameMax(1)
3536
{

src/Evaluators.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extern std::vector<ImHotKey::HotKey> mHotkeys;
126126

127127
void RenderImogenFrame();
128128
void NodeGraphLayout();
129-
void NodeGraphUpdateScrolling();
129+
void NodeGraphUpdateScrolling(GraphModel* model);
130130
void NodeGraphUpdateEvaluationOrder(NodeGraphControlerBase* delegate);
131131

132132

@@ -150,13 +150,16 @@ PYBIND11_EMBEDDED_MODULE(Imogen, m)
150150
});
151151
m.def("Connect", [](int nodeSource, int slotSource, int nodeDestination, int slotDestination) {
152152
// Imogen::instance->GetNodeGraphControler()->AddLink(nodeSource, slotSource, nodeDestination, slotDestination);
153-
NodeGraphAddLink(
153+
/*NodeGraphAddLink(
154154
Imogen::instance->GetNodeGraphControler(), nodeSource, slotSource, nodeDestination, slotDestination);
155+
*/
156+
Imogen::instance->GetNodeGraphControler().mModel.AddLink(
157+
nodeSource, slotSource, nodeDestination, slotDestination);
155158
});
156159
m.def("AutoLayout", []() {
157160
NodeGraphUpdateEvaluationOrder(Imogen::instance->GetNodeGraphControler());
158161
NodeGraphLayout();
159-
NodeGraphUpdateScrolling();
162+
NodeGraphUpdateScrolling(&Imogen::instance->GetNodeGraphControler().mModel);
160163
});
161164
m.def("DeleteGraph", []() { Imogen::instance->DeleteCurrentMaterial(); });
162165

src/GraphModel.cpp

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
// https://github.com/CedricGuillemet/Imogen
2+
//
3+
// The MIT License(MIT)
4+
//
5+
// Copyright(c) 2019 Cedric Guillemet
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files(the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions :
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
//
25+
26+
#include "GraphModel.h"
27+
#include <assert.h>
28+
#include "UndoRedo.h"
29+
30+
31+
GraphModel::GraphModel() : mbTransaction(false), mUndoRedoHandler(new UndoRedoHandler), mSelectedNodeIndex(-1)
32+
{
33+
}
34+
35+
GraphModel::~GraphModel()
36+
{
37+
delete mUndoRedoHandler;
38+
}
39+
40+
void GraphModel::Clear()
41+
{
42+
mNodes.clear();
43+
mLinks.clear();
44+
mRugs.clear();
45+
mUndoRedoHandler->Clear();
46+
mSelectedNodeIndex = -1;
47+
}
48+
49+
void GraphModel::BeginTransaction(bool undoable)
50+
{
51+
assert(!mbTransaction);
52+
53+
mbTransaction = true;
54+
}
55+
56+
void GraphModel::EndTransaction()
57+
{
58+
assert(mbTransaction);
59+
mbTransaction = false;
60+
}
61+
62+
void GraphModel::Undo()
63+
{
64+
assert(!mbTransaction);
65+
mUndoRedoHandler->Undo();
66+
}
67+
68+
void GraphModel::Redo()
69+
{
70+
assert(!mbTransaction);
71+
mUndoRedoHandler->Redo();
72+
}
73+
74+
75+
int GraphModel::AddNode(size_t type, ImVec2 position)
76+
{
77+
assert(mbTransaction);
78+
79+
// size_t index = nodes.size();
80+
mNodes.push_back(Node(type, position));
81+
82+
return mNodes.size() - 1;
83+
}
84+
void GraphModel::DelNode(size_t nodeIndex)
85+
{
86+
assert(mbTransaction);
87+
}
88+
void GraphModel::AddLink(size_t inputNodeIndex, size_t inputSlotIndex, size_t outputNodeIndex, size_t outputSlotIndex)
89+
{
90+
assert(mbTransaction);
91+
92+
if (inputNodeIndex >= mNodes.size() || outputNodeIndex >= mNodes.size())
93+
{
94+
// Log("Error : Link node index doesn't correspond to an existing node.");
95+
return;
96+
}
97+
98+
NodeLink nl;
99+
nl.mInputIdx = inputNodeIndex;
100+
nl.mInputSlot = inputSlotIndex;
101+
nl.mOutputIdx = outputNodeIndex;
102+
nl.mOutputSlot = outputSlotIndex;
103+
mLinks.push_back(nl);
104+
}
105+
void GraphModel::DelLink(size_t index, size_t slot)
106+
{
107+
assert(mbTransaction);
108+
}
109+
void GraphModel::AddRug(ImVec2 position, ImVec2 size, uint32_t color, const std::string& comment)
110+
{
111+
assert(mbTransaction);
112+
113+
mRugs.push_back({position, size, color, comment});
114+
}
115+
void GraphModel::DelRug(size_t rugIndex)
116+
{
117+
assert(mbTransaction);
118+
}
119+
120+
void GraphModel::SetRug(size_t rugIndex, const NodeRug& rug)
121+
{
122+
assert(mbTransaction);
123+
mRugs[rugIndex] = rug;
124+
}
125+
126+
void GraphModel::DeleteSelectedNodes()
127+
{
128+
URDummy urDummy;
129+
for (int selection = int(mNodes.size()) - 1; selection >= 0; selection--)
130+
{
131+
if (!mNodes[selection].mbSelected)
132+
continue;
133+
URDel<Node> undoRedoDelNode(int(selection),
134+
[]() { return &mNodes; },
135+
[this](int index) {
136+
// recompute link indices
137+
for (int id = 0; id < mLinks.size(); id++)
138+
{
139+
if (mLinks[id].mInputIdx > index)
140+
mLinks[id].mInputIdx--;
141+
if (mLinks[id].mOutputIdx > index)
142+
mLinks[id].mOutputIdx--;
143+
}
144+
//NodeGraphUpdateEvaluationOrder(controler); todo
145+
mSelectedNodeIndex = -1;
146+
},
147+
[this](int index) {
148+
// recompute link indices
149+
for (int id = 0; id < mLinks.size(); id++)
150+
{
151+
if (mLinks[id].mInputIdx >= index)
152+
mLinks[id].mInputIdx++;
153+
if (mLinks[id].mOutputIdx >= index)
154+
mLinks[id].mOutputIdx++;
155+
}
156+
157+
//NodeGraphUpdateEvaluationOrder(controler); todo
158+
mSelectedNodeIndex = -1;
159+
});
160+
161+
for (int id = 0; id < mLinks.size(); id++)
162+
{
163+
if (mLinks[id].mInputIdx == selection || mLinks[id].mOutputIdx == selection)
164+
DelLink(mLinks[id].mOutputIdx, mLinks[id].mOutputSlot);
165+
}
166+
// auto iter = links.begin();
167+
for (size_t i = 0; i < mLinks.size();)
168+
{
169+
auto& link = mLinks[i];
170+
if (link.mInputIdx == selection || link.mOutputIdx == selection)
171+
{
172+
URDel<NodeLink> undoRedoDelNodeLink(
173+
int(i),
174+
[this]() { return &mLinks; },
175+
[this](int index) {
176+
NodeLink& link = mLinks[index];
177+
DelLink(link.mOutputIdx, link.mOutputSlot);
178+
},
179+
[this](int index) {
180+
NodeLink& link = mLinks[index];
181+
AddLink(link.mInputIdx, link.mInputSlot, link.mOutputIdx, link.mOutputSlot);
182+
});
183+
184+
mLinks.erase(mLinks.begin() + i);
185+
}
186+
else
187+
{
188+
i++;
189+
}
190+
}
191+
192+
// recompute link indices
193+
for (int id = 0; id < mLinks.size(); id++)
194+
{
195+
if (mLinks[id].mInputIdx > selection)
196+
mLinks[id].mInputIdx--;
197+
if (mLinks[id].mOutputIdx > selection)
198+
mLinks[id].mOutputIdx--;
199+
}
200+
201+
// delete links
202+
mNodes.erase(mNodes.begin() + selection);
203+
//NodeGraphUpdateEvaluationOrder(controler); todo
204+
205+
// inform delegate
206+
//controler->UserDeleteNode(selection); todo
207+
}
208+
}
209+
bool GraphModel::IsIOUsed(int nodeIndex, int slotIndex, bool forOutput) const
210+
{
211+
/*for (auto& link : mLinks)
212+
{
213+
if ((link.InputIdx == nodeIndex && link.InputSlot == slotIndex && forOutput) ||
214+
(link.OutputIdx == nodeIndex && link.OutputSlot == slotIndex && !forOutput))
215+
{
216+
return true;
217+
}
218+
}
219+
*/
220+
return false;
221+
}

src/GraphModel.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// https://github.com/CedricGuillemet/Imogen
2+
//
3+
// The MIT License(MIT)
4+
//
5+
// Copyright(c) 2019 Cedric Guillemet
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files(the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions :
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
//
25+
#pragma once
26+
27+
#include "imgui.h"
28+
#include <vector>
29+
#include <string>
30+
#include <stdint.h>
31+
32+
struct UndoRedoHandler;
33+
34+
35+
36+
class GraphModel
37+
{
38+
public:
39+
GraphModel();
40+
~GraphModel();
41+
42+
43+
struct NodeRug
44+
{
45+
ImVec2 mPos, mSize;
46+
uint32_t mColor;
47+
std::string mText;
48+
};
49+
50+
51+
struct Node
52+
{
53+
Node(int type, ImVec2 position) : mType(type), mPos(position), mbSelected(false)
54+
{
55+
}
56+
Node() : mbSelected(false)
57+
{
58+
}
59+
int mType;
60+
ImVec2 mPos;
61+
bool mbSelected;
62+
};
63+
64+
struct NodeLink
65+
{
66+
int mInputIdx, mInputSlot, mOutputIdx, mOutputSlot;
67+
};
68+
69+
70+
// Transaction
71+
void Clear();
72+
void BeginTransaction(bool undoable);
73+
void EndTransaction();
74+
75+
// undo/redo
76+
77+
void Undo();
78+
void Redo();
79+
80+
// setters
81+
int AddNode(size_t type, ImVec2 position);
82+
void DelNode(size_t nodeIndex);
83+
void DeleteSelectedNodes();
84+
void AddLink(size_t inputNodeIndex, size_t inputSlotIndex, size_t outputNodeIndex, size_t outputSlotIndex);
85+
void DelLink(size_t index, size_t slot);
86+
void AddRug(ImVec2 position, ImVec2 size, uint32_t color, const std::string& comment);
87+
void DelRug(size_t rugIndex);
88+
void SetRug(size_t rugIndex, const NodeRug& rug);
89+
90+
91+
// getters
92+
93+
// linked to runtime...not sure
94+
bool NodeIsCubemap(size_t nodeIndex) const;
95+
bool NodeIs2D(size_t nodeIndex) const;
96+
bool NodeIsCompute(size_t nodeIndex) const;
97+
bool NodeHasUI(size_t nodeIndex) const;
98+
const std::vector<NodeRug>& GetRugs() const { return mRugs; }
99+
const std::vector<Node>& GetNodes() const
100+
{
101+
return mNodes;
102+
}
103+
const std::vector<NodeLink>& GetLinks() const
104+
{
105+
return mLinks;
106+
}
107+
bool IsIOUsed(int nodeIndex, int slotIndex, bool forOutput) const;
108+
109+
// clipboard
110+
void CopyNodes(const std::vector<size_t> nodes);
111+
void CutNodes(const std::vector<size_t> nodes);
112+
void PasteNodes();
113+
114+
115+
116+
117+
private:
118+
bool mbTransaction;
119+
UndoRedoHandler* mUndoRedoHandler;
120+
121+
int mSelectedNodeIndex;
122+
static std::vector<Node> mNodes;
123+
static std::vector<NodeLink> mLinks;
124+
static std::vector<NodeRug> mRugs;
125+
};

0 commit comments

Comments
 (0)