Use translated versions of the type names everywhere other than in the .sif(z) files.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_composite.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_composite.cpp
3 **      \brief Implementation of the "Composite" valuenode conversion.
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 "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_local_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 && i<link_count());
188
189         if(PlaceholderValueNode::Handle::cast_dynamic(x))
190         {
191                 components[i]=x;
192                 return true;
193         }
194
195         switch(get_type())
196         {
197                 case ValueBase::TYPE_VECTOR:
198                         if(x->get_type()==ValueBase(Real()).get_type() || PlaceholderValueNode::Handle::cast_dynamic(x))
199                         {
200                                 components[i]=x;
201                                 return true;
202                         }
203                         break;
204
205                 case ValueBase::TYPE_COLOR:
206                         if(x->get_type()==ValueBase(Real()).get_type() || PlaceholderValueNode::Handle::cast_dynamic(x))
207                         {
208                                 components[i]=x;
209                                 return true;
210                         }
211                         break;
212
213                 case ValueBase::TYPE_SEGMENT:
214                         if(x->get_type()==ValueBase(Point()).get_type() || PlaceholderValueNode::Handle::cast_dynamic(x))
215                         {
216                                 components[i]=x;
217                                 return true;
218                         }
219                         break;
220
221                 case ValueBase::TYPE_BLINEPOINT:
222                         if((i==0 || i==4 || i==5) && x->get_type()==ValueBase(Point()).get_type())
223                         {
224                                 components[i]=x;
225                                 return true;
226                         }
227                         if((i==1 || i==2) && x->get_type()==ValueBase(Real()).get_type())
228                         {
229                                 components[i]=x;
230                                 return true;
231                         }
232                         if(i==3 && x->get_type()==ValueBase(bool()).get_type())
233                         {
234                                 components[i]=x;
235                                 return true;
236                         }
237                         break;
238
239                 default:
240                         break;
241         }
242         return false;
243 }
244
245 ValueNode::LooseHandle
246 ValueNode_Composite::get_link_vfunc(int i)const
247 {
248         assert(i>=0 && i<link_count());
249
250         return components[i];
251 }
252
253 String
254 ValueNode_Composite::link_local_name(int i)const
255 {
256         assert(i>=0 && i<link_count());
257
258         switch(get_type())
259         {
260                 case ValueBase::TYPE_VECTOR:
261                         return strprintf("%c-Axis",'X'+i);
262
263                 case ValueBase::TYPE_COLOR:
264                         if(i==0)
265                                 return _("Red");
266                         else if(i==1)
267                                 return _("Green");
268                         else if(i==2)
269                                 return _("Blue");
270                         else if(i==3)
271                                 return _("Alpha");
272
273                 case ValueBase::TYPE_SEGMENT:
274                         if(i==0)
275                                 return _("Vertex 1");
276                         else if(i==1)
277                                 return _("Tangent 1");
278                         else if(i==2)
279                                 return _("Vertex 2");
280                         else if(i==3)
281                                 return _("Tangent 2");
282
283                 case ValueBase::TYPE_BLINEPOINT:
284                         if(i==0)
285                                 return _("Vertex");
286                         else if(i==1)
287                                 return _("Width");
288                         else if(i==2)
289                                 return _("Origin");
290                         else if(i==3)
291                                 return _("Split Tangents");
292                         else if(i==4)
293                                 return _("Tangent 1");
294                         else if(i==5)
295                                 return _("Tangent 2");
296
297                 default:
298                         return etl::strprintf(_("C%d"),i+1);
299         }
300 }
301
302
303 String
304 ValueNode_Composite::link_name(int i)const
305 {
306         assert(i>=0 && i<link_count());
307
308         return strprintf("c%d",i);
309 }
310
311 int
312 ValueNode_Composite::get_link_index_from_name(const String &name)const
313 {
314         if(name.empty())
315                 throw Exception::BadLinkName(name);
316
317         if(name[0]=='c')
318                 return name[1]-'0';
319
320         switch(get_type())
321         {
322         case ValueBase::TYPE_COLOR:
323                 if(name[0]=='r')
324                         return 0;
325                 if(name[0]=='g')
326                         return 1;
327                 if(name[0]=='b')
328                         return 2;
329                 if(name[0]=='a')
330                         return 3;
331         case ValueBase::TYPE_SEGMENT:
332                 if(name=="p1")
333                         return 0;
334                 if(name=="t1")
335                         return 1;
336                 if(name=="p2")
337                         return 2;
338                 if(name=="t2")
339                         return 3;
340         case ValueBase::TYPE_VECTOR:
341                 if(name[0]=='x')
342                         return 0;
343                 if(name[0]=='y')
344                         return 1;
345                 if(name[0]=='z')
346                         return 2;
347         case ValueBase::TYPE_BLINEPOINT:
348                 if(name[0]=='p' || name=="v1" || name=="p1")
349                         return 0;
350                 if(name=="w" || name=="width")
351                         return 1;
352                 if(name=="o" || name=="origin")
353                         return 2;
354                 if(name=="split")
355                         return 3;
356                 if(name=="t1")
357                         return 4;
358                 if(name=="t2")
359                         return 5;
360         default:
361                 break;
362         }
363
364         throw Exception::BadLinkName(name);
365 }
366
367 String
368 ValueNode_Composite::get_name()const
369 {
370         return "composite";
371 }
372
373 String
374 ValueNode_Composite::get_local_name()const
375 {
376         return _("Composite");
377 }
378
379 bool
380 ValueNode_Composite::check_type(ValueBase::Type type)
381 {
382         return
383                 type==ValueBase::TYPE_SEGMENT ||
384                 type==ValueBase::TYPE_VECTOR ||
385                 type==ValueBase::TYPE_COLOR ||
386                 type==ValueBase::TYPE_BLINEPOINT;
387 }