1 /* === S Y N F I G ========================================================= */
2 /*! \file valuedescset.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 ======================================================= */
33 #include "layerparamset.h"
34 #include "valuenodeconstset.h"
35 #include "valuedescconnect.h"
36 #include "waypointsetsmart.h"
38 #include "valuedescset.h"
39 #include <synfigapp/canvasinterface.h>
40 #include <synfig/valuenode_composite.h>
41 #include <synfig/valuenode_radialcomposite.h>
42 #include <synfig/valuenode_reference.h>
43 #include <synfigapp/main.h>
45 #include <synfigapp/general.h>
51 using namespace synfig;
52 using namespace synfigapp;
53 using namespace Action;
55 /* === M A C R O S ========================================================= */
57 ACTION_INIT_NO_GET_LOCAL_NAME(Action::ValueDescSet);
58 ACTION_SET_NAME(Action::ValueDescSet,"value_desc_set");
59 ACTION_SET_LOCAL_NAME(Action::ValueDescSet,N_("Set ValueDesc"));
60 ACTION_SET_TASK(Action::ValueDescSet,"set");
61 ACTION_SET_CATEGORY(Action::ValueDescSet,Action::CATEGORY_VALUEDESC);
62 ACTION_SET_PRIORITY(Action::ValueDescSet,0);
63 ACTION_SET_VERSION(Action::ValueDescSet,"0.0");
64 ACTION_SET_CVS_ID(Action::ValueDescSet,"$Id$");
66 /* === G L O B A L S ======================================================= */
68 /* === P R O C E D U R E S ================================================= */
70 /* === M E T H O D S ======================================================= */
72 Action::ValueDescSet::ValueDescSet():
78 Action::ValueDescSet::get_local_name()const
80 String name("ValueDesc");
85 else if(value_desc.parent_is_layer_param())
87 name = strprintf("'%s' -> %s", // layer -> parameter
88 value_desc.get_layer()->get_non_empty_description().c_str(),
89 value_desc.get_param_name().c_str());
90 if (value_desc.is_exported())
91 name += strprintf(" (%s)", value_desc.get_value_node()->get_id().c_str());
93 else if(value_desc.parent_is_value_node())
95 synfig::LinkableValueNode::Handle value_node(synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node()));
97 for(node=value_node.get();!node->parent_set.empty() && !dynamic_cast<Layer*>(node);node=*node->parent_set.begin());
98 Layer::Handle parent_layer(dynamic_cast<Layer*>(node));
100 name = strprintf("'%s' => %s", // layer -> sub-parameter
101 parent_layer->get_non_empty_description().c_str(),
102 value_node->link_local_name(value_desc.get_index()).c_str());
104 name = value_node->link_local_name(value_desc.get_index()); // sub-parameter
105 if (value_desc.is_exported())
106 name += strprintf(" (%s)", value_desc.get_value_node()->get_id().c_str());
108 else if (value_desc.is_exported())
109 name = strprintf(_("ValueNode (%s)"), value_desc.get_value_node()->get_id().c_str());
111 return strprintf(_("Set %s"), name.c_str());
115 Action::ValueDescSet::get_param_vocab()
117 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
119 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
120 .set_local_name(_("ValueDesc"))
123 ret.push_back(ParamDesc("new_value",Param::TYPE_VALUE)
124 .set_local_name(_("ValueBase"))
127 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
128 .set_local_name(_("Time"))
136 Action::ValueDescSet::is_candidate(const ParamList &x)
138 return candidate_check(get_param_vocab(),x);
142 Action::ValueDescSet::set_param(const synfig::String& name, const Action::Param ¶m)
144 if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
146 value_desc=param.get_value_desc();
151 if(name=="new_value" && param.get_type()==Param::TYPE_VALUE)
153 value=param.get_value();
158 if(name=="time" && param.get_type()==Param::TYPE_TIME)
160 time=param.get_time();
165 return Action::CanvasSpecific::set_param(name,param);
169 Action::ValueDescSet::is_ready()const
171 if(!value_desc || !value.is_valid())
173 return Action::CanvasSpecific::is_ready();
177 Action::ValueDescSet::prepare()
181 // If our tangents are merged, and
182 // our first tangent is being manipulated,
183 // then we also need to adjust the other
185 if( value_desc.parent_is_value_node() &&
186 value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT &&
187 (value_desc.get_index()==4 || value_desc.get_index()==5) &&
188 (*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
190 printf("a tangent got changed - #%d\n", value_desc.get_index()-3);
193 ValueNode_Composite::Handle parent_value_node;
194 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
195 assert(parent_value_node);
197 Vector t1((*parent_value_node->get_link("t1"))(time));
198 Vector t2((*parent_value_node->get_link("t2"))(time));
199 printf("current values are: t1(%.2f, %2.f) t2(%.2f, %.2f)\n", t1[0], t1[1], t2[0], t2[1]);
202 if (value_desc.get_index()==4) {
203 printf("copying change to tangent 2\n");
204 ValueNode_Composite::Handle parent_value_node;
205 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
207 assert(parent_value_node);
209 Action::Handle action(Action::create("value_desc_set"));
212 throw Error(_("Unable to find action value_desc_set (bug)"));
214 action->set_param("canvas",get_canvas());
215 action->set_param("canvas_interface",get_canvas_interface());
216 action->set_param("time",time);
217 action->set_param("new_value",value);
218 action->set_param("value_desc",ValueDesc(parent_value_node,5));
220 if(!action->is_ready())
221 throw Error(Error::TYPE_NOTREADY);
227 // If we are a reference value node, then
228 // we need to distribute the changes to the
229 // referenced value node
230 if(value_desc.is_value_node() && ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()))
232 ValueDesc reference_value_desc(ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()),0);
234 Action::Handle action(Action::create("value_desc_set"));
237 throw Error(_("Unable to find action value_desc_set (bug)"));
239 action->set_param("canvas",get_canvas());
240 action->set_param("canvas_interface",get_canvas_interface());
241 action->set_param("time",time);
242 action->set_param("new_value",value);
243 action->set_param("value_desc",reference_value_desc);
245 if(!action->is_ready())
246 throw Error(Error::TYPE_NOTREADY);
253 // If we are a composite value node, then
254 // we need to distribute the changes to the
256 if(value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
258 ValueBase components[6];
260 switch(value.get_type())
262 case ValueBase::TYPE_VECTOR:
263 components[0]=value.get(Vector())[0];
264 components[1]=value.get(Vector())[1];
267 case ValueBase::TYPE_COLOR:
268 components[0]=value.get(Color()).get_r();
269 components[1]=value.get(Color()).get_g();
270 components[2]=value.get(Color()).get_b();
271 components[3]=value.get(Color()).get_a();
274 case ValueBase::TYPE_SEGMENT:
275 components[0]=value.get(Segment()).p1;
276 components[1]=value.get(Segment()).t1;
277 components[2]=value.get(Segment()).p2;
278 components[3]=value.get(Segment()).t2;
281 case ValueBase::TYPE_BLINEPOINT:
283 BLinePoint bline_point(value);
284 components[0]=bline_point.get_vertex();
285 components[1]=bline_point.get_width();
286 components[2]=bline_point.get_origin();
287 components[3]=bline_point.get_split_tangent_flag();
288 components[4]=bline_point.get_tangent1();
289 components[5]=bline_point.get_tangent2();
294 throw Error(_("Bad type for composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
298 for(int i=0;i<n_components;i++)
300 ValueDesc component_value_desc(ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()),i);
302 Action::Handle action(Action::create("value_desc_set"));
305 throw Error(_("Unable to find action value_desc_set (bug)"));
307 action->set_param("canvas",get_canvas());
308 action->set_param("canvas_interface",get_canvas_interface());
309 action->set_param("time",time);
310 action->set_param("new_value",components[i]);
311 action->set_param("value_desc",component_value_desc);
313 if(!action->is_ready())
314 throw Error(Error::TYPE_NOTREADY);
323 // If we are a RADIAL composite value node, then
324 // we need to distribute the changes to the
326 if(value_desc.is_value_node() && ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()))
328 ValueBase components[6];
330 switch(value.get_type())
332 case ValueBase::TYPE_VECTOR:
334 Angle old_angle = (*(ValueNode_RadialComposite::Handle::cast_dynamic(
335 value_desc.get_value_node())->get_link_vfunc(1)))(time).get(Angle());
336 Vector vect(value.get(Vector()));
337 components[0]=vect.mag();
338 Angle change = Angle(Angle::tan(vect[1],vect[0])) - old_angle;
339 while (change < Angle::deg(-180)) change += Angle::deg(360);
340 while (change > Angle::deg(180)) change -= Angle::deg(360);
341 components[1]=old_angle + change;
345 case ValueBase::TYPE_COLOR:
346 components[0]=value.get(Color()).get_y();
347 components[1]=value.get(Color()).get_s();
348 components[2]=value.get(Color()).get_hue();
349 components[3]=value.get(Color()).get_a();
353 throw Error(_("Bad type for radial composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
356 for(int i=0;i<n_components;i++)
358 ValueDesc component_value_desc(ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()),i);
360 Action::Handle action(Action::create("value_desc_set"));
363 throw Error(_("Unable to find action value_desc_set (bug)"));
365 action->set_param("canvas",get_canvas());
366 action->set_param("canvas_interface",get_canvas_interface());
367 action->set_param("time",time);
368 action->set_param("new_value",components[i]);
369 action->set_param("value_desc",component_value_desc);
371 if(!action->is_ready())
372 throw Error(Error::TYPE_NOTREADY);
380 // If we are merging the tangents of a BLinePoint,
381 // we must also set the second tangent for things
382 // to interpolate properly
383 if (value_desc.parent_is_value_node() &&
384 value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT &&
385 value_desc.get_index()==3)
387 ValueNode_Composite::Handle parent_value_node;
388 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
390 assert(parent_value_node);
392 // are we splitting or merging the tangents?
393 if (value.get(bool()))
395 // we are splitting tangents
397 Action::Handle action(Action::create("value_desc_set"));
400 throw Error(_("Unable to find action value_desc_set (bug)"));
402 action->set_param("canvas",get_canvas());
403 action->set_param("canvas_interface",get_canvas_interface());
404 action->set_param("time",time);
405 action->set_param("new_value",(*parent_value_node->get_link(4))(time));
406 action->set_param("value_desc",ValueDesc(parent_value_node,5));
408 if(!action->is_ready())
409 throw Error(Error::TYPE_NOTREADY);
415 // we are merging tangents
417 // the merged tangent should be the average of the 2 tangents we're merging
418 ValueBase average(((Vector)((*parent_value_node->get_link("t1"))(time)) +
419 (Vector)((*parent_value_node->get_link("t2"))(time))) / 2);
422 Action::Handle action(Action::create("value_desc_set"));
425 throw Error(_("Unable to find action value_desc_set (bug)"));
427 action->set_param("canvas",get_canvas());
428 action->set_param("canvas_interface",get_canvas_interface());
429 action->set_param("time",time);
430 action->set_param("new_value",average);
431 action->set_param("value_desc",ValueDesc(parent_value_node,4));
433 if(!action->is_ready())
434 throw Error(Error::TYPE_NOTREADY);
440 Action::Handle action(Action::create("value_desc_set"));
443 throw Error(_("Unable to find action value_desc_set (bug)"));
445 action->set_param("canvas",get_canvas());
446 action->set_param("canvas_interface",get_canvas_interface());
447 action->set_param("time",time);
448 action->set_param("new_value",average);
449 action->set_param("value_desc",ValueDesc(parent_value_node,5));
451 if(!action->is_ready())
452 throw Error(Error::TYPE_NOTREADY);
461 if( value_desc.parent_is_value_node())
464 if(value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT)
467 if(value_desc.get_index()==4)
470 if((*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
480 // If we are in animate editing mode
481 if(get_edit_mode()&MODE_ANIMATE)
484 ValueNode_Animated::Handle& value_node(value_node_animated);
486 // If this value isn't a ValueNode_Animated, but
487 // it is somewhat constant, then go ahead and convert
488 // it to a ValueNode_Animated.
489 if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
492 if(value_desc.is_value_node())
493 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
495 value=value_desc.get_value();
497 if(!value_node)value_node=ValueNode_Animated::create(value,time);
498 //if(!value_node)value_node=ValueNode_Animated::create(value.get_type());
500 Action::Handle action;
502 if(!value_desc.is_value_node())
504 action=(ValueDescConnect::create());
505 action->set_param("dest",value_desc);
506 action->set_param("src",ValueNode::Handle(value_node));
510 action=Action::create("value_node_replace");
511 action->set_param("dest",value_desc.get_value_node());
512 action->set_param("src",ValueNode::Handle(value_node));
515 action->set_param("canvas",get_canvas());
516 action->set_param("canvas_interface",get_canvas_interface());
518 if(!action->is_ready())
519 throw Error(Error::TYPE_NOTREADY);
521 add_action_front(action);
525 value_node=value_node.cast_dynamic(value_desc.get_value_node());
529 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
531 Action::Handle action(WaypointSetSmart::create());
533 //Waypoint waypoint(value,time);
535 Waypoint waypoint(value_node->new_waypoint_at_time(time));
536 waypoint.set_value(value);
538 waypoint.set_before(synfigapp::Main::get_interpolation());
539 waypoint.set_after(synfigapp::Main::get_interpolation());
541 action->set_param("canvas",get_canvas());
542 action->set_param("canvas_interface",get_canvas_interface());
543 action->set_param("value_node",ValueNode::Handle(value_node));
544 action->set_param("waypoint",waypoint);
546 if(!action->is_ready())
547 throw Error(Error::TYPE_NOTREADY);
555 if(value_desc.is_value_node())
557 if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
559 Action::Handle action(ValueNodeConstSet::create());
561 action->set_param("canvas",get_canvas());
562 action->set_param("canvas_interface",get_canvas_interface());
563 action->set_param("value_node",value_desc.get_value_node());
564 action->set_param("new_value",value);
566 if(!action->is_ready())
567 throw Error(Error::TYPE_NOTREADY);
569 add_action_front(action);
573 if(ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()))
574 throw Error(_("You must be in Animate-Editing-Mode to directly manipulate this value"));
576 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
579 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
581 Action::Handle layer_param_set(LayerParamSet::create());
583 layer_param_set->set_param("canvas",get_canvas());
584 layer_param_set->set_param("canvas_interface",get_canvas_interface());
585 layer_param_set->set_param("layer",value_desc.get_layer());
586 layer_param_set->set_param("param",value_desc.get_param_name());
587 layer_param_set->set_param("new_value",value);
589 if(!layer_param_set->is_ready())
590 throw Error(Error::TYPE_NOTREADY);
592 add_action_front(layer_param_set);
596 throw Error(_("Unsupported ValueDesc type"));