1 /* === S Y N F I G ========================================================= */
3 ** \brief Header file for implementation of the "Placeholder" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === S T A R T =========================================================== */
26 #ifndef __SYNFIG_VALUENODE_H
27 #define __SYNFIG_VALUENODE_H
29 /* === H E A D E R S ======================================================= */
36 #include <ETL/stringf>
37 #include "exception.h"
39 #include <sigc++/signal.h>
47 /* === M A C R O S ========================================================= */
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__); \
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"), \
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()); \
67 signal_child_changed()(i); \
68 signal_value_changed()(); \
71 /* === T Y P E D E F S ===================================================== */
73 /* === C L A S S E S & S T R U C T S ======================================= */
78 class LinkableValueNode;
82 ** \brief Base class for all Value Nodes
84 class ValueNode : public synfig::Node
87 friend class LinkableValueNode;
90 -- ** -- T Y P E S -----------------------------------------------------------
95 typedef etl::handle<ValueNode> Handle;
97 typedef etl::loose_handle<ValueNode> LooseHandle;
99 typedef etl::handle<const ValueNode> ConstHandle;
101 typedef etl::rhandle<ValueNode> RHandle;
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();
109 -- ** -- D A T A -------------------------------------------------------------
113 //! The type of the Value Node
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">
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_;
125 -- ** -- S I G N A L S -------------------------------------------------------
130 //! ValueBase Changed
131 sigc::signal<void> signal_value_changed_;
133 //! Children Reordered
134 sigc::signal<void,int*> signal_children_reordered_;
137 sigc::signal<void,int> signal_child_changed_;
140 sigc::signal<void,int> signal_child_removed_;
143 sigc::signal<void,int> signal_child_inserted_;
146 sigc::signal<void> signal_id_changed_;
149 -- ** -- S I G N A L I N T E R F A C E -------------------------------------
154 //! ValueBase Changed
155 sigc::signal<void>& signal_value_changed() { return signal_value_changed_; }
157 //! Children Reordered
158 sigc::signal<void,int*>& signal_children_reordered() { return signal_children_reordered_; }
161 sigc::signal<void,int>& signal_child_changed() { return signal_child_changed_; }
164 sigc::signal<void,int>& signal_child_removed() { return signal_child_removed_; }
167 sigc::signal<void,int>& signal_child_inserted() { return signal_child_inserted_; }
170 sigc::signal<void>& signal_id_changed() { return signal_id_changed_; }
173 -- ** -- C O N S T R U C T O R S ---------------------------------------------
178 ValueNode(ValueBase::Type type=ValueBase::TYPE_NIL);
182 virtual ~ValueNode();
185 -- ** -- M E M B E R F U N C T I O N S -------------------------------------
190 //! Returns the value of the ValueNode at time \a t
191 virtual ValueBase operator()(Time /*t*/)const
192 { return ValueBase(); }
194 //! \internal Sets the id of the ValueNode
195 void set_id(const String &x);
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; }
202 //! Returns the name of the ValueNode type
203 virtual String get_name()const=0;
205 //! Returns the localized name of the ValueNode type
206 virtual String get_local_name()const=0;
208 //! Return a full description of the ValueNode and its parentage
209 virtual String get_description(bool show_exported_name = true)const;
212 //! Clones a Value Node
213 virtual ValueNode* clone(const GUID& deriv_guid=GUID())const=0;
215 //! Returns \true if the Value Node has an ID (has been exported)
216 bool is_exported()const { return !get_id().empty(); }
218 //! Returns the type of the ValueNode
219 ValueBase::Type get_type()const { return type; }
221 //! Returns a handle to the parent canvas, if it has one.
222 etl::loose_handle<Canvas> get_parent_canvas()const { return canvas_; }
224 //! Returns a handle to the parent canvas, if it has one.
225 etl::loose_handle<Canvas> get_root_canvas()const { return root_canvas_; }
227 //! Sets the parent canvas for the Value Node
228 void set_parent_canvas(etl::loose_handle<Canvas> x);
230 //! Sets the root canvas parent for the Value Node
231 void set_root_canvas(etl::loose_handle<Canvas> x);
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;
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);
244 //! Sets the type of the ValueNode
245 void set_type(ValueBase::Type t) { type=t; }
247 virtual void on_changed();
248 }; // END of class ValueNode
250 /*! \class PlaceholderValueNode
251 ** Seems to be a Place to hold a Value Node temporarly.
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
258 class PlaceholderValueNode : public ValueNode
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;
268 PlaceholderValueNode(ValueBase::Type type=ValueBase::TYPE_NIL);
272 virtual ValueBase operator()(Time t)const;
274 virtual String get_name()const;
276 virtual String get_local_name()const;
278 virtual ValueNode* clone(const GUID& deriv_guid=GUID())const;
280 static Handle create(ValueBase::Type type=ValueBase::TYPE_NIL);
283 virtual void get_times_vfunc(Node::time_set &/*set*/) const {}
284 }; // END of class PlaceholderValueNode
287 /*! \class LinkableValueNode
288 ** \brief Specialized Class of Value Nodes that has links to other
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.
295 class LinkableValueNode : public ValueNode
297 friend class ValueNode;
300 typedef etl::handle<LinkableValueNode> Handle;
302 typedef etl::loose_handle<LinkableValueNode> LooseHandle;
304 typedef etl::handle<const LinkableValueNode> ConstHandle;
306 typedef etl::rhandle<LinkableValueNode> RHandle;
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.
313 typedef LinkableValueNode* (*Factory)(const ValueBase&);
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
319 typedef bool (*CheckType)(ValueBase::Type);
325 CheckType check_type;
326 ReleaseVersion release_version; // which version of synfig introduced this valuenode type
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
336 typedef std::map<String,BookEntry> Book;
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);
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);
350 LinkableValueNode(ValueBase::Type type=ValueBase::TYPE_NIL):
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;
359 //! Frees all the subparameters of the Linkable Value Node.
360 //! Used by the derived classed destructors.
365 //! Returns the number of linked Value Nodes
366 virtual int link_count()const=0;
368 //! Returns the local name of the 'i' linked Value Node
369 virtual String link_local_name(int i)const=0;
371 //! Returns the name of the 'i' linked Value Node
372 virtual String link_name(int i)const=0;
374 //! Returns the child index Value Node based on the name
375 virtual int get_link_index_from_name(const String &name)const=0;
377 //! Clones a Value Node
378 virtual ValueNode* clone(const GUID& deriv_guid=GUID())const;
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); }
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;
393 //! Sets the type of the ValueNode
394 void set_type(ValueBase::Type t) { ValueNode::set_type(t); }
396 //! Virtual member to get the linked Value Node Handle
397 virtual ValueNode::LooseHandle get_link_vfunc(int i)const=0;
399 //! Wrapper for new operator, used by clone()
400 virtual LinkableValueNode* create_new()const=0;
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
406 /*! \class ValueNodeList
407 ** \brief A searchable value_node list container
408 ** \warning Do not confuse with ValueNode_DynamicList!
410 * Used by Canvas class to access to the exported value nodes.
412 class ValueNodeList : public std::list<ValueNode::RHandle>
414 int placeholder_count_;
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.
422 ValueNode::Handle find(const String &name);
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.
428 ValueNode::ConstHandle find(const String &name)const;
430 //! Removes the \a value_node from the list
431 bool erase(ValueNode::Handle value_node);
434 bool add(ValueNode::Handle value_node);
437 bool count(const String &id)const;
439 //! Similar to find, but will create a placeholder value_node if it cannot be found.
440 ValueNode::Handle surefind(const String &name);
442 //! Removes any value_nodes with reference counts of 1.
445 //! Placeholder Count
446 int placeholder_count()const { return placeholder_count_; }
449 ValueNode::LooseHandle find_value_node(const GUID& guid);
451 }; // END of namespace synfig
453 /* === E N D =============================================================== */