1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_repeat_gradient.cpp
3 ** \brief Implementation of the "Repeat Gradient" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 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(String(_("Repeat Gradient"))+_(":Bad type ")+ValueBase::type_local_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::operator()(Time t)const
99 if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
100 printf("%s:%d operator()\n", __FILE__, __LINE__);
102 const int count((*count_)(t).get(int()));
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()));
114 const float gradient_width_a(width/count);
115 const float gradient_width_b((1.0-width)/count);
117 Gradient::const_iterator iter;
118 Gradient::const_reverse_iterator riter;
120 ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
123 float pos(float(i)/count);
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;
129 for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
130 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
133 ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
138 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle value)
140 assert(i>=0 && i<link_count());
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);
155 ValueNode::LooseHandle
156 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
158 assert(i>=0 && i<link_count());
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_;
174 ValueNode_Repeat_Gradient::link_count()const
180 ValueNode_Repeat_Gradient::link_local_name(int i)const
182 assert(i>=0 && i<link_count());
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();
198 ValueNode_Repeat_Gradient::link_name(int i)const
200 assert(i>=0 && i<link_count());
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();
216 ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
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);
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;