Use translated versions of the type names everywhere other than in the .sif(z) files.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_repeat_gradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_repeat_gradient.cpp
3 **      \brief Implementation of the "Repeat Gradient" valuenode conversion.
4 **
5 **      $Id: valuenode_repeat_gradient.cpp 604 2007-09-05 14:29:02Z dooglus $
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 "general.h"
34 #include "valuenode_repeat_gradient.h"
35 #include "valuenode_const.h"
36 #include <stdexcept>
37 #include "color.h"
38 #include "gradient.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 synfig::ValueNode_Repeat_Gradient::ValueNode_Repeat_Gradient(const Gradient& x):LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
57 {
58         set_link("gradient",ValueNode_Const::create(x));
59         set_link("count",count_=ValueNode_Const::create(int(3)));
60         set_link("width",ValueNode_Const::create(0.5));
61         set_link("specify_start",ValueNode_Const::create(true));
62         set_link("specify_end",ValueNode_Const::create(true));
63         set_link("start_color",ValueNode_Const::create(Color::alpha()));
64         set_link("end_color",ValueNode_Const::create(Color::alpha()));
65 }
66
67 LinkableValueNode*
68 ValueNode_Repeat_Gradient::create_new()const
69 {
70         return new ValueNode_Repeat_Gradient(Gradient());
71 }
72
73 ValueNode_Repeat_Gradient*
74 ValueNode_Repeat_Gradient::create(const ValueBase& x)
75 {
76         ValueBase::Type id(x.get_type());
77
78         if(id!=ValueBase::TYPE_GRADIENT)
79         {
80                 assert(0);
81                 throw runtime_error(_("synfig::ValueNode_Repeat_Gradient:Bad type ")+ValueBase::type_local_name(id));
82         }
83
84         ValueNode_Repeat_Gradient* value_node=new ValueNode_Repeat_Gradient(x.get(Gradient()));
85
86         assert(value_node->get_type()==id);
87
88         return value_node;
89 }
90
91 synfig::ValueNode_Repeat_Gradient::~ValueNode_Repeat_Gradient()
92 {
93         unlink_all();
94 }
95
96 bool
97 synfig::ValueNode_Repeat_Gradient::set_gradient(ValueNode::Handle a)
98 {
99         if(a->get_type()!=ValueBase::TYPE_GRADIENT)
100                 return false;
101
102         gradient_=a;
103
104         return true;
105 }
106
107 bool
108 synfig::ValueNode_Repeat_Gradient::set_width(ValueNode::Handle x)
109 {
110         if(x->get_type()!=ValueBase::TYPE_REAL)
111                 return false;
112
113         width_=x;
114
115         return true;
116 }
117
118 bool
119 synfig::ValueNode_Repeat_Gradient::set_count(ValueNode::Handle b)
120 {
121         if(b->get_type()!=ValueBase::TYPE_INTEGER)
122                 return false;
123         count_=b;
124         return true;
125 }
126
127 bool
128 synfig::ValueNode_Repeat_Gradient::set_specify_start(ValueNode::Handle a)
129 {
130         if(a->get_type()!=ValueBase::TYPE_BOOL)
131                 return false;
132         specify_start_=a;
133         return true;
134 }
135
136 bool
137 synfig::ValueNode_Repeat_Gradient::set_specify_end(ValueNode::Handle a)
138 {
139         if(a->get_type()!=ValueBase::TYPE_BOOL)
140                 return false;
141         specify_end_=a;
142         return true;
143 }
144
145 bool
146 synfig::ValueNode_Repeat_Gradient::set_start_color(ValueNode::Handle a)
147 {
148         if(a->get_type()!=ValueBase::TYPE_COLOR)
149                 return false;
150         start_color_=a;
151         return true;
152 }
153
154 bool
155 synfig::ValueNode_Repeat_Gradient::set_end_color(ValueNode::Handle a)
156 {
157         if(a->get_type()!=ValueBase::TYPE_COLOR)
158                 return false;
159         end_color_=a;
160         return true;
161 }
162
163 synfig::ValueBase
164 synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
165 {
166         const int count((*count_)(t).get(int()));
167         int i;
168         Gradient ret;
169
170         if(count<=0)
171                 return ret;
172
173         const Gradient gradient((*gradient_)(t).get(Gradient()));
174         const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
175         const bool specify_start((*specify_start_)(t).get(bool()));
176         const bool specify_end((*specify_end_)(t).get(bool()));
177
178         const float gradient_width_a(width/count);
179         const float gradient_width_b((1.0-width)/count);
180
181         Gradient::const_iterator iter;
182         Gradient::const_reverse_iterator riter;
183         if (specify_start)
184                 ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
185         for(i=0;i<count;i++)
186         {
187                 float pos(float(i)/count);
188                 if (width != 0.0)
189                         for(iter=gradient.begin();iter!=gradient.end();iter++)
190                                 ret.push_back(Gradient::CPoint(pos+gradient_width_a*iter->pos,iter->color));
191                 pos+=gradient_width_a;
192                 if (width != 1.0)
193                         for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
194                                 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
195         }
196         if (specify_end)
197                 ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
198         return ret;
199 }
200
201 bool
202 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle x)
203 {
204         assert(i>=0 && i<link_count());
205
206         switch(i)
207         {
208                 case 0:
209                         if(set_gradient(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
210                         else { return false; }
211                 case 1:
212                         if(set_count(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
213                         else { return false; }
214                 case 2:
215                         if(set_width(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
216                         else { return false; }
217                 case 3:
218                         if(set_specify_start(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
219                         else { return false; }
220                 case 4:
221                         if(set_specify_end(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
222                         else { return false; }
223                 case 5:
224                         if(set_start_color(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
225                         else { return false; }
226                 case 6:
227                         if(set_end_color(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
228                         else { return false; }
229         }
230
231         return false;
232 }
233
234 ValueNode::LooseHandle
235 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
236 {
237         assert(i>=0 && i<link_count());
238
239         switch(i)
240         {
241                 case 0:  return gradient_;
242                 case 1:  return count_;
243                 case 2:  return width_;
244                 case 3:  return specify_start_;
245                 case 4:  return specify_end_;
246                 case 5:  return start_color_;
247                 case 6:  return end_color_;
248             default: return 0;
249         }
250 }
251
252 int
253 ValueNode_Repeat_Gradient::link_count()const
254 {
255         return 7;
256 }
257
258 String
259 ValueNode_Repeat_Gradient::link_local_name(int i)const
260 {
261         assert(i>=0 && i<link_count());
262
263         switch(i)
264         {
265                 case 0:  return _("Gradient");
266                 case 1:  return _("Count");
267                 case 2:  return _("Width");
268                 case 3:  return _("Specify Start");
269                 case 4:  return _("Specify End");
270                 case 5:  return _("Start Color");
271                 case 6:  return _("End Color");
272                 default: return String();
273         }
274 }
275
276 String
277 ValueNode_Repeat_Gradient::link_name(int i)const
278 {
279         assert(i>=0 && i<link_count());
280
281         switch(i)
282         {
283                 case 0:  return "gradient";
284                 case 1:  return "count";
285                 case 2:  return "width";
286                 case 3:  return "specify_start";
287                 case 4:  return "specify_end";
288                 case 5:  return "start_color";
289                 case 6:  return "end_color";
290                 default: return String();
291         }
292 }
293
294 int
295 ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
296 {
297         if(name=="gradient") return 0;
298         if(name=="count")    return 1;
299         if(name=="width")    return 2;
300         if(name=="specify_start") return 3;
301         if(name=="specify_end")   return 4;
302         if(name=="start_color")   return 5;
303         if(name=="end_color")     return 6;
304         throw Exception::BadLinkName(name);
305 }
306
307 String
308 ValueNode_Repeat_Gradient::get_name()const
309 {
310         return "repeat_gradient";
311 }
312
313 String
314 ValueNode_Repeat_Gradient::get_local_name()const
315 {
316         return _("Repeat Gradient");
317 }
318
319 bool
320 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
321 {
322         return type==ValueBase::TYPE_GRADIENT;
323 }