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 Vocab ret(get_children_vocab());
59 set_children_vocab(ret);
60 set_link("gradient",ValueNode_Const::create(x));
61 set_link("count",count_=ValueNode_Const::create(int(3)));
62 set_link("width",ValueNode_Const::create(0.5));
63 set_link("specify_start",ValueNode_Const::create(true));
64 set_link("specify_end",ValueNode_Const::create(true));
65 set_link("start_color",ValueNode_Const::create(Color::alpha()));
66 set_link("end_color",ValueNode_Const::create(Color::alpha()));
70 ValueNode_Repeat_Gradient::create_new()const
72 return new ValueNode_Repeat_Gradient(Gradient());
75 ValueNode_Repeat_Gradient*
76 ValueNode_Repeat_Gradient::create(const ValueBase& x)
78 ValueBase::Type id(x.get_type());
80 if(id!=ValueBase::TYPE_GRADIENT)
83 throw runtime_error(String(_("Repeat Gradient"))+_(":Bad type ")+ValueBase::type_local_name(id));
86 ValueNode_Repeat_Gradient* value_node=new ValueNode_Repeat_Gradient(x.get(Gradient()));
88 assert(value_node->get_type()==id);
93 synfig::ValueNode_Repeat_Gradient::~ValueNode_Repeat_Gradient()
99 synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
101 if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
102 printf("%s:%d operator()\n", __FILE__, __LINE__);
104 const int count((*count_)(t).get(int()));
111 const Gradient gradient((*gradient_)(t).get(Gradient()));
112 const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
113 const bool specify_start((*specify_start_)(t).get(bool()));
114 const bool specify_end((*specify_end_)(t).get(bool()));
116 const float gradient_width_a(width/count);
117 const float gradient_width_b((1.0-width)/count);
119 Gradient::const_iterator iter;
120 Gradient::const_reverse_iterator riter;
122 ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
125 float pos(float(i)/count);
127 for(iter=gradient.begin();iter!=gradient.end();iter++)
128 ret.push_back(Gradient::CPoint(pos+gradient_width_a*iter->pos,iter->color));
129 pos+=gradient_width_a;
131 for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
132 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
135 ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
140 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle value)
142 assert(i>=0 && i<link_count());
146 case 0: CHECK_TYPE_AND_SET_VALUE(gradient_, get_type());
147 case 1: CHECK_TYPE_AND_SET_VALUE(count_, ValueBase::TYPE_INTEGER);
148 case 2: CHECK_TYPE_AND_SET_VALUE(width_, ValueBase::TYPE_REAL);
149 case 3: CHECK_TYPE_AND_SET_VALUE(specify_start_, ValueBase::TYPE_BOOL);
150 case 4: CHECK_TYPE_AND_SET_VALUE(specify_end_, ValueBase::TYPE_BOOL);
151 case 5: CHECK_TYPE_AND_SET_VALUE(start_color_, ValueBase::TYPE_COLOR);
152 case 6: CHECK_TYPE_AND_SET_VALUE(end_color_, ValueBase::TYPE_COLOR);
157 ValueNode::LooseHandle
158 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
160 assert(i>=0 && i<link_count());
164 case 0: return gradient_;
165 case 1: return count_;
166 case 2: return width_;
167 case 3: return specify_start_;
168 case 4: return specify_end_;
169 case 5: return start_color_;
170 case 6: return end_color_;
176 ValueNode_Repeat_Gradient::get_name()const
178 return "repeat_gradient";
182 ValueNode_Repeat_Gradient::get_local_name()const
184 return _("Repeat Gradient");
188 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
190 return type==ValueBase::TYPE_GRADIENT;
193 LinkableValueNode::Vocab
194 ValueNode_Repeat_Gradient::get_children_vocab_vfunc()const
196 if(children_vocab.size())
197 return children_vocab;
199 LinkableValueNode::Vocab ret;
201 ret.push_back(ParamDesc(ValueBase(),"gradient")
202 .set_local_name(_("Gradient"))
203 .set_description(_("The source gradient to repeat"))
206 ret.push_back(ParamDesc(ValueBase(),"count")
207 .set_local_name(_("Count"))
208 .set_description(_("The number of repetition of the gradient"))
211 ret.push_back(ParamDesc(ValueBase(),"width")
212 .set_local_name(_("Width"))
213 .set_description(_("Specifies how much biased is the source gradeint in the repetition [0,1]"))
216 ret.push_back(ParamDesc(ValueBase(),"specify_start")
217 .set_local_name(_("Specify Start"))
218 .set_description(_("When checked, 'Start Color' is used as the start of the resulting gradient"))
221 ret.push_back(ParamDesc(ValueBase(),"specify_end")
222 .set_local_name(_("Specify End"))
223 .set_description(_("When checked, 'End Color' is used as the start of the resulting gradient"))
226 ret.push_back(ParamDesc(ValueBase(),"start_color")
227 .set_local_name(_("Start Color"))
228 .set_description(_("Used as the start of the resulting gradient"))
231 ret.push_back(ParamDesc(ValueBase(),"end_color")
232 .set_local_name(_("End Color"))
233 .set_description(_("Used as the end of the resulting gradient"))