1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_repeat_gradient.cpp
3 ** \brief Implementation of the "Repeat Gradient" valuenode conversion.
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
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
34 #include "valuenode_repeat_gradient.h"
35 #include "valuenode_const.h"
42 /* === U S I N G =========================================================== */
46 using namespace synfig;
48 /* === M A C R O S ========================================================= */
50 /* === G L O B A L S ======================================================= */
52 /* === P R O C E D U R E S ================================================= */
54 /* === M E T H O D S ======================================================= */
56 synfig::ValueNode_Repeat_Gradient::ValueNode_Repeat_Gradient(const Gradient& x):LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
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()));
68 ValueNode_Repeat_Gradient::create_new()const
70 return new ValueNode_Repeat_Gradient(Gradient());
73 ValueNode_Repeat_Gradient*
74 ValueNode_Repeat_Gradient::create(const ValueBase& x)
76 ValueBase::Type id(x.get_type());
78 if(id!=ValueBase::TYPE_GRADIENT)
81 throw runtime_error("synfig::ValueNode_Repeat_Gradient:Bad type "+ValueBase::type_name(id));
84 ValueNode_Repeat_Gradient* value_node=new ValueNode_Repeat_Gradient(x.get(Gradient()));
86 assert(value_node->get_type()==id);
91 synfig::ValueNode_Repeat_Gradient::~ValueNode_Repeat_Gradient()
97 synfig::ValueNode_Repeat_Gradient::set_gradient(ValueNode::Handle a)
99 if(a->get_type()!=ValueBase::TYPE_GRADIENT)
108 synfig::ValueNode_Repeat_Gradient::set_width(ValueNode::Handle x)
110 if(x->get_type()!=ValueBase::TYPE_REAL)
119 synfig::ValueNode_Repeat_Gradient::set_count(ValueNode::Handle b)
121 if(b->get_type()!=ValueBase::TYPE_INTEGER)
128 synfig::ValueNode_Repeat_Gradient::set_specify_start(ValueNode::Handle a)
130 if(a->get_type()!=ValueBase::TYPE_BOOL)
137 synfig::ValueNode_Repeat_Gradient::set_specify_end(ValueNode::Handle a)
139 if(a->get_type()!=ValueBase::TYPE_BOOL)
146 synfig::ValueNode_Repeat_Gradient::set_start_color(ValueNode::Handle a)
148 if(a->get_type()!=ValueBase::TYPE_COLOR)
155 synfig::ValueNode_Repeat_Gradient::set_end_color(ValueNode::Handle a)
157 if(a->get_type()!=ValueBase::TYPE_COLOR)
164 synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
166 const int count((*count_)(t).get(int()));
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()));
178 const float gradient_width_a(width/count);
179 const float gradient_width_b((1.0-width)/count);
181 Gradient::const_iterator iter;
182 Gradient::const_reverse_iterator riter;
184 ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
187 float pos(float(i)/count);
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;
193 for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
194 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
197 ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
202 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle x)
204 assert(i>=0 && i<link_count());
208 if(set_gradient(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
209 else { return false; }
211 if(set_count(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
212 else { return false; }
214 if(set_width(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
215 else { return false; }
217 if(set_specify_start(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
218 else { return false; }
220 if(set_specify_end(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
221 else { return false; }
223 if(set_start_color(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
224 else { return false; }
226 if(set_end_color(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
227 else { return false; }
233 ValueNode::LooseHandle
234 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
236 assert(i>=0 && i<link_count());
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_;
251 ValueNode_Repeat_Gradient::link_count()const
257 ValueNode_Repeat_Gradient::link_local_name(int i)const
259 assert(i>=0 && i<link_count());
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();
274 ValueNode_Repeat_Gradient::link_name(int i)const
276 assert(i>=0 && i<link_count());
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();
291 ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
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);
304 ValueNode_Repeat_Gradient::get_name()const
306 return "repeat_gradient";
310 ValueNode_Repeat_Gradient::get_local_name()const
312 return _("Repeat Gradient");
316 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
318 return type==ValueBase::TYPE_GRADIENT;