70c9ecdf01ff0da1fc0405901429f9f90ff85a89
[synfig.git] / synfig-core / 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(RandomNoise::SMOOTH_CUBIC)));
64         set_link("loop",ValueNode_Const::create(Real(0)));
65
66         switch(get_type())
67         {
68         case ValueBase::TYPE_ANGLE:
69                 set_link("link",ValueNode_Const::create(value.get(Angle())));
70                 break;
71         case ValueBase::TYPE_BOOL:
72                 set_link("link",ValueNode_Const::create(value.get(bool())));
73                 break;
74         case ValueBase::TYPE_COLOR:
75                 set_link("link",ValueNode_Const::create(value.get(Color())));
76                 break;
77         case ValueBase::TYPE_INTEGER:
78                 set_link("link",ValueNode_Const::create(value.get(int())));
79                 break;
80         case ValueBase::TYPE_REAL:
81                 set_link("link",ValueNode_Const::create(value.get(Real())));
82                 break;
83         case ValueBase::TYPE_TIME:
84                 set_link("link",ValueNode_Const::create(value.get(Time())));
85                 break;
86         case ValueBase::TYPE_VECTOR:
87                 set_link("link",ValueNode_Const::create(value.get(Vector())));
88                 break;
89         default:
90                 throw Exception::BadType(ValueBase::type_local_name(get_type()));
91         }
92 }
93
94 LinkableValueNode*
95 ValueNode_Random::create_new()const
96 {
97         return new ValueNode_Random(get_type());
98 }
99
100 ValueNode_Random*
101 ValueNode_Random::create(const ValueBase &x)
102 {
103         return new ValueNode_Random(x);
104 }
105
106 ValueNode_Random::~ValueNode_Random()
107 {
108         unlink_all();
109 }
110
111 ValueBase
112 ValueNode_Random::operator()(Time t)const
113 {
114         typedef const RandomNoise::SmoothType Smooth;
115
116         Real    radius  = (*radius_)(t).get(Real());
117         int             seed    = (*seed_)(t).get(int());
118         int             smooth  = (*smooth_)(t).get(int());
119         float   speed   = (*speed_ )(t).get(Real());
120         int             loop    = int((((*loop_ )(t).get(Real())) * speed) + 0.5);
121         speed *= t;
122
123         random.set_seed(seed);
124
125         switch(get_type())
126         {
127         case ValueBase::TYPE_ANGLE:
128                 return ((*link_)(t).get( Angle()) +
129                                 Angle::deg(random(Smooth(smooth), 0, 0, 0, speed, loop) * radius));
130
131         case ValueBase::TYPE_BOOL:
132                 return round_to_int((*link_)(t).get(  bool()) +
133                                                         random(Smooth(smooth), 0, 0, 0, speed, loop) * radius) > 0;
134
135         case ValueBase::TYPE_COLOR:
136                 return (((*link_)(t).get( Color()) +
137                                  Color(random(Smooth(smooth), 0, 0, 0, speed, loop),
138                                            random(Smooth(smooth), 1, 0, 0, speed, loop),
139                                            random(Smooth(smooth), 2, 0, 0, speed, loop), 0) * radius).clamped());
140
141         case ValueBase::TYPE_INTEGER:
142                 return round_to_int((*link_)(t).get(   int()) +
143                                                         random(Smooth(smooth), 0, 0, 0, speed, loop) * radius);
144
145         case ValueBase::TYPE_REAL:
146                 return ((*link_)(t).get(  Real()) +
147                                 random(Smooth(smooth), 0, 0, 0, speed, loop) * radius);
148
149         case ValueBase::TYPE_TIME:
150                 return ((*link_)(t).get(  Time()) +
151                                 random(Smooth(smooth), 0, 0, 0, speed, loop) * radius);
152
153         case ValueBase::TYPE_VECTOR:
154         {
155                 float length(random(Smooth(smooth), 0, 0, 0, speed, loop) * radius);
156                 Angle::rad angle(random(Smooth(smooth), 1, 0, 0, speed, loop) * PI);
157
158                 return ((*link_)(t).get(Vector()) +
159                                 Vector(Angle::cos(angle).get(), Angle::sin(angle).get()) * length);
160         }
161
162         default:
163                 assert(0);
164                 break;
165         }
166
167         return ValueBase();
168 }
169
170
171 String
172 ValueNode_Random::get_name()const
173 {
174         return "random";
175 }
176
177 String
178 ValueNode_Random::get_local_name()const
179 {
180         return _("Random");
181 }
182
183 bool
184 ValueNode_Random::set_link_vfunc(int i,ValueNode::Handle value)
185 {
186         assert(i>=0 && i<link_count());
187
188         switch(i)
189         {
190         case 0: CHECK_TYPE_AND_SET_VALUE(link_,   get_type());
191         case 1: CHECK_TYPE_AND_SET_VALUE(radius_, ValueBase::TYPE_REAL);
192         case 2: CHECK_TYPE_AND_SET_VALUE(seed_,   ValueBase::TYPE_INTEGER);
193         case 3: CHECK_TYPE_AND_SET_VALUE(speed_,  ValueBase::TYPE_REAL);
194         case 4: CHECK_TYPE_AND_SET_VALUE(smooth_, ValueBase::TYPE_INTEGER);
195         case 5: CHECK_TYPE_AND_SET_VALUE(loop_,  ValueBase::TYPE_REAL);
196         }
197         return false;
198 }
199
200 ValueNode::LooseHandle
201 ValueNode_Random::get_link_vfunc(int i)const
202 {
203         assert(i>=0 && i<link_count());
204
205         switch(i)
206         {
207         case 0: return link_;
208         case 1: return radius_;
209         case 2: return seed_;
210         case 3: return speed_;
211         case 4: return smooth_;
212         case 5: return loop_;
213         }
214         return 0;
215 }
216
217 int
218 ValueNode_Random::link_count()const
219 {
220         return 6;
221 }
222
223 String
224 ValueNode_Random::link_name(int i)const
225 {
226         assert(i>=0 && i<link_count());
227
228         switch(i)
229         {
230         case 0: return "link";
231         case 1: return "radius";
232         case 2: return "seed";
233         case 3: return "speed";
234         case 4: return "smooth";
235         case 5: return "loop";
236         }
237         return String();
238 }
239
240 String
241 ValueNode_Random::link_local_name(int i)const
242 {
243         assert(i>=0 && i<link_count());
244
245         switch(i)
246         {
247         case 0: return _("Link");
248         case 1: return _("Radius");
249         case 2: return _("Seed");
250         case 3: return _("Animation Speed");
251         case 4: return _("Interpolation");
252         case 5: return _("Loop Time");
253         }
254         return String();
255 }
256
257 int
258 ValueNode_Random::get_link_index_from_name(const String &name)const
259 {
260         if(name=="link"  ) return 0;
261         if(name=="radius") return 1;
262         if(name=="seed"  ) return 2;
263         if(name=="speed" ) return 3;
264         if(name=="smooth") return 4;
265         if(name=="loop"  ) return 5;
266         throw Exception::BadLinkName(name);
267 }
268
269 bool
270 ValueNode_Random::check_type(ValueBase::Type type)
271 {
272         return
273                 type==ValueBase::TYPE_ANGLE             ||
274                 type==ValueBase::TYPE_BOOL              ||
275                 type==ValueBase::TYPE_COLOR             ||
276                 type==ValueBase::TYPE_INTEGER   ||
277                 type==ValueBase::TYPE_REAL              ||
278                 type==ValueBase::TYPE_TIME              ||
279                 type==ValueBase::TYPE_VECTOR    ;
280 }
281
282 ValueNode*
283 ValueNode_Random::clone(const GUID& deriv_guid)const
284 {
285         ValueNode_Random* ret = (ValueNode_Random*)LinkableValueNode::clone(deriv_guid);
286         ret->randomize_seed();
287         return ret;
288 }
289
290 void
291 ValueNode_Random::randomize_seed()
292 {
293         int i = get_link_index_from_name("seed");
294         ValueNode::Handle link = get_link_vfunc(i);
295         if(!link->is_exported() && link->get_name() == "constant")
296         {
297                 int seed = time(NULL) + rand();
298                 if (seed < 0) seed = -seed;
299                 random.set_seed(seed);
300                 set_link(i, ValueNode_Const::create(seed));
301         }
302 }
303
304 LinkableValueNode::Vocab
305 ValueNode_Random::get_param_vocab()const
306 {
307         LinkableValueNode::Vocab ret;
308
309         ret.push_back(ParamDesc(ValueBase(),"link")
310                 .set_local_name(_("Link"))
311                 .set_description(_("The value node source that provides the central value"))
312         );
313
314         ret.push_back(ParamDesc(ValueBase(),"radius")
315                 .set_local_name(_("Radius"))
316                 .set_description(_("The value of the maximum random difference"))
317         );
318
319         ret.push_back(ParamDesc(ValueBase(),"seed")
320                 .set_local_name(_("Seed"))
321                 .set_description(_("Seeds the random number generator"))
322         );
323
324         ret.push_back(ParamDesc(ValueBase(),"speed")
325                 .set_local_name(_("Speed"))
326                 .set_description(_("Defines how often a new random value is chosen (in choices per second) "))
327         );
328
329         ret.push_back(ParamDesc(ValueBase(),"smooth")
330                 .set_local_name(_("Interpolation"))
331                 .set_description(_("Determines how the value is interpolated from one random choice to the next"))
332                 .set_hint("enum")
333                 .add_enum_value(RandomNoise::SMOOTH_DEFAULT,"default",_("No interpolation"))
334                 .add_enum_value(RandomNoise::SMOOTH_LINEAR,"linear",_("Linear"))
335                 .add_enum_value(RandomNoise::SMOOTH_COSINE,"cosine",_("Cosine"))
336                 .add_enum_value(RandomNoise::SMOOTH_SPLINE,"spline",_("Spline"))
337                 .add_enum_value(RandomNoise::SMOOTH_CUBIC,"cubic",_("Cubic"))
338         );
339
340
341         ret.push_back(ParamDesc(ValueBase(),"loop")
342                 .set_local_name(_("Loop Time"))
343                 .set_description(_("Makes the random value repeat after the given time"))
344         );
345         return ret;
346 }