Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / src / synfig / valuenode_repeat_gradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_repeat_gradient.cpp
3 **      \brief Implementation of the "Repeat Gradient" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "general.h"
34 #include "valuenode_repeat_gradient.h"
35 #include "valuenode_const.h"
36 #include <stdexcept>
37 #include "color.h"
38 #include "gradient.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 synfig::ValueNode_Repeat_Gradient::ValueNode_Repeat_Gradient(const Gradient& x):LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
57 {
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()));
67 }
68
69 LinkableValueNode*
70 ValueNode_Repeat_Gradient::create_new()const
71 {
72         return new ValueNode_Repeat_Gradient(Gradient());
73 }
74
75 ValueNode_Repeat_Gradient*
76 ValueNode_Repeat_Gradient::create(const ValueBase& x)
77 {
78         ValueBase::Type id(x.get_type());
79
80         if(id!=ValueBase::TYPE_GRADIENT)
81         {
82                 assert(0);
83                 throw runtime_error(String(_("Repeat Gradient"))+_(":Bad type ")+ValueBase::type_local_name(id));
84         }
85
86         ValueNode_Repeat_Gradient* value_node=new ValueNode_Repeat_Gradient(x.get(Gradient()));
87
88         assert(value_node->get_type()==id);
89
90         return value_node;
91 }
92
93 synfig::ValueNode_Repeat_Gradient::~ValueNode_Repeat_Gradient()
94 {
95         unlink_all();
96 }
97
98 synfig::ValueBase
99 synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
100 {
101         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
102                 printf("%s:%d operator()\n", __FILE__, __LINE__);
103
104         const int count((*count_)(t).get(int()));
105         int i;
106         Gradient ret;
107
108         if(count<=0)
109                 return ret;
110
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()));
115
116         const float gradient_width_a(width/count);
117         const float gradient_width_b((1.0-width)/count);
118
119         Gradient::const_iterator iter;
120         Gradient::const_reverse_iterator riter;
121         if (specify_start)
122                 ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
123         for(i=0;i<count;i++)
124         {
125                 float pos(float(i)/count);
126                 if (width != 0.0)
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;
130                 if (width != 1.0)
131                         for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
132                                 ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
133         }
134         if (specify_end)
135                 ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
136         return ret;
137 }
138
139 bool
140 ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle value)
141 {
142         assert(i>=0 && i<link_count());
143
144         switch(i)
145         {
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);
153         }
154         return false;
155 }
156
157 ValueNode::LooseHandle
158 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
159 {
160         assert(i>=0 && i<link_count());
161
162         switch(i)
163         {
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_;
171             default: return 0;
172         }
173 }
174
175 String
176 ValueNode_Repeat_Gradient::get_name()const
177 {
178         return "repeat_gradient";
179 }
180
181 String
182 ValueNode_Repeat_Gradient::get_local_name()const
183 {
184         return _("Repeat Gradient");
185 }
186
187 bool
188 ValueNode_Repeat_Gradient::check_type(ValueBase::Type type)
189 {
190         return type==ValueBase::TYPE_GRADIENT;
191 }
192
193 LinkableValueNode::Vocab
194 ValueNode_Repeat_Gradient::get_children_vocab_vfunc()const
195 {
196         if(children_vocab.size())
197                 return children_vocab;
198
199         LinkableValueNode::Vocab ret;
200
201         ret.push_back(ParamDesc(ValueBase(),"gradient")
202                 .set_local_name(_("Gradient"))
203                 .set_description(_("The source gradient to repeat"))
204         );
205
206         ret.push_back(ParamDesc(ValueBase(),"count")
207                 .set_local_name(_("Count"))
208                 .set_description(_("The number of repetition of the gradient"))
209         );
210
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]"))
214         );
215
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"))
219         );
220
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"))
224         );
225
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"))
229         );
230
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"))
234         );
235
236         return ret;
237 }