Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc3 / src / synfig / valuenode_stripes.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_stripes.cpp
3 **      \brief Template File
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_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         switch(i)
168         {
169                 case 0:
170                         if(set_color1(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
171                         else { return false; }
172                 case 1:
173                         if(set_color2(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
174                         else { return false; }
175                 case 2:
176                         if(set_stripes(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
177                         else { return false; }
178                 case 3:
179                         if(set_width(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
180                         else { return false; }
181         }
182
183         return false;
184 }
185
186 ValueNode::LooseHandle
187 ValueNode_Stripes::get_link_vfunc(int i)const
188 {
189         assert(i>=0 && i<link_count());
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         }
201         return 0;
202 }
203
204 int
205 ValueNode_Stripes::link_count()const
206 {
207         return 4;
208 }
209
210 String
211 ValueNode_Stripes::link_local_name(int i)const
212 {
213         assert(i>=0 && i<link_count());
214         switch(i)
215         {
216                 case 0:
217                         return _("Color 1");
218                 case 1:
219                         return _("Color 2");
220                 case 2:
221                         return _("Stripe Count");
222                 case 3:
223                         return _("Width");
224                 default:
225                         return String();
226         }
227 }
228
229 String
230 ValueNode_Stripes::link_name(int i)const
231 {
232         assert(i>=0 && i<link_count());
233         switch(i)
234         {
235                 case 0:
236                         return "color1";
237                 case 1:
238                         return "color2";
239                 case 2:
240                         return "stripes";
241                 case 3:
242                         return "width";
243                 default:
244                         return String();
245         }
246 }
247
248 int
249 ValueNode_Stripes::get_link_index_from_name(const String &name)const
250 {
251         if(name=="color1")
252                 return 0;
253         if(name=="color2")
254                 return 1;
255         if(name=="stripes")
256                 return 2;
257         if(name=="width")
258                 return 3;
259         throw Exception::BadLinkName(name);
260 }
261
262 String
263 ValueNode_Stripes::get_name()const
264 {
265         return "stripes";
266 }
267
268 String
269 ValueNode_Stripes::get_local_name()const
270 {
271         return _("Stripes");
272 }
273
274 bool
275 ValueNode_Stripes::check_type(ValueBase::Type type)
276 {
277         return type==ValueBase::TYPE_GRADIENT;
278 }