Use translated versions of the type names everywhere other than in the .sif(z) files.
[synfig.git] / synfig-core / trunk / 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 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "general.h"
33 #include "valuenode_stripes.h"
34 #include "valuenode_const.h"
35 #include <stdexcept>
36 #include "color.h"
37 #include "gradient.h"
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
55 synfig::ValueNode_Stripes::ValueNode_Stripes():LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
56 {
57         set_link("color1",ValueNode_Const::create(Color::alpha()));
58         set_link("color2",ValueNode_Const::create(Color::black()));
59         set_link("stripes",stripes_=ValueNode_Const::create(int(5)));
60         set_link("width",ValueNode_Const::create(0.5));
61 }
62
63 LinkableValueNode*
64 ValueNode_Stripes::create_new()const
65 {
66         return new ValueNode_Stripes();
67 }
68
69 ValueNode_Stripes*
70 ValueNode_Stripes::create(const ValueBase& x)
71 {
72         ValueBase::Type id(x.get_type());
73
74         if(id!=ValueBase::TYPE_GRADIENT)
75         {
76                 assert(0);
77                 throw runtime_error(_("synfig::ValueNode_Stripes:Bad type ")+ValueBase::type_local_name(id));
78         }
79
80         ValueNode_Stripes* value_node=new ValueNode_Stripes();
81
82         assert(value_node->get_type()==id);
83
84         return value_node;
85 }
86
87 synfig::ValueNode_Stripes::~ValueNode_Stripes()
88 {
89         unlink_all();
90 }
91
92 bool
93 synfig::ValueNode_Stripes::set_color1(ValueNode::Handle a)
94 {
95         if(a->get_type()!=ValueBase::TYPE_COLOR)
96                 return false;
97
98         color1_=a;
99
100         return true;
101 }
102
103 bool
104 synfig::ValueNode_Stripes::set_color2(ValueNode::Handle a)
105 {
106         if(a->get_type()!=ValueBase::TYPE_COLOR)
107                 return false;
108
109         color2_=a;
110
111         return true;
112 }
113
114 bool
115 synfig::ValueNode_Stripes::set_width(ValueNode::Handle x)
116 {
117         if(x->get_type()!=ValueBase::TYPE_REAL)
118                 return false;
119
120         width_=x;
121
122         return true;
123 }
124
125 bool
126 synfig::ValueNode_Stripes::set_stripes(ValueNode::Handle b)
127 {
128         if(b->get_type()!=ValueBase::TYPE_INTEGER)
129                 return false;
130         stripes_=b;
131         return true;
132 }
133
134 synfig::ValueBase
135 synfig::ValueNode_Stripes::operator()(Time t)const
136 {
137         const int total((*stripes_)(t).get(int()));
138         int i;
139         Gradient ret;
140
141         if(total<=0)
142                 return ret;
143
144         const Color color1((*color1_)(t).get(Color()));
145         const Color color2((*color2_)(t).get(Color()));
146         const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
147
148         const float stripe_width_a(width/total);
149         const float stripe_width_b((1.0-width)/total);
150
151         for(i=0;i<total;i++)
152         {
153                 float pos(float(i)/total+stripe_width_b/2);
154                 ret.push_back(Gradient::CPoint(pos,color1));
155                 ret.push_back(Gradient::CPoint(pos,color2));
156                 pos+=stripe_width_a;
157                 ret.push_back(Gradient::CPoint(pos,color2));
158                 ret.push_back(Gradient::CPoint(pos,color1));
159         }
160         return ret;
161 }
162
163 bool
164 ValueNode_Stripes::set_link_vfunc(int i,ValueNode::Handle x)
165 {
166         assert(i>=0 && i<link_count());
167
168         switch(i)
169         {
170                 case 0:
171                         if(set_color1(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
172                         else { return false; }
173                 case 1:
174                         if(set_color2(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
175                         else { return false; }
176                 case 2:
177                         if(set_stripes(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
178                         else { return false; }
179                 case 3:
180                         if(set_width(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
181                         else { return false; }
182         }
183
184         return false;
185 }
186
187 ValueNode::LooseHandle
188 ValueNode_Stripes::get_link_vfunc(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         }
203         return 0;
204 }
205
206 int
207 ValueNode_Stripes::link_count()const
208 {
209         return 4;
210 }
211
212 String
213 ValueNode_Stripes::link_local_name(int i)const
214 {
215         assert(i>=0 && i<link_count());
216
217         switch(i)
218         {
219                 case 0:
220                         return _("Color 1");
221                 case 1:
222                         return _("Color 2");
223                 case 2:
224                         return _("Stripe Count");
225                 case 3:
226                         return _("Width");
227                 default:
228                         return String();
229         }
230 }
231
232 String
233 ValueNode_Stripes::link_name(int i)const
234 {
235         assert(i>=0 && i<link_count());
236
237         switch(i)
238         {
239                 case 0:
240                         return "color1";
241                 case 1:
242                         return "color2";
243                 case 2:
244                         return "stripes";
245                 case 3:
246                         return "width";
247                 default:
248                         return String();
249         }
250 }
251
252 int
253 ValueNode_Stripes::get_link_index_from_name(const String &name)const
254 {
255         if(name=="color1")
256                 return 0;
257         if(name=="color2")
258                 return 1;
259         if(name=="stripes")
260                 return 2;
261         if(name=="width")
262                 return 3;
263         throw Exception::BadLinkName(name);
264 }
265
266 String
267 ValueNode_Stripes::get_name()const
268 {
269         return "stripes";
270 }
271
272 String
273 ValueNode_Stripes::get_local_name()const
274 {
275         return _("Stripes");
276 }
277
278 bool
279 ValueNode_Stripes::check_type(ValueBase::Type type)
280 {
281         return type==ValueBase::TYPE_GRADIENT;
282 }