Typo: 'consistant' -> 'consistent'.
[synfig.git] / synfig-studio / trunk / src / synfigapp / timegather.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file timegather.cpp
3 **      \brief Time Gather File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2004 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 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "timegather.h"
33 #include "value_desc.h"
34
35 #include <synfig/layer_pastecanvas.h>
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace synfigapp;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 /* === E N T R Y P O I N T ================================================= */
55
56 //! Definitions for build a list of accurate valuenode references
57
58 void synfigapp::timepoints_ref::insert(synfig::ValueNode_Animated::Handle v, synfig::Waypoint w)
59 {
60         ValueBaseTimeInfo       vt;
61         vt.val = v;
62
63         waytracker::iterator i = waypointbiglist.find(vt);
64
65         if(i != waypointbiglist.end())
66         {
67                 i->waypoints.insert(w);
68         }else
69         {
70                 vt.waypoints.insert(w);
71                 waypointbiglist.insert(vt);
72         }
73 }
74
75 void synfigapp::timepoints_ref::insert(synfigapp::ValueDesc v, synfig::Activepoint a)
76 {
77         ActiveTimeInfo  vt;
78         vt.val = v;
79
80         acttracker::iterator i = actpointbiglist.find(vt);
81
82         if(i != actpointbiglist.end())
83         {
84                 i->activepoints.insert(a);
85                 /*{ //if it fails...
86                         synfig::info("!!!!For some reason it wasn't able to insert the activepoint in the list (%s,%.4lg)",
87                                                         a.state?"true":"false", (double)a.time);
88                 }*/
89         }else
90         {
91                 vt.activepoints.insert(a);
92                 actpointbiglist.insert(vt);
93                 //synfig::info("Insert new activept list for valdesc");
94         }
95 }
96
97 //recursion functions
98 void synfigapp::recurse_canvas(synfig::Canvas::Handle h, const std::set<Time> &tlist,
99                                                                 timepoints_ref &vals, synfig::Time time_offset)
100 {
101
102         //synfig::info("Canvas...\n Recurse through layers");
103         // iterate through the layers
104
105         synfig::Canvas::iterator i = h->begin(), end = h->end();
106
107         for(; i != end; ++i)
108         {
109                 const Node::time_set &tset = (*i)->get_times();
110                 if(check_intersect(tset.begin(),tset.end(),tlist.begin(),tlist.end(),time_offset))
111                 {
112                         recurse_layer(*i,tlist,vals,time_offset);
113                 }
114         }
115 }
116
117 void synfigapp::recurse_layer(synfig::Layer::Handle h, const std::set<Time> &tlist,
118                                                                 timepoints_ref &vals, synfig::Time time_offset)
119 {
120         // iterate through the layers
121         //check for special case of paste canvas
122         etl::handle<synfig::Layer_PasteCanvas> p = etl::handle<synfig::Layer_PasteCanvas>::cast_dynamic(h);
123
124         //synfig::info("Layer...");
125
126         if(p)
127         {
128                 //synfig::info("We are a paste canvas so go into that");
129                 //recurse into the canvas
130                 const synfig::Node::time_set &tset = p->get_sub_canvas()->get_times();
131                 synfig::Time subcanvas_time_offset(time_offset + p->get_time_offset());
132
133                 if(check_intersect(tset.begin(),tset.end(),tlist.begin(),tlist.end(),subcanvas_time_offset))
134                         recurse_canvas(p->get_sub_canvas(),tlist,vals,subcanvas_time_offset);
135         }
136
137         //check all the valuenodes regardless...
138         //synfig::info("Recurse all valuenodes");
139         synfig::Layer::DynamicParamList::const_iterator         i = h->dynamic_param_list().begin(),
140                                                                                                         end = h->dynamic_param_list().end();
141         for(; i != end; ++i)
142         {
143                 const synfig::Node::time_set &tset = i->second->get_times();
144
145                 if(check_intersect(tset.begin(),tset.end(),tlist.begin(),tlist.end(),time_offset))
146                 {
147                         recurse_valuedesc(ValueDesc(h,i->first),tlist,vals,time_offset);
148                 }
149         }
150 }
151
152 template < typename IT, typename CMP >
153 static bool sorted(IT i,IT end, const CMP &cmp = CMP())
154 {
155         if(i == end) return true;
156
157         for(IT last = i++; i != end; last = i++)
158         {
159                 if(!cmp(*last,*i))
160                         return false;
161         }
162
163         return true;
164 }
165
166 void synfigapp::recurse_valuedesc(synfigapp::ValueDesc h, const std::set<Time> &tlist,
167                                                                 timepoints_ref &vals, synfig::Time time_offset)
168 {
169         //special cases for Animated, DynamicList, and Linkable
170
171         //synfig::info("ValueBasenode... %p, %s", h.get_value_node().get(),typeid(*h.get_value_node()).name());
172
173
174         //animated case
175         {
176                 synfig::ValueNode_Animated::Handle p = synfig::ValueNode_Animated::Handle::cast_dynamic(h.get_value_node());
177
178                 if(p)
179                 {
180                         //loop through and determine which waypoint we will need to reference
181                         const synfig::WaypointList &w = p->waypoint_list();
182
183                         synfig::WaypointList::const_iterator i = w.begin(),
184                                                                                                 end = w.end();
185
186                         std::set<Time>::const_iterator          j = tlist.begin(),
187                                                                                                 jend = tlist.end();
188                         for(; i != end && j != jend;)
189                         {
190                                 //synfig::info("tpair t(%.3f) = %.3f", (float)*j, (float)(i->get_time()));
191
192                                 if((*j+time_offset).is_equal(i->get_time()))
193                                 {
194                                         vals.insert(p,*i);
195                                         ++i,++j;
196                                 }else if(*i < *j+time_offset)
197                                 {
198                                         ++i;
199                                 }else ++j;
200                         }
201                         return;
202                 }
203         }
204
205         //parent dynamiclist case - just for active points for that object...
206         if(h.parent_is_value_node())
207         {
208                 synfig::ValueNode_DynamicList::Handle p = synfig::ValueNode_DynamicList::Handle::cast_dynamic(h.get_parent_value_node());
209
210                 if(p)
211                 {
212                         int index = h.get_index();
213
214                         //check all the active points in each list...
215                         const synfig::ActivepointList &a = p->list[index].timing_info;
216
217                         //synfig::info("Our parent = dynamic list, searching in %d activepts",a.size());
218
219                         std::set<Time>::const_iterator                  i = tlist.begin(),
220                                                                                                         end = tlist.end();
221
222                         synfig::ActivepointList::const_iterator         j = a.begin(),
223                                                                                                         jend = a.end();
224
225                         for(; j != jend && i != end;)
226                         {
227                                 double it = *i+time_offset;
228                                 double jt = j->get_time();
229                                 double diff = (double)(it - jt);
230
231                                 //synfig::info("\ttpair match(%.4lg) - %.4lg (diff = %lg",it,jt,diff);
232
233                                 //
234                                 if(abs(diff) < (double)Time::epsilon())
235                                 {
236                                         //synfig::info("\tActivepoint to add being referenced (%x,%s,%.4lg)",
237                                         //                              (int)j->get_uid(),j->state?"true":"false", (double)j->time);
238                                         vals.insert(ValueDesc(p,index),*j);
239                                         ++i,++j;
240                                 }else if(it < jt)
241                                 {
242                                         ++i;
243                                         //synfig::info("\tIncrementing time");
244                                 }
245                                 else
246                                 {
247                                         ++j;
248                                         //synfig::info("\tIncrementing actpt");
249                                 }
250                         }
251                 }
252         }
253
254         //dynamiclist case - we must still make sure that we read from the list entries the time values
255         //                                              because just the linked valuenodes will not do that
256         {
257                 synfig::ValueNode_DynamicList::Handle p = synfig::ValueNode_DynamicList::Handle::cast_dynamic(h.get_value_node());
258
259                 if(p)
260                 {
261                         //synfig::info("Process dynamic list valuenode");
262                         int index = 0;
263
264                         std::vector<synfig::ValueNode_DynamicList::ListEntry>::const_iterator
265                                                         i = p->list.begin(),
266                                                         end = p->list.end();
267
268                         for(; i != end; ++i, ++index)
269                         {
270                                 const Node::time_set &tset = i->get_times();
271
272                                 if(check_intersect(tset.begin(),tset.end(),tlist.begin(),tlist.end(),time_offset))
273                                 {
274                                         recurse_valuedesc(ValueDesc(p,index),tlist,vals,time_offset);
275                                 }
276                         }
277                         return;
278                 }
279         }
280
281         //the linkable case...
282         {
283                 etl::handle<synfig::LinkableValueNode> p = etl::handle<synfig::LinkableValueNode>::cast_dynamic(h.get_value_node());
284
285                 if(p)
286                 {
287                         //synfig::info("Process Linkable ValueBasenode");
288                         int i = 0, size = p->link_count();
289
290                         for(; i < size; ++i)
291                         {
292                                 ValueNode::Handle v = p->get_link(i);
293                                 const Node::time_set &tset = v->get_times();
294
295                                 if(check_intersect(tset.begin(),tset.end(),tlist.begin(),tlist.end(),time_offset))
296                                 {
297                                         recurse_valuedesc(ValueDesc(p,i),tlist,vals,time_offset);
298                                 }
299                         }
300                 }
301         }
302 }