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"
63 /* === U S I N G =========================================================== */
67 using namespace synfig;
68 using namespace studio;
70 /* === M A C R O S ========================================================= */
72 /* 0.33333333333333333 makes for nice short tangent handles,
73 1.0 makes them draw as their real length */
74 #define TANGENT_HANDLE_SCALE 0.33333333333333333
76 /* leave this alone or the bezier won't lie on top of the bline */
77 #define TANGENT_BEZIER_SCALE 0.33333333333333333
79 /* === G L O B A L S ======================================================= */
81 /* === P R O C E D U R E S ================================================= */
83 /* === M E T H O D S ======================================================= */
85 /* === E N T R Y P O I N T ================================================= */
87 Duckmatic::Duckmatic():
88 type_mask(Duck::TYPE_ALL-Duck::TYPE_WIDTH),
91 grid_size(1.0/4.0,1.0/4.0),
92 show_persistent_strokes(true)
95 drag_offset_=Point(0,0);
99 Duckmatic::~Duckmatic()
102 //synfig::info("Duckmatic::~Duckmatic(): Deleted. Duck Count=%d",Duck::duck_count);
106 Duckmatic::clear_ducks()
108 duck_data_share_map.clear();
111 //duck_list_.clear();
112 bezier_list_.clear();
113 stroke_list_.clear();
115 if(show_persistent_strokes)
116 stroke_list_=persistent_stroke_list_;
119 //! Returns \a true if the given duck is currently selected
121 Duckmatic::duck_is_selected(const etl::handle<Duck> &duck)const
123 return duck && selected_ducks.count(duck->get_guid());
127 Duckmatic::set_grid_size(const synfig::Vector &s)
132 signal_grid_changed();
137 Duckmatic::set_grid_snap(bool x)
142 signal_grid_changed();
147 Duckmatic::set_guide_snap(bool x)
152 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))
386 Duckmatic::translate_selected_ducks(const synfig::Vector& vector)
389 duck_dragger_->duck_drag(this,vector);
393 Duckmatic::start_duck_drag(const synfig::Vector& offset)
396 duck_dragger_->begin_duck_drag(this,offset);
398 //drag_offset_=offset;
399 drag_offset_=find_duck(offset)->get_trans_point();
403 Duckmatic::end_duck_drag()
406 return duck_dragger_->end_duck_drag(this);
411 Duckmatic::snap_point_to_grid(const synfig::Point& x, float radius)const
415 GuideList::const_iterator guide_x,guide_y;
416 bool has_guide_x(false), has_guide_y(false);
418 guide_x=find_guide_x(ret,radius);
419 if(guide_x!=guide_list_x_.end())
422 guide_y=find_guide_y(ret,radius);
423 if(guide_y!=guide_list_y_.end())
429 floor(ret[0]/get_grid_size()[0]+0.5)*get_grid_size()[0],
430 floor(ret[1]/get_grid_size()[1]+0.5)*get_grid_size()[1]
433 if(abs(snap[0]-ret[0])<=radius && (!has_guide_x || abs(snap[0]-ret[0])<=abs(*guide_x-ret[0])))
434 ret[0]=snap[0],has_guide_x=false;
435 if(abs(snap[1]-ret[1])<=radius && (!has_guide_y || abs(snap[1]-ret[1])<=abs(*guide_y-ret[1])))
436 ret[1]=snap[1],has_guide_y=false;
450 if(abs(ret[0])<abs(ret[1]))
461 DuckDrag_Translate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
463 last_translate_=Vector(0,0);
465 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
470 const DuckList selected_ducks(duckmatic->get_selected_ducks());
471 DuckList::const_iterator iter;
474 for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
476 Point p((*iter)->get_trans_point());
477 positions.push_back(p);
482 DuckDrag_Translate::end_duck_drag(Duckmatic* duckmatic)
484 if(last_translate_.mag()>0.0001)
486 duckmatic->signal_edited_selected_ducks();
491 duckmatic->signal_user_click_selected_ducks(0);
497 DuckDrag_Translate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
499 const DuckList selected_ducks(duckmatic->get_selected_ducks());
500 DuckList::const_iterator iter;
502 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_);
505 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
507 if((*iter)->get_type()!=Duck::TYPE_VERTEX &&
508 (*iter)->get_type()!=Duck::TYPE_POSITION)
510 (*iter)->set_trans_point(positions[i]+vect);
512 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
514 if((*iter)->get_type()==Duck::TYPE_VERTEX ||
515 (*iter)->get_type()==Duck::TYPE_POSITION)
517 (*iter)->set_trans_point(positions[i]+vect);
519 last_translate_=vect;
523 Duckmatic::signal_edited_selected_ducks()
525 const DuckList ducks(get_selected_ducks());
526 DuckList::const_iterator iter;
528 synfig::GUIDSet old_set(selected_ducks);
530 // If we have more than 20 things to move, then display
531 // something to explain that it may take a moment
532 smart_ptr<OneMoment> wait; if(ducks.size()>20)wait.spawn();
534 // Go ahead and call everyone's signals
535 for(iter=ducks.begin();iter!=ducks.end();++iter)
537 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
539 if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
541 selected_ducks=old_set;
542 throw String("Bad edit");
547 if(!(*iter)->signal_edited()((*iter)->get_point()))
549 selected_ducks=old_set;
550 throw String("Bad edit");
554 selected_ducks=old_set;
558 Duckmatic::signal_user_click_selected_ducks(int button)
560 const DuckList ducks(get_selected_ducks());
561 DuckList::const_iterator iter;
563 for(iter=ducks.begin();iter!=ducks.end();++iter)
565 (*iter)->signal_user_click(button)();
570 Duckmatic::add_duck(const etl::handle<Duck> &duck)
572 //if(!duck_map.count(duck->get_guid()))
574 if(duck_data_share_map.count(duck->get_data_guid()))
576 duck->set_shared_point(duck_data_share_map[duck->get_data_guid()]);
580 etl::smart_ptr<synfig::Point> point(new Point(duck->get_point()));
581 duck->set_shared_point(point);
582 duck_data_share_map[duck->get_data_guid()]=point;
585 duck_map.insert(duck);
588 last_duck_guid=duck->get_guid();
592 Duckmatic::add_bezier(const etl::handle<Bezier> &bezier)
594 bezier_list_.push_back(bezier);
598 Duckmatic::add_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
600 assert(stroke_point_list);
602 std::list<etl::handle<Stroke> >::iterator iter;
604 for(iter=stroke_list_.begin();iter!=stroke_list_.end();++iter)
606 if((*iter)->stroke_data==stroke_point_list)
610 etl::handle<Stroke> stroke(new Stroke());
612 stroke->stroke_data=stroke_point_list;
615 stroke_list_.push_back(stroke);
619 Duckmatic::add_persistent_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
621 add_stroke(stroke_point_list,color);
622 persistent_stroke_list_.push_back(stroke_list_.back());
626 Duckmatic::clear_persistent_strokes()
628 persistent_stroke_list_.clear();
632 Duckmatic::set_show_persistent_strokes(bool x)
634 if(x!=show_persistent_strokes)
636 show_persistent_strokes=x;
638 stroke_list_=persistent_stroke_list_;
640 stroke_list_.clear();
645 Duckmatic::erase_duck(const etl::handle<Duck> &duck)
647 duck_map.erase(duck->get_guid());
650 etl::handle<Duckmatic::Duck>
651 Duckmatic::find_similar_duck(etl::handle<Duck> duck)
653 DuckMap::const_iterator iter(duck_map.find(duck->get_guid()));
654 if(iter!=duck_map.end())
658 /* std::list<handle<Duck> >::reverse_iterator iter;
660 for(iter=duck_list_.rbegin();iter!=duck_list_.rend();++iter)
662 if(*iter!=duck && **iter==*duck)
664 //synfig::info("Found similar duck! (iter:%08x vs. duck:%08x)",iter->get(), duck.get());
672 etl::handle<Duckmatic::Duck>
673 Duckmatic::add_similar_duck(etl::handle<Duck> duck)
675 etl::handle<Duck> similar(find_similar_duck(duck));
685 Duckmatic::erase_bezier(const etl::handle<Bezier> &bezier)
687 std::list<handle<Bezier> >::iterator iter;
689 for(iter=bezier_list_.begin();iter!=bezier_list_.end();++iter)
693 bezier_list_.erase(iter);
697 synfig::warning("Unable to find bezier to erase!");
700 etl::handle<Duckmatic::Duck>
701 Duckmatic::last_duck()const
703 DuckMap::const_iterator iter(duck_map.find(last_duck_guid));
704 if(iter!=duck_map.end())
709 etl::handle<Duckmatic::Bezier>
710 Duckmatic::last_bezier()const
712 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
818 Duckmatic::save_sketch(const synfig::String& filename)const
820 ChangeLocale change_locale(LC_NUMERIC, "C");
821 std::ofstream file(filename.c_str());
823 if(!file)return false;
825 file<<"SKETCH"<<endl;
827 std::list<etl::handle<Stroke> >::const_iterator iter;
829 for(iter=persistent_stroke_list_.begin();iter!=persistent_stroke_list_.end();++iter)
832 <<(*iter)->color.get_r()<<' '
833 <<(*iter)->color.get_g()<<' '
834 <<(*iter)->color.get_b()
836 std::list<synfig::Point>::const_iterator viter;
837 for(viter=(*iter)->stroke_data->begin();viter!=(*iter)->stroke_data->end();++viter)
845 if(!file)return false;
846 sketch_filename_=filename;
847 signal_sketch_saved_();
852 Duckmatic::load_sketch(const synfig::String& filename)
854 ChangeLocale change_locale(LC_NUMERIC, "C");
855 std::ifstream file(filename.c_str());
865 synfig::error("Not a sketch");
869 etl::smart_ptr<std::list<synfig::Point> > stroke_data;
885 if(!strscanf(line,"C %f %f %f",&r, &g, &b))
887 synfig::warning("Bad color line \"%s\"",line.c_str());
890 add_persistent_stroke(stroke_data, synfig::Color(r,g,b));
898 add_persistent_stroke(stroke_data, synfig::Color(0,0,0));
901 if(!strscanf(line,"V %f %f",&x, &y))
902 synfig::warning("Bad vertex \"%s\"",line.c_str());
904 stroke_data->push_back(synfig::Vector(x,y));
907 synfig::warning("Unexpected sketch token '%c'",line[0]);
912 sketch_filename_=filename;
916 Duckmatic::Push::Push(Duckmatic *duckmatic_):
917 duckmatic_(duckmatic_)
919 duck_map=duckmatic_->duck_map;
920 bezier_list_=duckmatic_->bezier_list_;
921 duck_data_share_map=duckmatic_->duck_data_share_map;
922 stroke_list_=duckmatic_->stroke_list_;
923 duck_dragger_=duckmatic_->duck_dragger_;
927 Duckmatic::Push::~Push()
934 Duckmatic::Push::restore()
936 duckmatic_->duck_map=duck_map;
937 duckmatic_->bezier_list_=bezier_list_;
938 duckmatic_->duck_data_share_map=duck_data_share_map;
939 duckmatic_->stroke_list_=stroke_list_;
940 duckmatic_->duck_dragger_=duck_dragger_;
944 inline String guid_string(const synfigapp::ValueDesc& x)
946 if(x.parent_is_layer_param())
947 return strprintf("%s",x.get_layer()->get_guid().get_string().c_str())+x.get_param_name();
948 //if(x.is_value_node())
949 return strprintf("%s",x.get_value_node()->get_guid().get_string().c_str());
952 inline GUID calc_duck_guid(const synfigapp::ValueDesc& x,const synfig::TransformStack& transform_stack)
956 if(x.parent_is_layer_param())
958 ret=x.get_layer()->get_guid()^GUID::hasher(x.get_param_name());
962 ret=x.get_value_node()->get_guid();
965 ret^=transform_stack.get_guid();
971 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)
973 GUID duck_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(modifier));
974 etl::handle<Duck> duck=new Duck();
981 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)
983 ValueBase::Type type=value_desc.get_value_type();
984 #define REAL_COOKIE reinterpret_cast<synfig::ParamDesc*>(28)
987 case ValueBase::TYPE_REAL:
989 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
991 etl::handle<Duck> duck=new Duck();
992 duck->set_transform_stack(transform_stack);
993 duck->set_radius(true);
994 duck->set_type(Duck::TYPE_RADIUS);
996 duck->set_point(Point(value_desc.get_value(get_time()).get(Real()),0));
997 duck->set_name(guid_string(value_desc));
998 if(value_desc.is_value_node())
1000 // If the ValueNode can be directly manipulated,
1001 // then set it as so.
1002 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1006 duck->set_editable(true);
1009 if(param_desc && param_desc!=REAL_COOKIE)
1011 if(!param_desc->get_origin().empty())
1013 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1015 duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1017 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1018 duck->set_origin(last_duck());
1020 duck->set_scalar(param_desc->get_scalar());
1023 duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_REAL:
1024 duck->signal_edited().connect(
1028 &studio::CanvasView::on_duck_changed
1033 duck->set_value_desc(value_desc);
1035 duck->signal_user_click(2).connect(
1040 &studio::CanvasView::popup_param_menu
1048 duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1056 case ValueBase::TYPE_ANGLE:
1058 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
1060 etl::handle<Duck> duck=new Duck();
1061 duck->set_type(Duck::TYPE_ANGLE);
1062 duck->set_transform_stack(transform_stack);
1063 synfig::Angle angle;
1065 angle=value_desc.get_value(get_time()).get(Angle());
1066 duck->set_point(Point(Angle::cos(angle).get(),Angle::sin(angle).get()));
1067 duck->set_name(guid_string(value_desc));
1068 if(value_desc.is_value_node())
1070 ValueNode::Handle value_node=value_desc.get_value_node();
1071 //duck->set_name(strprintf("%x",value_node.get()));
1073 // If the ValueNode can be directly manipulated,
1074 // then set it as so.
1075 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1079 //angle=(value_desc.get_value().get(Angle()));
1080 //duck->set_point(Point(Angle::cos(angle).get(),Angle::sin(angle).get()));
1081 //duck->set_name(strprintf("%x",value_desc.get_layer().get())+value_desc.get_param_name());
1082 duck->set_editable(true);
1085 if(param_desc && param_desc!=REAL_COOKIE)
1087 if(!param_desc->get_origin().empty())
1089 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1091 duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1093 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1094 duck->set_origin(last_duck());
1096 duck->set_scalar(param_desc->get_scalar());
1099 duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_ANGLE:
1100 duck->signal_edited_angle().clear();
1101 duck->signal_edited_angle().connect(
1105 &studio::CanvasView::on_duck_angle_changed
1110 duck->set_value_desc(value_desc);
1112 duck->signal_user_click(2).connect(
1117 &studio::CanvasView::popup_param_menu
1124 duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1132 case ValueBase::TYPE_VECTOR:
1134 etl::handle<Duck> duck=new Duck();
1135 duck->set_transform_stack(transform_stack);
1137 duck->set_point(value_desc.get_value(get_time()).get(Point()));
1138 duck->set_name(guid_string(value_desc));
1139 if(value_desc.is_value_node())
1141 //duck->set_name(strprintf("%x",value_desc.get_value_node().get()));
1143 // If the ValueNode can be directly manipulated,
1144 // then set it as so.
1145 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1149 //duck->set_point(value_desc.get_value().get(Point()));
1150 //duck->set_name(strprintf("%x",value_desc.get_layer().get())+value_desc.get_param_name());
1151 duck->set_editable(true);
1154 // If we were passed a parameter description
1157 if(!param_desc->get_connect().empty())
1159 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_connect());
1160 Duck::Handle connect_duck;
1161 if(duck_map.find(calc_duck_guid(value_desc_origin,transform_stack)^GUID::hasher(0))!=duck_map.end())
1163 connect_duck=duck_map[calc_duck_guid(value_desc_origin,transform_stack)^GUID::hasher(0)];
1167 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1168 connect_duck=last_duck();
1170 duck->set_connect_duck(connect_duck);
1172 if(!param_desc->get_box().empty())
1174 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_box());
1175 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1176 duck->set_box_duck(last_duck());
1179 // If we have an origin
1180 if(!param_desc->get_origin().empty())
1182 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1184 duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1186 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1187 duck->set_origin(last_duck());
1188 duck->set_type(Duck::TYPE_VERTEX);
1191 duck->set_type(Duck::TYPE_POSITION);
1193 duck->set_scalar(param_desc->get_scalar());
1196 duck->set_type(Duck::TYPE_POSITION);
1198 duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_VECTOR:
1199 duck->signal_edited().connect(
1203 &studio::CanvasView::on_duck_changed
1208 duck->set_value_desc(value_desc);
1210 duck->signal_user_click(2).connect(
1215 &studio::CanvasView::popup_param_menu
1222 duck->set_guid(calc_duck_guid(value_desc,transform_stack)^GUID::hasher(multiple));
1228 /* case ValueBase::TYPE_SEGMENT:
1230 etl::handle<Bezier> bezier(new Bezier());
1231 ValueNode_Composite::Handle value_node;
1233 if(value_desc.is_value_node() &&
1234 (value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
1237 if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
1239 bezier->p1=last_duck();
1240 bezier->p1->set_type(Duck::TYPE_VERTEX);
1241 if(!add_to_ducks(synfigapp::ValueDesc(value_node,1),canvas_view,transform_stack))
1243 bezier->c1=last_duck();
1244 bezier->c1->set_type(Duck::TYPE_TANGENT);
1245 bezier->c1->set_origin(bezier->p1);
1246 bezier->c1->set_scalar(TANGENT_BEZIER_SCALE);
1247 bezier->c1->set_tangent(true);
1249 if(!add_to_ducks(synfigapp::ValueDesc(value_node,2),canvas_view,transform_stack))
1251 bezier->p2=last_duck();
1252 bezier->p2->set_type(Duck::TYPE_VERTEX);
1253 if(!add_to_ducks(synfigapp::ValueDesc(value_node,3),canvas_view,transform_stack))
1255 bezier->c2=last_duck();
1256 bezier->c2->set_type(Duck::TYPE_TANGENT);
1257 bezier->c2->set_origin(bezier->p2);
1258 bezier->c2->set_scalar(-TANGENT_BEZIER_SCALE);
1259 bezier->c2->set_tangent(true);
1261 bezier->signal_user_click(2).connect(
1265 &studio::CanvasView::popup_param_menu_bezier
1273 else if(value_desc.get_value().is_valid())
1275 Segment segment=value_desc.get_value();
1276 etl::handle<Duck> duck_p,duck_c;
1277 synfig::String name;
1280 name=param_desc->get_local_name();
1284 name=guid_string(value_desc);
1287 duck_p=new class Duck(segment.p1);
1288 duck_p->set_name(name+".P1");
1289 duck_p->set_type(Duck::TYPE_VERTEX);
1292 duck_c=new class Duck(segment.t1);
1293 duck_c->set_name(name+".T1");
1294 duck_c->set_type(Duck::TYPE_TANGENT);
1296 duck_c->set_origin(duck_p);
1297 duck_c->set_scalar(TANGENT_HANDLE_SCALE);
1298 duck_c->set_tangent(true);
1303 duck_p=new class Duck(segment.p2);
1304 duck_p->set_name(name+".P2");
1305 duck_p->set_type(Duck::TYPE_VERTEX);
1308 duck_c=new class Duck(segment.t2);
1309 duck_c->set_type(Duck::TYPE_TANGENT);
1310 duck_c->set_name(name+".T2");
1312 duck_c->set_origin(duck_p);
1313 duck_c->set_scalar(-TANGENT_HANDLE_SCALE);
1314 duck_c->set_tangent(true);
1325 case ValueBase::TYPE_BLINEPOINT:
1328 if(value_desc.is_value_node() &&
1329 ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
1332 ValueNode_Composite::Handle value_node;
1333 value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node());
1335 if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
1337 etl::handle<Duck> vertex_duck(last_duck());
1338 vertex_duck->set_type(Duck::TYPE_VERTEX);
1339 if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
1341 etl::handle<Duck> t1_duck(last_duck());
1343 t1_duck->set_origin(vertex_duck);
1344 t1_duck->set_scalar(-TANGENT_HANDLE_SCALE);
1345 t1_duck->set_tangent(true);
1347 etl::handle<Duck> t2_duck;
1349 // If the tangents are split
1350 if((*value_node->get_link("split"))(get_time()).get(bool()))
1352 if(!add_to_ducks(synfigapp::ValueDesc(value_node,5),canvas_view,transform_stack))
1354 t2_duck=last_duck();
1355 t2_duck->set_origin(vertex_duck);
1356 t2_duck->set_scalar(TANGENT_HANDLE_SCALE);
1357 t2_duck->set_tangent(true);
1361 if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
1363 t2_duck=last_duck();
1364 t2_duck->set_origin(vertex_duck);
1365 t2_duck->set_scalar(TANGENT_HANDLE_SCALE);
1366 t2_duck->set_tangent(true);
1373 case ValueBase::TYPE_LIST:
1376 if(value_desc.is_value_node() &&
1377 ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node())
1380 ValueNode_BLine::Handle value_node;
1381 value_node=ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node());
1385 etl::handle<Bezier> bezier;
1386 etl::handle<Duck> first_duck;
1387 etl::handle<Duck> duck, tduck;
1389 for(i=0;i<value_node->link_count();i++)
1391 float amount(value_node->list[i].amount_at_time(get_time()));
1394 if(first==-1)first=i;
1396 BLinePoint bline_point((*value_node->get_link(i))(get_time()));
1398 ValueNode_Composite::Handle vertex_value_node(
1399 ValueNode_Composite::Handle::cast_dynamic(
1400 value_node->get_link(i)
1404 // Add the vertex duck
1405 if(vertex_value_node)
1407 if(add_to_ducks(synfigapp::ValueDesc(vertex_value_node,0),canvas_view,transform_stack))
1412 duck->set_type(Duck::TYPE_VERTEX);
1414 duck->signal_user_click(2).clear();
1415 duck->signal_user_click(2).connect(
1420 &studio::CanvasView::popup_param_menu
1424 synfigapp::ValueDesc(value_node,i)
1427 duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1431 if(!param_desc->get_origin().empty())
1433 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1434 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1435 duck->set_origin(last_duck());
1437 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1438 if(value.same_type_as(synfig::Point()))
1439 duck->set_origin(value.get(synfig::Point()));
1449 duck=new Duck(bline_point.get_vertex());
1452 duck->set_transform_stack(transform_stack);
1453 duck->set_editable(false);
1454 //duck->set_name(strprintf("%x-vertex",value_node->get_link(i).get()));
1455 duck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".v");
1457 duck->set_type(Duck::TYPE_VERTEX);
1460 if(!param_desc->get_origin().empty())
1462 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1463 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1464 duck->set_origin(last_duck());
1466 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1467 if(value.same_type_as(synfig::Point()))
1468 duck->set_origin(value.get(synfig::Point()));
1472 duck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".v"));
1473 duck=add_similar_duck(duck);
1477 // Add the width duck only if we have a hint of scale
1478 if(param_desc && !param_desc->get_hint().empty())
1480 etl::handle<Duck> width;
1481 if(add_to_ducks(synfigapp::ValueDesc(vertex_value_node,1),canvas_view,transform_stack,REAL_COOKIE))
1484 width->set_origin(duck);
1485 width->set_type(Duck::TYPE_WIDTH);
1486 width->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".w");
1488 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_hint()).get_value(get_time()));
1489 if(value.same_type_as(synfig::Real()))
1490 width->set_scalar(value.get(synfig::Real())*0.5f);
1494 synfig::error("Unable to add width duck!");
1499 // Add the tangent1 duck
1500 if(vertex_value_node)
1502 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,4),canvas_view,transform_stack))
1508 tduck=new Duck(bline_point.get_tangent1());
1509 tduck->set_transform_stack(transform_stack);
1510 tduck->set_editable(false);
1511 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t1");
1512 // tduck->set_name(strprintf("%x-tangent1",value_node->get_link(i).get()));
1513 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".t1"));
1514 tduck=add_similar_duck(tduck);
1518 tduck->set_origin(duck);
1519 tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1520 tduck->set_tangent(true);
1525 bezier->signal_user_click(2).connect(
1529 &studio::CanvasView::popup_param_menu_bezier
1531 synfigapp::ValueDesc(value_node,i)
1535 duck->signal_user_click(2).clear();
1536 duck->signal_user_click(2).connect(
1541 &studio::CanvasView::popup_param_menu
1545 synfigapp::ValueDesc(value_node,i)
1548 duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1554 if(i+1>=value_node->link_count() && !value_node->get_loop())
1557 bezier=new Bezier();
1559 // Add the tangent2 duck
1560 if(vertex_value_node)
1562 int i=bline_point.get_split_tangent_flag()?5:4;
1563 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,i),canvas_view,transform_stack,0,2))
1569 if(bline_point.get_split_tangent_flag())
1571 tduck=new Duck(bline_point.get_tangent2());
1572 tduck->set_transform_stack(transform_stack);
1573 //tduck->set_name(strprintf("%x-tangent2",value_node->get_link(i).get()));
1574 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t2");
1575 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".t2"));
1579 tduck=new Duck(bline_point.get_tangent1());
1580 tduck->set_transform_stack(transform_stack);
1581 //tduck->set_name(strprintf("%x-tangent1",value_node->get_link(i).get()));
1582 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t1");
1583 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".t1"));
1585 tduck->set_editable(false);
1586 tduck=add_similar_duck(tduck);
1590 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1591 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1592 duck->set_origin(last_duck());
1594 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1595 if(value.same_type_as(synfig::Point()))
1596 duck->set_origin(value.get(synfig::Point()));
1598 // if(!param_desc->get_origin().empty())
1599 // duck->set_origin(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()).get(synfig::Point()));
1601 duck->signal_user_click(2).clear();
1602 duck->signal_user_click(2).connect(
1607 &studio::CanvasView::popup_param_menu
1611 synfigapp::ValueDesc(value_node,0)
1614 duck->set_value_desc(synfigapp::ValueDesc(value_node,0));
1618 tduck->set_origin(duck);
1619 tduck->set_scalar(TANGENT_BEZIER_SCALE);
1620 tduck->set_tangent(true);
1626 // Loop if necessary
1627 if(bezier && value_node->get_loop())
1629 BLinePoint bline_point((*value_node->get_link(first))(get_time()));
1631 ValueNode_Composite::Handle vertex_value_node(
1632 ValueNode_Composite::Handle::cast_dynamic(
1633 value_node->get_link(first)
1637 // Add the vertex duck
1640 if(vertex_value_node)
1642 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,0),canvas_view,transform_stack))
1647 if(!param_desc->get_origin().empty())
1648 duck->set_origin(value_desc.get_layer()->get_param(param_desc->get_origin()).get(synfig::Point()));
1650 duck->set_type(Duck::TYPE_VERTEX);
1654 duck=new Duck(bline_point.get_vertex());
1655 duck->set_transform_stack(transform_stack);
1658 if(!param_desc->get_origin().empty())
1659 duck->set_origin(value_desc.get_layer()->get_param(param_desc->get_origin()).get(synfig::Point()));
1661 duck->set_editable(false);
1662 duck->set_name(strprintf("%x-vertex",value_node->get_link(first).get()));
1663 duck->set_type(Duck::TYPE_VERTEX);
1664 duck=add_similar_duck(duck);
1668 // Add the tangent1 duck
1669 if(vertex_value_node)
1671 if(!add_to_ducks(synfigapp::ValueDesc(vertex_value_node,4),canvas_view,transform_stack))
1677 tduck=new Duck(bline_point.get_tangent1());
1678 tduck->set_transform_stack(transform_stack);
1679 tduck->set_editable(false);
1680 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,first))+".t1");
1681 //tduck->set_name(strprintf("%x-tangent1",value_node->get_link(first).get()));
1682 tduck=add_similar_duck(tduck);
1683 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^GUID::hasher(".t1"));
1687 tduck->set_origin(duck);
1688 tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1689 tduck->set_tangent(true);
1693 bezier->signal_user_click(2).connect(
1697 &studio::CanvasView::popup_param_menu_bezier
1699 synfigapp::ValueDesc(value_node,first)
1702 duck->signal_user_click(2).clear();
1703 duck->signal_user_click(2).connect(
1708 &studio::CanvasView::popup_param_menu
1712 synfigapp::ValueDesc(value_node,first)
1715 duck->set_value_desc(synfigapp::ValueDesc(value_node,first));
1723 else // Check for DynamicList
1724 if(value_desc.is_value_node() &&
1725 ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node())
1728 ValueNode_DynamicList::Handle value_node;
1729 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node());
1732 if(value_node->get_contained_type()==ValueBase::TYPE_VECTOR)
1735 for(i=0;i<value_node->link_count();i++)
1737 if(!value_node->list[i].status_at_time(get_time()))
1739 if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))
1741 etl::handle<Duck> duck(last_duck());
1745 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1746 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1747 duck->set_origin(last_duck());
1749 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1750 if(value.same_type_as(synfig::Point()))
1751 duck->set_origin(value.get(synfig::Point()));
1753 // if(!param_desc->get_origin().empty())
1754 // last_duck()->set_origin(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()).get(synfig::Point()));
1756 duck->set_type(Duck::TYPE_VERTEX);
1757 bezier.p1=bezier.p2;bezier.c1=bezier.c2;
1758 bezier.p2=bezier.c2=duck;
1762 handle<Bezier> bezier_(new Bezier());
1763 bezier_->p1=bezier.p1;
1764 bezier_->c1=bezier.c1;
1765 bezier_->p2=bezier.p2;
1766 bezier_->c2=bezier.c2;
1767 add_bezier(bezier_);
1768 last_bezier()->signal_user_click(2).connect(
1772 &studio::CanvasView::popup_param_menu_bezier
1774 synfigapp::ValueDesc(value_node,i)
1780 /*else if(value_node->get_contained_type()==ValueBase::TYPE_SEGMENT)
1782 for(i=0;i<value_node->link_count();i++)
1784 if(!value_node->list[i].status_at_time(get_time()))
1786 if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))