f2496dacd127726058cc7538c27a2364a1adeb7d
[synfig.git] / synfig-core / 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 }
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(String(_("Gradient Rotate"))+_(":Bad type ")+ValueBase::type_local_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 synfig::ValueBase
91 synfig::ValueNode_GradientRotate::operator()(Time t)const
92 {
93         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
94                 printf("%s:%d operator()\n", __FILE__, __LINE__);
95
96         Gradient gradient;
97         gradient=(*ref_gradient)(t).get(gradient);
98         Real offset((*ref_offset)(t).get(Real()));
99         Gradient::iterator iter;
100         for(iter=gradient.begin();iter!=gradient.end();++iter)
101                 iter->pos+=offset;
102
103         return gradient;
104 }
105
106 bool
107 ValueNode_GradientRotate::set_link_vfunc(int i,ValueNode::Handle value)
108 {
109         assert(i>=0 && i<link_count());
110
111         switch(i)
112         {
113         case 0: CHECK_TYPE_AND_SET_VALUE(ref_gradient, ValueBase::TYPE_GRADIENT);
114         case 1: CHECK_TYPE_AND_SET_VALUE(ref_offset,   ValueBase::TYPE_REAL);
115         }
116         return false;
117 }
118
119 ValueNode::LooseHandle
120 ValueNode_GradientRotate::get_link_vfunc(int i)const
121 {
122         assert(i>=0 && i<link_count());
123
124         switch(i)
125         {
126                 case 0:
127                         return ref_gradient;
128                 case 1:
129                         return ref_offset;
130         }
131         return 0;
132 }
133
134 int
135 ValueNode_GradientRotate::link_count()const
136 {
137         return 2;
138 }
139
140 String
141 ValueNode_GradientRotate::link_local_name(int i)const
142 {
143         assert(i>=0 && i<link_count());
144
145         switch(i)
146         {
147                 case 0:
148                         return _("Gradient");
149                 case 1:
150                         return _("Offset");
151                 default:
152                         return String();
153         }
154 }
155
156 String
157 ValueNode_GradientRotate::link_name(int i)const
158 {
159         assert(i>=0 && i<link_count());
160
161         switch(i)
162         {
163                 case 0:
164                         return "gradient";
165                 case 1:
166                         return "offset";
167                 default: return String();
168         }
169 }
170
171 int
172 ValueNode_GradientRotate::get_link_index_from_name(const String &name)const
173 {
174         if(name=="gradient")
175                 return 0;
176         if(name=="offset")
177                 return 1;
178         throw Exception::BadLinkName(name);
179 }
180
181 String
182 ValueNode_GradientRotate::get_name()const
183 {
184         return "gradient_rotate";
185 }
186
187 String
188 ValueNode_GradientRotate::get_local_name()const
189 {
190         return _("Gradient Rotate");
191 }
192
193 bool
194 ValueNode_GradientRotate::check_type(ValueBase::Type type)
195 {
196         return type==ValueBase::TYPE_GRADIENT;
197 }
198
199 LinkableValueNode::Vocab
200 ValueNode_GradientRotate::get_children_vocab_vfunc()const
201 {
202         LinkableValueNode::Vocab ret;
203
204         ret.push_back(ParamDesc(ValueBase(),"gradient")
205                 .set_local_name(_("Gradient"))
206                 .set_description(_("The source gradient to rotate"))
207         );
208
209         ret.push_back(ParamDesc(ValueBase(),"offset")
210                 .set_local_name(_("Offset"))
211                 .set_description(_("The amount to offset the gradient"))
212         );
213
214         return ret;
215 }