Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_stripes.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_stripes.cpp
3 **      \brief Implementation of the "Stripes" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 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_stripes.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_Stripes::ValueNode_Stripes():LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
57 {
58         Vocab ret(get_children_vocab());
59         set_children_vocab(ret);
60         set_link("color1",ValueNode_Const::create(Color::alpha()));
61         set_link("color2",ValueNode_Const::create(Color::black()));
62         set_link("stripes",stripes_=ValueNode_Const::create(int(5)));
63         set_link("width",ValueNode_Const::create(0.5));
64 }
65
66 LinkableValueNode*
67 ValueNode_Stripes::create_new()const
68 {
69         return new ValueNode_Stripes();
70 }
71
72 ValueNode_Stripes*
73 ValueNode_Stripes::create(const ValueBase& x)
74 {
75         ValueBase::Type id(x.get_type());
76
77         if(id!=ValueBase::TYPE_GRADIENT)
78         {
79                 assert(0);
80                 throw runtime_error(String(_("Stripes"))+_(":Bad type ")+ValueBase::type_local_name(id));
81         }
82
83         ValueNode_Stripes* value_node=new ValueNode_Stripes();
84
85         assert(value_node->get_type()==id);
86
87         return value_node;
88 }
89
90 synfig::ValueNode_Stripes::~ValueNode_Stripes()
91 {
92         unlink_all();
93 }
94
95 synfig::ValueBase
96 synfig::ValueNode_Stripes::operator()(Time t)const
97 {
98         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
99                 printf("%s:%d operator()\n", __FILE__, __LINE__);
100
101         const int total((*stripes_)(t).get(int()));
102         int i;
103         Gradient ret;
104
105         if(total<=0)
106                 return ret;
107
108         const Color color1((*color1_)(t).get(Color()));
109         const Color color2((*color2_)(t).get(Color()));
110         const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
111
112         const float stripe_width_a(width/total);
113         const float stripe_width_b((1.0-width)/total);
114
115         for(i=0;i<total;i++)
116         {
117                 float pos(float(i)/total+stripe_width_b/2);
118                 ret.push_back(Gradient::CPoint(pos,color1));
119                 ret.push_back(Gradient::CPoint(pos,color2));
120                 pos+=stripe_width_a;
121                 ret.push_back(Gradient::CPoint(pos,color2));
122                 ret.push_back(Gradient::CPoint(pos,color1));
123         }
124         return ret;
125 }
126
127 bool
128 ValueNode_Stripes::set_link_vfunc(int i,ValueNode::Handle value)
129 {
130         assert(i>=0 && i<link_count());
131
132         switch(i)
133         {
134         case 0: CHECK_TYPE_AND_SET_VALUE(color1_,  ValueBase::TYPE_COLOR);
135         case 1: CHECK_TYPE_AND_SET_VALUE(color2_,  ValueBase::TYPE_COLOR);
136         case 2: CHECK_TYPE_AND_SET_VALUE(stripes_, ValueBase::TYPE_INTEGER);
137         case 3: CHECK_TYPE_AND_SET_VALUE(width_,   ValueBase::TYPE_REAL);
138         }
139         return false;
140 }
141
142 ValueNode::LooseHandle
143 ValueNode_Stripes::get_link_vfunc(int i)const
144 {
145         assert(i>=0 && i<link_count());
146
147         switch(i)
148         {
149                 case 0:
150                         return color1_;
151                 case 1:
152                         return color2_;
153                 case 2:
154                         return stripes_;
155                 case 3:
156                         return width_;
157         }
158         return 0;
159 }
160
161 int
162 ValueNode_Stripes::link_count()const
163 {
164         return 4;
165 }
166
167 String
168 ValueNode_Stripes::link_local_name(int i)const
169 {
170         assert(i>=0 && i<link_count());
171
172         switch(i)
173         {
174                 case 0:
175                         return _("Color 1");
176                 case 1:
177                         return _("Color 2");
178                 case 2:
179                         return _("Stripe Count");
180                 case 3:
181                         return _("Width");
182                 default:
183                         return String();
184         }
185 }
186
187 String
188 ValueNode_Stripes::link_name(int i)const
189 {
190         assert(i>=0 && i<link_count());
191
192         switch(i)
193         {
194                 case 0:
195                         return "color1";
196                 case 1:
197                         return "color2";
198                 case 2:
199                         return "stripes";
200                 case 3:
201                         return "width";
202                 default:
203                         return String();
204         }
205 }
206
207 int
208 ValueNode_Stripes::get_link_index_from_name(const String &name)const
209 {
210         if(name=="color1")
211                 return 0;
212         if(name=="color2")
213                 return 1;
214         if(name=="stripes")
215                 return 2;
216         if(name=="width")
217                 return 3;
218         throw Exception::BadLinkName(name);
219 }
220
221 String
222 ValueNode_Stripes::get_name()const
223 {
224         return "stripes";
225 }
226
227 String
228 ValueNode_Stripes::get_local_name()const
229 {
230         return _("Stripes");
231 }
232
233 bool
234 ValueNode_Stripes::check_type(ValueBase::Type type)
235 {
236         return type==ValueBase::TYPE_GRADIENT;
237 }
238
239 LinkableValueNode::Vocab
240 ValueNode_Stripes::get_children_vocab_vfunc()const
241 {
242         if(children_vocab.size())
243                 return children_vocab;
244
245         LinkableValueNode::Vocab ret;
246
247         ret.push_back(ParamDesc(ValueBase(),"color1")
248                 .set_local_name(_("Color 1"))
249                 .set_description(_("One color of the gradient stripes"))
250         );
251
252         ret.push_back(ParamDesc(ValueBase(),"color2")
253                 .set_local_name(_("Color 2"))
254                 .set_description(_("Other color of the gradient stripes"))
255         );
256
257                 ret.push_back(ParamDesc(ValueBase(),"stripes")
258                 .set_local_name(_("Stripe Count"))
259                 .set_description(_("Number of stripes in the gradient"))
260         );
261
262                 ret.push_back(ParamDesc(ValueBase(),"width")
263                 .set_local_name(_("Width"))
264                 .set_description(_("Width of stripes in the gradient between [0,1]"))
265         );
266
267         return ret;
268 }