neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
node.hpp
Go to the documentation of this file.
1// node.hpp
2/*
3 neoGFX Design Studio
4 Copyright(C) 2020 Leigh Johnston
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
26
28{
29 template <typename Base, pin_type Type>
30 class node_pin : public reference_counted<Base>
31 {
32 typedef Base base_type;
33 public:
34 node_pin(i_node& aNode) :
35 iNode{ aNode }
36 {
37 }
38 public:
39 pin_type type() const override
40 {
41 return Type;
42 }
43 i_node& get() const override
44 {
45 return iNode;
46 }
47 bool connected() const override
48 {
49 for (auto const& c : get().connections())
50 if (&c->source() == this || &c->destination() == this)
51 return true;
52 return false;
53 }
54 public:
56 {
57 return as_widget().color();
58 }
59 i_pin_widget const& as_widget() const override
60 {
61 return *iWidget;
62 }
64 {
65 return *iWidget;
66 }
67 void set_widget(i_pin_widget& aWidget) override
68 {
69 iWidget = &aWidget;
70 }
71 private:
72 i_node& iNode;
73 i_pin_widget* iWidget = nullptr;
74 };
75
76 class node_input_pin : public node_pin<i_node_input_pin, pin_type::Input>
77 {
79 public:
81 };
82
83 class node_output_pin : public node_pin<i_node_output_pin, pin_type::Output>
84 {
86 public:
88 };
89
90 class node_connection : public reference_counted<i_node_connection>
91 {
92 public:
93 node_connection(i_node_pin& aSource, i_node_pin& aDestination) :
94 iSource{ aSource },
95 iDestination{ aDestination }
96 {
97 }
98 public:
99 i_node_pin const& source() const override
100 {
101 return *iSource;
102 }
103 i_node_pin& source() override
104 {
105 return *iSource;
106 }
107 i_node_pin const& destination() const override
108 {
109 return *iDestination;
110 }
112 {
113 return *iDestination;
114 }
115 private:
116 ref_ptr<i_node_pin> iSource;
117 ref_ptr<i_node_pin> iDestination;
118 };
119
120 class node : public i_node
121 {
122 typedef node self_type;
123 typedef i_node base_type;
124 public:
125 define_declared_event(InputAdded, input_added, i_node_input_pin&)
127 define_declared_event(OutputAdded, output_added, i_node_output_pin&)
129 define_declared_event(ConnectionAdded, connection_added, i_node_connection&)
131 public:
136 public:
137 node_inputs_t const& inputs() const override
138 {
139 return iInputs;
140 }
141 node_outputs_t const& outputs() const override
142 {
143 return iOutputs;
144 }
145 node_connections_t const& connections() const override
146 {
147 return iConnections;
148 }
149 std::size_t connected_inputs() const override
150 {
151 std::size_t result = 0;
152 for (auto& c : connections())
153 if (&c->destination().get() == this && c->destination().type() == pin_type::Input)
154 ++result;
155 return result;
156 }
157 std::size_t connected_outputs() const override
158 {
159 std::size_t result = 0;
160 for (auto& c : connections())
161 if (&c->source().get() == this && c->source().type() == pin_type::Output)
162 ++result;
163 return result;
164 }
165 public:
166 void add_input(i_node_input_pin& aPin) override
167 {
168 iInputs.emplace_back(aPin);
169 InputAdded.trigger(*iInputs.back());
170 }
171 void remove_input(i_node_input_pin& aPin) override
172 {
173 auto existing = std::find_if(iInputs.begin(), iInputs.end(), [&](auto&& e) { return &*e == &aPin; });
174 if (existing != iInputs.end())
175 {
176 ref_ptr<i_node_input_pin> temp = *existing;
177 iInputs.erase(existing);
178 InputRemoved.trigger(*temp);
179 }
180 }
181 void add_output(i_node_output_pin& aPin) override
182 {
183 iOutputs.emplace_back(aPin);
184 OutputAdded.trigger(*iOutputs.back());
185 }
186 void remove_output(i_node_output_pin& aPin) override
187 {
188 auto existing = std::find_if(iOutputs.begin(), iOutputs.end(), [&](auto&& e) { return &*e == &aPin; });
189 if (existing != iOutputs.end())
190 {
191 ref_ptr<i_node_output_pin> temp = *existing;
192 iOutputs.erase(existing);
193 OutputRemoved.trigger(*temp);
194 }
195 }
196 void add_connection(i_node_pin& aSource, i_node_pin& aDestination) override
197 {
198 iConnections.push_back(make_ref<node_connection>(aSource, aDestination));
199 ConnectionAdded.trigger(*iConnections.back());
200 }
201 void remove_connection(i_node_pin& aSource, i_node_pin& aDestination) override
202 {
203 auto existing = std::find_if(iConnections.begin(), iConnections.end(), [&](auto&& e) { return &e->source() == &aSource && &e->destination() == &aDestination; });
204 if (existing != iConnections.end())
205 {
206 ref_ptr<node_connection> temp = *existing;
207 iConnections.erase(existing);
208 ConnectionRemoved.trigger(*temp);
209 }
210 }
211 private:
212 node_inputs_t iInputs;
213 node_outputs_t iOutputs;
214 node_connections_t iConnections;
215 };
216}
virtual node_connections_t const & connections() const =0
virtual color_or_gradient color() const =0
i_node_pin const & destination() const override
Definition node.hpp:107
i_node_pin & destination() override
Definition node.hpp:111
i_node_pin & source() override
Definition node.hpp:103
i_node_pin const & source() const override
Definition node.hpp:99
node_connection(i_node_pin &aSource, i_node_pin &aDestination)
Definition node.hpp:93
i_pin_widget & as_widget() override
Definition node.hpp:63
i_pin_widget const & as_widget() const override
Definition node.hpp:59
i_node & get() const override
Definition node.hpp:43
bool connected() const override
Definition node.hpp:47
neogfx::color_or_gradient color() const override
Definition node.hpp:55
node_pin(i_node &aNode)
Definition node.hpp:34
void set_widget(i_pin_widget &aWidget) override
Definition node.hpp:67
pin_type type() const override
Definition node.hpp:39
void add_input(i_node_input_pin &aPin) override
Definition node.hpp:166
void remove_input(i_node_input_pin &aPin) override
Definition node.hpp:171
void remove_output(i_node_output_pin &aPin) override
Definition node.hpp:186
i_node_input_pin i_node_output_pin connection_removed
Definition node.hpp:130
i_node_input_pin output_removed
Definition node.hpp:128
void add_connection(i_node_pin &aSource, i_node_pin &aDestination) override
Definition node.hpp:196
neolib::vector< ref_ptr< i_node_output_pin > > node_outputs_t
Definition node.hpp:134
neolib::vector< ref_ptr< i_node_input_pin > > node_inputs_t
Definition node.hpp:133
void remove_connection(i_node_pin &aSource, i_node_pin &aDestination) override
Definition node.hpp:201
std::size_t connected_inputs() const override
Definition node.hpp:149
define_declared_event(InputAdded, input_added, i_node_input_pin &) define_declared_event(InputRemoved
void add_output(i_node_output_pin &aPin) override
Definition node.hpp:181
std::size_t connected_outputs() const override
Definition node.hpp:157
neolib::vector< ref_ptr< i_node_connection > > node_connections_t
Definition node.hpp:135
node_inputs_t const & inputs() const override
Definition node.hpp:137
node_outputs_t const & outputs() const override
Definition node.hpp:141
node_connections_t const & connections() const override
Definition node.hpp:145
void emplace_back(Args &&... aArgs)
Definition vector.hpp:206
const value_type & back() const final
Definition vector.hpp:222
void push_back(abstract_value_type const &aValue) final
Definition vector.hpp:201
#define define_declared_event(name, declName,...)
Definition event.hpp:195