Rename get_param_vocab to get_children_vocab and use a wrapper for the pure virtual...
[synfig.git] / synfig-core / 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$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 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         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
100                 printf("%s:%d operator()\n", __FILE__, __LINE__);
101
102         const int count((*count_)(t).get(int()));
103         int i;
104         Gradient ret;
105
106         if(count<=0)
107                 return ret;
108
109         const Gradient gradient((*gradient_)(t).get(Gradient()));
110         const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
111         const bool specify_start((*specify_start_)(t).get(bool()));
112         const bool specify_end((*specify_end_)(t).get(bool()));
113
114         const float gradient_width_a(width/count);
115         const float gradient_width_b((1.0-width)/count);
116
117         Gradient::const_iterator iter;
118         Gradient::const_reverse_iterator riter;
119         if (specify_start)
120                 ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
121         for(i=0;i<count;i++)
122         {
123                 float pos(float(i)/count);
124                 if (width != 0.0)
125                         for(iter=gradient.begin();iter!=gradient.end();iter++)
126                                 ret.push_back(Gradient::CPoint(pos+gradient_width_a*iter->pos,iter->color));
127                 pos+=gradient_width_a;
128                 if (width != 1.0)
129                         for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
130                                 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
131         }
132         if (specify_end)
133                 ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
134         return ret;
135 }
136
137 bool
138 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle value)
139 {
140         assert(i>=0 && i<link_count());
141
142         switch(i)
143         {
144         case 0: CHECK_TYPE_AND_SET_VALUE(gradient_,          get_type());
145         case 1: CHECK_TYPE_AND_SET_VALUE(count_,                 ValueBase::TYPE_INTEGER);
146         case 2: CHECK_TYPE_AND_SET_VALUE(width_,                 ValueBase::TYPE_REAL);
147         case 3: CHECK_TYPE_AND_SET_VALUE(specify_start_, ValueBase::TYPE_BOOL);
148         case 4: CHECK_TYPE_AND_SET_VALUE(specify_end_,   ValueBase::TYPE_BOOL);
149         case 5: CHECK_TYPE_AND_SET_VALUE(start_color_,   ValueBase::TYPE_COLOR);
150         case 6: CHECK_TYPE_AND_SET_VALUE(end_color_,     ValueBase::TYPE_COLOR);
151         }
152         return false;
153 }
154
155 ValueNode::LooseHandle
156 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
157 {
158         assert(i>=0 && i<link_count());
159
160         switch(i)
161         {
162                 case 0:  return gradient_;
163                 case 1:  return count_;
164                 case 2:  return width_;
165                 case 3:  return specify_start_;
166                 case 4:  return specify_end_;
167                 case 5:  return start_color_;
168                 case 6:  return end_color_;
169             default: return 0;
170         }
171 }
172
173 int
174 ValueNode_Repeat_Gradient::link_count()const
175 {
176         return 7;
177 }
178
179 String
180 ValueNode_Repeat_Gradient::link_local_name(int i)const
181 {
182         assert(i>=0 && i<link_count());
183
184         switch(i)
185         {
186                 case 0:  return _("Gradient");
187                 case 1:  return _("Count");
188                 case 2:  return _("Width");
189                 case 3:  return _("Specify Start");
190                 case 4:  return _("Specify End");
191                 case 5:  return _("Start Color");
192                 case 6:  return _("End Color");
193                 default: return String();
194         }
195 }
196
197 String
198 ValueNode_Repeat_Gradient::link_name(int i)const
199 {
200         assert(i>=0 && i<link_count());
201
202         switch(i)
203         {
204                 case 0:  return "gradient";
205                 case 1:  return "count";
206                 case 2:  return "width";
207                 case 3:  return "specify_start";
208                 case 4:  return "specify_end";
209                 case 5:  return "start_color";
210                 case 6:  return "end_color";
211                 default: return String();
212         }
213 }
214
215 int
216 ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
217 {
218         if(name=="gradient") return 0;
219         if(name=="count")    return 1;
220         if(name=="width")    return 2;
221         if(name=="specify_start") return 3;
222         if(name=="specify_end")   return 4;
223         if(name=="start_color")   return 5;
224         if(name=="end_color")     return 6;
225         throw Exception::BadLinkName(name);
226 }
227
228 String
229 ValueNode_Repeat_Gradient::get_name()const
230 {
231         return "repeat_gradient";
232 }
233
234 String
235 ValueNode_Repeat_Gradient::get_local_name()const
236 {
237         return _("Repeat Gradient");
238 }
239
240 bool
241 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
242 {
243         return type==ValueBase::TYPE_GRADIENT;
244 }
245
246 LinkableValueNode::Vocab
247 ValueNode_Repeat_Gradient::get_children_vocab_vfunc()const
248 {
249         LinkableValueNode::Vocab ret;
250
251         ret.push_back(ParamDesc(ValueBase(),"gradient")
252                 .set_local_name(_("Gradient"))
253                 .set_description(_("The source gradient to repeat"))
254         );
255
256         ret.push_back(ParamDesc(ValueBase(),"count")
257                 .set_local_name(_("Count"))
258                 .set_description(_("The number of repetition of the gradient"))
259         );
260
261         ret.push_back(ParamDesc(ValueBase(),"width")
262                 .set_local_name(_("Width"))
263                 .set_description(_("Specifies how much biased is the source gradeint in the repetition [0,1]"))
264         );
265
266         ret.push_back(ParamDesc(ValueBase(),"specify_start")
267                 .set_local_name(_("Specify Start"))
268                 .set_description(_("When checked, 'Start Color' is used as the start of the resulting gradient"))
269         );
270
271         ret.push_back(ParamDesc(ValueBase(),"specify_end")
272                 .set_local_name(_("Specify End"))
273                 .set_description(_("When checked, 'End Color' is used as the start of the resulting gradient"))
274         );
275
276         ret.push_back(ParamDesc(ValueBase(),"start_color")
277                 .set_local_name(_("Start Color"))
278                 .set_description(_("Used as the start of the resulting gradient"))
279         );
280
281         ret.push_back(ParamDesc(ValueBase(),"end_color")
282                 .set_local_name(_("End Color"))
283                 .set_description(_("Used as the end of the resulting gradient"))
284         );
285
286         return ret;
287 }