Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / stable / 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, 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 "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(String(_("Gradient Rotate"))+_(":Bad type ")+ValueBase::type_local_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 synfig::ValueBase
92 synfig::ValueNode_GradientRotate::operator()(Time t)const
93 {
94         Gradient gradient;
95         gradient=(*ref_gradient)(t).get(gradient);
96         Real offset((*ref_offset)(t).get(Real()));
97         Gradient::iterator iter;
98         for(iter=gradient.begin();iter!=gradient.end();++iter)
99                 iter->pos+=offset;
100
101         return gradient;
102 }
103
104 bool
105 ValueNode_GradientRotate::set_link_vfunc(int i,ValueNode::Handle value)
106 {
107         assert(i>=0 && i<link_count());
108
109         switch(i)
110         {
111         case 0: CHECK_TYPE_AND_SET_VALUE(ref_gradient, ValueBase::TYPE_GRADIENT);
112         case 1: CHECK_TYPE_AND_SET_VALUE(ref_offset,   ValueBase::TYPE_REAL);
113         }
114         return false;
115 }
116
117 ValueNode::LooseHandle
118 ValueNode_GradientRotate::get_link_vfunc(int i)const
119 {
120         assert(i>=0 && i<link_count());
121
122         switch(i)
123         {
124                 case 0:
125                         return ref_gradient;
126                 case 1:
127                         return ref_offset;
128         }
129         return 0;
130 }
131
132 int
133 ValueNode_GradientRotate::link_count()const
134 {
135         return 2;
136 }
137
138 String
139 ValueNode_GradientRotate::link_local_name(int i)const
140 {
141         assert(i>=0 && i<link_count());
142
143         switch(i)
144         {
145                 case 0:
146                         return _("Gradient");
147                 case 1:
148                         return _("Offset");
149                 default:
150                         return String();
151         }
152 }
153
154 String
155 ValueNode_GradientRotate::link_name(int i)const
156 {
157         assert(i>=0 && i<link_count());
158
159         switch(i)
160         {
161                 case 0:
162                         return "gradient";
163                 case 1:
164                         return "offset";
165                 default: return String();
166         }
167 }
168
169 int
170 ValueNode_GradientRotate::get_link_index_from_name(const String &name)const
171 {
172         if(name=="gradient")
173                 return 0;
174         if(name=="offset")
175                 return 1;
176         throw Exception::BadLinkName(name);
177 }
178
179 String
180 ValueNode_GradientRotate::get_name()const
181 {
182         return "gradient_rotate";
183 }
184
185 String
186 ValueNode_GradientRotate::get_local_name()const
187 {
188         return _("Gradient Rotate");
189 }
190
191 bool
192 ValueNode_GradientRotate::check_type(ValueBase::Type type)
193 {
194         return type==ValueBase::TYPE_GRADIENT;
195 }