fbac8229bc6b4e396389785b65994c1f009fbfd4
[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(String(_("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 synfig::ValueBase
97 synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
98 {
99         const int count((*count_)(t).get(int()));
100         int i;
101         Gradient ret;
102
103         if(count<=0)
104                 return ret;
105
106         const Gradient gradient((*gradient_)(t).get(Gradient()));
107         const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
108         const bool specify_start((*specify_start_)(t).get(bool()));
109         const bool specify_end((*specify_end_)(t).get(bool()));
110
111         const float gradient_width_a(width/count);
112         const float gradient_width_b((1.0-width)/count);
113
114         Gradient::const_iterator iter;
115         Gradient::const_reverse_iterator riter;
116         if (specify_start)
117                 ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
118         for(i=0;i<count;i++)
119         {
120                 float pos(float(i)/count);
121                 if (width != 0.0)
122                         for(iter=gradient.begin();iter!=gradient.end();iter++)
123                                 ret.push_back(Gradient::CPoint(pos+gradient_width_a*iter->pos,iter->color));
124                 pos+=gradient_width_a;
125                 if (width != 1.0)
126                         for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
127                                 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
128         }
129         if (specify_end)
130                 ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
131         return ret;
132 }
133
134 bool
135 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle value)
136 {
137         assert(i>=0 && i<link_count());
138
139         switch(i)
140         {
141         case 0: CHECK_TYPE_AND_SET_VALUE(gradient_,          get_type());
142         case 1: CHECK_TYPE_AND_SET_VALUE(count_,                 ValueBase::TYPE_INTEGER);
143         case 2: CHECK_TYPE_AND_SET_VALUE(width_,                 ValueBase::TYPE_REAL);
144         case 3: CHECK_TYPE_AND_SET_VALUE(specify_start_, ValueBase::TYPE_BOOL);
145         case 4: CHECK_TYPE_AND_SET_VALUE(specify_end_,   ValueBase::TYPE_BOOL);
146         case 5: CHECK_TYPE_AND_SET_VALUE(start_color_,   ValueBase::TYPE_COLOR);
147         case 6: CHECK_TYPE_AND_SET_VALUE(end_color_,     ValueBase::TYPE_COLOR);
148         }
149         return false;
150 }
151
152 ValueNode::LooseHandle
153 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
154 {
155         assert(i>=0 && i<link_count());
156
157         switch(i)
158         {
159                 case 0:  return gradient_;
160                 case 1:  return count_;
161                 case 2:  return width_;
162                 case 3:  return specify_start_;
163                 case 4:  return specify_end_;
164                 case 5:  return start_color_;
165                 case 6:  return end_color_;
166             default: return 0;
167         }
168 }
169
170 int
171 ValueNode_Repeat_Gradient::link_count()const
172 {
173         return 7;
174 }
175
176 String
177 ValueNode_Repeat_Gradient::link_local_name(int i)const
178 {
179         assert(i>=0 && i<link_count());
180
181         switch(i)
182         {
183                 case 0:  return _("Gradient");
184                 case 1:  return _("Count");
185                 case 2:  return _("Width");
186                 case 3:  return _("Specify Start");
187                 case 4:  return _("Specify End");
188                 case 5:  return _("Start Color");
189                 case 6:  return _("End Color");
190                 default: return String();
191         }
192 }
193
194 String
195 ValueNode_Repeat_Gradient::link_name(int i)const
196 {
197         assert(i>=0 && i<link_count());
198
199         switch(i)
200         {
201                 case 0:  return "gradient";
202                 case 1:  return "count";
203                 case 2:  return "width";
204                 case 3:  return "specify_start";
205                 case 4:  return "specify_end";
206                 case 5:  return "start_color";
207                 case 6:  return "end_color";
208                 default: return String();
209         }
210 }
211
212 int
213 ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
214 {
215         if(name=="gradient") return 0;
216         if(name=="count")    return 1;
217         if(name=="width")    return 2;
218         if(name=="specify_start") return 3;
219         if(name=="specify_end")   return 4;
220         if(name=="start_color")   return 5;
221         if(name=="end_color")     return 6;
222         throw Exception::BadLinkName(name);
223 }
224
225 String
226 ValueNode_Repeat_Gradient::get_name()const
227 {
228         return "repeat_gradient";
229 }
230
231 String
232 ValueNode_Repeat_Gradient::get_local_name()const
233 {
234         return _("Repeat Gradient");
235 }
236
237 bool
238 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
239 {
240         return type==ValueBase::TYPE_GRADIENT;
241 }