Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_06 / 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 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "layerparamset.h"
33 #include "valuenodeconstset.h"
34 #include "valuedescconnect.h"
35 #include "waypointsetsmart.h"
36
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>
43
44 #endif
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49 using namespace synfigapp;
50 using namespace Action;
51
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__; }
56
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$");
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_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                         Vector vect(value.get(Vector()));
335                         components[0]=vect.mag();
336                         components[1]=Angle(Angle::tan(vect[1],vect[0]));
337                         n_components=2;
338                 }
339                         break;
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();
345                         n_components=4;
346                         break;
347                 default:
348                         throw Error("Bad type for radial composite (%s)",ValueBase::type_name(value.get_type()).c_str());
349                         break;
350                 }
351                 for(int i=0;i<n_components;i++)
352                 {
353                         ValueDesc component_value_desc(ValueNode_RadialComposite::Handle::cast_dynamic(value_desc.get_value_node()),i);
354
355                         Action::Handle action(Action::create("value_desc_set"));
356
357                         if(!action)
358                                 throw Error(_("Unable to find action value_desc_set (bug)"));
359
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);
365
366                         if(!action->is_ready())
367                                 throw Error(Error::TYPE_NOTREADY);
368
369                         add_action(action);
370                 }
371
372                 return;
373         }
374
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)
381         {
382                 ValueNode_Composite::Handle parent_value_node;
383                 parent_value_node=parent_value_node.cast_dynamic(value_desc.get_parent_value_node());
384
385                 assert(parent_value_node);
386
387                 // are we splitting or merging the tangents?
388             if (value.get(bool()))
389             {
390                         // we are splitting tangents
391
392                         Action::Handle action(Action::create("value_desc_set"));
393
394                         if(!action)
395                                 throw Error(_("Unable to find action value_desc_set (bug)"));
396
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));
402
403                         if(!action->is_ready())
404                                 throw Error(Error::TYPE_NOTREADY);
405
406                         add_action(action);
407             }
408             else
409             {
410                         // we are merging tangents
411
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);
415
416                         {
417                                 Action::Handle action(Action::create("value_desc_set"));
418
419                                 if(!action)
420                                         throw Error(_("Unable to find action value_desc_set (bug)"));
421
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));
427
428                                 if(!action->is_ready())
429                                         throw Error(Error::TYPE_NOTREADY);
430
431                                 add_action(action);
432                         }
433
434                         {
435                                 Action::Handle action(Action::create("value_desc_set"));
436
437                                 if(!action)
438                                         throw Error(_("Unable to find action value_desc_set (bug)"));
439
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));
445
446                                 if(!action->is_ready())
447                                         throw Error(Error::TYPE_NOTREADY);
448
449                                 add_action(action);
450                         }
451             }
452
453         }
454
455 /*      DEBUGPOINT();
456         if(     value_desc.parent_is_value_node())
457         {
458                 DEBUGPOINT();
459                 if(value_desc.get_parent_value_node()->get_type()==ValueBase::TYPE_BLINEPOINT)
460                 {
461                         DEBUGPOINT();
462                         if(value_desc.get_index()==4)
463                         {
464                                 DEBUGPOINT();
465                                 if((*value_desc.get_parent_value_node())(time).get(BLinePoint()).get_split_tangent_flag()==false)
466                                 {
467                                         DEBUGPOINT();
468                                 }
469                         }
470                 }
471         }
472 */
473
474
475         // If we are in animate editing mode
476         if(get_edit_mode()&MODE_ANIMATE)
477         {
478
479                 ValueNode_Animated::Handle& value_node(value_node_animated);
480
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()))
485                 {
486                         ValueBase value;
487                         if(value_desc.is_value_node())
488                                 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
489                         else
490                                 value=value_desc.get_value();
491
492                         if(!value_node)value_node=ValueNode_Animated::create(value,time);
493                         //if(!value_node)value_node=ValueNode_Animated::create(value.get_type());
494
495                         Action::Handle action;
496
497                         if(!value_desc.is_value_node())
498                         {
499                                 action=(ValueDescConnect::create());
500                                 action->set_param("dest",value_desc);
501                                 action->set_param("src",ValueNode::Handle(value_node));
502                         }
503                         else
504                         {
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));
508                         }
509
510                         action->set_param("canvas",get_canvas());
511                         action->set_param("canvas_interface",get_canvas_interface());
512
513                         if(!action->is_ready())
514                                 throw Error(Error::TYPE_NOTREADY);
515
516                                 DEBUGPOINT();
517                         add_action_front(action);
518                 }
519                 else
520                 {
521                         value_node=value_node.cast_dynamic(value_desc.get_value_node());
522                 }
523
524                                 DEBUGPOINT();
525                 if(!value_node)
526                         throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
527
528                 Action::Handle action(WaypointSetSmart::create());
529
530                 //Waypoint waypoint(value,time);
531
532                 Waypoint waypoint(value_node->new_waypoint_at_time(time));
533                 waypoint.set_value(value);
534
535                 waypoint.set_before(synfigapp::Main::get_interpolation());
536                 waypoint.set_after(synfigapp::Main::get_interpolation());
537
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);
542
543                 if(!action->is_ready())
544                         throw Error(Error::TYPE_NOTREADY);
545
546                                 DEBUGPOINT();
547                 add_action(action);
548
549                 return;
550         }
551         else
552         {
553                 if(value_desc.is_value_node())
554                 {
555                         if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
556                         {
557                                 Action::Handle action(ValueNodeConstSet::create());
558
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);
563
564                                 if(!action->is_ready())
565                                         throw Error(Error::TYPE_NOTREADY);
566
567                                 add_action_front(action);
568                                 return;
569                         }
570                         else
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"));
573                         else
574                                 throw Error(_("Direct manipulation of this ValueNode type is not yet supported"));
575                 }
576                 else
577                 if(value_desc.parent_is_layer_param() && !value_desc.is_value_node())
578                 {
579                         Action::Handle layer_param_set(LayerParamSet::create());
580
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);
586
587                         if(!layer_param_set->is_ready())
588                                 throw Error(Error::TYPE_NOTREADY);
589
590                         add_action_front(layer_param_set);
591                         return;
592                 }
593
594                 throw Error(_("Unsupported ValueDesc type"));
595         }
596 }