e5edf2a4ac4f21e6d504e11e3003a75fa9e561d4
[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                 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())
90         {
91                 synfig::LinkableValueNode::Handle value_node(synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node()));
92                 synfig::Node* 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));
95                 if(parent_layer)
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());
99                 else
100                         name = value_node->link_local_name(value_desc.get_index());
101         }
102
103         return strprintf(_("Set %s"), name.c_str());
104 }
105
106 Action::ParamVocab
107 Action::ValueDescSet::get_param_vocab()
108 {
109         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
110
111         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
112                 .set_local_name(_("ValueDesc"))
113         );
114
115         ret.push_back(ParamDesc("new_value",Param::TYPE_VALUE)
116                 .set_local_name(_("ValueBase"))
117         );
118
119         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
120                 .set_local_name(_("Time"))
121                 .set_optional()
122         );
123
124         return ret;
125 }
126
127 bool
128 Action::ValueDescSet::is_candidate(const ParamList &x)
129 {
130         return candidate_check(get_param_vocab(),x);
131 }
132
133 bool
134 Action::ValueDescSet::set_param(const synfig::String& name, const Action::Param &param)
135 {
136         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
137         {
138                 value_desc=param.get_value_desc();
139
140                 return true;
141         }
142
143         if(name=="new_value" && param.get_type()==Param::TYPE_VALUE)
144         {
145                 value=param.get_value();
146
147                 return true;
148         }
149
150         if(name=="time" && param.get_type()==Param::TYPE_TIME)
151         {
152                 time=param.get_time();
153
154                 return true;
155         }
156
157         return Action::CanvasSpecific::set_param(name,param);
158 }
159
160 bool
161 Action::ValueDescSet::is_ready()const
162 {
163         if(!value_desc || !value.is_valid())
164                 return false;
165         return Action::CanvasSpecific::is_ready();
166 }
167
168 void
169 Action::ValueDescSet::prepare()
170 {
171         clear();
172
173         // If our tangents are merged, and
174         // our first tangent is being manipulated,
175         // then we also need to adjust the other
176         // tangent.
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)
181         {
182                 printf("a tangent got changed - #%d\n", value_desc.get_index()-3);
183
184                 {
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);
188
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]);
192                 }
193
194                 if (value_desc.get_index()==4) {
195                         printf("copying change to tangent 2\n");
196                         DEBUGPOINT();
197                         ValueNode_Composite::Handle parent_value_node;
198                         parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
199
200                         assert(parent_value_node);
201
202                         Action::Handle action(Action::create("value_desc_set"));
203
204                         if(!action)
205                                 throw Error(_("Unable to find action value_desc_set (bug)"));
206
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));
212
213                         if(!action->is_ready())
214                                 throw Error(Error::TYPE_NOTREADY);
215
216                         add_action(action);
217                 }
218         }
219
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()))
224         {
225                 ValueDesc reference_value_desc(ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()),0);
226
227                 Action::Handle action(Action::create("value_desc_set"));
228
229                 if(!action)
230                         throw Error(_("Unable to find action value_desc_set (bug)"));
231
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);
237
238                 if(!action->is_ready())
239                         throw Error(Error::TYPE_NOTREADY);
240
241                 add_action(action);
242
243                 return;
244         }
245
246         // If we are a composite value node, then
247         // we need to distribute the changes to the
248         // individual parts
249         if(value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
250         {
251                 ValueBase components[6];
252                 int n_components(0);
253                 switch(value.get_type())
254                 {
255                 case ValueBase::TYPE_VECTOR:
256                         components[0]=value.get(Vector())[0];
257                         components[1]=value.get(Vector())[1];
258                         n_components=2;
259                         break;
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();
265                         n_components=4;
266                         break;
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;
272                         n_components=4;
273                         break;
274                 case ValueBase::TYPE_BLINEPOINT:
275                 {
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();
283                         n_components=6;
284                         break;
285                 }
286                 default:
287                         throw Error(_("Bad type for composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
288                         break;
289                 }
290
291                 for(int i=0;i<n_components;i++)
292                 {
293                         ValueDesc component_value_desc(ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()),i);
294
295                         Action::Handle action(Action::create("value_desc_set"));
296
297                         if(!action)
298                                 throw Error(_("Unable to find action value_desc_set (bug)"));
299
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);
305
306                         if(!action->is_ready())
307                                 throw Error(Error::TYPE_NOTREADY);
308
309                         add_action(action);
310                 }
311
312                 return;
313         }
314
315
316         // If we are a RADIAL composite value node, then
317         // we need to distribute the changes to the
318         // individual parts
319         if(value_desc.is_value_node() && ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()))
320         {
321                 ValueBase components[6];
322                 int n_components(0);
323                 switch(value.get_type())
324                 {
325                 case ValueBase::TYPE_VECTOR:
326                 {
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;
335                         n_components=2;
336                 }
337                         break;
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();
343                         n_components=4;
344                         break;
345                 default:
346                         throw Error(_("Bad type for radial composite (%s)"),ValueBase::type_local_name(value.get_type()).c_str());
347                         break;
348                 }
349                 for(int i=0;i<n_components;i++)
350                 {
351                         ValueDesc component_value_desc(ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()),i);
352
353                         Action::Handle action(Action::create("value_desc_set"));
354
355                         if(!action)
356                                 throw Error(_("Unable to find action value_desc_set (bug)"));
357
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);
363
364                         if(!action->is_ready())
365                                 throw Error(Error::TYPE_NOTREADY);
366
367                         add_action(action);
368                 }
369
370                 return;
371         }
372
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)
379         {
380                 ValueNode_Composite::Handle parent_value_node;
381                 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
382
383                 assert(parent_value_node);
384
385                 // are we splitting or merging the tangents?
386             if (value.get(bool()))
387             {
388                         // we are splitting tangents
389
390                         Action::Handle action(Action::create("value_desc_set"));
391
392                         if(!action)
393                                 throw Error(_("Unable to find action value_desc_set (bug)"));
394
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));
400
401                         if(!action->is_ready())
402                                 throw Error(Error::TYPE_NOTREADY);
403
404                         add_action(action);
405             }
406             else
407             {
408                         // we are merging tangents
409
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);
413
414                         {
415                                 Action::Handle action(Action::create("value_desc_set"));
416
417                                 if(!action)
418                                         throw Error(_("Unable to find action value_desc_set (bug)"));
419
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));
425
426                                 if(!action->is_ready())
427                                         throw Error(Error::TYPE_NOTREADY);
428
429                                 add_action(action);
430                         }
431
432                         {
433                                 Action::Handle action(Action::create("value_desc_set"));
434
435                                 if(!action)
436                                         throw Error(_("Unable to find action value_desc_set (bug)"));
437
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));
443
444                                 if(!action->is_ready())
445                                         throw Error(Error::TYPE_NOTREADY);
446
447                                 add_action(action);
448                         }
449             }
450
451         }
452
453 /*      DEBUGPOINT();
454         if(     value_desc.parent_is_value_node())
455         {
456                 DEBUGPOINT();
457                 if(value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT)
458                 {
459                         DEBUGPOINT();
460                         if(value_desc.get_index()==4)
461                         {
462                                 DEBUGPOINT();
463                                 if((*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
464                                 {
465                                         DEBUGPOINT();
466                                 }
467                         }
468                 }
469         }
470 */
471
472
473         // If we are in animate editing mode
474         if(get_edit_mode()&MODE_ANIMATE)
475         {
476
477                 ValueNode_Animated::Handle& value_node(value_node_animated);
478
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()))
483                 {
484                         ValueBase value;
485                         if(value_desc.is_value_node())
486                                 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
487                         else
488                                 value=value_desc.get_value();
489
490                         if(!value_node)value_node=ValueNode_Animated::create(value,time);
491                         //if(!value_node)value_node=ValueNode_Animated::create(value.get_type());
492
493                         Action::Handle action;
494
495                         if(!value_desc.is_value_node())
496                         {
497                                 action=(ValueDescConnect::create());
498                                 action->set_param("dest",value_desc);
499                                 action->set_param("src",ValueNode::Handle(value_node));
500                         }
501                         else
502                         {
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));
506                         }
507
508                         action->set_param("canvas",get_canvas());
509                         action->set_param("canvas_interface",get_canvas_interface());
510
511                         if(!action->is_ready())
512                                 throw Error(Error::TYPE_NOTREADY);
513
514                                 DEBUGPOINT();
515                         add_action_front(action);
516                 }
517                 else
518                 {
519                         value_node=value_node.cast_dynamic(value_desc.get_value_node());
520                 }
521
522                                 DEBUGPOINT();
523                 if(!value_node)
524                         throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
525
526                 Action::Handle action(WaypointSetSmart::create());
527
528                 //Waypoint waypoint(value,time);
529
530                 Waypoint waypoint(value_node->new_waypoint_at_time(time));
531                 waypoint.set_value(value);
532
533                 waypoint.set_before(synfigapp::Main::get_interpolation());
534                 waypoint.set_after(synfigapp::Main::get_interpolation());
535
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);
540
541                 if(!action->is_ready())
542                         throw Error(Error::TYPE_NOTREADY);
543
544                                 DEBUGPOINT();
545                 add_action(action);
546
547                 return;
548         }
549         else
550         {
551                 if(value_desc.is_value_node())
552                 {
553                         if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
554                         {
555                                 Action::Handle action(ValueNodeConstSet::create());
556
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);
561
562                                 if(!action->is_ready())
563                                         throw Error(Error::TYPE_NOTREADY);
564
565                                 add_action_front(action);
566                                 return;
567                         }
568                         else
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"));
571                         else
572                                 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
573                 }
574                 else
575                 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
576                 {
577                         Action::Handle layer_param_set(LayerParamSet::create());
578
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);
584
585                         if(!layer_param_set->is_ready())
586                                 throw Error(Error::TYPE_NOTREADY);
587
588                         add_action_front(layer_param_set);
589                         return;
590                 }
591
592                 throw Error(_("Unsupported ValueDesc type"));
593         }
594 }