Made all the assert() lines which check the valuenode sub-parameter index range the...
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_gradientrotate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_gradientrotate.cpp
3 **      \brief Implementation of the "Gradient Rotate" valuenode conversion.
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 "general.h"
34 #include "valuenode_gradientrotate.h"
35 #include "valuenode_const.h"
36 #include <stdexcept>
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_GradientRotate::ValueNode_GradientRotate(const Gradient& x):
56         LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
57 {
58         set_link("gradient",ValueNode_Const::create(x));
59         set_link("offset",ValueNode_Const::create(Real(0)));
60         DCAST_HACK_ENABLE();
61 }
62
63 LinkableValueNode*
64 ValueNode_GradientRotate::create_new()const
65 {
66         return new ValueNode_GradientRotate(Gradient());
67 }
68
69 ValueNode_GradientRotate*
70 ValueNode_GradientRotate::create(const ValueBase& x)
71 {
72         ValueBase::Type id(x.get_type());
73         if(id!=ValueBase::TYPE_GRADIENT)
74         {
75                 assert(0);
76                 throw runtime_error("synfig::ValueNode_GradientRotate:Bad type "+ValueBase::type_name(id));
77         }
78
79         ValueNode_GradientRotate* value_node=new ValueNode_GradientRotate(x.get(Gradient()));
80
81         assert(value_node->get_type()==id);
82
83         return value_node;
84 }
85
86 synfig::ValueNode_GradientRotate::~ValueNode_GradientRotate()
87 {
88         unlink_all();
89 }
90
91 bool
92 synfig::ValueNode_GradientRotate::set_gradient(ValueNode::Handle a)
93 {
94         if(a->get_type()!=ValueBase::TYPE_GRADIENT&& !PlaceholderValueNode::Handle::cast_dynamic(a))
95                 return false;
96
97         ref_gradient=a;
98
99         return true;
100 }
101
102 bool
103 synfig::ValueNode_GradientRotate::set_offset(ValueNode::Handle b)
104 {
105         if(b->get_type()!=ValueBase::TYPE_REAL&& !PlaceholderValueNode::Handle::cast_dynamic(b))
106                 return false;
107         ref_offset=b;
108         return true;
109 }
110
111 synfig::ValueBase
112 synfig::ValueNode_GradientRotate::operator()(Time t)const
113 {
114         Gradient gradient;
115         gradient=(*ref_gradient)(t).get(gradient);
116         Real offset((*ref_offset)(t).get(Real()));
117         Gradient::iterator iter;
118         for(iter=gradient.begin();iter!=gradient.end();++iter)
119                 iter->pos+=offset;
120
121         return gradient;
122 }
123
124 bool
125 ValueNode_GradientRotate::set_link_vfunc(int i,ValueNode::Handle x)
126 {
127         assert(i>=0 && i<link_count());
128
129         switch(i)
130         {
131                 case 0:
132                         if(set_gradient(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
133                         else { return false; }
134                 case 1:
135                         if(set_offset(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
136                         else { return false; }
137         }
138
139         return false;
140 }
141
142 ValueNode::LooseHandle
143 ValueNode_GradientRotate::get_link_vfunc(int i)const
144 {
145         assert(i>=0 && i<link_count());
146
147         switch(i)
148         {
149                 case 0:
150                         return ref_gradient;
151                 case 1:
152                         return ref_offset;
153         }
154         return 0;
155 }
156
157 int
158 ValueNode_GradientRotate::link_count()const
159 {
160         return 2;
161 }
162
163 String
164 ValueNode_GradientRotate::link_local_name(int i)const
165 {
166         assert(i>=0 && i<link_count());
167
168         switch(i)
169         {
170                 case 0:
171                         return _("Gradient");
172                 case 1:
173                         return _("Offset");
174                 default:
175                         return String();
176         }
177 }
178
179 String
180 ValueNode_GradientRotate::link_name(int i)const
181 {
182         assert(i>=0 && i<link_count());
183
184         switch(i)
185         {
186                 case 0:
187                         return "gradient";
188                 case 1:
189                         return "offset";
190                 default: return String();
191         }
192 }
193
194 int
195 ValueNode_GradientRotate::get_link_index_from_name(const String &name)const
196 {
197         if(name=="gradient")
198                 return 0;
199         if(name=="offset")
200                 return 1;
201         throw Exception::BadLinkName(name);
202 }
203
204 String
205 ValueNode_GradientRotate::get_name()const
206 {
207         return "gradient_rotate";
208 }
209
210 String
211 ValueNode_GradientRotate::get_local_name()const
212 {
213         return _("Gradient Rotate");
214 }
215
216 bool
217 ValueNode_GradientRotate::check_type(ValueBase::Type type)
218 {
219         return type==ValueBase::TYPE_GRADIENT;
220 }