Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / synfig / valuenode_composite.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_composite.cpp
3 **      \brief Template File
4 **
5 **      $Id: valuenode_composite.cpp,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
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 "valuenode_composite.h"
33 #include "valuenode_const.h"
34 #include <stdexcept>
35 #include "general.h"
36 #include "valuenode_radialcomposite.h"
37 #include "vector.h"
38 #include "color.h"
39 #include "segment.h"
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 synfig::ValueNode_Composite::ValueNode_Composite(const ValueBase &value):
58         LinkableValueNode(value.get_type())
59 {
60         switch(get_type())
61         {
62                 case ValueBase::TYPE_VECTOR:
63                         set_link("x",ValueNode_Const::create(value.get(Vector())[0]));
64                         set_link("y",ValueNode_Const::create(value.get(Vector())[1]));
65                         break;
66                 case ValueBase::TYPE_COLOR:
67                         set_link("r",ValueNode_Const::create(value.get(Color()).get_r()));
68                         set_link("g",ValueNode_Const::create(value.get(Color()).get_g()));
69                         set_link("b",ValueNode_Const::create(value.get(Color()).get_b()));
70                         set_link("a",ValueNode_Const::create(value.get(Color()).get_a()));
71                         break;
72                 case ValueBase::TYPE_SEGMENT:
73                         set_link("p1",ValueNode_Const::create(value.get(Segment()).p1));
74                         set_link("t1",ValueNode_Const::create(value.get(Segment()).t1));
75                         set_link("p2",ValueNode_Const::create(value.get(Segment()).p2));
76                         set_link("t2",ValueNode_Const::create(value.get(Segment()).t2));
77                         break;
78                 case ValueBase::TYPE_BLINEPOINT:
79                 {
80                         BLinePoint bline_point(value);
81                         set_link(0,ValueNode_Const::create(bline_point.get_vertex()));
82                         set_link(1,ValueNode_Const::create(bline_point.get_width()));
83                         set_link(2,ValueNode_Const::create(bline_point.get_origin()));
84                         set_link(3,ValueNode_Const::create(bline_point.get_split_tangent_flag()));
85                         set_link(4,ValueNode_RadialComposite::create(bline_point.get_tangent1()));
86                         set_link(5,ValueNode_RadialComposite::create(bline_point.get_tangent2()));
87                         break;
88                 }
89                 default:
90                         assert(0);
91                         throw Exception::BadType(ValueBase::type_name(get_type()));                     
92         }
93 }
94
95 ValueNode_Composite::~ValueNode_Composite()
96 {
97         unlink_all();
98 }
99
100 ValueNode_Composite*
101 ValueNode_Composite::create(const ValueBase &value)
102 {
103         return new ValueNode_Composite(value);
104 }
105         
106 LinkableValueNode*
107 ValueNode_Composite::create_new()const
108 {
109         return new ValueNode_Composite(ValueBase(get_type()));
110 }
111
112 ValueBase
113 synfig::ValueNode_Composite::operator()(Time t)const
114 {
115         switch(get_type())
116         {
117                 case ValueBase::TYPE_VECTOR:
118                 {
119                         Vector vect;
120                         assert(components[0] && components[1]);
121                         vect[0]=(*components[0])(t).get(Vector::value_type());
122                         vect[1]=(*components[1])(t).get(Vector::value_type());
123                         return vect;
124                 }
125                 case ValueBase::TYPE_COLOR:
126                 {
127                         Color color;
128                         assert(components[0] && components[1] && components[2] && components[3]);
129                         color.set_r((*components[0])(t).get(Vector::value_type()));
130                         color.set_g((*components[1])(t).get(Vector::value_type()));
131                         color.set_b((*components[2])(t).get(Vector::value_type()));
132                         color.set_a((*components[3])(t).get(Vector::value_type()));
133                         return color;
134                 }
135                 case ValueBase::TYPE_SEGMENT:
136                 {
137                         Segment seg;
138                         assert(components[0] && components[1] && components[2] && components[3]);
139                         seg.p1=(*components[0])(t).get(Point());
140                         seg.t1=(*components[1])(t).get(Vector());
141                         seg.p2=(*components[2])(t).get(Point());
142                         seg.t2=(*components[3])(t).get(Vector());
143                         return seg;
144                 }
145                 case ValueBase::TYPE_BLINEPOINT:
146                 {
147                         BLinePoint ret;
148                         assert(components[0] && components[1] && components[2] && components[3] && components[4] && components[5]);
149                         ret.set_vertex((*components[0])(t).get(Point()));
150                         ret.set_width((*components[1])(t).get(Real()));
151                         ret.set_origin((*components[2])(t).get(Real()));
152                         ret.set_split_tangent_flag((*components[3])(t).get(bool()));
153                         ret.set_tangent1((*components[4])(t).get(Vector()));
154                         if(ret.get_split_tangent_flag())
155                                 ret.set_tangent2((*components[5])(t).get(Vector()));
156                         return ret;
157                 }
158                 default:
159                         synfig::error(string("ValueNode_Composite::operator():")+_("Bad type for composite"));
160                         assert(components[0]);
161                         return (*components[0])(t);
162         }
163 }
164
165 int
166 ValueNode_Composite::link_count()const
167 {
168         switch(get_type())
169         {
170         case ValueBase::TYPE_VECTOR:
171                 return 2;
172         case ValueBase::TYPE_COLOR:
173                 return 4;
174         case ValueBase::TYPE_SEGMENT:
175                 return 4;
176         case ValueBase::TYPE_BLINEPOINT:
177                 return 6;
178         default:
179                 synfig::warning(string("ValueNode_Composite::component_count():")+_("Bad type for composite"));
180                 return 1;
181         }
182 }
183
184 bool
185 ValueNode_Composite::set_link_vfunc(int i,ValueNode::Handle x)
186 {
187         assert(i>=0);
188         assert(i<6);
189         
190         if(PlaceholderValueNode::Handle::cast_dynamic(x))
191         {
192                 components[i]=x;
193                 return true;
194         }
195
196         switch(get_type())
197         {
198                 case ValueBase::TYPE_VECTOR:
199                         assert(i<2);
200                         if(x->get_type()==ValueBase(Real()).get_type() || PlaceholderValueNode::Handle::cast_dynamic(x))
201                         {
202                                 components[i]=x;
203                                 return true;
204                         }
205                         break;
206
207                 case ValueBase::TYPE_COLOR:
208                         assert(i<4);
209                         if(x->get_type()==ValueBase(Real()).get_type() || PlaceholderValueNode::Handle::cast_dynamic(x))
210                         {
211                                 components[i]=x;
212                                 return true;
213                         }
214                         break;
215                         
216                 case ValueBase::TYPE_SEGMENT:
217                         assert(i<4);
218                         if(x->get_type()==ValueBase(Point()).get_type() || PlaceholderValueNode::Handle::cast_dynamic(x))
219                         {
220                                 components[i]=x;
221                                 return true;
222                         }
223                         break;
224
225                 case ValueBase::TYPE_BLINEPOINT:
226                         assert(i<6);
227                         if((i==0 || i==4 || i==5) && x->get_type()==ValueBase(Point()).get_type())
228                         {
229                                 components[i]=x;
230                                 return true;
231                         }
232                         if((i==1 || i==2) && x->get_type()==ValueBase(Real()).get_type())
233                         {
234                                 components[i]=x;
235                                 return true;
236                         }
237                         if(i==3 && x->get_type()==ValueBase(bool()).get_type())
238                         {
239                                 components[i]=x;
240                                 return true;
241                         }
242                         break;
243                         
244                 default:
245                         break;
246         }
247         return false;   
248 }
249
250 ValueNode::LooseHandle
251 ValueNode_Composite::get_link_vfunc(int i)const
252 {
253         assert(i>=0 && i<6);
254         return components[i];
255 }
256
257 String
258 ValueNode_Composite::link_local_name(int i)const
259 {
260         assert(i>=0 && i<6);
261         switch(get_type())
262         {
263                 case ValueBase::TYPE_VECTOR:
264                         return strprintf("%c-Axis",'X'+i);
265
266                 case ValueBase::TYPE_COLOR:
267                         if(i==0)
268                                 return _("Red");
269                         else if(i==1)
270                                 return _("Green");
271                         else if(i==2)
272                                 return _("Blue");
273                         else if(i==3)
274                                 return _("Alpha");
275                         
276                 case ValueBase::TYPE_SEGMENT:
277                         if(i==0)
278                                 return _("Vertex 1");
279                         else if(i==1)
280                                 return _("Tangent 1");
281                         else if(i==2)
282                                 return _("Vertex 2");
283                         else if(i==3)
284                                 return _("Tangent 2");
285
286                 case ValueBase::TYPE_BLINEPOINT:
287                         if(i==0)
288                                 return _("Vertex");
289                         else if(i==1)
290                                 return _("Width");
291                         else if(i==2)
292                                 return _("Origin");
293                         else if(i==3)
294                                 return _("Split Tangents");
295                         else if(i==4)
296                                 return _("Tangent 1");
297                         else if(i==5)
298                                 return _("Tangent 2");
299
300                 default:
301                         break;
302         }
303         return etl::strprintf(_("C%d"),i+1);    
304 }       
305
306
307 String
308 ValueNode_Composite::link_name(int i)const
309 {
310         assert(i>=0 && i<5);
311         return strprintf("c%d",i);
312 }       
313
314 int
315 ValueNode_Composite::get_link_index_from_name(const String &name)const
316 {
317         if(name.empty())
318                 throw Exception::BadLinkName(name);
319
320         if(name[0]=='c')
321                 return name[1]-'0';
322
323         switch(get_type())
324         {
325         case ValueBase::TYPE_COLOR:
326                 if(name[0]=='r')
327                         return 0;
328                 if(name[0]=='g')
329                         return 1;
330                 if(name[0]=='b')
331                         return 2;
332                 if(name[0]=='a')
333                         return 3;
334         case ValueBase::TYPE_SEGMENT:
335                 if(name=="p1")
336                         return 0;
337                 if(name=="t1")
338                         return 1;
339                 if(name=="p2")
340                         return 2;
341                 if(name=="t2")
342                         return 3;
343         case ValueBase::TYPE_VECTOR:
344                 if(name[0]=='x')
345                         return 0;
346                 if(name[0]=='y')
347                         return 1;
348                 if(name[0]=='z')
349                         return 2;
350         case ValueBase::TYPE_BLINEPOINT:
351                 if(name[0]=='p' || name=="v1" || name=="p1")
352                         return 0;
353                 if(name=="w" || name=="width")
354                         return 1;
355                 if(name=="o" || name=="origin")
356                         return 2;
357                 if(name=="split")
358                         return 3;
359                 if(name=="t1")
360                         return 4;
361                 if(name=="t2")
362                         return 5;
363         default:                        
364                 break;
365         }
366
367         throw Exception::BadLinkName(name);
368 }
369
370 String
371 ValueNode_Composite::get_name()const
372 {
373         return "composite";
374 }
375
376 String
377 ValueNode_Composite::get_local_name()const
378 {
379         return _("Composite");
380 }
381
382 bool
383 ValueNode_Composite::check_type(ValueBase::Type type)
384 {
385         return
386                 type==ValueBase::TYPE_SEGMENT ||
387                 type==ValueBase::TYPE_VECTOR ||
388                 type==ValueBase::TYPE_COLOR ||
389                 type==ValueBase::TYPE_BLINEPOINT;
390 }