Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / duckmatic.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file duckmatic.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 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 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32 #include <fstream>
33 #include <iostream>
34 #include <algorithm>
35
36 #include <ETL/hermite>
37
38 #include "duckmatic.h"
39 #include <synfigapp/value_desc.h>
40 #include <synfig/general.h>
41 #include <synfig/paramdesc.h>
42 #include <synfig/valuenode_timedswap.h>
43 #include <synfig/valuenode_animated.h>
44 #include <synfig/valuenode_composite.h>
45 #include <synfig/valuenode_scale.h>
46 #include <synfig/valuenode_bline.h>
47
48 #include <synfig/curve_helper.h>
49
50 #include <sigc++/retype_return.h>
51 #include <sigc++/retype.h>
52 #include <sigc++/hide.h>
53 #include <sigc++/bind.h>
54
55 #include "canvasview.h"
56
57 #include "onemoment.h"
58
59 #endif
60
61 /* === U S I N G =========================================================== */
62
63 using namespace std;
64 using namespace etl;
65 using namespace synfig;
66 using namespace studio;
67
68 /* === M A C R O S ========================================================= */
69
70 /* 0.33333333333333333 makes for nice short tangent handles,
71    1.0 makes them draw as their real length */
72 #define TANGENT_HANDLE_SCALE 0.33333333333333333
73
74 /* leave this alone or the bezier won't lie on top of the bline */
75 #define TANGENT_BEZIER_SCALE 0.33333333333333333
76
77 /* === G L O B A L S ======================================================= */
78
79 /* === P R O C E D U R E S ================================================= */
80
81 /* === M E T H O D S ======================================================= */
82
83 /* === E N T R Y P O I N T ================================================= */
84
85 Duckmatic::Duckmatic():
86         type_mask(Duck::TYPE_ALL-Duck::TYPE_WIDTH),
87         grid_snap(false),
88         guide_snap(false),
89         grid_size(1.0/4.0,1.0/4.0),
90         show_persistant_strokes(true)
91 {
92         axis_lock=false;
93         drag_offset_=Point(0,0);
94         clear_duck_dragger();
95 }
96
97 Duckmatic::~Duckmatic()
98 {
99         clear_ducks();
100         //synfig::info("Duckmatic::~Duckmatic(): Deleted. Duck Count=%d",Duck::duck_count);
101 }
102
103 void
104 Duckmatic::clear_ducks()
105 {
106         duck_data_share_map.clear();
107         duck_map.clear();
108
109         //duck_list_.clear();
110         bezier_list_.clear();
111         stroke_list_.clear();
112
113         if(show_persistant_strokes)
114                 stroke_list_=persistant_stroke_list_;
115 }
116
117 //! Returns \a true if the given duck is currently selected
118 bool
119 Duckmatic::duck_is_selected(const etl::handle<Duck> &duck)const
120 {
121         return duck && selected_ducks.count(duck->get_guid());
122 }
123
124 void
125 Duckmatic::set_grid_size(const synfig::Vector &s)
126 {
127         if(grid_size!=s)
128         {
129                 grid_size=s;
130                 signal_grid_changed();
131         }
132 }
133
134 void
135 Duckmatic::set_grid_snap(bool x)
136 {
137         if(grid_snap!=x)
138         {
139                 grid_snap=x;
140                 signal_grid_changed();
141         }
142 }
143
144 void
145 Duckmatic::set_guide_snap(bool x)
146 {
147         if(guide_snap!=x)
148         {
149                 guide_snap=x;
150                 signal_grid_changed();
151         }
152 }
153
154
155
156 Duckmatic::GuideList::iterator
157 Duckmatic::find_guide_x(synfig::Point pos, float radius)
158 {
159         GuideList::iterator iter,best(guide_list_x_.end());
160         float dist(radius);
161         for(iter=guide_list_x_.begin();iter!=guide_list_x_.end();++iter)
162         {
163                 float amount(abs(*iter-pos[0]));
164                 if(amount<dist)
165                 {
166                         dist=amount;
167                         best=iter;
168                 }
169         }
170         return best;
171 }
172
173 Duckmatic::GuideList::iterator
174 Duckmatic::find_guide_y(synfig::Point pos, float radius)
175 {
176         GuideList::iterator iter,best(guide_list_y_.end());
177         float dist(radius);
178         for(iter=guide_list_y_.begin();iter!=guide_list_y_.end();++iter)
179         {
180                 float amount(abs(*iter-pos[1]));
181                 if(amount<=dist)
182                 {
183                         dist=amount;
184                         best=iter;
185                 }
186         }
187         return best;
188 }
189
190 void
191 Duckmatic::clear_selected_ducks()
192 {
193         selected_ducks.clear();
194         signal_duck_selection_changed_();
195 }
196
197 etl::handle<Duckmatic::Duck>
198 Duckmatic::get_selected_duck()const
199 {
200         if(selected_ducks.empty() || duck_map.empty())
201                 return 0;
202         return duck_map.find(*selected_ducks.begin())->second;
203 }
204
205 void
206 Duckmatic::refresh_selected_ducks()
207 {
208 /*
209         std::set<etl::handle<Duck> >::iterator iter;
210         std::set<etl::handle<Duck> > new_set;
211         if(duck_list().empty())
212         {
213                 selected_duck_list.clear();
214                 signal_duck_selection_changed_();
215                 return;
216         }
217
218         for(iter=selected_duck_list.begin();iter!=selected_duck_list.end();++iter)
219         {
220                 etl::handle<Duck> similar(find_similar_duck(*iter));
221                 if(similar)
222                 {
223                         new_set.insert(similar);
224                 }
225         }
226         selected_duck_list=new_set;
227 */
228         GUIDSet old_set(selected_ducks);
229         GUIDSet::const_iterator iter;
230
231         for(iter=old_set.begin();iter!=old_set.end();++iter)
232         {
233                 if(duck_map.count(*iter)==0)
234                         selected_ducks.erase(*iter);
235         }
236
237         signal_duck_selection_changed_();
238 }
239
240 bool
241 Duckmatic::is_duck_group_selectable(const etl::handle<Duck>& x)const
242 {
243         const Type type(get_type_mask());
244
245         if(( x->get_type() && (!(type & x->get_type())) ) )
246         {
247                 return false;
248         }
249         if(x->get_value_desc().parent_is_layer_param() && type & Duck::TYPE_POSITION)
250         {
251                 Layer::Handle layer(x->get_value_desc().get_layer());
252                 String layer_name(layer->get_name());
253                 if(
254                         layer_name=="outline" ||
255                         layer_name=="region" ||
256                         layer_name=="polygon" ||
257                         layer_name=="curve_gradient"
258                 )
259                         return false;
260                 if((layer_name=="PasteCanvas"|| layer_name=="paste_canvas") &&
261                    !layer->get_param("children_lock").get(bool()))
262                         return false;
263         }
264         return true;
265 }
266
267 void
268 Duckmatic::select_all_ducks()
269 {
270         DuckMap::const_iterator iter;
271         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
272                 if(is_duck_group_selectable(iter->second))
273                         select_duck(iter->second);
274 }
275
276 void
277 Duckmatic::toggle_select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
278 {
279         Vector vmin, vmax;
280         vmin[0]=std::min(tl[0],br[0]);
281         vmin[1]=std::min(tl[1],br[1]);
282         vmax[0]=std::max(tl[0],br[0]);
283         vmax[1]=std::max(tl[1],br[1]);
284
285         DuckMap::const_iterator iter;
286         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
287         {
288                 Point p(iter->second->get_trans_point());
289                 if(p[0]<=vmax[0] && p[0]>=vmin[0] && p[1]<=vmax[1] && p[1]>=vmin[1] &&
290                    is_duck_group_selectable(iter->second))
291                         toggle_select_duck(iter->second);
292         }
293 }
294
295 void
296 Duckmatic::select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
297 {
298         Vector vmin, vmax;
299         vmin[0]=std::min(tl[0],br[0]);
300         vmin[1]=std::min(tl[1],br[1]);
301         vmax[0]=std::max(tl[0],br[0]);
302         vmax[1]=std::max(tl[1],br[1]);
303
304 //      Type type(get_type_mask());
305
306         DuckMap::const_iterator iter;
307         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
308         {
309                 Point p(iter->second->get_trans_point());
310                 if(p[0]<=vmax[0] && p[0]>=vmin[0] && p[1]<=vmax[1] && p[1]>=vmin[1])
311                 {
312                         if(is_duck_group_selectable(iter->second))
313                                 select_duck(iter->second);
314                 }
315         }
316 }
317
318 int
319 Duckmatic::count_selected_ducks()const
320 {
321         return selected_ducks.size();
322 }
323
324 void
325 Duckmatic::select_duck(const etl::handle<Duck> &duck)
326 {
327         if(duck)
328         {
329                 selected_ducks.insert(duck->get_guid());
330                 signal_duck_selection_changed_();
331         }
332 }
333
334 DuckList
335 Duckmatic::get_selected_ducks()const
336 {
337         DuckList ret;
338         GUIDSet::const_iterator iter;
339         const Type type(get_type_mask());
340
341         for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
342         {
343                 const DuckMap::const_iterator d_iter(duck_map.find(*iter));
344
345                 if(d_iter==duck_map.end())
346                         continue;
347
348                 if(( d_iter->second->get_type() && (!(type & d_iter->second->get_type())) ) )
349                         continue;
350
351                 ret.push_back(d_iter->second);
352         }
353         return ret;
354 }
355
356 DuckList
357 Duckmatic::get_duck_list()const
358 {
359         DuckList ret;
360         DuckMap::const_iterator iter;
361         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
362                 ret.push_back(iter->second);
363         return ret;
364 }
365
366 void
367 Duckmatic::unselect_duck(const etl::handle<Duck> &duck)
368 {
369         if(duck && selected_ducks.count(duck->get_guid()))
370         {
371                 selected_ducks.erase(duck->get_guid());
372                 signal_duck_selection_changed_();
373         }
374 }
375
376 void
377 Duckmatic::toggle_select_duck(const etl::handle<Duck> &duck)
378 {
379         if(duck_is_selected(duck))
380                 unselect_duck(duck);
381         else
382                 select_duck(duck);
383 }
384
385
386 void
387 Duckmatic::translate_selected_ducks(const synfig::Vector& vector)
388 {
389         if(duck_dragger_)
390                 duck_dragger_->duck_drag(this,vector);
391 }
392
393 void
394 Duckmatic::start_duck_drag(const synfig::Vector& offset)
395 {
396         if(duck_dragger_)
397                 duck_dragger_->begin_duck_drag(this,offset);
398
399         //drag_offset_=offset;
400         drag_offset_=find_duck(offset)->get_trans_point();
401 }
402
403 bool
404 Duckmatic::end_duck_drag()
405 {
406         if(duck_dragger_)
407                 return duck_dragger_->end_duck_drag(this);
408         return false;
409 }
410
411 Point
412 Duckmatic::snap_point_to_grid(const Point& x, float radius)const
413 {
414         Point ret(x);
415
416         GuideList::const_iterator guide_x,guide_y;
417         bool has_guide_x(false), has_guide_y(false);
418
419         guide_x=find_guide_x(ret,radius);
420         if(guide_x!=guide_list_x_.end())
421                 has_guide_x=true;
422
423         guide_y=find_guide_y(ret,radius);
424         if(guide_y!=guide_list_y_.end())
425                 has_guide_y=true;
426
427         if(get_grid_snap())
428         {
429                 Point snap(
430                         floor(ret[0]/get_grid_size()[0]+0.5)*get_grid_size()[0],
431                         floor(ret[1]/get_grid_size()[1]+0.5)*get_grid_size()[1]
432                 );
433
434                 if(abs(snap[0]-ret[0])<=radius && (!has_guide_x || abs(snap[0]-ret[0])<=abs(*guide_x-ret[0])))
435                         ret[0]=snap[0],has_guide_x=false;
436                 if(abs(snap[1]-ret[1])<=radius && (!has_guide_y || abs(snap[1]-ret[1])<=abs(*guide_y-ret[1])))
437                         ret[1]=snap[1],has_guide_y=false;
438         }
439
440         if(guide_snap)
441         {
442                 if(has_guide_x)
443                         ret[0]=*guide_x;
444                 if(has_guide_y)
445                         ret[1]=*guide_y;
446         }
447
448         if(axis_lock)
449         {
450                 ret-=drag_offset_;
451                 if(abs(ret[0])<abs(ret[1]))
452                         ret[0]=0;
453                 else
454                         ret[1]=0;
455                 ret+=drag_offset_;
456         }
457
458         return ret;
459 }
460
461 void
462 DuckDrag_Translate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
463 {
464         last_translate_=Vector(0,0);
465         {
466                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
467
468                 snap=Vector(0,0);
469         }
470
471         const DuckList selected_ducks(duckmatic->get_selected_ducks());
472         DuckList::const_iterator iter;
473
474         positions.clear();
475         for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
476         {
477                 Point p((*iter)->get_trans_point());
478                 positions.push_back(p);
479         }
480 }
481
482 bool
483 DuckDrag_Translate::end_duck_drag(Duckmatic* duckmatic)
484 {
485         if(last_translate_.mag()>0.0001)
486         {
487                 duckmatic->signal_edited_selected_ducks();
488                 return true;
489         }
490         else
491         {
492                 duckmatic->signal_user_click_selected_ducks(0);
493                 return false;
494         }
495 }
496
497 void
498 DuckDrag_Translate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
499 {
500         const DuckList selected_ducks(duckmatic->get_selected_ducks());
501         DuckList::const_iterator iter;
502
503         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_);
504         int i;
505
506         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
507         {
508                 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
509                 (*iter)->set_trans_point(positions[i]+vect);
510         }
511         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
512         {
513                 if((*iter)->get_type()==Duck::TYPE_VERTEX||(*iter)->get_type()==Duck::TYPE_POSITION)continue;
514                 (*iter)->set_trans_point(positions[i]+vect);
515         }
516         last_translate_=vect;
517 }
518
519
520
521
522
523
524
525
526
527
528 void
529 Duckmatic::signal_edited_selected_ducks()
530 {
531         const DuckList ducks(get_selected_ducks());
532         DuckList::const_iterator iter;
533
534         synfig::GUIDSet old_set(selected_ducks);
535
536         // If we have more than 20 things to move, then display
537         // something to explain that it may take a moment
538         smart_ptr<OneMoment> wait; if(ducks.size()>20)wait.spawn();
539
540         // Go ahead and call everyone's signals
541         for(iter=ducks.begin();iter!=ducks.end();++iter)
542         {
543                 if(!(*iter)->signal_edited()((*iter)->get_point()))
544                 {
545                         selected_ducks=old_set;
546                         throw String("Bad edit");
547                 }
548         }
549         selected_ducks=old_set;
550 }
551
552 void
553 Duckmatic::signal_user_click_selected_ducks(int button)
554 {
555         const DuckList ducks(get_selected_ducks());
556         DuckList::const_iterator iter;
557
558         for(iter=ducks.begin();iter!=ducks.end();++iter)
559         {
560                 (*iter)->signal_user_click(button)();
561         }
562 }
563
564
565
566
567
568 void
569 Duckmatic::add_duck(const etl::handle<Duck> &duck)
570 {
571         //if(!duck_map.count(duck->get_guid()))
572         {
573                 if(duck_data_share_map.count(duck->get_data_guid()))
574                 {
575                         duck->set_shared_point(duck_data_share_map[duck->get_data_guid()]);
576                 }
577                 else
578                 {
579                         etl::smart_ptr<synfig::Point> point(new Point(duck->get_point()));
580                         duck->set_shared_point(point);
581                         duck_data_share_map[duck->get_data_guid()]=point;
582                 }
583
584                 duck_map.insert(duck);
585         }
586
587         last_duck_guid=duck->get_guid();
588 }
589
590 void
591 Duckmatic::add_bezier(const etl::handle<Bezier> &bezier)
592 {
593         bezier_list_.push_back(bezier);
594 }
595
596 void
597 Duckmatic::add_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
598 {
599         assert(stroke_point_list);
600
601         std::list<etl::handle<Stroke> >::iterator iter;
602
603         for(iter=stroke_list_.begin();iter!=stroke_list_.end();++iter)
604         {
605                 if((*iter)->stroke_data==stroke_point_list)
606                         return;
607         }
608
609         etl::handle<Stroke> stroke(new Stroke());
610
611         stroke->stroke_data=stroke_point_list;
612         stroke->color=color;
613
614         stroke_list_.push_back(stroke);
615 }
616
617 void
618 Duckmatic::add_persistant_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
619 {
620         add_stroke(stroke_point_list,color);
621         persistant_stroke_list_.push_back(stroke_list_.back());
622 }
623
624 void
625 Duckmatic::clear_persistant_strokes()
626 {
627         persistant_stroke_list_.clear();
628 }
629
630 void
631 Duckmatic::set_show_persistant_strokes(bool x)
632 {
633         if(x!=show_persistant_strokes)
634         {
635                 show_persistant_strokes=x;
636                 if(x)
637                         stroke_list_=persistant_stroke_list_;
638                 else
639                         stroke_list_.clear();
640         }
641 }
642
643 void
644 Duckmatic::erase_duck(const etl::handle<Duck> &duck)
645 {
646         duck_map.erase(duck->get_guid());
647 }
648
649 etl::handle<Duckmatic::Duck>
650 Duckmatic::find_similar_duck(etl::handle<Duck> duck)
651 {
652         DuckMap::const_iterator iter(duck_map.find(duck->get_guid()));
653         if(iter!=duck_map.end())
654                 return iter->second;
655         return 0;
656
657 /*      std::list<handle<Duck> >::reverse_iterator iter;
658
659         for(iter=duck_list_.rbegin();iter!=duck_list_.rend();++iter)
660         {
661                 if(*iter!=duck && **iter==*duck)
662                 {
663                         //synfig::info("Found similar duck! (iter:%08x vs. duck:%08x)",iter->get(), duck.get());
664                         return *iter;
665                 }
666         }
667         return 0;
668 */
669 }
670
671 etl::handle<Duckmatic::Duck>
672 Duckmatic::add_similar_duck(etl::handle<Duck> duck)
673 {
674         etl::handle<Duck> similar(find_similar_duck(duck));
675         if(!similar)
676         {
677                 add_duck(duck);
678                 return duck;
679         }
680         return similar;
681 }
682
683 void
684 Duckmatic::erase_bezier(const etl::handle<Bezier> &bezier)
685 {
686         std::list<handle<Bezier> >::iterator iter;
687
688         for(iter=bezier_list_.begin();iter!=bezier_list_.end();++iter)
689         {
690                 if(*iter==bezier)
691                 {
692                         bezier_list_.erase(iter);
693                         return;
694                 }
695         }
696         synfig::warning("Unable to find bezier to erase!");
697 }
698
699 etl::handle<Duckmatic::Duck>
700 Duckmatic::last_duck()const
701 {
702         DuckMap::const_iterator iter(duck_map.find(last_duck_guid));
703         if(iter!=duck_map.end())
704                 return iter->second;
705         return 0;
706 }
707
708 etl::handle<Duckmatic::Bezier>
709 Duckmatic::last_bezier()const
710 {
711         return bezier_list_.back();
712 }
713
714
715 etl::handle<Duckmatic::Duck>
716 Duckmatic::find_duck(synfig::Point point, synfig::Real radius, Duck::Type type)
717 {
718         if(radius==0)radius=10000000;
719
720         if(type==Duck::TYPE_DEFAULT)
721                 type=get_type_mask();
722
723         Real closest(10000000);
724         etl::handle<Duck> ret;
725
726         DuckMap::const_iterator iter;
727
728         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
729         {
730                 const Duck::Handle& duck(iter->second);
731
732                 if(!duck->get_editable())
733                         continue;
734                 Real dist((duck->get_trans_point()-point).mag_squared());
735
736                 if(duck->get_type()&Duck::TYPE_VERTEX)
737                         dist*=1.0001;
738
739                 if(dist<=closest && !( duck->get_type() && (!(type & duck->get_type())) ) )
740                 {
741                         {
742                                 closest=dist;
743                                 ret=duck;
744                         }
745                 }
746         }
747
748         if(radius==0 || closest<radius*radius)
749                 return ret;
750
751         return 0;
752 }
753
754 etl::handle<Duckmatic::Bezier>
755 Duckmatic::find_bezier(synfig::Point point, synfig::Real radius,float* location)
756 {
757         return find_bezier(point,radius,radius,location);
758 }
759
760 etl::handle<Duckmatic::Bezier>
761 Duckmatic::find_bezier(synfig::Point pos, synfig::Real scale, synfig::Real radius, float* location)
762 {
763         if(radius==0)radius=10000000;
764         Real closest(10000000);
765         etl::handle<Bezier> ret;
766
767         bezier<Point>   curve;
768
769         Real    d,step;
770         float   time = 0;
771         float   best_time = 0;
772
773         for(std::list<handle<Bezier> >::const_iterator iter=bezier_list().begin();iter!=bezier_list().end();++iter)
774         {
775                 curve[0] = (*iter)->p1->get_trans_point();
776                 curve[1] = (*iter)->c1->get_trans_point();
777                 curve[2] = (*iter)->c2->get_trans_point();
778                 curve[3] = (*iter)->p2->get_trans_point();
779                 curve.sync();
780
781 #if 0
782                 // I don't know why this doesn't work
783                 time=curve.find_closest(pos,6);
784                 d=((curve(time)-pos).mag_squared());
785
786 #else
787                 //set the step size based on the size of the picture
788                 d = (curve[1] - curve[0]).mag() + (curve[2]-curve[1]).mag()     + (curve[3]-curve[2]).mag();
789
790                 step = d/(2*scale); //want to make the distance between lines happy
791
792                 step = max(step,0.01); //100 samples should be plenty
793                 step = min(step,0.1); //10 is minimum
794
795                 d = find_closest(curve,pos,step,&closest,&time);
796 #endif
797
798                 if(d < closest)
799                 {
800                         closest = d;
801                         ret = *iter;
802                         best_time=time;
803                 }
804         }
805
806         if(closest < radius*radius)
807         {
808                 if(location)
809                         *location = best_time;  // We need to square-root this because we were dealing with squared distances
810
811                 return ret;
812         }
813
814         return 0;
815 }
816
817
818
819 bool
820 Duckmatic::save_sketch(const synfig::String& filename)const
821 {
822         std::ofstream file(filename.c_str());
823
824         if(!file)return false;
825
826         file<<"SKETCH"<<endl;
827
828         std::list<etl::handle<Stroke> >::const_iterator iter;
829
830         for(iter=persistant_stroke_list_.begin();iter!=persistant_stroke_list_.end();++iter)
831         {
832                 file<<"C "
833                         <<(*iter)->color.get_r()<<' '
834                         <<(*iter)->color.get_g()<<' '
835                         <<(*iter)->color.get_b()
836                 <<endl;
837                 std::list<synfig::Point>::const_iterator viter;
838                 for(viter=(*iter)->stroke_data->begin();viter!=(*iter)->stroke_data->end();++viter)
839                 {
840                         file<<"V "
841                                 <<(*viter)[0]<<' '
842                                 <<(*viter)[1]
843                         <<endl;
844                 }
845         }
846         if(!file)return false;
847         sketch_filename_=filename;
848         signal_sketch_saved_();
849         return true;
850 }
851
852 bool
853 Duckmatic::load_sketch(const synfig::String& filename)
854 {
855         std::ifstream file(filename.c_str());
856
857         if(!file)
858                 return false;
859
860         std::string line;
861         getline(file,line);
862
863         if(line!="SKETCH")
864         {
865                 synfig::error("Not a sketch");
866                 return false;
867         }
868
869
870         etl::smart_ptr<std::list<synfig::Point> > stroke_data;
871
872         while(file)
873         {
874                 getline(file,line);
875
876                 if(line.empty())
877                         continue;
878
879                 switch(line[0])
880                 {
881                 case 'C':
882                 case 'c':
883                         {
884                                 stroke_data.spawn();
885                                 float r,g,b;
886                                 if(!strscanf(line,"C %f %f %f",&r, &g, &b))
887                                 {
888                                         synfig::warning("Bad color line \"%s\"",line.c_str());
889                                         r=0;g=0;b=0;
890                                 }
891                                 add_persistant_stroke(stroke_data, synfig::Color(r,g,b));
892                         }
893                         break;
894                 case 'V':
895                 case 'v':
896                         if(!stroke_data)
897                         {
898                                 stroke_data.spawn();
899                                 add_persistant_stroke(stroke_data, synfig::Color(0,0,0));
900                         }
901                         float x,y;
902                         if(!strscanf(line,"V %f %f",&x, &y))
903                                 synfig::warning("Bad vertex \"%s\"",line.c_str());
904                         else
905                                 stroke_data->push_back(synfig::Vector(x,y));
906                         break;
907                 default:
908                         synfig::warning("Unexpected sketch token '%c'",line[0]);
909                         break;
910                 }
911         }
912
913         sketch_filename_=filename;
914         return true;
915 }
916
917
918
919
920
921
922
923
924 Duckmatic::Push::Push(Duckmatic *duckmatic_):
925         duckmatic_(duckmatic_)
926 {
927         duck_map=duckmatic_->duck_map;
928         bezier_list_=duckmatic_->bezier_list_;
929         duck_data_share_map=duckmatic_->duck_data_share_map;
930         stroke_list_=duckmatic_->stroke_list_;
931         duck_dragger_=duckmatic_->duck_dragger_;
932         needs_restore=true;
933 }
934
935 Duckmatic::Push::~Push()
936 {
937         if(needs_restore)
938                 restore();
939 }
940
941 void
942 Duckmatic::Push::restore()
943 {
944         duckmatic_->duck_map=duck_map;
945         duckmatic_->bezier_list_=bezier_list_;
946         duckmatic_->duck_data_share_map=duck_data_share_map;
947         duckmatic_->stroke_list_=stroke_list_;
948         duckmatic_->duck_dragger_=duck_dragger_;
949         needs_restore=false;
950 }
951
952
953
954
955
956
957
958
959
960 inline String guid_string(const synfigapp::ValueDesc& x)
961 {
962         if(x.parent_is_layer_param())
963                 return strprintf("%s",x.get_layer()->get_guid().get_string().c_str())+x.get_param_name();
964         //if(x.is_value_node())
965                 return strprintf("%s",x.get_value_node()->get_guid().get_string().c_str());
966 }
967
968 inline GUID calc_duck_guid(const synfigapp::ValueDesc& x,const synfig::TransformStack& transform_stack)
969 {
970         GUID ret(0);
971
972         if(x.parent_is_layer_param())
973         {
974                 ret=x.get_layer()->get_guid()^GUID::hasher(x.get_param_name());
975         }
976         else
977         {
978                 ret=x.get_value_node()->get_guid();
979         }
980
981         ret^=transform_stack.get_guid();
982         return ret;
983 }
984
985 /*
986 Duck::Handle
987 Duckmatic::create_duck_from(const synfigapp::ValueDesc& value_desc,etl::handle<CanvasView> canvas_view, const synfig::TransformStack& transform_stack, int modifier, synfig::ParamDesc *param_desc)
988 {
989         GUID duck_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(modifier));
990         etl::handle<Duck> duck=new Duck();
991
992         return duck;
993 }
994 */
995
996 bool
997 Duckmatic::add_to_ducks(const synfigapp::ValueDesc& value_desc,etl::handle<CanvasView> canvas_view, const synfig::TransformStack& transform_stack, synfig::ParamDesc *param_desc, int multiple)
998 {
999         ValueBase::Type type=value_desc.get_value_type();
1000 #define REAL_COOKIE             reinterpret_cast<synfig::ParamDesc*>(28)
1001         switch(type)
1002         {
1003         case ValueBase::TYPE_REAL:
1004
1005                 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
1006                 {
1007                         etl::handle<Duck> duck=new Duck();
1008                         duck->set_transform_stack(transform_stack);
1009                         duck->set_radius(true);
1010                         duck->set_type(Duck::TYPE_RADIUS);
1011
1012                         duck->set_point(Point(value_desc.get_value(get_time()).get(Real()),0));
1013                         duck->set_name(guid_string(value_desc));
1014                         if(value_desc.is_value_node())
1015                         {
1016                                 // If the ValueNode can be directly manipulated,
1017                                 // then set it as so.
1018                                 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1019                         }
1020                         else
1021                         {
1022                                 duck->set_editable(true);
1023                         }
1024
1025                         if(param_desc && param_desc!=REAL_COOKIE)
1026                         {
1027                                 if(!param_desc->get_origin().empty())
1028                                 {
1029                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1030                                         /*
1031                                         duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1032                                         */
1033                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1034                                         duck->set_origin(last_duck());
1035                                 }
1036                                 duck->set_scalar(param_desc->get_scalar());
1037                         }
1038
1039                         duck->signal_edited().clear();
1040                         duck->signal_edited().connect(
1041                                 sigc::bind(
1042                                         sigc::mem_fun(
1043                                                 *canvas_view,
1044                                                 &studio::CanvasView::on_duck_changed
1045                                         ),
1046                                         value_desc
1047                                 )
1048                         );
1049                         duck->set_value_desc(value_desc);
1050
1051                         duck->signal_user_click(2).connect(
1052                                 sigc::bind(
1053                                         sigc::bind(
1054                                                 sigc::mem_fun(
1055                                                         *canvas_view,
1056                                                         &studio::CanvasView::popup_param_menu
1057                                                 ),
1058                                                 0.0f
1059                                         ),
1060                                         value_desc
1061                                 )
1062                         );
1063
1064                         duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1065
1066                         add_duck(duck);
1067
1068                         return true;
1069                 }
1070                 break;
1071
1072         case ValueBase::TYPE_ANGLE:
1073
1074                 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
1075                 {
1076                         etl::handle<Duck> duck=new Duck();
1077                         duck->set_type(Duck::TYPE_ANGLE);
1078                         duck->set_transform_stack(transform_stack);
1079                         synfig::Angle angle;
1080
1081                         angle=value_desc.get_value(get_time()).get(Angle());
1082                         duck->set_point(Point(Angle::cos(angle).get(),Angle::sin(angle).get()));
1083                         duck->set_name(guid_string(value_desc));
1084                         if(value_desc.is_value_node())
1085                         {
1086                                 ValueNode::Handle value_node=value_desc.get_value_node();
1087                                 //duck->set_name(strprintf("%x",value_node.get()));
1088
1089                                 // If the ValueNode can be directly manipulated,
1090                                 // then set it as so.
1091                                 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1092                         }
1093                         else
1094                         {
1095                                 //angle=(value_desc.get_value().get(Angle()));
1096                                 //duck->set_point(Point(Angle::cos(angle).get(),Angle::sin(angle).get()));
1097                                 //duck->set_name(strprintf("%x",value_desc.get_layer().get())+value_desc.get_param_name());
1098                                 duck->set_editable(true);
1099                         }
1100
1101                         if(param_desc && param_desc!=REAL_COOKIE)
1102                         {
1103                                 if(!param_desc->get_origin().empty())
1104                                 {
1105                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1106                                         /*
1107                                         duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1108                                         */
1109                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1110                                         duck->set_origin(last_duck());
1111                                 }
1112                                 duck->set_scalar(param_desc->get_scalar());
1113                         }
1114
1115                         duck->signal_edited().clear();
1116                         duck->signal_edited().connect(
1117                                 sigc::bind(
1118                                         sigc::mem_fun(
1119                                                 *canvas_view,
1120                                                 &studio::CanvasView::on_duck_changed
1121                                         ),
1122                                         value_desc
1123                                 )
1124                         );
1125                         duck->set_value_desc(value_desc);
1126
1127                         duck->signal_user_click(2).connect(
1128                                 sigc::bind(
1129                                         sigc::bind(
1130                                                 sigc::mem_fun(
1131                                                         *canvas_view,
1132                                                         &studio::CanvasView::popup_param_menu
1133                                                 ),
1134                                                 0.0f
1135                                         ),
1136                                         value_desc
1137                                 )
1138                         );
1139                         duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1140
1141                         add_duck(duck);
1142
1143                         return true;
1144                 }
1145                 break;
1146
1147         case ValueBase::TYPE_VECTOR:
1148                 {
1149                         etl::handle<Duck> duck=new Duck();
1150                         duck->set_transform_stack(transform_stack);
1151
1152                         duck->set_point(value_desc.get_value(get_time()).get(Point()));
1153                         duck->set_name(guid_string(value_desc));
1154                         if(value_desc.is_value_node())
1155                         {
1156                                 //duck->set_name(strprintf("%x",value_desc.get_value_node().get()));
1157
1158                                 // If the ValueNode can be directly manipulated,
1159                                 // then set it as so.
1160                                 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1161                         }
1162                         else
1163                         {
1164                                 //duck->set_point(value_desc.get_value().get(Point()));
1165                                 //duck->set_name(strprintf("%x",value_desc.get_layer().get())+value_desc.get_param_name());
1166                                 duck->set_editable(true);
1167                         }
1168
1169                         // If we were passed a parameter description
1170                         if(param_desc)
1171                         {
1172                                 if(!param_desc->get_connect().empty())
1173                                 {
1174                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_connect());
1175                                         Duck::Handle connect_duck;
1176                                         if(duck_map.find(calc_duck_guid(value_desc_origin,transform_stack)^GUID::hasher(0))!=duck_map.end())
1177                                         {
1178                                                 connect_duck=duck_map[calc_duck_guid(value_desc_origin,transform_stack)^GUID::hasher(0)];
1179                                         }
1180                                         else
1181                                         {
1182                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1183                                                 connect_duck=last_duck();
1184                                         }
1185                                         duck->set_connect_duck(connect_duck);
1186                                 }
1187                                 if(!param_desc->get_box().empty())
1188                                 {
1189                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_box());
1190                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1191                                         duck->set_box_duck(last_duck());
1192                                 }
1193
1194                                 // If we have an origin
1195                                 if(!param_desc->get_origin().empty())
1196                                 {
1197                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1198                                         /*
1199                                         duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1200                                         */
1201                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1202                                         duck->set_origin(last_duck());
1203                                         duck->set_type(Duck::TYPE_VERTEX);
1204                                 }
1205                                 else
1206                                         duck->set_type(Duck::TYPE_POSITION);
1207
1208                                 duck->set_scalar(param_desc->get_scalar());
1209                         }
1210                         else
1211                                 duck->set_type(Duck::TYPE_POSITION);
1212
1213
1214                         duck->signal_edited().clear();
1215                         duck->signal_edited().connect(
1216                                 sigc::bind(
1217                                         sigc::mem_fun(
1218                                                 *canvas_view,
1219                                                 &studio::CanvasView::on_duck_changed
1220                                         ),
1221                                         value_desc
1222                                 )
1223                         );
1224                         duck->set_value_desc(value_desc);
1225
1226                         duck->signal_user_click(2).connect(
1227                                 sigc::bind(
1228                                         sigc::bind(
1229                                                 sigc::mem_fun(
1230                                                         *canvas_view,
1231                                                         &studio::CanvasView::popup_param_menu
1232                                                 ),
1233                                                 1.0f
1234                                         ),
1235                                         value_desc
1236                                 )
1237                         );
1238                         duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1239                         add_duck(duck);
1240
1241                         return true;
1242                 }
1243                 break;
1244 /*      case ValueBase::TYPE_SEGMENT:
1245                 {
1246                         etl::handle<Bezier> bezier(new Bezier());
1247                         ValueNode_Composite::Handle value_node;
1248
1249                         if(value_desc.is_value_node() &&
1250                                 (value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
1251                         )
1252                         {
1253                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
1254                                         return false;
1255                                 bezier->p1=last_duck();
1256                                 bezier->p1->set_type(Duck::TYPE_VERTEX);
1257                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,1),canvas_view,transform_stack))
1258                                         return false;
1259                                 bezier->c1=last_duck();
1260                                 bezier->c1->set_type(Duck::TYPE_TANGENT);
1261                                 bezier->c1->set_origin(bezier->p1);
1262                                 bezier->c1->set_scalar(TANGENT_BEZIER_SCALE);
1263                                 bezier->c1->set_tangent(true);
1264
1265
1266                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,2),canvas_view,transform_stack))
1267                                         return false;
1268                                 bezier->p2=last_duck();
1269                                 bezier->p2->set_type(Duck::TYPE_VERTEX);
1270                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,3),canvas_view,transform_stack))
1271                                         return false;
1272                                 bezier->c2=last_duck();
1273                                 bezier->c2->set_type(Duck::TYPE_TANGENT);
1274                                 bezier->c2->set_origin(bezier->p2);
1275                                 bezier->c2->set_scalar(-TANGENT_BEZIER_SCALE);
1276                                 bezier->c2->set_tangent(true);
1277
1278                                 bezier->signal_user_click(2).connect(
1279                                         sigc::bind(
1280                                                 sigc::mem_fun(
1281                                                         *canvas_view,
1282                                                         &studio::CanvasView::popup_param_menu_bezier
1283                                                 ),
1284                                                 value_desc
1285                                         )
1286                                 );
1287
1288                                 add_bezier(bezier);
1289                         }
1290                         else if(value_desc.get_value().is_valid())
1291                         {
1292                                 Segment segment=value_desc.get_value();
1293                                 etl::handle<Duck> duck_p,duck_c;
1294                                 synfig::String name;
1295                                 if(param_desc)
1296                                 {
1297                                         name=param_desc->get_local_name();
1298                                 }
1299                                 else
1300                                 {
1301                                         name=guid_string(value_desc);
1302                                 }
1303
1304                                 duck_p=new class Duck(segment.p1);
1305                                 duck_p->set_name(name+".P1");
1306                                 duck_p->set_type(Duck::TYPE_VERTEX);
1307                                 add_duck(duck_p);
1308
1309                                 duck_c=new class Duck(segment.t1);
1310                                 duck_c->set_name(name+".T1");
1311                                 duck_c->set_type(Duck::TYPE_TANGENT);
1312                                 add_duck(duck_c);
1313                                 duck_c->set_origin(duck_p);
1314                                 duck_c->set_scalar(TANGENT_HANDLE_SCALE);
1315                                 duck_c->set_tangent(true);
1316
1317                                 bezier->p1=duck_p;
1318                                 bezier->c1=duck_c;
1319
1320                                 duck_p=new class Duck(segment.p2);
1321                                 duck_p->set_name(name+".P2");
1322                                 duck_p->set_type(Duck::TYPE_VERTEX);
1323                                 add_duck(duck_p);
1324
1325                                 duck_c=new class Duck(segment.t2);
1326                                 duck_c->set_type(Duck::TYPE_TANGENT);
1327                                 duck_c->set_name(name+".T2");
1328                                 add_duck(duck_c);
1329                                 duck_c->set_origin(duck_p);
1330                                 duck_c->set_scalar(-TANGENT_HANDLE_SCALE);
1331                                 duck_c->set_tangent(true);
1332
1333                                 bezier->p2=duck_p;
1334                                 bezier->c2=duck_c;
1335                                 add_bezier(bezier);
1336                         }
1337
1338                         return true;
1339                 }
1340                 break;
1341 */
1342         case ValueBase::TYPE_BLINEPOINT:
1343         {
1344
1345                 if(value_desc.is_value_node() &&
1346                         ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
1347                 )
1348                 {
1349                         ValueNode_Composite::Handle value_node;
1350                         value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node());
1351
1352
1353                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
1354                                 return false;
1355                         etl::handle<Duck> vertex_duck(last_duck());
1356                         vertex_duck->set_type(Duck::TYPE_VERTEX);
1357                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
1358                                 return false;
1359                         etl::handle<Duck> t1_duck(last_duck());
1360
1361                         t1_duck->set_origin(vertex_duck);
1362                         t1_duck->set_scalar(-TANGENT_HANDLE_SCALE);
1363                         t1_duck->set_tangent(true);
1364
1365                         etl::handle<Duck> t2_duck;
1366
1367                         // If the tangents are split
1368                         if((*value_node->get_link("split"))(get_time()).get(bool()))
1369                         {
1370                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,5),canvas_view,transform_stack))
1371                                         return false;
1372                                 t2_duck=last_duck();
1373                                 t2_duck->set_origin(vertex_duck);
1374                                 t2_duck->set_scalar(TANGENT_HANDLE_SCALE);
1375                                 t2_duck->set_tangent(true);
1376                         }
1377                         else
1378                         {
1379                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
1380                                         return false;
1381                                 t2_duck=last_duck();
1382                                 t2_duck->set_origin(vertex_duck);
1383                                 t2_duck->set_scalar(TANGENT_HANDLE_SCALE);
1384                                 t2_duck->set_tangent(true);
1385                         }
1386                         return true;
1387                 }
1388
1389         }
1390         break;
1391         case ValueBase::TYPE_LIST:
1392         {
1393                 // Check for BLine
1394                 if(value_desc.is_value_node() &&
1395                         ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node())
1396                 )
1397                 {
1398                         ValueNode_BLine::Handle value_node;
1399                         value_node=ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node());
1400
1401                         int i,first=-1;
1402
1403                         etl::handle<Bezier> bezier;
1404                         etl::handle<Duck> first_duck;
1405                         etl::handle<Duck> duck, tduck;
1406
1407                         for(i=0;i<value_node->link_count();i++)
1408                         {
1409                                 float amount(value_node->list[i].amount_at_time(get_time()));
1410                                 if(amount<0.9999f)
1411                                         continue;
1412                                 if(first==-1)first=i;
1413
1414                                 BLinePoint bline_point((*value_node->get_link(i))(get_time()));
1415
1416                                 ValueNode_Composite::Handle vertex_value_node(
1417                                         ValueNode_Composite::Handle::cast_dynamic(
1418                                                 value_node->get_link(i)
1419                                         )
1420                                 );
1421
1422                                 // Add the vertex duck
1423                                 if(vertex_value_node)
1424                                 {
1425                                         if(add_to_ducks(synfigapp::ValueDesc(vertex_value_node,0),canvas_view,transform_stack))
1426                                         {
1427                                                 duck=last_duck();
1428                                                 if(i==first)
1429                                                         first_duck=duck;
1430                                                 duck->set_type(Duck::TYPE_VERTEX);
1431
1432                                                 duck->signal_user_click(2).clear();
1433                                                 duck->signal_user_click(2).connect(
1434                                                         sigc::bind(
1435                                                                 sigc::bind(
1436                                                                         sigc::mem_fun(
1437                                                                                 *canvas_view,
1438                                                                                 &studio::CanvasView::popup_param_menu
1439                                                                         ),
1440                                                                         1.0f
1441                                                                 ),
1442                                                                 synfigapp::ValueDesc(value_node,i)
1443                                                         )
1444                                                 );
1445                                                 duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1446
1447                                                 if(param_desc)
1448                                                 {
1449                                                         if(!param_desc->get_origin().empty())
1450                                                         {
1451                                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1452                                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1453                                                                 duck->set_origin(last_duck());
1454 /*
1455                                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1456                                                                 if(value.same_type_as(synfig::Point()))
1457                                                                         duck->set_origin(value.get(synfig::Point()));
1458 */
1459                                                         }
1460                                                 }
1461                                         }
1462                                         else
1463                                                 return false;
1464                                 }
1465                                 else
1466                                 {
1467                                         duck=new Duck(bline_point.get_vertex());
1468                                         if(i==first)
1469                                                 first_duck=duck;
1470                                         duck->set_transform_stack(transform_stack);
1471                                         duck->set_editable(false);
1472                                         //duck->set_name(strprintf("%x-vertex",value_node->get_link(i).get()));
1473                                         duck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".v");
1474
1475                                         duck->set_type(Duck::TYPE_VERTEX);
1476                                         if(param_desc)
1477                                         {
1478                                                 if(!param_desc->get_origin().empty())
1479                                                 {
1480                                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1481                                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1482                                                                 duck->set_origin(last_duck());
1483 /*
1484                                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1485                                                                 if(value.same_type_as(synfig::Point()))
1486                                                                         duck->set_origin(value.get(synfig::Point()));
1487 */
1488                                                 }
1489                                         }
1490                                         duck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".v"));
1491                                         duck=add_similar_duck(duck);
1492 //                                      add_duck(duck);
1493                                 }
1494
1495                                 // Add the width duck only if we have a hint of scale
1496                                 if(param_desc && !param_desc->get_hint().empty())
1497                                 {
1498                                         etl::handle<Duck> width;
1499                                         if(add_to_ducks(synfigapp::ValueDesc(vertex_value_node,1),canvas_view,transform_stack,REAL_COOKIE))
1500                                         {
1501                                                 width=last_duck();
1502                                                 width->set_origin(duck);
1503                                                 width->set_type(Duck::TYPE_WIDTH);
1504                                                 width->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".w");
1505                                                 {
1506                                                         ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_hint()).get_value(get_time()));
1507                                                         if(value.same_type_as(synfig::Real()))
1508                                                                 width->set_scalar(value.get(synfig::Real())*0.5f);
1509                                                 }
1510                                         }
1511                                         else
1512                                                 synfig::error("Unable to add width duck!");
1513                                 }
1514
1515                                 if(bezier)
1516                                 {
1517                                         // Add the tangent1 duck
1518                                         if(vertex_value_node)
1519                                         {
1520                                                 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,4),canvas_view,transform_stack))
1521                                                         return false;
1522                                                 tduck=last_duck();
1523                                         }
1524                                         else
1525                                         {
1526                                                 tduck=new Duck(bline_point.get_tangent1());
1527                                                 tduck->set_transform_stack(transform_stack);
1528                                                 tduck->set_editable(false);
1529                                                 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t1");
1530 //                                              tduck->set_name(strprintf("%x-tangent1",value_node->get_link(i).get()));
1531                                                 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".t1"));
1532                                                 tduck=add_similar_duck(tduck);
1533 //                                              add_duck(duck);
1534                                         }
1535
1536                                         tduck->set_origin(duck);
1537                                         tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1538                                         tduck->set_tangent(true);
1539
1540                                         bezier->p2=duck;
1541                                         bezier->c2=tduck;
1542
1543                                         bezier->signal_user_click(2).connect(
1544                                                 sigc::bind(
1545                                                         sigc::mem_fun(
1546                                                                 *canvas_view,
1547                                                                 &studio::CanvasView::popup_param_menu_bezier
1548                                                         ),
1549                                                         synfigapp::ValueDesc(value_node,i)
1550                                                 )
1551                                         );
1552
1553                                         duck->signal_user_click(2).clear();
1554                                         duck->signal_user_click(2).connect(
1555                                                 sigc::bind(
1556                                                         sigc::bind(
1557                                                                 sigc::mem_fun(
1558                                                                         *canvas_view,
1559                                                                         &studio::CanvasView::popup_param_menu
1560                                                                 ),
1561                                                                 1.0f
1562                                                         ),
1563                                                         synfigapp::ValueDesc(value_node,i)
1564                                                 )
1565                                         );
1566                                         duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1567
1568                                         add_bezier(bezier);
1569                                         bezier=0;
1570                                 }
1571
1572                                 if(i+1>=value_node->link_count() && !value_node->get_loop())
1573                                         continue;
1574
1575                                 bezier=new Bezier();
1576
1577                                 // Add the tangent2 duck
1578                                 if(vertex_value_node)
1579                                 {
1580                                         int i=bline_point.get_split_tangent_flag()?5:4;
1581                                         if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,i),canvas_view,transform_stack,0,2))
1582                                                 return false;
1583                                         tduck=last_duck();
1584                                 }
1585                                 else
1586                                 {
1587                                         if(bline_point.get_split_tangent_flag())
1588                                         {
1589                                                 tduck=new Duck(bline_point.get_tangent2());
1590                                                 tduck->set_transform_stack(transform_stack);
1591                                                 //tduck->set_name(strprintf("%x-tangent2",value_node->get_link(i).get()));
1592                                                 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t2");
1593                                                 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".t2"));
1594                                         }
1595                                         else
1596                                         {
1597                                                 tduck=new Duck(bline_point.get_tangent1());
1598                                                 tduck->set_transform_stack(transform_stack);
1599                                                 //tduck->set_name(strprintf("%x-tangent1",value_node->get_link(i).get()));
1600                                                 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t1");
1601                                                 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".t1"));
1602                                         }
1603                                         tduck->set_editable(false);
1604                                         tduck=add_similar_duck(tduck);
1605 //                                      add_duck(duck);
1606                                         if(param_desc)
1607                                         {
1608                                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1609                                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1610                                                                 duck->set_origin(last_duck());
1611 /*
1612                                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1613                                                                 if(value.same_type_as(synfig::Point()))
1614                                                                         duck->set_origin(value.get(synfig::Point()));
1615 */
1616 //                                              if(!param_desc->get_origin().empty())
1617 //                                                      duck->set_origin(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()).get(synfig::Point()));
1618                                         }
1619                                         duck->signal_user_click(2).clear();
1620                                         duck->signal_user_click(2).connect(
1621                                                 sigc::bind(
1622                                                         sigc::bind(
1623                                                                 sigc::mem_fun(
1624                                                                         *canvas_view,
1625                                                                         &studio::CanvasView::popup_param_menu
1626                                                                 ),
1627                                                                 1.0f
1628                                                         ),
1629                                                         synfigapp::ValueDesc(value_node,0)
1630                                                 )
1631                                         );
1632                                         duck->set_value_desc(synfigapp::ValueDesc(value_node,0));
1633
1634                                 }
1635
1636                                 tduck->set_origin(duck);
1637                                 tduck->set_scalar(TANGENT_BEZIER_SCALE);
1638                                 tduck->set_tangent(true);
1639
1640                                 bezier->p1=duck;
1641                                 bezier->c1=tduck;
1642
1643                         }
1644                         // Loop if necessary
1645                         if(bezier && value_node->get_loop())
1646                         {
1647                                 BLinePoint bline_point((*value_node->get_link(first))(get_time()));
1648
1649                                 ValueNode_Composite::Handle vertex_value_node(
1650                                         ValueNode_Composite::Handle::cast_dynamic(
1651                                                 value_node->get_link(first)
1652                                         )
1653                                 );
1654
1655                                 // Add the vertex duck
1656                                 duck=first_duck;
1657                                 /*
1658                                 if(vertex_value_node)
1659                                 {
1660                                         if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,0),canvas_view,transform_stack))
1661                                                 return false;
1662                                         duck=last_duck();
1663                                         if(param_desc)
1664                                         {
1665                                                 if(!param_desc->get_origin().empty())
1666                                                         duck->set_origin(value_desc.get_layer()->get_param(param_desc->get_origin()).get(synfig::Point()));
1667                                         }
1668                                         duck->set_type(Duck::TYPE_VERTEX);
1669                                 }
1670                                 else
1671                                 {
1672                                         duck=new Duck(bline_point.get_vertex());
1673                                         duck->set_transform_stack(transform_stack);
1674                                         if(param_desc)
1675                                         {
1676                                                 if(!param_desc->get_origin().empty())
1677                                                         duck->set_origin(value_desc.get_layer()->get_param(param_desc->get_origin()).get(synfig::Point()));
1678                                         }
1679                                         duck->set_editable(false);
1680                                         duck->set_name(strprintf("%x-vertex",value_node->get_link(first).get()));
1681                                         duck->set_type(Duck::TYPE_VERTEX);
1682                                         duck=add_similar_duck(duck);
1683                                 }
1684                                 */
1685
1686                                         // Add the tangent1 duck
1687                                         if(vertex_value_node)
1688                                         {
1689                                                 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,4),canvas_view,transform_stack))
1690                                                         return false;
1691                                                 tduck=last_duck();
1692                                         }
1693                                         else
1694                                         {
1695                                                 tduck=new Duck(bline_point.get_tangent1());
1696                                                 tduck->set_transform_stack(transform_stack);
1697                                                 tduck->set_editable(false);
1698                                                 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,first))+".t1");
1699                                                 //tduck->set_name(strprintf("%x-tangent1",value_node->get_link(first).get()));
1700                                                 tduck=add_similar_duck(tduck);
1701                                                 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".t1"));
1702                                                 //add_duck(duck);
1703                                         }
1704
1705                                         tduck->set_origin(duck);
1706                                         tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1707                                         tduck->set_tangent(true);
1708
1709                                         bezier->p2=duck;
1710                                         bezier->c2=tduck;
1711                                         bezier->signal_user_click(2).connect(
1712                                                 sigc::bind(
1713                                                         sigc::mem_fun(
1714                                                                 *canvas_view,
1715                                                                 &studio::CanvasView::popup_param_menu_bezier
1716                                                         ),
1717                                                         synfigapp::ValueDesc(value_node,first)
1718                                                 )
1719                                         );
1720                                         duck->signal_user_click(2).clear();
1721                                         duck->signal_user_click(2).connect(
1722                                                 sigc::bind(
1723                                                         sigc::bind(
1724                                                                 sigc::mem_fun(
1725                                                                         *canvas_view,
1726                                                                         &studio::CanvasView::popup_param_menu
1727                                                                 ),
1728                                                                 1.0f
1729                                                         ),
1730                                                         synfigapp::ValueDesc(value_node,first)
1731                                                 )
1732                                         );
1733                                         duck->set_value_desc(synfigapp::ValueDesc(value_node,first));
1734
1735                                         add_bezier(bezier);
1736                                         bezier=0;
1737
1738                         }
1739                         return true;
1740                 }
1741                 else // Check for DynamicList
1742                 if(value_desc.is_value_node() &&
1743                         ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node())
1744                 )
1745                 {
1746                         ValueNode_DynamicList::Handle value_node;
1747                         value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node());
1748                         int i;
1749
1750                         if(value_node->get_contained_type()==ValueBase::TYPE_VECTOR)
1751                         {
1752                                 Bezier bezier;
1753                                 for(i=0;i<value_node->link_count();i++)
1754                                 {
1755                                         if(!value_node->list[i].status_at_time(get_time()))
1756                                                 continue;
1757                                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))
1758                                                 return false;
1759                                         etl::handle<Duck> duck(last_duck());
1760
1761                                         if(param_desc)
1762                                         {
1763                                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1764                                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1765                                                                 duck->set_origin(last_duck());
1766 /*
1767                                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1768                                                                 if(value.same_type_as(synfig::Point()))
1769                                                                         duck->set_origin(value.get(synfig::Point()));
1770 */
1771 //                                              if(!param_desc->get_origin().empty())
1772 //                                                      last_duck()->set_origin(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()).get(synfig::Point()));
1773                                         }
1774                                         duck->set_type(Duck::TYPE_VERTEX);
1775                                         bezier.p1=bezier.p2;bezier.c1=bezier.c2;
1776                                         bezier.p2=bezier.c2=duck;
1777
1778                                         if(i>0)
1779                                         {
1780                                                 handle<Bezier> bezier_(new Bezier());
1781                                                 bezier_->p1=bezier.p1;
1782                                                 bezier_->c1=bezier.c1;
1783                                                 bezier_->p2=bezier.p2;
1784                                                 bezier_->c2=bezier.c2;
1785                                                 add_bezier(bezier_);
1786                                                 last_bezier()->signal_user_click(2).connect(
1787                                                         sigc::bind(
1788                                                                 sigc::mem_fun(
1789                                                                         *canvas_view,
1790                                                                         &studio::CanvasView::popup_param_menu_bezier
1791                                                                 ),
1792                                                                 synfigapp::ValueDesc(value_node,i)
1793                                                         )
1794                                                 );
1795                                         }
1796                                 }
1797                         }
1798                         /*else if(value_node->get_contained_type()==ValueBase::TYPE_SEGMENT)
1799                         {
1800                                 for(i=0;i<value_node->link_count();i++)
1801                                 {
1802                                         if(!value_node->list[i].status_at_time(get_time()))
1803                                                 continue;
1804                                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))
1805                                                 return false;
1806                                 }
1807                         }
1808                         */
1809                         else
1810                                 return false;
1811                 }
1812                 else
1813                 {
1814                         // WRITEME
1815                 }
1816
1817                 return true;
1818         }
1819
1820
1821         break;
1822         default:
1823                 break;
1824         }
1825         return false;
1826 }