Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc2 / src / synfig / valuenode_repeat_gradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_repeat_gradient.cpp
3 **      \brief Template File
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_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         switch(i)
206         {
207                 case 0:
208                         if(set_gradient(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
209                         else { return false; }
210                 case 1:
211                         if(set_count(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
212                         else { return false; }
213                 case 2:
214                         if(set_width(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
215                         else { return false; }
216                 case 3:
217                         if(set_specify_start(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
218                         else { return false; }
219                 case 4:
220                         if(set_specify_end(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
221                         else { return false; }
222                 case 5:
223                         if(set_start_color(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
224                         else { return false; }
225                 case 6:
226                         if(set_end_color(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
227                         else { return false; }
228         }
229
230         return false;
231 }
232
233 ValueNode::LooseHandle
234 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
235 {
236         assert(i>=0 && i<link_count());
237         switch(i)
238         {
239                 case 0:  return gradient_;
240                 case 1:  return count_;
241                 case 2:  return width_;
242                 case 3:  return specify_start_;
243                 case 4:  return specify_end_;
244                 case 5:  return start_color_;
245                 case 6:  return end_color_;
246             default: return 0;
247         }
248 }
249
250 int
251 ValueNode_Repeat_Gradient::link_count()const
252 {
253         return 7;
254 }
255
256 String
257 ValueNode_Repeat_Gradient::link_local_name(int i)const
258 {
259         assert(i>=0 && i<link_count());
260         switch(i)
261         {
262                 case 0:  return _("Gradient");
263                 case 1:  return _("Count");
264                 case 2:  return _("Width");
265                 case 3:  return _("Specify Start");
266                 case 4:  return _("Specify End");
267                 case 5:  return _("Start Color");
268                 case 6:  return _("End Color");
269                 default: return String();
270         }
271 }
272
273 String
274 ValueNode_Repeat_Gradient::link_name(int i)const
275 {
276         assert(i>=0 && i<link_count());
277         switch(i)
278         {
279                 case 0:  return "gradient";
280                 case 1:  return "count";
281                 case 2:  return "width";
282                 case 3:  return "specify_start";
283                 case 4:  return "specify_end";
284                 case 5:  return "start_color";
285                 case 6:  return "end_color";
286                 default: return String();
287         }
288 }
289
290 int
291 ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
292 {
293         if(name=="gradient") return 0;
294         if(name=="count")    return 1;
295         if(name=="width")    return 2;
296         if(name=="specify_start") return 3;
297         if(name=="specify_end")   return 4;
298         if(name=="start_color")   return 5;
299         if(name=="end_color")     return 6;
300         throw Exception::BadLinkName(name);
301 }
302
303 String
304 ValueNode_Repeat_Gradient::get_name()const
305 {
306         return "repeat_gradient";
307 }
308
309 String
310 ValueNode_Repeat_Gradient::get_local_name()const
311 {
312         return _("Repeat Gradient");
313 }
314
315 bool
316 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
317 {
318         return type==ValueBase::TYPE_GRADIENT;
319 }