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
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "layerparamset.h"
33 #include "valuenodeconstset.h"
34 #include "valuedescconnect.h"
35 #include "waypointsetsmart.h"
37 #include "valuedescset.h"
38 #include <synfigapp/canvasinterface.h>
39 #include <synfig/valuenode_composite.h>
40 #include <synfig/valuenode_radialcomposite.h>
41 #include <synfig/valuenode_reference.h>
42 #include <synfigapp/main.h>
48 using namespace synfig;
49 using namespace synfigapp;
50 using namespace Action;
52 /* === M A C R O S ========================================================= */
53 #define ACTION_INIT2(class) \
54 Action::Base* class::create() { return new class(); } \
55 synfig::String class::get_name()const { return name__; }
57 ACTION_INIT2(Action::ValueDescSet);
58 ACTION_SET_NAME(Action::ValueDescSet,"value_desc_set");
59 ACTION_SET_LOCAL_NAME(Action::ValueDescSet,"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 if(value_desc.get_layer()->get_description().empty())
88 name=value_desc.get_layer()->get_local_name();
90 name=value_desc.get_layer()->get_description();
91 name+="->"+value_desc.get_param_name();
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()));
96 name=value_node->link_local_name(value_desc.get_index());
99 for(node=value_node.get();!node->parent_set.empty() && !dynamic_cast<Layer*>(node);node=*node->parent_set.begin());
100 Layer::Handle parent_layer(dynamic_cast<Layer*>(node));
103 if(parent_layer->get_description().empty())
104 name=parent_layer->get_local_name()+"=>"+name;
106 name=parent_layer->get_description()+"=>"+name;
110 return strprintf(_("Set %s"),name.c_str());
114 Action::ValueDescSet::get_param_vocab()
116 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
118 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
119 .set_local_name(_("ValueDesc"))
122 ret.push_back(ParamDesc("new_value",Param::TYPE_VALUE)
123 .set_local_name(_("ValueBase"))
126 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
127 .set_local_name(_("Time"))
135 Action::ValueDescSet::is_candidate(const ParamList &x)
137 return candidate_check(get_param_vocab(),x);
141 Action::ValueDescSet::set_param(const synfig::String& name, const Action::Param ¶m)
143 if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
145 value_desc=param.get_value_desc();
150 if(name=="new_value" && param.get_type()==Param::TYPE_VALUE)
152 value=param.get_value();
157 if(name=="time" && param.get_type()==Param::TYPE_TIME)
159 time=param.get_time();
164 return Action::CanvasSpecific::set_param(name,param);
168 Action::ValueDescSet::is_ready()const
170 if(!value_desc || !value.is_valid())
172 return Action::CanvasSpecific::is_ready();
176 Action::ValueDescSet::prepare()
180 // If our tangents are merged, and
181 // our first tangent is being manipulated,
182 // then we also need to adjust the other
184 if( value_desc.parent_is_value_node() &&
185 value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT &&
186 (value_desc.get_index()==4 || value_desc.get_index()==5) &&
187 (*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
189 printf("a tangent got changed - #%d\n", value_desc.get_index()-3);
192 ValueNode_Composite::Handle parent_value_node;
193 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
194 assert(parent_value_node);
196 Vector t1((*parent_value_node->get_link("t1"))(time));
197 Vector t2((*parent_value_node->get_link("t2"))(time));
198 printf("current values are: t1(%.2f, %2.f) t2(%.2f, %.2f)\n", t1[0], t1[1], t2[0], t2[1]);
201 if (value_desc.get_index()==4) {
202 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_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 Vector vect(value.get(Vector()));
335 components[0]=vect.mag();
336 components[1]=Angle(Angle::tan(vect[1],vect[0]));
340 case ValueBase::TYPE_COLOR:
341 components[0]=value.get(Color()).get_y();
342 components[1]=value.get(Color()).get_s();
343 components[2]=value.get(Color()).get_hue();
344 components[3]=value.get(Color()).get_a();
348 throw Error("Bad type for radial composite (%s)",ValueBase::type_name(value.get_type()).c_str());
351 for(int i=0;i<n_components;i++)
353 ValueDesc component_value_desc(ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()),i);
355 Action::Handle action(Action::create("value_desc_set"));
358 throw Error(_("Unable to find action value_desc_set (bug)"));
360 action->set_param("canvas",get_canvas());
361 action->set_param("canvas_interface",get_canvas_interface());
362 action->set_param("time",time);
363 action->set_param("new_value",components[i]);
364 action->set_param("value_desc",component_value_desc);
366 if(!action->is_ready())
367 throw Error(Error::TYPE_NOTREADY);
375 // If we are merging the tangents of a BLinePoint,
376 // we must also set the second tangent for things
377 // to interpolate properly
378 if (value_desc.parent_is_value_node() &&
379 value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT &&
380 value_desc.get_index()==3)
382 ValueNode_Composite::Handle parent_value_node;
383 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
385 assert(parent_value_node);
387 // are we splitting or merging the tangents?
388 if (value.get(bool()))
390 // we are splitting tangents
392 Action::Handle action(Action::create("value_desc_set"));
395 throw Error(_("Unable to find action value_desc_set (bug)"));
397 action->set_param("canvas",get_canvas());
398 action->set_param("canvas_interface",get_canvas_interface());
399 action->set_param("time",time);
400 action->set_param("new_value",(*parent_value_node->get_link(4))(time));
401 action->set_param("value_desc",ValueDesc(parent_value_node,5));
403 if(!action->is_ready())
404 throw Error(Error::TYPE_NOTREADY);
410 // we are merging tangents
412 // the merged tangent should be the average of the 2 tangents we're merging
413 ValueBase average(((Vector)((*parent_value_node->get_link("t1"))(time)) +
414 (Vector)((*parent_value_node->get_link("t2"))(time))) / 2);
417 Action::Handle action(Action::create("value_desc_set"));
420 throw Error(_("Unable to find action value_desc_set (bug)"));
422 action->set_param("canvas",get_canvas());
423 action->set_param("canvas_interface",get_canvas_interface());
424 action->set_param("time",time);
425 action->set_param("new_value",average);
426 action->set_param("value_desc",ValueDesc(parent_value_node,4));
428 if(!action->is_ready())
429 throw Error(Error::TYPE_NOTREADY);
435 Action::Handle action(Action::create("value_desc_set"));
438 throw Error(_("Unable to find action value_desc_set (bug)"));
440 action->set_param("canvas",get_canvas());
441 action->set_param("canvas_interface",get_canvas_interface());
442 action->set_param("time",time);
443 action->set_param("new_value",average);
444 action->set_param("value_desc",ValueDesc(parent_value_node,5));
446 if(!action->is_ready())
447 throw Error(Error::TYPE_NOTREADY);
456 if( value_desc.parent_is_value_node())
459 if(value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT)
462 if(value_desc.get_index()==4)
465 if((*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
475 // If we are in animate editing mode
476 if(get_edit_mode()&MODE_ANIMATE)
479 ValueNode_Animated::Handle& value_node(value_node_animated);
481 // If this value isn't a ValueNode_Animated, but
482 // it is somewhat constant, then go ahead and convert
483 // it to a ValueNode_Animated.
484 if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
487 if(value_desc.is_value_node())
488 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
490 value=value_desc.get_value();
492 if(!value_node)value_node=ValueNode_Animated::create(value,time);
493 //if(!value_node)value_node=ValueNode_Animated::create(value.get_type());
495 Action::Handle action;
497 if(!value_desc.is_value_node())
499 action=(ValueDescConnect::create());
500 action->set_param("dest",value_desc);
501 action->set_param("src",ValueNode::Handle(value_node));
505 action=Action::create("value_node_replace");
506 action->set_param("dest",value_desc.get_value_node());
507 action->set_param("src",ValueNode::Handle(value_node));
510 action->set_param("canvas",get_canvas());
511 action->set_param("canvas_interface",get_canvas_interface());
513 if(!action->is_ready())
514 throw Error(Error::TYPE_NOTREADY);
517 add_action_front(action);
521 value_node=value_node.cast_dynamic(value_desc.get_value_node());
526 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
528 Action::Handle action(WaypointSetSmart::create());
530 //Waypoint waypoint(value,time);
532 Waypoint waypoint(value_node->new_waypoint_at_time(time));
533 waypoint.set_value(value);
535 waypoint.set_before(synfigapp::Main::get_interpolation());
536 waypoint.set_after(synfigapp::Main::get_interpolation());
538 action->set_param("canvas",get_canvas());
539 action->set_param("canvas_interface",get_canvas_interface());
540 action->set_param("value_node",ValueNode::Handle(value_node));
541 action->set_param("waypoint",waypoint);
543 if(!action->is_ready())
544 throw Error(Error::TYPE_NOTREADY);
553 if(value_desc.is_value_node())
555 if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
557 Action::Handle action(ValueNodeConstSet::create());
559 action->set_param("canvas",get_canvas());
560 action->set_param("canvas_interface",get_canvas_interface());
561 action->set_param("value_node",value_desc.get_value_node());
562 action->set_param("new_value",value);
564 if(!action->is_ready())
565 throw Error(Error::TYPE_NOTREADY);
567 add_action_front(action);
571 if(ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()))
572 throw Error(_("You must be in Animate-Editing-Mode to directly manipulate this value"));
574 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
577 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
579 Action::Handle layer_param_set(LayerParamSet::create());
581 layer_param_set->set_param("canvas",get_canvas());
582 layer_param_set->set_param("canvas_interface",get_canvas_interface());
583 layer_param_set->set_param("layer",value_desc.get_layer());
584 layer_param_set->set_param("param",value_desc.get_param_name());
585 layer_param_set->set_param("new_value",value);
587 if(!layer_param_set->is_ready())
588 throw Error(Error::TYPE_NOTREADY);
590 add_action_front(layer_param_set);
594 throw Error(_("Unsupported ValueDesc type"));