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