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