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())
86 name = strprintf(_("'%s' -> %s"),
87 value_desc.get_layer()->get_non_empty_description().c_str(),
88 value_desc.get_param_name().c_str());
89 else if(value_desc.parent_is_value_node())
91 synfig::LinkableValueNode::Handle value_node(synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node()));
93 for(node=value_node.get();!node->parent_set.empty() && !dynamic_cast<Layer*>(node);node=*node->parent_set.begin());
94 Layer::Handle parent_layer(dynamic_cast<Layer*>(node));
96 name = strprintf(_("'%s' => %s"),
97 parent_layer->get_non_empty_description().c_str(),
98 value_node->link_local_name(value_desc.get_index()).c_str());
100 name = value_node->link_local_name(value_desc.get_index());
103 return strprintf(_("Set %s"), name.c_str());
107 Action::ValueDescSet::get_param_vocab()
109 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
111 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
112 .set_local_name(_("ValueDesc"))
115 ret.push_back(ParamDesc("new_value",Param::TYPE_VALUE)
116 .set_local_name(_("ValueBase"))
119 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
120 .set_local_name(_("Time"))
128 Action::ValueDescSet::is_candidate(const ParamList &x)
130 return candidate_check(get_param_vocab(),x);
134 Action::ValueDescSet::set_param(const synfig::String& name, const Action::Param ¶m)
136 if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
138 value_desc=param.get_value_desc();
143 if(name=="new_value" && param.get_type()==Param::TYPE_VALUE)
145 value=param.get_value();
150 if(name=="time" && param.get_type()==Param::TYPE_TIME)
152 time=param.get_time();
157 return Action::CanvasSpecific::set_param(name,param);
161 Action::ValueDescSet::is_ready()const
163 if(!value_desc || !value.is_valid())
165 return Action::CanvasSpecific::is_ready();
169 Action::ValueDescSet::prepare()
173 // If our tangents are merged, and
174 // our first tangent is being manipulated,
175 // then we also need to adjust the other
177 if( value_desc.parent_is_value_node() &&
178 value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT &&
179 (value_desc.get_index()==4 || value_desc.get_index()==5) &&
180 (*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
182 printf("a tangent got changed - #%d\n", value_desc.get_index()-3);
185 ValueNode_Composite::Handle parent_value_node;
186 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
187 assert(parent_value_node);
189 Vector t1((*parent_value_node->get_link("t1"))(time));
190 Vector t2((*parent_value_node->get_link("t2"))(time));
191 printf("current values are: t1(%.2f, %2.f) t2(%.2f, %.2f)\n", t1[0], t1[1], t2[0], t2[1]);
194 if (value_desc.get_index()==4) {
195 printf("copying change to tangent 2\n");
197 ValueNode_Composite::Handle parent_value_node;
198 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
200 assert(parent_value_node);
202 Action::Handle action(Action::create("value_desc_set"));
205 throw Error(_("Unable to find action value_desc_set (bug)"));
207 action->set_param("canvas",get_canvas());
208 action->set_param("canvas_interface",get_canvas_interface());
209 action->set_param("time",time);
210 action->set_param("new_value",value);
211 action->set_param("value_desc",ValueDesc(parent_value_node,5));
213 if(!action->is_ready())
214 throw Error(Error::TYPE_NOTREADY);
220 // If we are a reference value node, then
221 // we need to distribute the changes to the
222 // referenced value node
223 if(value_desc.is_value_node() && ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()))
225 ValueDesc reference_value_desc(ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()),0);
227 Action::Handle action(Action::create("value_desc_set"));
230 throw Error(_("Unable to find action value_desc_set (bug)"));
232 action->set_param("canvas",get_canvas());
233 action->set_param("canvas_interface",get_canvas_interface());
234 action->set_param("time",time);
235 action->set_param("new_value",value);
236 action->set_param("value_desc",reference_value_desc);
238 if(!action->is_ready())
239 throw Error(Error::TYPE_NOTREADY);
246 // If we are a composite value node, then
247 // we need to distribute the changes to the
249 if(value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
251 ValueBase components[6];
253 switch(value.get_type())
255 case ValueBase::TYPE_VECTOR:
256 components[0]=value.get(Vector())[0];
257 components[1]=value.get(Vector())[1];
260 case ValueBase::TYPE_COLOR:
261 components[0]=value.get(Color()).get_r();
262 components[1]=value.get(Color()).get_g();
263 components[2]=value.get(Color()).get_b();
264 components[3]=value.get(Color()).get_a();
267 case ValueBase::TYPE_SEGMENT:
268 components[0]=value.get(Segment()).p1;
269 components[1]=value.get(Segment()).t1;
270 components[2]=value.get(Segment()).p2;
271 components[3]=value.get(Segment()).t2;
274 case ValueBase::TYPE_BLINEPOINT:
276 BLinePoint bline_point(value);
277 components[0]=bline_point.get_vertex();
278 components[1]=bline_point.get_width();
279 components[2]=bline_point.get_origin();
280 components[3]=bline_point.get_split_tangent_flag();
281 components[4]=bline_point.get_tangent1();
282 components[5]=bline_point.get_tangent2();
287 throw Error(_("Bad type for composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
291 for(int i=0;i<n_components;i++)
293 ValueDesc component_value_desc(ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()),i);
295 Action::Handle action(Action::create("value_desc_set"));
298 throw Error(_("Unable to find action value_desc_set (bug)"));
300 action->set_param("canvas",get_canvas());
301 action->set_param("canvas_interface",get_canvas_interface());
302 action->set_param("time",time);
303 action->set_param("new_value",components[i]);
304 action->set_param("value_desc",component_value_desc);
306 if(!action->is_ready())
307 throw Error(Error::TYPE_NOTREADY);
316 // If we are a RADIAL composite value node, then
317 // we need to distribute the changes to the
319 if(value_desc.is_value_node() && ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()))
321 ValueBase components[6];
323 switch(value.get_type())
325 case ValueBase::TYPE_VECTOR:
327 Angle old_angle = (*(ValueNode_RadialComposite::Handle::cast_dynamic(
328 value_desc.get_value_node())->get_link_vfunc(1)))(time).get(Angle());
329 Vector vect(value.get(Vector()));
330 components[0]=vect.mag();
331 Angle change = Angle(Angle::tan(vect[1],vect[0])) - old_angle;
332 while (change < Angle::deg(-180)) change += Angle::deg(360);
333 while (change > Angle::deg(180)) change -= Angle::deg(360);
334 components[1]=old_angle + change;
338 case ValueBase::TYPE_COLOR:
339 components[0]=value.get(Color()).get_y();
340 components[1]=value.get(Color()).get_s();
341 components[2]=value.get(Color()).get_hue();
342 components[3]=value.get(Color()).get_a();
346 throw Error(_("Bad type for radial composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
349 for(int i=0;i<n_components;i++)
351 ValueDesc component_value_desc(ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()),i);
353 Action::Handle action(Action::create("value_desc_set"));
356 throw Error(_("Unable to find action value_desc_set (bug)"));
358 action->set_param("canvas",get_canvas());
359 action->set_param("canvas_interface",get_canvas_interface());
360 action->set_param("time",time);
361 action->set_param("new_value",components[i]);
362 action->set_param("value_desc",component_value_desc);
364 if(!action->is_ready())
365 throw Error(Error::TYPE_NOTREADY);
373 // If we are merging the tangents of a BLinePoint,
374 // we must also set the second tangent for things
375 // to interpolate properly
376 if (value_desc.parent_is_value_node() &&
377 value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT &&
378 value_desc.get_index()==3)
380 ValueNode_Composite::Handle parent_value_node;
381 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
383 assert(parent_value_node);
385 // are we splitting or merging the tangents?
386 if (value.get(bool()))
388 // we are splitting tangents
390 Action::Handle action(Action::create("value_desc_set"));
393 throw Error(_("Unable to find action value_desc_set (bug)"));
395 action->set_param("canvas",get_canvas());
396 action->set_param("canvas_interface",get_canvas_interface());
397 action->set_param("time",time);
398 action->set_param("new_value",(*parent_value_node->get_link(4))(time));
399 action->set_param("value_desc",ValueDesc(parent_value_node,5));
401 if(!action->is_ready())
402 throw Error(Error::TYPE_NOTREADY);
408 // we are merging tangents
410 // the merged tangent should be the average of the 2 tangents we're merging
411 ValueBase average(((Vector)((*parent_value_node->get_link("t1"))(time)) +
412 (Vector)((*parent_value_node->get_link("t2"))(time))) / 2);
415 Action::Handle action(Action::create("value_desc_set"));
418 throw Error(_("Unable to find action value_desc_set (bug)"));
420 action->set_param("canvas",get_canvas());
421 action->set_param("canvas_interface",get_canvas_interface());
422 action->set_param("time",time);
423 action->set_param("new_value",average);
424 action->set_param("value_desc",ValueDesc(parent_value_node,4));
426 if(!action->is_ready())
427 throw Error(Error::TYPE_NOTREADY);
433 Action::Handle action(Action::create("value_desc_set"));
436 throw Error(_("Unable to find action value_desc_set (bug)"));
438 action->set_param("canvas",get_canvas());
439 action->set_param("canvas_interface",get_canvas_interface());
440 action->set_param("time",time);
441 action->set_param("new_value",average);
442 action->set_param("value_desc",ValueDesc(parent_value_node,5));
444 if(!action->is_ready())
445 throw Error(Error::TYPE_NOTREADY);
454 if( value_desc.parent_is_value_node())
457 if(value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT)
460 if(value_desc.get_index()==4)
463 if((*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
473 // If we are in animate editing mode
474 if(get_edit_mode()&MODE_ANIMATE)
477 ValueNode_Animated::Handle& value_node(value_node_animated);
479 // If this value isn't a ValueNode_Animated, but
480 // it is somewhat constant, then go ahead and convert
481 // it to a ValueNode_Animated.
482 if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
485 if(value_desc.is_value_node())
486 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
488 value=value_desc.get_value();
490 if(!value_node)value_node=ValueNode_Animated::create(value,time);
491 //if(!value_node)value_node=ValueNode_Animated::create(value.get_type());
493 Action::Handle action;
495 if(!value_desc.is_value_node())
497 action=(ValueDescConnect::create());
498 action->set_param("dest",value_desc);
499 action->set_param("src",ValueNode::Handle(value_node));
503 action=Action::create("value_node_replace");
504 action->set_param("dest",value_desc.get_value_node());
505 action->set_param("src",ValueNode::Handle(value_node));
508 action->set_param("canvas",get_canvas());
509 action->set_param("canvas_interface",get_canvas_interface());
511 if(!action->is_ready())
512 throw Error(Error::TYPE_NOTREADY);
515 add_action_front(action);
519 value_node=value_node.cast_dynamic(value_desc.get_value_node());
524 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
526 Action::Handle action(WaypointSetSmart::create());
528 //Waypoint waypoint(value,time);
530 Waypoint waypoint(value_node->new_waypoint_at_time(time));
531 waypoint.set_value(value);
533 waypoint.set_before(synfigapp::Main::get_interpolation());
534 waypoint.set_after(synfigapp::Main::get_interpolation());
536 action->set_param("canvas",get_canvas());
537 action->set_param("canvas_interface",get_canvas_interface());
538 action->set_param("value_node",ValueNode::Handle(value_node));
539 action->set_param("waypoint",waypoint);
541 if(!action->is_ready())
542 throw Error(Error::TYPE_NOTREADY);
551 if(value_desc.is_value_node())
553 if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
555 Action::Handle action(ValueNodeConstSet::create());
557 action->set_param("canvas",get_canvas());
558 action->set_param("canvas_interface",get_canvas_interface());
559 action->set_param("value_node",value_desc.get_value_node());
560 action->set_param("new_value",value);
562 if(!action->is_ready())
563 throw Error(Error::TYPE_NOTREADY);
565 add_action_front(action);
569 if(ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()))
570 throw Error(_("You must be in Animate-Editing-Mode to directly manipulate this value"));
572 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
575 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
577 Action::Handle layer_param_set(LayerParamSet::create());
579 layer_param_set->set_param("canvas",get_canvas());
580 layer_param_set->set_param("canvas_interface",get_canvas_interface());
581 layer_param_set->set_param("layer",value_desc.get_layer());
582 layer_param_set->set_param("param",value_desc.get_param_name());
583 layer_param_set->set_param("new_value",value);
585 if(!layer_param_set->is_ready())
586 throw Error(Error::TYPE_NOTREADY);
588 add_action_front(layer_param_set);
592 throw Error(_("Unsupported ValueDesc type"));