Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.08 / src / modules / mod_noise / valuenode_random.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_random.cpp
3 **      \brief Template File
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 "valuenode_random.h"
34 #include "synfig/valuenode_const.h"
35 #include "synfig/general.h"
36 #include "synfig/color.h"
37 #include <ETL/misc>
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 ValueNode_Random::ValueNode_Random(const ValueBase &value):
56         LinkableValueNode(value.get_type())
57 {
58         random.set_seed(time(NULL));
59
60         set_link("radius",ValueNode_Const::create(Real(1)));
61         set_link("seed",ValueNode_Const::create(random.get_seed()));
62         set_link("speed",ValueNode_Const::create(Real(1)));
63         set_link("smooth",ValueNode_Const::create(int(Random::SMOOTH_CUBIC)));
64
65         switch(get_type())
66         {
67         case ValueBase::TYPE_ANGLE:
68                 set_link("link",ValueNode_Const::create(value.get(Angle())));
69                 break;
70         case ValueBase::TYPE_BOOL:
71                 set_link("link",ValueNode_Const::create(value.get(bool())));
72                 break;
73         case ValueBase::TYPE_COLOR:
74                 set_link("link",ValueNode_Const::create(value.get(Color())));
75                 break;
76         case ValueBase::TYPE_INTEGER:
77                 set_link("link",ValueNode_Const::create(value.get(int())));
78                 break;
79         case ValueBase::TYPE_REAL:
80                 set_link("link",ValueNode_Const::create(value.get(Real())));
81                 break;
82         case ValueBase::TYPE_TIME:
83                 set_link("link",ValueNode_Const::create(value.get(Time())));
84                 break;
85         case ValueBase::TYPE_VECTOR:
86                 set_link("link",ValueNode_Const::create(value.get(Vector())));
87                 break;
88         default:
89                 throw Exception::BadType(ValueBase::type_local_name(get_type()));
90         }
91
92         DCAST_HACK_ENABLE();
93 }
94
95 LinkableValueNode*
96 ValueNode_Random::create_new()const
97 {
98         return new ValueNode_Random(get_type());
99 }
100
101 ValueNode_Random*
102 ValueNode_Random::create(const ValueBase &x)
103 {
104         return new ValueNode_Random(x);
105 }
106
107 ValueNode_Random::~ValueNode_Random()
108 {
109         unlink_all();
110 }
111
112 ValueBase
113 ValueNode_Random::operator()(Time t)const
114 {
115         typedef const Random::SmoothType Smooth;
116
117         Real    radius  = (*radius_)(t).get(Real());
118         int             seed    = (*seed_)(t).get(int());
119         int             smooth  = (*smooth_)(t).get(int());
120         float   speed   = (*speed_ )(t).get(Real()) * t;
121
122         random.set_seed(seed);
123
124         switch(get_type())
125         {
126         case ValueBase::TYPE_ANGLE:
127                 return ((*link_)(t).get( Angle()) +
128                                 Angle::deg(random(Smooth(smooth), 0, 0, 0, speed) * radius));
129
130         case ValueBase::TYPE_BOOL:
131                 return round_to_int((*link_)(t).get(  bool()) +
132                                                         random(Smooth(smooth), 0, 0, 0, speed) * radius) > 0;
133
134         case ValueBase::TYPE_COLOR:
135                 return (((*link_)(t).get( Color()) +
136                                  Color(random(Smooth(smooth), 0, 0, 0, speed),
137                                            random(Smooth(smooth), 1, 0, 0, speed),
138                                            random(Smooth(smooth), 2, 0, 0, speed), 0) * radius).clamped());
139
140         case ValueBase::TYPE_INTEGER:
141                 return round_to_int((*link_)(t).get(   int()) +
142                                                         random(Smooth(smooth), 0, 0, 0, speed) * radius);
143
144         case ValueBase::TYPE_REAL:
145                 return ((*link_)(t).get(  Real()) +
146                                 random(Smooth(smooth), 0, 0, 0, speed) * radius);
147
148         case ValueBase::TYPE_TIME:
149                 return ((*link_)(t).get(  Time()) +
150                                 random(Smooth(smooth), 0, 0, 0, speed) * radius);
151
152         case ValueBase::TYPE_VECTOR:
153         {
154                 float length(random(Smooth(smooth), 0, 0, 0, speed) * radius);
155                 Angle::rad angle(random(Smooth(smooth), 1, 0, 0, speed) * PI);
156
157                 return ((*link_)(t).get(Vector()) +
158                                 Vector(Angle::cos(angle).get(), Angle::sin(angle).get()) * length);
159         }
160
161         default:
162                 assert(0);
163                 break;
164         }
165
166         return ValueBase();
167 }
168
169
170 String
171 ValueNode_Random::get_name()const
172 {
173         return "random";
174 }
175
176 String
177 ValueNode_Random::get_local_name()const
178 {
179         return _("Random");
180 }
181
182 bool
183 ValueNode_Random::set_link_vfunc(int i,ValueNode::Handle value)
184 {
185         assert(i>=0 && i<link_count());
186
187         switch(i)
188         {
189         case 0: CHECK_TYPE_AND_SET_VALUE(link_,   get_type());
190         case 1: CHECK_TYPE_AND_SET_VALUE(radius_, ValueBase::TYPE_REAL);
191         case 2: CHECK_TYPE_AND_SET_VALUE(seed_,   ValueBase::TYPE_INTEGER);
192         case 3: CHECK_TYPE_AND_SET_VALUE(speed_,  ValueBase::TYPE_REAL);
193         case 4: CHECK_TYPE_AND_SET_VALUE(smooth_, ValueBase::TYPE_INTEGER);
194         }
195         return false;
196 }
197
198 ValueNode::LooseHandle
199 ValueNode_Random::get_link_vfunc(int i)const
200 {
201         assert(i>=0 && i<link_count());
202
203         switch(i)
204         {
205         case 0: return link_;
206         case 1: return radius_;
207         case 2: return seed_;
208         case 3: return speed_;
209         case 4: return smooth_;
210         }
211         return 0;
212 }
213
214 int
215 ValueNode_Random::link_count()const
216 {
217         return 5;
218 }
219
220 String
221 ValueNode_Random::link_name(int i)const
222 {
223         assert(i>=0 && i<link_count());
224
225         switch(i)
226         {
227         case 0: return "link";
228         case 1: return "radius";
229         case 2: return "seed";
230         case 3: return "speed";
231         case 4: return "smooth";
232         }
233         return String();
234 }
235
236 String
237 ValueNode_Random::link_local_name(int i)const
238 {
239         assert(i>=0 && i<link_count());
240
241         switch(i)
242         {
243         case 0: return _("Link");
244         case 1: return _("Radius");
245         case 2: return _("Seed");
246         case 3: return _("Animation Speed");
247         case 4: return _("Interpolation");
248         }
249         return String();
250 }
251
252 int
253 ValueNode_Random::get_link_index_from_name(const String &name)const
254 {
255         if(name=="link"  ) return 0;
256         if(name=="radius") return 1;
257         if(name=="seed"  ) return 2;
258         if(name=="speed" ) return 3;
259         if(name=="smooth") return 4;
260         throw Exception::BadLinkName(name);
261 }
262
263 bool
264 ValueNode_Random::check_type(ValueBase::Type type)
265 {
266         return
267                 type==ValueBase::TYPE_ANGLE             ||
268                 type==ValueBase::TYPE_BOOL              ||
269                 type==ValueBase::TYPE_COLOR             ||
270                 type==ValueBase::TYPE_INTEGER   ||
271                 type==ValueBase::TYPE_REAL              ||
272                 type==ValueBase::TYPE_TIME              ||
273                 type==ValueBase::TYPE_VECTOR    ;
274 }
275
276 ValueNode*
277 ValueNode_Random::clone(const GUID& deriv_guid)const
278 {
279         ValueNode_Random* ret = (ValueNode_Random*)LinkableValueNode::clone(deriv_guid);
280         ret->randomize_seed();
281         return ret;
282 }
283
284 void
285 ValueNode_Random::randomize_seed()
286 {
287         int i = get_link_index_from_name("seed");
288         ValueNode::Handle link = get_link_vfunc(i);
289         if(!link->is_exported() && link->get_name() == "constant")
290         {
291                 int seed = time(NULL) + rand();
292                 if (seed < 0) seed = -seed;
293                 random.set_seed(seed);
294                 set_link(i, ValueNode_Const::create(seed));
295         }
296 }