379b3f0346b1088e23373c38a931c0029e339368
[synfig.git] / synfig-core / src / synfig / valuenode.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode.h
3 **      \brief Header file for implementation of the "Placeholder" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === S T A R T =========================================================== */
25
26 #ifndef __SYNFIG_VALUENODE_H
27 #define __SYNFIG_VALUENODE_H
28
29 /* === H E A D E R S ======================================================= */
30
31 #include "vector.h"
32 #include "value.h"
33 #include "string.h"
34 #include "releases.h"
35 #include <ETL/handle>
36 #include <ETL/stringf>
37 #include "exception.h"
38 #include <map>
39 #include <sigc++/signal.h>
40 #include "guid.h"
41 #include <ETL/angle>
42
43 #include "node.h"
44
45 #include <set>
46
47 /* === M A C R O S ========================================================= */
48
49 #define CHECK_TYPE_AND_SET_VALUE(variable, type)                                                \
50         /* I don't think this ever happens - maybe remove this code? */         \
51         if (get_type() == ValueBase::TYPE_NIL) {                                                        \
52                 warning("%s:%d get_type() IS nil sometimes!",                                   \
53                                 __FILE__, __LINE__);                                                                    \
54                 return false;                                                                                                   \
55         }                                                                                                                                       \
56         if (get_type() != ValueBase::TYPE_NIL &&                                                        \
57                 !(ValueBase::same_type_as(value->get_type(), type)) &&                  \
58                 !PlaceholderValueNode::Handle::cast_dynamic(value)) {                   \
59                 error(_("%s:%d wrong type for %s: need %s but got %s"),                 \
60                           __FILE__, __LINE__,                                                                           \
61                           link_local_name(i).c_str(),                                                           \
62                           ValueBase::type_local_name(type).c_str(),                                     \
63                           ValueBase::type_local_name(value->get_type()).c_str());       \
64                 return false;                                                                                                   \
65         }                                                                                                                                       \
66         variable = value;                                                                                                       \
67         signal_child_changed()(i);                                                                                      \
68         signal_value_changed()();                                                                                       \
69         return true
70
71 /* === T Y P E D E F S ===================================================== */
72
73 /* === C L A S S E S & S T R U C T S ======================================= */
74
75 namespace synfig {
76
77 class Canvas;
78 class LinkableValueNode;
79 class Layer;
80
81 /*!     \class ValueNode
82 **      \brief Base class for all Value Nodes
83 */
84 class ValueNode : public synfig::Node
85 {
86         friend class Layer;
87         friend class LinkableValueNode;
88
89         /*
90  --     ** -- T Y P E S -----------------------------------------------------------
91         */
92
93 public:
94
95         typedef etl::handle<ValueNode> Handle;
96
97         typedef etl::loose_handle<ValueNode> LooseHandle;
98
99         typedef etl::handle<const ValueNode> ConstHandle;
100
101         typedef etl::rhandle<ValueNode> RHandle;
102
103         //!Instantiates the book of ValaueNodes and register all the valid valuenodes on it
104         static bool subsys_init();
105         //!Deletes the book of ValueNodes
106         static bool subsys_stop();
107
108         /*
109  --     ** -- D A T A -------------------------------------------------------------
110         */
111
112 private:
113         //! The type of the Value Node
114         //! \see ValueBase
115         ValueBase::Type type;
116         //! The name of the Value Node. This is the string that is used in the
117         //! sif file to define the value type: i.e. <param name="amount">
118         String name;
119         //! The canvas this Value Node belongs to
120         etl::loose_handle<Canvas> canvas_;
121         //! The root canvas this Value Node belongs to
122         etl::loose_handle<Canvas> root_canvas_;
123
124         /*
125  -- ** -- S I G N A L S -------------------------------------------------------
126         */
127
128 private:
129
130         //!     ValueBase Changed
131         sigc::signal<void> signal_value_changed_;
132
133         //!     Children Reordered
134         sigc::signal<void,int*> signal_children_reordered_;
135
136         //!     Child Changed
137         sigc::signal<void,int> signal_child_changed_;
138
139         //!     Child Removed
140         sigc::signal<void,int> signal_child_removed_;
141
142         //!     Child Inserted
143         sigc::signal<void,int> signal_child_inserted_;
144
145         //!     ID Changed
146         sigc::signal<void> signal_id_changed_;
147
148         /*
149  -- ** -- S I G N A L   I N T E R F A C E -------------------------------------
150         */
151
152 public:
153
154         //!     ValueBase Changed
155         sigc::signal<void>& signal_value_changed() { return signal_value_changed_; }
156
157         //!     Children Reordered
158         sigc::signal<void,int*>& signal_children_reordered() { return signal_children_reordered_; }
159
160         //!     Child Changed
161         sigc::signal<void,int>& signal_child_changed() { return signal_child_changed_; }
162
163         //!     Child Removed
164         sigc::signal<void,int>& signal_child_removed() { return signal_child_removed_; }
165
166         //!     Child Inserted
167         sigc::signal<void,int>& signal_child_inserted() { return signal_child_inserted_; }
168
169         //!     ID Changed
170         sigc::signal<void>& signal_id_changed() { return signal_id_changed_; }
171
172         /*
173  --     ** -- C O N S T R U C T O R S ---------------------------------------------
174         */
175
176 protected:
177
178         ValueNode(ValueBase::Type type=ValueBase::TYPE_NIL);
179
180 public:
181
182         virtual ~ValueNode();
183
184         /*
185  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
186         */
187
188 public:
189
190         //! Returns the value of the ValueNode at time \a t
191         virtual ValueBase operator()(Time /*t*/)const
192                 { return ValueBase(); }
193
194         //! \internal Sets the id of the ValueNode
195         void set_id(const String &x);
196
197         //! Returns the id of the ValueNode
198         /*!     The ID is used for keeping track of a
199         **      specific instance of a ValueNode. */
200         const String &get_id()const { return name; }
201
202         //! Returns the name of the ValueNode type
203         virtual String get_name()const=0;
204
205         //! Returns the localized name of the ValueNode type
206         virtual String get_local_name()const=0;
207
208         //! Return a full description of the ValueNode and its parentage
209         virtual String get_description(bool show_exported_name = true)const;
210
211
212         //! Clones a Value Node
213         virtual ValueNode* clone(const GUID& deriv_guid=GUID())const=0;
214
215         //! Returns \true if the Value Node has an ID (has been exported)
216         bool is_exported()const { return !get_id().empty(); }
217
218         //! Returns the type of the ValueNode
219         ValueBase::Type get_type()const { return type; }
220
221         //! Returns a handle to the parent canvas, if it has one.
222         etl::loose_handle<Canvas> get_parent_canvas()const { return canvas_; }
223
224         //! Returns a handle to the parent canvas, if it has one.
225         etl::loose_handle<Canvas> get_root_canvas()const { return root_canvas_; }
226
227         //! Sets the parent canvas for the Value Node
228         void set_parent_canvas(etl::loose_handle<Canvas> x);
229
230         //! Sets the root canvas parent for the Value Node
231         void set_root_canvas(etl::loose_handle<Canvas> x);
232
233         //! Returns the relative ID of a Node when accessed form the \x Canvas
234         String get_relative_id(etl::loose_handle<const Canvas> x)const;
235
236         //! Replaces the Value Node with a given one. It look up all its parents
237         //! remove it self from them and adds the given Value Node
238         //! Notice that it is called twice and the second time it uses
239         //! a replaceable handle to the Node
240         //! \see etl::rhandle
241         int replace(etl::handle<ValueNode> x);
242
243 protected:
244         //! Sets the type of the ValueNode
245         void set_type(ValueBase::Type t) { type=t; }
246
247         virtual void on_changed();
248 }; // END of class ValueNode
249
250 /*!     \class PlaceholderValueNode
251 **      Seems to be a Place to hold a Value Node temporarly.
252 *
253 *       Doesn't seem to implement any functionality. Seems to be used when the
254 *       value node cannot be created using the Const, Animated or Linkable
255 *       Value Nodes.
256 *
257 */
258 class PlaceholderValueNode : public ValueNode
259 {
260 public:
261         typedef etl::handle<PlaceholderValueNode> Handle;
262         typedef etl::loose_handle<PlaceholderValueNode> LooseHandle;
263         typedef etl::handle<const PlaceholderValueNode> ConstHandle;
264         typedef etl::rhandle<PlaceholderValueNode> RHandle;
265
266 private:
267
268         PlaceholderValueNode(ValueBase::Type type=ValueBase::TYPE_NIL);
269
270 public:
271
272         virtual ValueBase operator()(Time t)const;
273
274         virtual String get_name()const;
275
276         virtual String get_local_name()const;
277
278         virtual ValueNode* clone(const GUID& deriv_guid=GUID())const;
279
280         static Handle create(ValueBase::Type type=ValueBase::TYPE_NIL);
281
282 protected:
283         virtual void get_times_vfunc(Node::time_set &/*set*/) const {}
284 }; // END of class PlaceholderValueNode
285
286
287 /*!     \class LinkableValueNode
288 **      \brief Specialized Class of Value Nodes that has links to other
289 ** Value Nodes
290 *
291 *       This Value Node is calculated based on a math calculation or a time
292 *       evaluation of the linked Value Nodes. It is commonly known as
293 *       Converted Value Nodes. The derived clases defines the behavior.
294 */
295 class LinkableValueNode : public ValueNode
296 {
297         friend class ValueNode;
298 public:
299
300         typedef etl::handle<LinkableValueNode> Handle;
301
302         typedef etl::loose_handle<LinkableValueNode> LooseHandle;
303
304         typedef etl::handle<const LinkableValueNode> ConstHandle;
305
306         typedef etl::rhandle<LinkableValueNode> RHandle;
307
308
309         //! Type that represents a pointer to a ValueNode's constructor
310         /*! As a pointer to the constructor, it represents a "factory" of
311         **  objects of this class.
312         */
313         typedef LinkableValueNode* (*Factory)(const ValueBase&);
314
315         //! This represents a pointer to a Type check member fucntion
316         /*! As a pointer to the member, it represents a fucntion that checks
317         **  the type of the provided ValueBase
318         */
319         typedef bool (*CheckType)(ValueBase::Type);
320
321         struct BookEntry
322         {
323                 String local_name;
324                 Factory factory;
325                 CheckType check_type;
326                 ReleaseVersion release_version; // which version of synfig introduced this valuenode type
327         };
328
329         //! Book of types of linkable value nodes indexed by type name.
330         /*! While the sifz file is read, each time a new LinkableValueNode entry
331         **  is found, the factory constructor that the "factory" pointer member
332         **  of the "BookEntry" struct points to, is called, and a new object of
333         **  that type is created.
334         **  \sa LinkableValueNode::Factory
335         */
336         typedef std::map<String,BookEntry> Book;
337
338         static Book& book();
339
340         //! Creates a Linkable Value Node based on the name and the returned
341         //! value type. Returns a valid Handle if both (name and type) match
342         static Handle create(const String &name, const ValueBase& x);
343
344         //! Each derived Linkable Value Node has to implement this fucntion and
345         //! should return true only if the type matches. \name is the name of
346         //! the linked value node and \x is the returned value type
347         static bool check_type(const String &name, ValueBase::Type x);
348
349 public:
350         LinkableValueNode(ValueBase::Type type=ValueBase::TYPE_NIL):
351                 ValueNode(type) { }
352
353 protected:
354         //! Stores the Value Node \x in the sub parameter i after check if the
355         //! type is the same.
356         //! It has to be defined by the derived class.
357         virtual bool set_link_vfunc(int i,ValueNode::Handle x)=0;
358
359         //! Frees all the subparameters of the Linkable Value Node.
360         //! Used by the derived classed destructors.
361         void unlink_all();
362
363 public:
364
365         //! Returns the number of linked Value Nodes
366         virtual int link_count()const=0;
367
368         //! Returns the local name of the 'i' linked Value Node
369         virtual String link_local_name(int i)const=0;
370
371         //! Returns the name of the 'i' linked Value Node
372         virtual String link_name(int i)const=0;
373
374         //! Returns the child index Value Node based on the name
375         virtual int get_link_index_from_name(const String &name)const=0;
376
377         //! Clones a Value Node
378         virtual ValueNode* clone(const GUID& deriv_guid=GUID())const;
379
380         //! Sets a new Value Node link by its index
381         bool set_link(int i,ValueNode::Handle x);
382         //! Sets a new Value Node link by its name
383         bool set_link(const String &name,ValueNode::Handle x) { return set_link(get_link_index_from_name(name),x);      }
384
385         //! Returns a Loose Handle to the Value Node based on the link's index
386         ValueNode::LooseHandle get_link(int i)const;
387         //! Returns a Loose Handle to the Value Node based on the link's name
388         ValueNode::LooseHandle get_link(const String &name)const { return get_link(get_link_index_from_name(name)); }
389         //! Return a full description of the linked ValueNode given by the index
390         String get_description(int index = -1, bool show_exported_name = true)const;
391
392 protected:
393         //! Sets the type of the ValueNode
394         void set_type(ValueBase::Type t) { ValueNode::set_type(t); }
395
396         //! Virtual member to get the linked Value Node Handle
397         virtual ValueNode::LooseHandle get_link_vfunc(int i)const=0;
398
399         //! Wrapper for new operator, used by clone()
400         virtual LinkableValueNode* create_new()const=0;
401
402         //! Returns the cached times values for all the children (linked Value Nodes)
403         virtual void get_times_vfunc(Node::time_set &set) const;
404 }; // END of class LinkableValueNode
405
406 /*!     \class ValueNodeList
407 **      \brief A searchable value_node list container
408 **      \warning Do not confuse with ValueNode_DynamicList!
409 *
410 *  Used by Canvas class to access to the exported value nodes.
411 */
412 class ValueNodeList : public std::list<ValueNode::RHandle>
413 {
414         int placeholder_count_;
415 public:
416         ValueNodeList();
417
418         //! Finds the ValueNode in the list with the given \a name
419         /*!     \return If found, returns a handle to the ValueNode.
420         **              Otherwise, returns an empty handle.
421         */
422         ValueNode::Handle find(const String &name);
423
424         //! Finds the ValueNode in the list with the given \a name
425         /*!     \return If found, returns a handle to the ValueNode.
426         **              Otherwise, returns an empty handle.
427         */
428         ValueNode::ConstHandle find(const String &name)const;
429
430         //! Removes the \a value_node from the list
431         bool erase(ValueNode::Handle value_node);
432
433         //! \writeme
434         bool add(ValueNode::Handle value_node);
435
436         //! \writeme
437         bool count(const String &id)const;
438
439         //! Similar to find, but will create a placeholder value_node if it cannot be found.
440         ValueNode::Handle surefind(const String &name);
441
442         //! Removes any value_nodes with reference counts of 1.
443         void audit();
444
445         //! Placeholder Count
446         int placeholder_count()const { return placeholder_count_; }
447 };
448
449 ValueNode::LooseHandle find_value_node(const GUID& guid);
450
451 }; // END of namespace synfig
452
453 /* === E N D =============================================================== */
454
455 #endif