Typo: 'totaly' -> 'totally'.
[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 #endif
46
47 using namespace std;
48 using namespace etl;
49 using namespace synfig;
50 using namespace synfigapp;
51 using namespace Action;
52
53 /* === M A C R O S ========================================================= */
54 #define ACTION_INIT2(class) \
55         Action::Base* class::create() { return new class(); }   \
56         synfig::String class::get_name()const { return name__; }
57
58 ACTION_INIT2(Action::ValueDescSet);
59 ACTION_SET_NAME(Action::ValueDescSet,"value_desc_set");
60 ACTION_SET_LOCAL_NAME(Action::ValueDescSet,"Set ValueDesc");
61 ACTION_SET_TASK(Action::ValueDescSet,"set");
62 ACTION_SET_CATEGORY(Action::ValueDescSet,Action::CATEGORY_VALUEDESC);
63 ACTION_SET_PRIORITY(Action::ValueDescSet,0);
64 ACTION_SET_VERSION(Action::ValueDescSet,"0.0");
65 ACTION_SET_CVS_ID(Action::ValueDescSet,"$Id$");
66
67 /* === G L O B A L S ======================================================= */
68
69 /* === P R O C E D U R E S ================================================= */
70
71 /* === M E T H O D S ======================================================= */
72
73 Action::ValueDescSet::ValueDescSet():
74         time(0)
75 {
76 }
77
78 synfig::String
79 Action::ValueDescSet::get_local_name()const
80 {
81         String name("ValueDesc");
82
83         if(!value_desc)
84         {
85         }
86         else if(value_desc.parent_is_layer_param())
87         {
88                 if(value_desc.get_layer()->get_description().empty())
89                         name=value_desc.get_layer()->get_local_name();
90                 else
91                         name=value_desc.get_layer()->get_description();
92                 name+="->"+value_desc.get_param_name();
93         }
94         else if(value_desc.parent_is_value_node())
95         {
96                 synfig::LinkableValueNode::Handle value_node(synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node()));
97                 name=value_node->link_local_name(value_desc.get_index());
98
99                 synfig::Node* node;
100                 for(node=value_node.get();!node->parent_set.empty() && !dynamic_cast<Layer*>(node);node=*node->parent_set.begin());
101                 Layer::Handle parent_layer(dynamic_cast<Layer*>(node));
102                 if(parent_layer)
103                 {
104                         if(parent_layer->get_description().empty())
105                                 name=parent_layer->get_local_name()+"=>"+name;
106                         else
107                                 name=parent_layer->get_description()+"=>"+name;
108                 }
109         }
110
111         return strprintf(_("Set %s"),name.c_str());
112 }
113
114 Action::ParamVocab
115 Action::ValueDescSet::get_param_vocab()
116 {
117         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
118
119         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
120                 .set_local_name(_("ValueDesc"))
121         );
122
123         ret.push_back(ParamDesc("new_value",Param::TYPE_VALUE)
124                 .set_local_name(_("ValueBase"))
125         );
126
127         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
128                 .set_local_name(_("Time"))
129                 .set_optional()
130         );
131
132         return ret;
133 }
134
135 bool
136 Action::ValueDescSet::is_candidate(const ParamList &x)
137 {
138         return candidate_check(get_param_vocab(),x);
139 }
140
141 bool
142 Action::ValueDescSet::set_param(const synfig::String& name, const Action::Param &param)
143 {
144         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
145         {
146                 value_desc=param.get_value_desc();
147
148                 return true;
149         }
150
151         if(name=="new_value" && param.get_type()==Param::TYPE_VALUE)
152         {
153                 value=param.get_value();
154
155                 return true;
156         }
157
158         if(name=="time" && param.get_type()==Param::TYPE_TIME)
159         {
160                 time=param.get_time();
161
162                 return true;
163         }
164
165         return Action::CanvasSpecific::set_param(name,param);
166 }
167
168 bool
169 Action::ValueDescSet::is_ready()const
170 {
171         if(!value_desc || !value.is_valid())
172                 return false;
173         return Action::CanvasSpecific::is_ready();
174 }
175
176 void
177 Action::ValueDescSet::prepare()
178 {
179         clear();
180
181         // If our tangents are merged, and
182         // our first tangent is being manipulated,
183         // then we also need to adjust the other
184         // tangent.
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)
189         {
190                 printf("a tangent got changed - #%d\n", value_desc.get_index()-3);
191
192                 {
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);
196
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]);
200                 }
201
202                 if (value_desc.get_index()==4) {
203                         printf("copying change to tangent 2\n");
204                         DEBUGPOINT();
205                         ValueNode_Composite::Handle parent_value_node;
206                         parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
207
208                         assert(parent_value_node);
209
210                         Action::Handle action(Action::create("value_desc_set"));
211
212                         if(!action)
213                                 throw Error(_("Unable to find action value_desc_set (bug)"));
214
215                         action->set_param("canvas",get_canvas());
216                         action->set_param("canvas_interface",get_canvas_interface());
217                         action->set_param("time",time);
218                         action->set_param("new_value",value);
219                         action->set_param("value_desc",ValueDesc(parent_value_node,5));
220
221                         if(!action->is_ready())
222                                 throw Error(Error::TYPE_NOTREADY);
223
224                         add_action(action);
225                 }
226         }
227
228         // If we are a reference value node, then
229         // we need to distribute the changes to the
230         // referenced value node
231         if(value_desc.is_value_node() && ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()))
232         {
233                 ValueDesc reference_value_desc(ValueNode_Reference::Handle::cast_dynamic(value_desc.get_value_node()),0);
234
235                 Action::Handle action(Action::create("value_desc_set"));
236
237                 if(!action)
238                         throw Error(_("Unable to find action value_desc_set (bug)"));
239
240                 action->set_param("canvas",get_canvas());
241                 action->set_param("canvas_interface",get_canvas_interface());
242                 action->set_param("time",time);
243                 action->set_param("new_value",value);
244                 action->set_param("value_desc",reference_value_desc);
245
246                 if(!action->is_ready())
247                         throw Error(Error::TYPE_NOTREADY);
248
249                 add_action(action);
250
251                 return;
252         }
253
254         // If we are a composite value node, then
255         // we need to distribute the changes to the
256         // individual parts
257         if(value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
258         {
259                 ValueBase components[6];
260                 int n_components(0);
261                 switch(value.get_type())
262                 {
263                 case ValueBase::TYPE_VECTOR:
264                         components[0]=value.get(Vector())[0];
265                         components[1]=value.get(Vector())[1];
266                         n_components=2;
267                         break;
268                 case ValueBase::TYPE_COLOR:
269                         components[0]=value.get(Color()).get_r();
270                         components[1]=value.get(Color()).get_g();
271                         components[2]=value.get(Color()).get_b();
272                         components[3]=value.get(Color()).get_a();
273                         n_components=4;
274                         break;
275                 case ValueBase::TYPE_SEGMENT:
276                         components[0]=value.get(Segment()).p1;
277                         components[1]=value.get(Segment()).t1;
278                         components[2]=value.get(Segment()).p2;
279                         components[3]=value.get(Segment()).t2;
280                         n_components=4;
281                         break;
282                 case ValueBase::TYPE_BLINEPOINT:
283                 {
284                         BLinePoint bline_point(value);
285                         components[0]=bline_point.get_vertex();
286                         components[1]=bline_point.get_width();
287                         components[2]=bline_point.get_origin();
288                         components[3]=bline_point.get_split_tangent_flag();
289                         components[4]=bline_point.get_tangent1();
290                         components[5]=bline_point.get_tangent2();
291                         n_components=6;
292                         break;
293                 }
294                 default:
295                         throw Error("Bad type for composite (%s)",ValueBase::type_name(value.get_type()).c_str());
296                         break;
297                 }
298
299                 for(int i=0;i<n_components;i++)
300                 {
301                         ValueDesc component_value_desc(ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()),i);
302
303                         Action::Handle action(Action::create("value_desc_set"));
304
305                         if(!action)
306                                 throw Error(_("Unable to find action value_desc_set (bug)"));
307
308                         action->set_param("canvas",get_canvas());
309                         action->set_param("canvas_interface",get_canvas_interface());
310                         action->set_param("time",time);
311                         action->set_param("new_value",components[i]);
312                         action->set_param("value_desc",component_value_desc);
313
314                         if(!action->is_ready())
315                                 throw Error(Error::TYPE_NOTREADY);
316
317                         add_action(action);
318                 }
319
320                 return;
321         }
322
323
324         // If we are a RADIAL composite value node, then
325         // we need to distribute the changes to the
326         // individual parts
327         if(value_desc.is_value_node() && ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()))
328         {
329                 ValueBase components[6];
330                 int n_components(0);
331                 switch(value.get_type())
332                 {
333                 case ValueBase::TYPE_VECTOR:
334                 {
335                         Vector vect(value.get(Vector()));
336                         components[0]=vect.mag();
337                         components[1]=Angle(Angle::tan(vect[1],vect[0]));
338                         n_components=2;
339                 }
340                         break;
341                 case ValueBase::TYPE_COLOR:
342                         components[0]=value.get(Color()).get_y();
343                         components[1]=value.get(Color()).get_s();
344                         components[2]=value.get(Color()).get_hue();
345                         components[3]=value.get(Color()).get_a();
346                         n_components=4;
347                         break;
348                 default:
349                         throw Error("Bad type for radial composite (%s)",ValueBase::type_name(value.get_type()).c_str());
350                         break;
351                 }
352                 for(int i=0;i<n_components;i++)
353                 {
354                         ValueDesc component_value_desc(ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()),i);
355
356                         Action::Handle action(Action::create("value_desc_set"));
357
358                         if(!action)
359                                 throw Error(_("Unable to find action value_desc_set (bug)"));
360
361                         action->set_param("canvas",get_canvas());
362                         action->set_param("canvas_interface",get_canvas_interface());
363                         action->set_param("time",time);
364                         action->set_param("new_value",components[i]);
365                         action->set_param("value_desc",component_value_desc);
366
367                         if(!action->is_ready())
368                                 throw Error(Error::TYPE_NOTREADY);
369
370                         add_action(action);
371                 }
372
373                 return;
374         }
375
376         // If we are merging the tangents of a BLinePoint,
377         // we must also set the second tangent for things
378         // to interpolate properly
379         if (value_desc.parent_is_value_node() &&
380             value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT &&
381             value_desc.get_index()==3)
382         {
383                 ValueNode_Composite::Handle parent_value_node;
384                 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
385
386                 assert(parent_value_node);
387
388                 // are we splitting or merging the tangents?
389             if (value.get(bool()))
390             {
391                         // we are splitting tangents
392
393                         Action::Handle action(Action::create("value_desc_set"));
394
395                         if(!action)
396                                 throw Error(_("Unable to find action value_desc_set (bug)"));
397
398                         action->set_param("canvas",get_canvas());
399                         action->set_param("canvas_interface",get_canvas_interface());
400                         action->set_param("time",time);
401                         action->set_param("new_value",(*parent_value_node->get_link(4))(time));
402                         action->set_param("value_desc",ValueDesc(parent_value_node,5));
403
404                         if(!action->is_ready())
405                                 throw Error(Error::TYPE_NOTREADY);
406
407                         add_action(action);
408             }
409             else
410             {
411                         // we are merging tangents
412
413                         // the merged tangent should be the average of the 2 tangents we're merging
414                         ValueBase average(((Vector)((*parent_value_node->get_link("t1"))(time)) +
415                                                            (Vector)((*parent_value_node->get_link("t2"))(time))) / 2);
416
417                         {
418                                 Action::Handle action(Action::create("value_desc_set"));
419
420                                 if(!action)
421                                         throw Error(_("Unable to find action value_desc_set (bug)"));
422
423                                 action->set_param("canvas",get_canvas());
424                                 action->set_param("canvas_interface",get_canvas_interface());
425                                 action->set_param("time",time);
426                                 action->set_param("new_value",average);
427                                 action->set_param("value_desc",ValueDesc(parent_value_node,4));
428
429                                 if(!action->is_ready())
430                                         throw Error(Error::TYPE_NOTREADY);
431
432                                 add_action(action);
433                         }
434
435                         {
436                                 Action::Handle action(Action::create("value_desc_set"));
437
438                                 if(!action)
439                                         throw Error(_("Unable to find action value_desc_set (bug)"));
440
441                                 action->set_param("canvas",get_canvas());
442                                 action->set_param("canvas_interface",get_canvas_interface());
443                                 action->set_param("time",time);
444                                 action->set_param("new_value",average);
445                                 action->set_param("value_desc",ValueDesc(parent_value_node,5));
446
447                                 if(!action->is_ready())
448                                         throw Error(Error::TYPE_NOTREADY);
449
450                                 add_action(action);
451                         }
452             }
453
454         }
455
456 /*      DEBUGPOINT();
457         if(     value_desc.parent_is_value_node())
458         {
459                 DEBUGPOINT();
460                 if(value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT)
461                 {
462                         DEBUGPOINT();
463                         if(value_desc.get_index()==4)
464                         {
465                                 DEBUGPOINT();
466                                 if((*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
467                                 {
468                                         DEBUGPOINT();
469                                 }
470                         }
471                 }
472         }
473 */
474
475
476         // If we are in animate editing mode
477         if(get_edit_mode()&MODE_ANIMATE)
478         {
479
480                 ValueNode_Animated::Handle& value_node(value_node_animated);
481
482                 // If this value isn't a ValueNode_Animated, but
483                 // it is somewhat constant, then go ahead and convert
484                 // it to a ValueNode_Animated.
485                 if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
486                 {
487                         ValueBase value;
488                         if(value_desc.is_value_node())
489                                 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
490                         else
491                                 value=value_desc.get_value();
492
493                         if(!value_node)value_node=ValueNode_Animated::create(value,time);
494                         //if(!value_node)value_node=ValueNode_Animated::create(value.get_type());
495
496                         Action::Handle action;
497
498                         if(!value_desc.is_value_node())
499                         {
500                                 action=(ValueDescConnect::create());
501                                 action->set_param("dest",value_desc);
502                                 action->set_param("src",ValueNode::Handle(value_node));
503                         }
504                         else
505                         {
506                                 action=Action::create("value_node_replace");
507                                 action->set_param("dest",value_desc.get_value_node());
508                                 action->set_param("src",ValueNode::Handle(value_node));
509                         }
510
511                         action->set_param("canvas",get_canvas());
512                         action->set_param("canvas_interface",get_canvas_interface());
513
514                         if(!action->is_ready())
515                                 throw Error(Error::TYPE_NOTREADY);
516
517                                 DEBUGPOINT();
518                         add_action_front(action);
519                 }
520                 else
521                 {
522                         value_node=value_node.cast_dynamic(value_desc.get_value_node());
523                 }
524
525                                 DEBUGPOINT();
526                 if(!value_node)
527                         throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
528
529                 Action::Handle action(WaypointSetSmart::create());
530
531                 //Waypoint waypoint(value,time);
532
533                 Waypoint waypoint(value_node->new_waypoint_at_time(time));
534                 waypoint.set_value(value);
535
536                 waypoint.set_before(synfigapp::Main::get_interpolation());
537                 waypoint.set_after(synfigapp::Main::get_interpolation());
538
539                 action->set_param("canvas",get_canvas());
540                 action->set_param("canvas_interface",get_canvas_interface());
541                 action->set_param("value_node",ValueNode::Handle(value_node));
542                 action->set_param("waypoint",waypoint);
543
544                 if(!action->is_ready())
545                         throw Error(Error::TYPE_NOTREADY);
546
547                                 DEBUGPOINT();
548                 add_action(action);
549
550                 return;
551         }
552         else
553         {
554                 if(value_desc.is_value_node())
555                 {
556                         if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
557                         {
558                                 Action::Handle action(ValueNodeConstSet::create());
559
560                                 action->set_param("canvas",get_canvas());
561                                 action->set_param("canvas_interface",get_canvas_interface());
562                                 action->set_param("value_node",value_desc.get_value_node());
563                                 action->set_param("new_value",value);
564
565                                 if(!action->is_ready())
566                                         throw Error(Error::TYPE_NOTREADY);
567
568                                 add_action_front(action);
569                                 return;
570                         }
571                         else
572                         if(ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()))
573                                 throw Error(_("You must be in Animate-Editing-Mode to directly manipulate this value"));
574                         else
575                                 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
576                 }
577                 else
578                 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
579                 {
580                         Action::Handle layer_param_set(LayerParamSet::create());
581
582                         layer_param_set->set_param("canvas",get_canvas());
583                         layer_param_set->set_param("canvas_interface",get_canvas_interface());
584                         layer_param_set->set_param("layer",value_desc.get_layer());
585                         layer_param_set->set_param("param",value_desc.get_param_name());
586                         layer_param_set->set_param("new_value",value);
587
588                         if(!layer_param_set->is_ready())
589                                 throw Error(Error::TYPE_NOTREADY);
590
591                         add_action_front(layer_param_set);
592                         return;
593                 }
594
595                 throw Error(_("Unsupported ValueDesc type"));
596         }
597 }