Made a new ACTION_INIT_NO_GET_LOCAL_NAME macro for actions which want to define their...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / valuedescset.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedescset.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "layerparamset.h"
34 #include "valuenodeconstset.h"
35 #include "valuedescconnect.h"
36 #include "waypointsetsmart.h"
37
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>
44
45 #include <synfigapp/general.h>
46
47 #endif
48
49 using namespace std;
50 using namespace etl;
51 using namespace synfig;
52 using namespace synfigapp;
53 using namespace Action;
54
55 /* === M A C R O S ========================================================= */
56
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$");
65
66 /* === G L O B A L S ======================================================= */
67
68 /* === P R O C E D U R E S ================================================= */
69
70 /* === M E T H O D S ======================================================= */
71
72 Action::ValueDescSet::ValueDescSet():
73         time(0)
74 {
75 }
76
77 synfig::String
78 Action::ValueDescSet::get_local_name()const
79 {
80         String name("ValueDesc");
81
82         if(!value_desc)
83         {
84         }
85         else if(value_desc.parent_is_layer_param())
86         {
87                 if(value_desc.get_layer()->get_description().empty())
88                         name=value_desc.get_layer()->get_local_name();
89                 else
90                         name=value_desc.get_layer()->get_description();
91                 name+="->"+value_desc.get_param_name();
92         }
93         else if(value_desc.parent_is_value_node())
94         {
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());
97
98                 synfig::Node* node;
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));
101                 if(parent_layer)
102                 {
103                         if(parent_layer->get_description().empty())
104                                 name=parent_layer->get_local_name()+"=>"+name;
105                         else
106                                 name=parent_layer->get_description()+"=>"+name;
107                 }
108         }
109
110         return strprintf(_("Set %s"),name.c_str());
111 }
112
113 Action::ParamVocab
114 Action::ValueDescSet::get_param_vocab()
115 {
116         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
117
118         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
119                 .set_local_name(_("ValueDesc"))
120         );
121
122         ret.push_back(ParamDesc("new_value",Param::TYPE_VALUE)
123                 .set_local_name(_("ValueBase"))
124         );
125
126         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
127                 .set_local_name(_("Time"))
128                 .set_optional()
129         );
130
131         return ret;
132 }
133
134 bool
135 Action::ValueDescSet::is_candidate(const ParamList &x)
136 {
137         return candidate_check(get_param_vocab(),x);
138 }
139
140 bool
141 Action::ValueDescSet::set_param(const synfig::String& name, const Action::Param &param)
142 {
143         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
144         {
145                 value_desc=param.get_value_desc();
146
147                 return true;
148         }
149
150         if(name=="new_value" && param.get_type()==Param::TYPE_VALUE)
151         {
152                 value=param.get_value();
153
154                 return true;
155         }
156
157         if(name=="time" && param.get_type()==Param::TYPE_TIME)
158         {
159                 time=param.get_time();
160
161                 return true;
162         }
163
164         return Action::CanvasSpecific::set_param(name,param);
165 }
166
167 bool
168 Action::ValueDescSet::is_ready()const
169 {
170         if(!value_desc || !value.is_valid())
171                 return false;
172         return Action::CanvasSpecific::is_ready();
173 }
174
175 void
176 Action::ValueDescSet::prepare()
177 {
178         clear();
179
180         // If our tangents are merged, and
181         // our first tangent is being manipulated,
182         // then we also need to adjust the other
183         // tangent.
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)
188         {
189                 printf("a tangent got changed - #%d\n", value_desc.get_index()-3);
190
191                 {
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);
195
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]);
199                 }
200
201                 if (value_desc.get_index()==4) {
202                         printf("copying change to tangent 2\n");
203                         DEBUGPOINT();
204                         ValueNode_Composite::Handle parent_value_node;
205                         parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
206
207                         assert(parent_value_node);
208
209                         Action::Handle action(Action::create("value_desc_set"));
210
211                         if(!action)
212                                 throw Error(_("Unable to find action value_desc_set (bug)"));
213
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));
219
220                         if(!action->is_ready())
221                                 throw Error(Error::TYPE_NOTREADY);
222
223                         add_action(action);
224                 }
225         }
226
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()))
231         {
232                 ValueDesc reference_value_desc(ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()),0);
233
234                 Action::Handle action(Action::create("value_desc_set"));
235
236                 if(!action)
237                         throw Error(_("Unable to find action value_desc_set (bug)"));
238
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);
244
245                 if(!action->is_ready())
246                         throw Error(Error::TYPE_NOTREADY);
247
248                 add_action(action);
249
250                 return;
251         }
252
253         // If we are a composite value node, then
254         // we need to distribute the changes to the
255         // individual parts
256         if(value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
257         {
258                 ValueBase components[6];
259                 int n_components(0);
260                 switch(value.get_type())
261                 {
262                 case ValueBase::TYPE_VECTOR:
263                         components[0]=value.get(Vector())[0];
264                         components[1]=value.get(Vector())[1];
265                         n_components=2;
266                         break;
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();
272                         n_components=4;
273                         break;
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;
279                         n_components=4;
280                         break;
281                 case ValueBase::TYPE_BLINEPOINT:
282                 {
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();
290                         n_components=6;
291                         break;
292                 }
293                 default:
294                         throw Error(_("Bad type for composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
295                         break;
296                 }
297
298                 for(int i=0;i<n_components;i++)
299                 {
300                         ValueDesc component_value_desc(ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()),i);
301
302                         Action::Handle action(Action::create("value_desc_set"));
303
304                         if(!action)
305                                 throw Error(_("Unable to find action value_desc_set (bug)"));
306
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);
312
313                         if(!action->is_ready())
314                                 throw Error(Error::TYPE_NOTREADY);
315
316                         add_action(action);
317                 }
318
319                 return;
320         }
321
322
323         // If we are a RADIAL composite value node, then
324         // we need to distribute the changes to the
325         // individual parts
326         if(value_desc.is_value_node() && ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()))
327         {
328                 ValueBase components[6];
329                 int n_components(0);
330                 switch(value.get_type())
331                 {
332                 case ValueBase::TYPE_VECTOR:
333                 {
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;
342                         n_components=2;
343                 }
344                         break;
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();
350                         n_components=4;
351                         break;
352                 default:
353                         throw Error(_("Bad type for radial composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
354                         break;
355                 }
356                 for(int i=0;i<n_components;i++)
357                 {
358                         ValueDesc component_value_desc(ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()),i);
359
360                         Action::Handle action(Action::create("value_desc_set"));
361
362                         if(!action)
363                                 throw Error(_("Unable to find action value_desc_set (bug)"));
364
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);
370
371                         if(!action->is_ready())
372                                 throw Error(Error::TYPE_NOTREADY);
373
374                         add_action(action);
375                 }
376
377                 return;
378         }
379
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)
386         {
387                 ValueNode_Composite::Handle parent_value_node;
388                 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
389
390                 assert(parent_value_node);
391
392                 // are we splitting or merging the tangents?
393             if (value.get(bool()))
394             {
395                         // we are splitting tangents
396
397                         Action::Handle action(Action::create("value_desc_set"));
398
399                         if(!action)
400                                 throw Error(_("Unable to find action value_desc_set (bug)"));
401
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));
407
408                         if(!action->is_ready())
409                                 throw Error(Error::TYPE_NOTREADY);
410
411                         add_action(action);
412             }
413             else
414             {
415                         // we are merging tangents
416
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);
420
421                         {
422                                 Action::Handle action(Action::create("value_desc_set"));
423
424                                 if(!action)
425                                         throw Error(_("Unable to find action value_desc_set (bug)"));
426
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));
432
433                                 if(!action->is_ready())
434                                         throw Error(Error::TYPE_NOTREADY);
435
436                                 add_action(action);
437                         }
438
439                         {
440                                 Action::Handle action(Action::create("value_desc_set"));
441
442                                 if(!action)
443                                         throw Error(_("Unable to find action value_desc_set (bug)"));
444
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));
450
451                                 if(!action->is_ready())
452                                         throw Error(Error::TYPE_NOTREADY);
453
454                                 add_action(action);
455                         }
456             }
457
458         }
459
460 /*      DEBUGPOINT();
461         if(     value_desc.parent_is_value_node())
462         {
463                 DEBUGPOINT();
464                 if(value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT)
465                 {
466                         DEBUGPOINT();
467                         if(value_desc.get_index()==4)
468                         {
469                                 DEBUGPOINT();
470                                 if((*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
471                                 {
472                                         DEBUGPOINT();
473                                 }
474                         }
475                 }
476         }
477 */
478
479
480         // If we are in animate editing mode
481         if(get_edit_mode()&MODE_ANIMATE)
482         {
483
484                 ValueNode_Animated::Handle& value_node(value_node_animated);
485
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()))
490                 {
491                         ValueBase value;
492                         if(value_desc.is_value_node())
493                                 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
494                         else
495                                 value=value_desc.get_value();
496
497                         if(!value_node)value_node=ValueNode_Animated::create(value,time);
498                         //if(!value_node)value_node=ValueNode_Animated::create(value.get_type());
499
500                         Action::Handle action;
501
502                         if(!value_desc.is_value_node())
503                         {
504                                 action=(ValueDescConnect::create());
505                                 action->set_param("dest",value_desc);
506                                 action->set_param("src",ValueNode::Handle(value_node));
507                         }
508                         else
509                         {
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));
513                         }
514
515                         action->set_param("canvas",get_canvas());
516                         action->set_param("canvas_interface",get_canvas_interface());
517
518                         if(!action->is_ready())
519                                 throw Error(Error::TYPE_NOTREADY);
520
521                                 DEBUGPOINT();
522                         add_action_front(action);
523                 }
524                 else
525                 {
526                         value_node=value_node.cast_dynamic(value_desc.get_value_node());
527                 }
528
529                                 DEBUGPOINT();
530                 if(!value_node)
531                         throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
532
533                 Action::Handle action(WaypointSetSmart::create());
534
535                 //Waypoint waypoint(value,time);
536
537                 Waypoint waypoint(value_node->new_waypoint_at_time(time));
538                 waypoint.set_value(value);
539
540                 waypoint.set_before(synfigapp::Main::get_interpolation());
541                 waypoint.set_after(synfigapp::Main::get_interpolation());
542
543                 action->set_param("canvas",get_canvas());
544                 action->set_param("canvas_interface",get_canvas_interface());
545                 action->set_param("value_node",ValueNode::Handle(value_node));
546                 action->set_param("waypoint",waypoint);
547
548                 if(!action->is_ready())
549                         throw Error(Error::TYPE_NOTREADY);
550
551                                 DEBUGPOINT();
552                 add_action(action);
553
554                 return;
555         }
556         else
557         {
558                 if(value_desc.is_value_node())
559                 {
560                         if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
561                         {
562                                 Action::Handle action(ValueNodeConstSet::create());
563
564                                 action->set_param("canvas",get_canvas());
565                                 action->set_param("canvas_interface",get_canvas_interface());
566                                 action->set_param("value_node",value_desc.get_value_node());
567                                 action->set_param("new_value",value);
568
569                                 if(!action->is_ready())
570                                         throw Error(Error::TYPE_NOTREADY);
571
572                                 add_action_front(action);
573                                 return;
574                         }
575                         else
576                         if(ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()))
577                                 throw Error(_("You must be in Animate-Editing-Mode to directly manipulate this value"));
578                         else
579                                 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
580                 }
581                 else
582                 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
583                 {
584                         Action::Handle layer_param_set(LayerParamSet::create());
585
586                         layer_param_set->set_param("canvas",get_canvas());
587                         layer_param_set->set_param("canvas_interface",get_canvas_interface());
588                         layer_param_set->set_param("layer",value_desc.get_layer());
589                         layer_param_set->set_param("param",value_desc.get_param_name());
590                         layer_param_set->set_param("new_value",value);
591
592                         if(!layer_param_set->is_ready())
593                                 throw Error(Error::TYPE_NOTREADY);
594
595                         add_action_front(layer_param_set);
596                         return;
597                 }
598
599                 throw Error(_("Unsupported ValueDesc type"));
600         }
601 }