1 /* === S Y N F I G ========================================================= */
2 /*! \file duckmatic.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
36 #include <ETL/hermite>
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>
48 #include <synfig/curve_helper.h>
50 #include <sigc++/retype_return.h>
51 #include <sigc++/retype.h>
52 #include <sigc++/hide.h>
53 #include <sigc++/bind.h>
55 #include "canvasview.h"
57 #include "onemoment.h"
61 /* === U S I N G =========================================================== */
65 using namespace synfig;
66 using namespace studio;
68 /* === M A C R O S ========================================================= */
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
74 /* leave this alone or the bezier won't lie on top of the bline */
75 #define TANGENT_BEZIER_SCALE 0.33333333333333333
77 /* === G L O B A L S ======================================================= */
79 /* === P R O C E D U R E S ================================================= */
81 /* === M E T H O D S ======================================================= */
83 /* === E N T R Y P O I N T ================================================= */
85 Duckmatic::Duckmatic():
86 type_mask(Duck::TYPE_ALL-Duck::TYPE_WIDTH),
89 grid_size(1.0/4.0,1.0/4.0),
90 show_persistant_strokes(true)
93 drag_offset_=Point(0,0);
97 Duckmatic::~Duckmatic()
100 //synfig::info("Duckmatic::~Duckmatic(): Deleted. Duck Count=%d",Duck::duck_count);
104 Duckmatic::clear_ducks()
106 duck_data_share_map.clear();
109 //duck_list_.clear();
110 bezier_list_.clear();
111 stroke_list_.clear();
113 if(show_persistant_strokes)
114 stroke_list_=persistant_stroke_list_;
117 //! Returns \a true if the given duck is currently selected
119 Duckmatic::duck_is_selected(const etl::handle<Duck> &duck)const
121 return duck && selected_ducks.count(duck->get_guid());
125 Duckmatic::set_grid_size(const synfig::Vector &s)
130 signal_grid_changed();
135 Duckmatic::set_grid_snap(bool x)
140 signal_grid_changed();
145 Duckmatic::set_guide_snap(bool x)
150 signal_grid_changed();
156 Duckmatic::GuideList::iterator
157 Duckmatic::find_guide_x(synfig::Point pos, float radius)
159 GuideList::iterator iter,best(guide_list_x_.end());
161 for(iter=guide_list_x_.begin();iter!=guide_list_x_.end();++iter)
163 float amount(abs(*iter-pos[0]));
173 Duckmatic::GuideList::iterator
174 Duckmatic::find_guide_y(synfig::Point pos, float radius)
176 GuideList::iterator iter,best(guide_list_y_.end());
178 for(iter=guide_list_y_.begin();iter!=guide_list_y_.end();++iter)
180 float amount(abs(*iter-pos[1]));
191 Duckmatic::clear_selected_ducks()
193 selected_ducks.clear();
194 signal_duck_selection_changed_();
197 etl::handle<Duckmatic::Duck>
198 Duckmatic::get_selected_duck()const
200 if(selected_ducks.empty() || duck_map.empty())
202 return duck_map.find(*selected_ducks.begin())->second;
206 Duckmatic::refresh_selected_ducks()
209 std::set<etl::handle<Duck> >::iterator iter;
210 std::set<etl::handle<Duck> > new_set;
211 if(duck_list().empty())
213 selected_duck_list.clear();
214 signal_duck_selection_changed_();
218 for(iter=selected_duck_list.begin();iter!=selected_duck_list.end();++iter)
220 etl::handle<Duck> similar(find_similar_duck(*iter));
223 new_set.insert(similar);
226 selected_duck_list=new_set;
228 GUIDSet old_set(selected_ducks);
229 GUIDSet::const_iterator iter;
231 for(iter=old_set.begin();iter!=old_set.end();++iter)
233 if(duck_map.count(*iter)==0)
234 selected_ducks.erase(*iter);
237 signal_duck_selection_changed_();
241 Duckmatic::is_duck_group_selectable(const etl::handle<Duck>& x)const
243 const Type type(get_type_mask());
245 if(( x->get_type() && (!(type & x->get_type())) ) )
249 if(x->get_value_desc().parent_is_layer_param() && type & Duck::TYPE_POSITION)
251 Layer::Handle layer(x->get_value_desc().get_layer());
252 String layer_name(layer->get_name());
254 layer_name=="outline" ||
255 layer_name=="region" ||
256 layer_name=="polygon" ||
257 layer_name=="curve_gradient"
260 if((layer_name=="PasteCanvas"|| layer_name=="paste_canvas") &&
261 !layer->get_param("children_lock").get(bool()))
268 Duckmatic::select_all_ducks()
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);
277 Duckmatic::toggle_select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
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]);
285 DuckMap::const_iterator iter;
286 for(iter=duck_map.begin();iter!=duck_map.end();++iter)
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);
296 Duckmatic::select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
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]);
304 // Type type(get_type_mask());
306 DuckMap::const_iterator iter;
307 for(iter=duck_map.begin();iter!=duck_map.end();++iter)
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])
312 if(is_duck_group_selectable(iter->second))
313 select_duck(iter->second);
319 Duckmatic::count_selected_ducks()const
321 return selected_ducks.size();
325 Duckmatic::select_duck(const etl::handle<Duck> &duck)
329 selected_ducks.insert(duck->get_guid());
330 signal_duck_selection_changed_();
335 Duckmatic::get_selected_ducks()const
338 GUIDSet::const_iterator iter;
339 const Type type(get_type_mask());
341 for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
343 const DuckMap::const_iterator d_iter(duck_map.find(*iter));
345 if(d_iter==duck_map.end())
348 if(( d_iter->second->get_type() && (!(type & d_iter->second->get_type())) ) )
351 ret.push_back(d_iter->second);
357 Duckmatic::get_duck_list()const
360 DuckMap::const_iterator iter;
361 for(iter=duck_map.begin();iter!=duck_map.end();++iter)
362 ret.push_back(iter->second);
367 Duckmatic::unselect_duck(const etl::handle<Duck> &duck)
369 if(duck && selected_ducks.count(duck->get_guid()))
371 selected_ducks.erase(duck->get_guid());
372 signal_duck_selection_changed_();
377 Duckmatic::toggle_select_duck(const etl::handle<Duck> &duck)
379 if(duck_is_selected(duck))
387 Duckmatic::translate_selected_ducks(const synfig::Vector& vector)
390 duck_dragger_->duck_drag(this,vector);
394 Duckmatic::start_duck_drag(const synfig::Vector& offset)
397 duck_dragger_->begin_duck_drag(this,offset);
399 //drag_offset_=offset;
400 drag_offset_=find_duck(offset)->get_trans_point();
404 Duckmatic::end_duck_drag()
407 return duck_dragger_->end_duck_drag(this);
412 Duckmatic::snap_point_to_grid(const Point& x, float radius)const
416 GuideList::const_iterator guide_x,guide_y;
417 bool has_guide_x(false), has_guide_y(false);
419 guide_x=find_guide_x(ret,radius);
420 if(guide_x!=guide_list_x_.end())
423 guide_y=find_guide_y(ret,radius);
424 if(guide_y!=guide_list_y_.end())
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]
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;
451 if(abs(ret[0])<abs(ret[1]))
462 DuckDrag_Translate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
464 last_translate_=Vector(0,0);
466 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
471 const DuckList selected_ducks(duckmatic->get_selected_ducks());
472 DuckList::const_iterator iter;
475 for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
477 Point p((*iter)->get_trans_point());
478 positions.push_back(p);
483 DuckDrag_Translate::end_duck_drag(Duckmatic* duckmatic)
485 if(last_translate_.mag()>0.0001)
487 duckmatic->signal_edited_selected_ducks();
492 duckmatic->signal_user_click_selected_ducks(0);
498 DuckDrag_Translate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
500 const DuckList selected_ducks(duckmatic->get_selected_ducks());
501 DuckList::const_iterator iter;
503 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_);
506 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
508 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
509 (*iter)->set_trans_point(positions[i]+vect);
511 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
513 if((*iter)->get_type()==Duck::TYPE_VERTEX||(*iter)->get_type()==Duck::TYPE_POSITION)continue;
514 (*iter)->set_trans_point(positions[i]+vect);
516 last_translate_=vect;
529 Duckmatic::signal_edited_selected_ducks()
531 const DuckList ducks(get_selected_ducks());
532 DuckList::const_iterator iter;
534 synfig::GUIDSet old_set(selected_ducks);
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();
540 // Go ahead and call everyone's signals
541 for(iter=ducks.begin();iter!=ducks.end();++iter)
543 if(!(*iter)->signal_edited()((*iter)->get_point()))
545 selected_ducks=old_set;
546 throw String("Bad edit");
549 selected_ducks=old_set;
553 Duckmatic::signal_user_click_selected_ducks(int button)
555 const DuckList ducks(get_selected_ducks());
556 DuckList::const_iterator iter;
558 for(iter=ducks.begin();iter!=ducks.end();++iter)
560 (*iter)->signal_user_click(button)();
569 Duckmatic::add_duck(const etl::handle<Duck> &duck)
571 //if(!duck_map.count(duck->get_guid()))
573 if(duck_data_share_map.count(duck->get_data_guid()))
575 duck->set_shared_point(duck_data_share_map[duck->get_data_guid()]);
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;
584 duck_map.insert(duck);
587 last_duck_guid=duck->get_guid();
591 Duckmatic::add_bezier(const etl::handle<Bezier> &bezier)
593 bezier_list_.push_back(bezier);
597 Duckmatic::add_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
599 assert(stroke_point_list);
601 std::list<etl::handle<Stroke> >::iterator iter;
603 for(iter=stroke_list_.begin();iter!=stroke_list_.end();++iter)
605 if((*iter)->stroke_data==stroke_point_list)
609 etl::handle<Stroke> stroke(new Stroke());
611 stroke->stroke_data=stroke_point_list;
614 stroke_list_.push_back(stroke);
618 Duckmatic::add_persistant_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
620 add_stroke(stroke_point_list,color);
621 persistant_stroke_list_.push_back(stroke_list_.back());
625 Duckmatic::clear_persistant_strokes()
627 persistant_stroke_list_.clear();
631 Duckmatic::set_show_persistant_strokes(bool x)
633 if(x!=show_persistant_strokes)
635 show_persistant_strokes=x;
637 stroke_list_=persistant_stroke_list_;
639 stroke_list_.clear();
644 Duckmatic::erase_duck(const etl::handle<Duck> &duck)
646 duck_map.erase(duck->get_guid());
649 etl::handle<Duckmatic::Duck>
650 Duckmatic::find_similar_duck(etl::handle<Duck> duck)
652 DuckMap::const_iterator iter(duck_map.find(duck->get_guid()));
653 if(iter!=duck_map.end())
657 /* std::list<handle<Duck> >::reverse_iterator iter;
659 for(iter=duck_list_.rbegin();iter!=duck_list_.rend();++iter)
661 if(*iter!=duck && **iter==*duck)
663 //synfig::info("Found similar duck! (iter:%08x vs. duck:%08x)",iter->get(), duck.get());
671 etl::handle<Duckmatic::Duck>
672 Duckmatic::add_similar_duck(etl::handle<Duck> duck)
674 etl::handle<Duck> similar(find_similar_duck(duck));
684 Duckmatic::erase_bezier(const etl::handle<Bezier> &bezier)
686 std::list<handle<Bezier> >::iterator iter;
688 for(iter=bezier_list_.begin();iter!=bezier_list_.end();++iter)
692 bezier_list_.erase(iter);
696 synfig::warning("Unable to find bezier to erase!");
699 etl::handle<Duckmatic::Duck>
700 Duckmatic::last_duck()const
702 DuckMap::const_iterator iter(duck_map.find(last_duck_guid));
703 if(iter!=duck_map.end())
708 etl::handle<Duckmatic::Bezier>
709 Duckmatic::last_bezier()const
711 return bezier_list_.back();
715 etl::handle<Duckmatic::Duck>
716 Duckmatic::find_duck(synfig::Point point, synfig::Real radius, Duck::Type type)
718 if(radius==0)radius=10000000;
720 if(type==Duck::TYPE_DEFAULT)
721 type=get_type_mask();
723 Real closest(10000000);
724 etl::handle<Duck> ret;
726 DuckMap::const_iterator iter;
728 for(iter=duck_map.begin();iter!=duck_map.end();++iter)
730 const Duck::Handle& duck(iter->second);
732 if(!duck->get_editable())
734 Real dist((duck->get_trans_point()-point).mag_squared());
736 if(duck->get_type()&Duck::TYPE_VERTEX)
739 if(dist<=closest && !( duck->get_type() && (!(type & duck->get_type())) ) )
748 if(radius==0 || closest<radius*radius)
754 etl::handle<Duckmatic::Bezier>
755 Duckmatic::find_bezier(synfig::Point point, synfig::Real radius,float* location)
757 return find_bezier(point,radius,radius,location);
760 etl::handle<Duckmatic::Bezier>
761 Duckmatic::find_bezier(synfig::Point pos, synfig::Real scale, synfig::Real radius, float* location)
763 if(radius==0)radius=10000000;
764 Real closest(10000000);
765 etl::handle<Bezier> ret;
773 for(std::list<handle<Bezier> >::const_iterator iter=bezier_list().begin();iter!=bezier_list().end();++iter)
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();
782 // I don't know why this doesn't work
783 time=curve.find_closest(pos,6);
784 d=((curve(time)-pos).mag_squared());
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();
790 step = d/(2*scale); //want to make the distance between lines happy
792 step = max(step,0.01); //100 samples should be plenty
793 step = min(step,0.1); //10 is minimum
795 d = find_closest(curve,pos,step,&closest,&time);
806 if(closest < radius*radius)
809 *location = best_time; // We need to square-root this because we were dealing with squared distances
820 Duckmatic::save_sketch(const synfig::String& filename)const
822 std::ofstream file(filename.c_str());
824 if(!file)return false;
826 file<<"SKETCH"<<endl;
828 std::list<etl::handle<Stroke> >::const_iterator iter;
830 for(iter=persistant_stroke_list_.begin();iter!=persistant_stroke_list_.end();++iter)
833 <<(*iter)->color.get_r()<<' '
834 <<(*iter)->color.get_g()<<' '
835 <<(*iter)->color.get_b()
837 std::list<synfig::Point>::const_iterator viter;
838 for(viter=(*iter)->stroke_data->begin();viter!=(*iter)->stroke_data->end();++viter)
846 if(!file)return false;
847 sketch_filename_=filename;
848 signal_sketch_saved_();
853 Duckmatic::load_sketch(const synfig::String& filename)
855 std::ifstream file(filename.c_str());
865 synfig::error("Not a sketch");
870 etl::smart_ptr<std::list<synfig::Point> > stroke_data;
886 if(!strscanf(line,"C %f %f %f",&r, &g, &b))
888 synfig::warning("Bad color line \"%s\"",line.c_str());
891 add_persistant_stroke(stroke_data, synfig::Color(r,g,b));
899 add_persistant_stroke(stroke_data, synfig::Color(0,0,0));
902 if(!strscanf(line,"V %f %f",&x, &y))
903 synfig::warning("Bad vertex \"%s\"",line.c_str());
905 stroke_data->push_back(synfig::Vector(x,y));
908 synfig::warning("Unexpected sketch token '%c'",line[0]);
913 sketch_filename_=filename;
924 Duckmatic::Push::Push(Duckmatic *duckmatic_):
925 duckmatic_(duckmatic_)
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_;
935 Duckmatic::Push::~Push()
942 Duckmatic::Push::restore()
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_;
960 inline String guid_string(const synfigapp::ValueDesc& x)
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());
968 inline GUID calc_duck_guid(const synfigapp::ValueDesc& x,const synfig::TransformStack& transform_stack)
972 if(x.parent_is_layer_param())
974 ret=x.get_layer()->get_guid()^GUID::hasher(x.get_param_name());
978 ret=x.get_value_node()->get_guid();
981 ret^=transform_stack.get_guid();
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)
989 GUID duck_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(modifier));
990 etl::handle<Duck> duck=new Duck();
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)
999 ValueBase::Type type=value_desc.get_value_type();
1000 #define REAL_COOKIE reinterpret_cast<synfig::ParamDesc*>(28)
1003 case ValueBase::TYPE_REAL:
1005 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
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);
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())
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()));
1022 duck->set_editable(true);
1025 if(param_desc && param_desc!=REAL_COOKIE)
1027 if(!param_desc->get_origin().empty())
1029 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1031 duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1033 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1034 duck->set_origin(last_duck());
1036 duck->set_scalar(param_desc->get_scalar());
1039 duck->signal_edited().clear();
1040 duck->signal_edited().connect(
1044 &studio::CanvasView::on_duck_changed
1049 duck->set_value_desc(value_desc);
1051 duck->signal_user_click(2).connect(
1056 &studio::CanvasView::popup_param_menu
1064 duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1072 case ValueBase::TYPE_ANGLE:
1074 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
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;
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())
1086 ValueNode::Handle value_node=value_desc.get_value_node();
1087 //duck->set_name(strprintf("%x",value_node.get()));
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()));
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);
1101 if(param_desc && param_desc!=REAL_COOKIE)
1103 if(!param_desc->get_origin().empty())
1105 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1107 duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1109 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1110 duck->set_origin(last_duck());
1112 duck->set_scalar(param_desc->get_scalar());
1115 duck->signal_edited().clear();
1116 duck->signal_edited().connect(
1120 &studio::CanvasView::on_duck_changed
1125 duck->set_value_desc(value_desc);
1127 duck->signal_user_click(2).connect(
1132 &studio::CanvasView::popup_param_menu
1139 duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1147 case ValueBase::TYPE_VECTOR:
1149 etl::handle<Duck> duck=new Duck();
1150 duck->set_transform_stack(transform_stack);
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())
1156 //duck->set_name(strprintf("%x",value_desc.get_value_node().get()));
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()));
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);
1169 // If we were passed a parameter description
1172 if(!param_desc->get_connect().empty())
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())
1178 connect_duck=duck_map[calc_duck_guid(value_desc_origin,transform_stack)^GUID::hasher(0)];
1182 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1183 connect_duck=last_duck();
1185 duck->set_connect_duck(connect_duck);
1187 if(!param_desc->get_box().empty())
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());
1194 // If we have an origin
1195 if(!param_desc->get_origin().empty())
1197 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1199 duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1201 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1202 duck->set_origin(last_duck());
1203 duck->set_type(Duck::TYPE_VERTEX);
1206 duck->set_type(Duck::TYPE_POSITION);
1208 duck->set_scalar(param_desc->get_scalar());
1211 duck->set_type(Duck::TYPE_POSITION);
1214 duck->signal_edited().clear();
1215 duck->signal_edited().connect(
1219 &studio::CanvasView::on_duck_changed
1224 duck->set_value_desc(value_desc);
1226 duck->signal_user_click(2).connect(
1231 &studio::CanvasView::popup_param_menu
1238 duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1244 /* case ValueBase::TYPE_SEGMENT:
1246 etl::handle<Bezier> bezier(new Bezier());
1247 ValueNode_Composite::Handle value_node;
1249 if(value_desc.is_value_node() &&
1250 (value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
1253 if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
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))
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);
1266 if(!add_to_ducks(synfigapp::ValueDesc(value_node,2),canvas_view,transform_stack))
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))
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);
1278 bezier->signal_user_click(2).connect(
1282 &studio::CanvasView::popup_param_menu_bezier
1290 else if(value_desc.get_value().is_valid())
1292 Segment segment=value_desc.get_value();
1293 etl::handle<Duck> duck_p,duck_c;
1294 synfig::String name;
1297 name=param_desc->get_local_name();
1301 name=guid_string(value_desc);
1304 duck_p=new class Duck(segment.p1);
1305 duck_p->set_name(name+".P1");
1306 duck_p->set_type(Duck::TYPE_VERTEX);
1309 duck_c=new class Duck(segment.t1);
1310 duck_c->set_name(name+".T1");
1311 duck_c->set_type(Duck::TYPE_TANGENT);
1313 duck_c->set_origin(duck_p);
1314 duck_c->set_scalar(TANGENT_HANDLE_SCALE);
1315 duck_c->set_tangent(true);
1320 duck_p=new class Duck(segment.p2);
1321 duck_p->set_name(name+".P2");
1322 duck_p->set_type(Duck::TYPE_VERTEX);
1325 duck_c=new class Duck(segment.t2);
1326 duck_c->set_type(Duck::TYPE_TANGENT);
1327 duck_c->set_name(name+".T2");
1329 duck_c->set_origin(duck_p);
1330 duck_c->set_scalar(-TANGENT_HANDLE_SCALE);
1331 duck_c->set_tangent(true);
1342 case ValueBase::TYPE_BLINEPOINT:
1345 if(value_desc.is_value_node() &&
1346 ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
1349 ValueNode_Composite::Handle value_node;
1350 value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node());
1353 if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
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))
1359 etl::handle<Duck> t1_duck(last_duck());
1361 t1_duck->set_origin(vertex_duck);
1362 t1_duck->set_scalar(-TANGENT_HANDLE_SCALE);
1363 t1_duck->set_tangent(true);
1365 etl::handle<Duck> t2_duck;
1367 // If the tangents are split
1368 if((*value_node->get_link("split"))(get_time()).get(bool()))
1370 if(!add_to_ducks(synfigapp::ValueDesc(value_node,5),canvas_view,transform_stack))
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);
1379 if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
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);
1391 case ValueBase::TYPE_LIST:
1394 if(value_desc.is_value_node() &&
1395 ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node())
1398 ValueNode_BLine::Handle value_node;
1399 value_node=ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node());
1403 etl::handle<Bezier> bezier;
1404 etl::handle<Duck> first_duck;
1405 etl::handle<Duck> duck, tduck;
1407 for(i=0;i<value_node->link_count();i++)
1409 float amount(value_node->list[i].amount_at_time(get_time()));
1412 if(first==-1)first=i;
1414 BLinePoint bline_point((*value_node->get_link(i))(get_time()));
1416 ValueNode_Composite::Handle vertex_value_node(
1417 ValueNode_Composite::Handle::cast_dynamic(
1418 value_node->get_link(i)
1422 // Add the vertex duck
1423 if(vertex_value_node)
1425 if(add_to_ducks(synfigapp::ValueDesc(vertex_value_node,0),canvas_view,transform_stack))
1430 duck->set_type(Duck::TYPE_VERTEX);
1432 duck->signal_user_click(2).clear();
1433 duck->signal_user_click(2).connect(
1438 &studio::CanvasView::popup_param_menu
1442 synfigapp::ValueDesc(value_node,i)
1445 duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1449 if(!param_desc->get_origin().empty())
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());
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()));
1467 duck=new Duck(bline_point.get_vertex());
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");
1475 duck->set_type(Duck::TYPE_VERTEX);
1478 if(!param_desc->get_origin().empty())
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());
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()));
1490 duck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".v"));
1491 duck=add_similar_duck(duck);
1495 // Add the width duck only if we have a hint of scale
1496 if(param_desc && !param_desc->get_hint().empty())
1498 etl::handle<Duck> width;
1499 if(add_to_ducks(synfigapp::ValueDesc(vertex_value_node,1),canvas_view,transform_stack,REAL_COOKIE))
1502 width->set_origin(duck);
1503 width->set_type(Duck::TYPE_WIDTH);
1504 width->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".w");
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);
1512 synfig::error("Unable to add width duck!");
1517 // Add the tangent1 duck
1518 if(vertex_value_node)
1520 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,4),canvas_view,transform_stack))
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);
1536 tduck->set_origin(duck);
1537 tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1538 tduck->set_tangent(true);
1543 bezier->signal_user_click(2).connect(
1547 &studio::CanvasView::popup_param_menu_bezier
1549 synfigapp::ValueDesc(value_node,i)
1553 duck->signal_user_click(2).clear();
1554 duck->signal_user_click(2).connect(
1559 &studio::CanvasView::popup_param_menu
1563 synfigapp::ValueDesc(value_node,i)
1566 duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1572 if(i+1>=value_node->link_count() && !value_node->get_loop())
1575 bezier=new Bezier();
1577 // Add the tangent2 duck
1578 if(vertex_value_node)
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))
1587 if(bline_point.get_split_tangent_flag())
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"));
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"));
1603 tduck->set_editable(false);
1604 tduck=add_similar_duck(tduck);
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());
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()));
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()));
1619 duck->signal_user_click(2).clear();
1620 duck->signal_user_click(2).connect(
1625 &studio::CanvasView::popup_param_menu
1629 synfigapp::ValueDesc(value_node,0)
1632 duck->set_value_desc(synfigapp::ValueDesc(value_node,0));
1636 tduck->set_origin(duck);
1637 tduck->set_scalar(TANGENT_BEZIER_SCALE);
1638 tduck->set_tangent(true);
1644 // Loop if necessary
1645 if(bezier && value_node->get_loop())
1647 BLinePoint bline_point((*value_node->get_link(first))(get_time()));
1649 ValueNode_Composite::Handle vertex_value_node(
1650 ValueNode_Composite::Handle::cast_dynamic(
1651 value_node->get_link(first)
1655 // Add the vertex duck
1658 if(vertex_value_node)
1660 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,0),canvas_view,transform_stack))
1665 if(!param_desc->get_origin().empty())
1666 duck->set_origin(value_desc.get_layer()->get_param(param_desc->get_origin()).get(synfig::Point()));
1668 duck->set_type(Duck::TYPE_VERTEX);
1672 duck=new Duck(bline_point.get_vertex());
1673 duck->set_transform_stack(transform_stack);
1676 if(!param_desc->get_origin().empty())
1677 duck->set_origin(value_desc.get_layer()->get_param(param_desc->get_origin()).get(synfig::Point()));
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);
1686 // Add the tangent1 duck
1687 if(vertex_value_node)
1689 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,4),canvas_view,transform_stack))
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"));
1705 tduck->set_origin(duck);
1706 tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1707 tduck->set_tangent(true);
1711 bezier->signal_user_click(2).connect(
1715 &studio::CanvasView::popup_param_menu_bezier
1717 synfigapp::ValueDesc(value_node,first)
1720 duck->signal_user_click(2).clear();
1721 duck->signal_user_click(2).connect(
1726 &studio::CanvasView::popup_param_menu
1730 synfigapp::ValueDesc(value_node,first)
1733 duck->set_value_desc(synfigapp::ValueDesc(value_node,first));
1741 else // Check for DynamicList
1742 if(value_desc.is_value_node() &&
1743 ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node())
1746 ValueNode_DynamicList::Handle value_node;
1747 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node());
1750 if(value_node->get_contained_type()==ValueBase::TYPE_VECTOR)
1753 for(i=0;i<value_node->link_count();i++)
1755 if(!value_node->list[i].status_at_time(get_time()))
1757 if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))
1759 etl::handle<Duck> duck(last_duck());
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());
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()));
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()));
1774 duck->set_type(Duck::TYPE_VERTEX);
1775 bezier.p1=bezier.p2;bezier.c1=bezier.c2;
1776 bezier.p2=bezier.c2=duck;
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(
1790 &studio::CanvasView::popup_param_menu_bezier
1792 synfigapp::ValueDesc(value_node,i)
1798 /*else if(value_node->get_contained_type()==ValueBase::TYPE_SEGMENT)
1800 for(i=0;i<value_node->link_count();i++)
1802 if(!value_node->list[i].status_at_time(get_time()))
1804 if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))