1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_repeat_gradient.cpp
3 ** \brief Template File
5 ** $Id: valuenode_repeat_gradient.cpp 604 2007-09-05 14:29:02Z dooglus $
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
33 #include "valuenode_repeat_gradient.h"
34 #include "valuenode_const.h"
41 /* === U S I N G =========================================================== */
45 using namespace synfig;
47 /* === M A C R O S ========================================================= */
49 /* === G L O B A L S ======================================================= */
51 /* === P R O C E D U R E S ================================================= */
53 /* === M E T H O D S ======================================================= */
55 synfig::ValueNode_Repeat_Gradient::ValueNode_Repeat_Gradient(const Gradient& x):LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
57 set_link("gradient",ValueNode_Const::create(x));
58 set_link("count",count_=ValueNode_Const::create(int(3)));
59 set_link("width",ValueNode_Const::create(0.5));
60 set_link("specify_start",ValueNode_Const::create(true));
61 set_link("specify_end",ValueNode_Const::create(true));
62 set_link("start_color",ValueNode_Const::create(Color::alpha()));
63 set_link("end_color",ValueNode_Const::create(Color::alpha()));
67 ValueNode_Repeat_Gradient::create_new()const
69 return new ValueNode_Repeat_Gradient(Gradient());
72 ValueNode_Repeat_Gradient*
73 ValueNode_Repeat_Gradient::create(const ValueBase& x)
75 ValueBase::Type id(x.get_type());
77 if(id!=ValueBase::TYPE_GRADIENT)
80 throw runtime_error("synfig::ValueNode_Repeat_Gradient:Bad type "+ValueBase::type_name(id));
83 ValueNode_Repeat_Gradient* value_node=new ValueNode_Repeat_Gradient(x.get(Gradient()));
85 assert(value_node->get_type()==id);
90 synfig::ValueNode_Repeat_Gradient::~ValueNode_Repeat_Gradient()
96 synfig::ValueNode_Repeat_Gradient::set_gradient(ValueNode::Handle a)
98 if(a->get_type()!=ValueBase::TYPE_GRADIENT)
107 synfig::ValueNode_Repeat_Gradient::set_width(ValueNode::Handle x)
109 if(x->get_type()!=ValueBase::TYPE_REAL)
118 synfig::ValueNode_Repeat_Gradient::set_count(ValueNode::Handle b)
120 if(b->get_type()!=ValueBase::TYPE_INTEGER)
127 synfig::ValueNode_Repeat_Gradient::set_specify_start(ValueNode::Handle a)
129 if(a->get_type()!=ValueBase::TYPE_BOOL)
136 synfig::ValueNode_Repeat_Gradient::set_specify_end(ValueNode::Handle a)
138 if(a->get_type()!=ValueBase::TYPE_BOOL)
145 synfig::ValueNode_Repeat_Gradient::set_start_color(ValueNode::Handle a)
147 if(a->get_type()!=ValueBase::TYPE_COLOR)
154 synfig::ValueNode_Repeat_Gradient::set_end_color(ValueNode::Handle a)
156 if(a->get_type()!=ValueBase::TYPE_COLOR)
163 synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
165 const int count((*count_)(t).get(int()));
172 const Gradient gradient((*gradient_)(t).get(Gradient()));
173 const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
174 const bool specify_start((*specify_start_)(t).get(bool()));
175 const bool specify_end((*specify_end_)(t).get(bool()));
177 const float gradient_width_a(width/count);
178 const float gradient_width_b((1.0-width)/count);
180 Gradient::const_iterator iter;
181 Gradient::const_reverse_iterator riter;
183 ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
186 float pos(float(i)/count);
188 for(iter=gradient.begin();iter!=gradient.end();iter++)
189 ret.push_back(Gradient::CPoint(pos+gradient_width_a*iter->pos,iter->color));
190 pos+=gradient_width_a;
192 for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
193 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
196 ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
201 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle x)
203 assert(i>=0 && i<link_count());
207 if(set_gradient(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
208 else { return false; }
210 if(set_count(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
211 else { return false; }
213 if(set_width(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
214 else { return false; }
216 if(set_specify_start(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
217 else { return false; }
219 if(set_specify_end(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
220 else { return false; }
222 if(set_start_color(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
223 else { return false; }
225 if(set_end_color(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
226 else { return false; }
232 ValueNode::LooseHandle
233 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
235 assert(i>=0 && i<link_count());
238 case 0: return gradient_;
239 case 1: return count_;
240 case 2: return width_;
241 case 3: return specify_start_;
242 case 4: return specify_end_;
243 case 5: return start_color_;
244 case 6: return end_color_;
250 ValueNode_Repeat_Gradient::link_count()const
256 ValueNode_Repeat_Gradient::link_local_name(int i)const
258 assert(i>=0 && i<link_count());
261 case 0: return _("Gradient");
262 case 1: return _("Count");
263 case 2: return _("Width");
264 case 3: return _("Specify Start");
265 case 4: return _("Specify End");
266 case 5: return _("Start Color");
267 case 6: return _("End Color");
268 default: return String();
273 ValueNode_Repeat_Gradient::link_name(int i)const
275 assert(i>=0 && i<link_count());
278 case 0: return "gradient";
279 case 1: return "count";
280 case 2: return "width";
281 case 3: return "specify_start";
282 case 4: return "specify_end";
283 case 5: return "start_color";
284 case 6: return "end_color";
285 default: return String();
290 ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
292 if(name=="gradient") return 0;
293 if(name=="count") return 1;
294 if(name=="width") return 2;
295 if(name=="specify_start") return 3;
296 if(name=="specify_end") return 4;
297 if(name=="start_color") return 5;
298 if(name=="end_color") return 6;
299 throw Exception::BadLinkName(name);
303 ValueNode_Repeat_Gradient::get_name()const
305 return "repeat_gradient";
309 ValueNode_Repeat_Gradient::get_local_name()const
311 return _("Repeat Gradient");
315 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
317 return type==ValueBase::TYPE_GRADIENT;