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