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, 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 const int count((*count_)(t).get(int()));
106 const Gradient gradient((*gradient_)(t).get(Gradient()));
107 const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
108 const bool specify_start((*specify_start_)(t).get(bool()));
109 const bool specify_end((*specify_end_)(t).get(bool()));
111 const float gradient_width_a(width/count);
112 const float gradient_width_b((1.0-width)/count);
114 Gradient::const_iterator iter;
115 Gradient::const_reverse_iterator riter;
117 ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
120 float pos(float(i)/count);
122 for(iter=gradient.begin();iter!=gradient.end();iter++)
123 ret.push_back(Gradient::CPoint(pos+gradient_width_a*iter->pos,iter->color));
124 pos+=gradient_width_a;
126 for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
127 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
130 ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
135 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle value)
137 assert(i>=0 && i<link_count());
141 case 0: CHECK_TYPE_AND_SET_VALUE(gradient_, get_type());
142 case 1: CHECK_TYPE_AND_SET_VALUE(count_, ValueBase::TYPE_INTEGER);
143 case 2: CHECK_TYPE_AND_SET_VALUE(width_, ValueBase::TYPE_REAL);
144 case 3: CHECK_TYPE_AND_SET_VALUE(specify_start_, ValueBase::TYPE_BOOL);
145 case 4: CHECK_TYPE_AND_SET_VALUE(specify_end_, ValueBase::TYPE_BOOL);
146 case 5: CHECK_TYPE_AND_SET_VALUE(start_color_, ValueBase::TYPE_COLOR);
147 case 6: CHECK_TYPE_AND_SET_VALUE(end_color_, ValueBase::TYPE_COLOR);
152 ValueNode::LooseHandle
153 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
155 assert(i>=0 && i<link_count());
159 case 0: return gradient_;
160 case 1: return count_;
161 case 2: return width_;
162 case 3: return specify_start_;
163 case 4: return specify_end_;
164 case 5: return start_color_;
165 case 6: return end_color_;
171 ValueNode_Repeat_Gradient::link_count()const
177 ValueNode_Repeat_Gradient::link_local_name(int i)const
179 assert(i>=0 && i<link_count());
183 case 0: return _("Gradient");
184 case 1: return _("Count");
185 case 2: return _("Width");
186 case 3: return _("Specify Start");
187 case 4: return _("Specify End");
188 case 5: return _("Start Color");
189 case 6: return _("End Color");
190 default: return String();
195 ValueNode_Repeat_Gradient::link_name(int i)const
197 assert(i>=0 && i<link_count());
201 case 0: return "gradient";
202 case 1: return "count";
203 case 2: return "width";
204 case 3: return "specify_start";
205 case 4: return "specify_end";
206 case 5: return "start_color";
207 case 6: return "end_color";
208 default: return String();
213 ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
215 if(name=="gradient") return 0;
216 if(name=="count") return 1;
217 if(name=="width") return 2;
218 if(name=="specify_start") return 3;
219 if(name=="specify_end") return 4;
220 if(name=="start_color") return 5;
221 if(name=="end_color") return 6;
222 throw Exception::BadLinkName(name);
226 ValueNode_Repeat_Gradient::get_name()const
228 return "repeat_gradient";
232 ValueNode_Repeat_Gradient::get_local_name()const
234 return _("Repeat Gradient");
238 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
240 return type==ValueBase::TYPE_GRADIENT;