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"
40 /* === U S I N G =========================================================== */
44 using namespace synfig;
46 /* === M A C R O S ========================================================= */
48 /* === G L O B A L S ======================================================= */
50 /* === P R O C E D U R E S ================================================= */
52 /* === M E T H O D S ======================================================= */
54 synfig::ValueNode_Repeat_Gradient::ValueNode_Repeat_Gradient():LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
56 set_link("gradient",ValueNode_Const::create(Gradient()));
57 set_link("count",count_=ValueNode_Const::create(int(3)));
58 set_link("width",ValueNode_Const::create(0.5));
62 ValueNode_Repeat_Gradient::create_new()const
64 return new ValueNode_Repeat_Gradient();
67 ValueNode_Repeat_Gradient*
68 ValueNode_Repeat_Gradient::create(const ValueBase& x)
70 ValueBase::Type id(x.get_type());
72 if(id!=ValueBase::TYPE_GRADIENT)
75 throw runtime_error("synfig::ValueNode_Repeat_Gradient:Bad type "+ValueBase::type_name(id));
78 ValueNode_Repeat_Gradient* value_node=new ValueNode_Repeat_Gradient();
79 value_node->set_gradient(ValueNode_Const::create(x.get(Gradient())));
81 assert(value_node->get_type()==id);
86 synfig::ValueNode_Repeat_Gradient::~ValueNode_Repeat_Gradient()
92 synfig::ValueNode_Repeat_Gradient::set_gradient(ValueNode::Handle a)
94 if(a->get_type()!=ValueBase::TYPE_GRADIENT)
103 synfig::ValueNode_Repeat_Gradient::set_width(ValueNode::Handle x)
105 if(x->get_type()!=ValueBase::TYPE_REAL)
114 synfig::ValueNode_Repeat_Gradient::set_count(ValueNode::Handle b)
116 if(b->get_type()!=ValueBase::TYPE_INTEGER)
123 synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
125 const int count((*count_)(t).get(int()));
132 const Gradient gradient((*gradient_)(t).get(Gradient()));
133 const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
135 const float gradient_width_a(width/count);
136 const float gradient_width_b((1.0-width)/count);
138 Gradient::const_iterator iter;
139 Gradient::const_reverse_iterator riter;
142 float pos(float(i)/count);
144 for(iter=gradient.begin();iter!=gradient.end();iter++)
145 ret.push_back(Gradient::CPoint(pos+gradient_width_a*iter->pos,iter->color));
146 pos+=gradient_width_a;
148 for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
149 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
155 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle x)
157 assert(i>=0 && i<link_count());
161 if(set_gradient(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
162 else { return false; }
164 if(set_count(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
165 else { return false; }
167 if(set_width(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
168 else { return false; }
174 ValueNode::LooseHandle
175 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
177 assert(i>=0 && i<link_count());
180 case 0: return gradient_;
181 case 1: return count_;
182 case 2: return width_;
188 ValueNode_Repeat_Gradient::link_count()const
194 ValueNode_Repeat_Gradient::link_local_name(int i)const
196 assert(i>=0 && i<link_count());
199 case 0: return _("Gradient");
200 case 1: return _("Count");
201 case 2: return _("Width");
202 default: return String();
207 ValueNode_Repeat_Gradient::link_name(int i)const
209 assert(i>=0 && i<link_count());
212 case 0: return "gradient";
213 case 1: return "count";
214 case 2: return "width";
215 default: return String();
220 ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
222 if(name=="gradient") return 0;
223 if(name=="count") return 1;
224 if(name=="width") return 2;
225 throw Exception::BadLinkName(name);
229 ValueNode_Repeat_Gradient::get_name()const
231 return "repeat_gradient";
235 ValueNode_Repeat_Gradient::get_local_name()const
237 return _("Repeat Gradient");
241 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
243 return type==ValueBase::TYPE_GRADIENT;