|
| 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 | +} |
0 commit comments