Before this fix, PasteCanvas' "time offset" values were being reflected in the positi...
[synfig.git] / synfig-core / trunk / src / synfig / node.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file node.h
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === S T A R T =========================================================== */
24
25 #ifndef __SYNFIG_PARENTNODE_H
26 #define __SYNFIG_PARENTNODE_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include <sigc++/signal.h>
31 #include <set>
32 #include "time.h"
33 #include "guid.h"
34 #include <ETL/handle>
35 #include "interpolation.h"
36 #include "mutex.h"
37
38 /* === M A C R O S ========================================================= */
39
40 // When a PasteCanvas layer has a non-zero 'time offset' parameter, should
41 // the waypoints shown for the canvas be adjusted?  This currently only
42 // partially works - see the TODO at the end of layer_pastecanvas.cpp
43 // #define ADJUST_WAYPOINTS_FOR_TIME_OFFSET
44
45 /* === T Y P E D E F S ===================================================== */
46
47 /* === C L A S S E S & S T R U C T S ======================================= */
48
49 namespace synfig {
50
51 class TimePoint
52 {
53         GUID guid;
54         Time time;
55         Interpolation before,after;
56 public:
57
58         TimePoint(const Time& x=Time::begin()):
59                 guid(0),
60                 time(x),
61                 before(INTERPOLATION_NIL),
62                 after(INTERPOLATION_NIL)
63         {
64         }
65
66         const GUID& get_guid()const { return guid; }
67         const Time& get_time()const { return time; }
68         Interpolation get_before()const { return before; }
69         Interpolation get_after()const { return after; }
70
71         void set_guid(const GUID& x) { guid=x; }
72         void set_time(const Time& x) { time=x; }
73         void set_before(Interpolation x) { before=x; }
74         void set_after(Interpolation x) { after=x; }
75
76         void absorb(const TimePoint& x);
77 }; // END of class TimePoint
78
79 inline TimePoint operator+(TimePoint lhs,const Time& rhs)
80         { lhs.set_time(lhs.get_time()+rhs); return lhs; }
81
82 inline TimePoint operator-(TimePoint lhs,const Time& rhs)
83         { lhs.set_time(lhs.get_time()-rhs); return lhs; }
84
85 inline bool operator<(const TimePoint& lhs,const TimePoint& rhs)
86         { return lhs.get_time()<rhs.get_time(); }
87
88 inline bool operator<(const TimePoint& lhs,const Time& rhs)
89         { return lhs.get_time()<rhs; }
90
91 inline bool operator<(const Time& lhs,const TimePoint& rhs)
92         { return lhs<rhs.get_time(); }
93
94 inline bool operator==(const TimePoint& lhs,const TimePoint& rhs)
95         { return lhs.get_time()==rhs.get_time(); }
96
97 inline bool operator!=(const TimePoint& lhs,const TimePoint& rhs)
98         { return lhs.get_time()!=rhs.get_time(); }
99
100 class TimePointSet : public std::set<TimePoint>
101 {
102 public:
103         iterator insert(const TimePoint& x);
104
105         template <typename ITER> void insert(ITER begin, ITER end)
106                 { for(;begin!=end;++begin) insert(*begin); }
107
108 }; // END of class TimePointSet
109
110 class Node : public etl::rshared_object
111 {
112         /*
113  --     ** -- T Y P E S -----------------------------------------------------------
114         */
115
116 public:
117
118         //! \writeme
119         typedef TimePointSet    time_set;
120
121         /*
122  --     ** -- D A T A -------------------------------------------------------------
123         */
124
125 private:
126
127         //! \writeme
128         GUID guid_;
129
130         //! cached time values for all the children
131         mutable time_set        times;
132
133         //! \writeme
134         mutable bool            bchanged;
135
136         //! \writeme
137         mutable int time_last_changed_;
138
139         //! \writeme
140         mutable RWLock rw_lock_;
141
142         //! \writeme
143         bool deleting_;
144
145 public:
146
147         //! \todo This should really be private
148         std::set<Node*>         parent_set;
149
150         /*
151  -- ** -- S I G N A L S -------------------------------------------------------
152         */
153
154 private:
155
156         sigc::signal<void> signal_changed_;
157
158         //!     GUID Changed
159         /*! \note The second parameter is the *OLD* guid! */
160         sigc::signal<void,GUID> signal_guid_changed_;
161
162         //!     Deleted
163         sigc::signal<void> signal_deleted_;
164
165         /*
166  -- ** -- S I G N A L   I N T E R F A C E -------------------------------------
167         */
168
169 public:
170
171         sigc::signal<void>& signal_deleted() { return signal_deleted_; }
172
173         sigc::signal<void>& signal_changed() { return signal_changed_; }
174
175         //!     GUID Changed
176         /*! \note The second parameter is the *OLD* guid! */
177         sigc::signal<void,GUID>& signal_guid_changed() { return signal_guid_changed_; }
178
179         /*
180  --     ** -- C O N S T R U C T O R S ---------------------------------------------
181         */
182
183 protected:
184
185         Node();
186
187         // This class cannot be copied -- use clone() if necessary
188 private:
189         Node(const Node &x);
190
191 public:
192         virtual ~Node();
193
194         /*
195  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
196         */
197
198 public:
199
200         void changed();
201
202         //! Gets the GUID for this value node
203         const GUID& get_guid()const;
204
205         //! Sets the GUID for this value node
206         void set_guid(const GUID& x);
207
208         int get_time_last_changed()const;
209
210         void add_child(Node*x);
211
212         void remove_child(Node*x);
213
214         int parent_count()const;
215
216         const time_set &get_times() const;
217
218         RWLock& get_rw_lock()const { return rw_lock_; }
219
220 protected:
221
222         void begin_delete();
223
224         /*
225  --     ** -- V I R T U A L   F U N C T I O N S -----------------------------------
226         */
227
228 protected:
229         virtual void on_changed();
230
231         virtual void on_guid_changed(GUID guid);
232
233         /*!     Function to be overloaded that fills
234         */
235         virtual void get_times_vfunc(time_set &set) const = 0;
236 };
237
238 synfig::Node* find_node(const synfig::GUID& guid);
239
240 template<typename T> etl::handle<T>
241 guid_cast(const synfig::GUID& guid)
242 {
243         return etl::handle<T>::cast_dynamic(synfig::find_node(guid));
244 }
245
246 typedef etl::handle<Node> NodeHandle;
247
248 }; // END of namespace synfig
249
250 /* === E N D =============================================================== */
251
252 #endif