690d97380c000efd4d5425c46000a0885e169de3
[synfig.git] / synfig-core / src / synfig / valuenode_gradientcolor.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_gradientcolor.cpp
3 **      \brief Implementation of the "Gradient Color" 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 "valuenode_gradientcolor.h"
34 #include "valuenode_const.h"
35 #include "gradient.h"
36 #include "general.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 ValueNode_GradientColor::ValueNode_GradientColor(const ValueBase &value):
55         LinkableValueNode(value.get_type())
56 {
57         switch(value.get_type())
58         {
59         case ValueBase::TYPE_COLOR:
60                 set_link("gradient", ValueNode_Const::create(Gradient(value.get(Color()),value.get(Color()))));
61                 set_link("index",    ValueNode_Const::create(Real(0.5)));
62                 set_link("loop",    ValueNode_Const::create(bool(false)));
63                 break;
64         default:
65                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
66         }
67 }
68
69 LinkableValueNode*
70 ValueNode_GradientColor::create_new()const
71 {
72         return new ValueNode_GradientColor(get_type());
73 }
74
75 ValueNode_GradientColor*
76 ValueNode_GradientColor::create(const ValueBase &x)
77 {
78         return new ValueNode_GradientColor(x);
79 }
80
81 ValueNode_GradientColor::~ValueNode_GradientColor()
82 {
83         unlink_all();
84 }
85
86 ValueBase
87 ValueNode_GradientColor::operator()(Time t)const
88 {
89         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
90                 printf("%s:%d operator()\n", __FILE__, __LINE__);
91
92         Real index((*index_)(t).get(Real()));
93         bool loop((*loop_)(t).get(bool()));
94         if (loop) index -= floor(index);
95         return (*gradient_)(t).get(Gradient())(index);
96 }
97
98
99 bool
100 ValueNode_GradientColor::set_link_vfunc(int i,ValueNode::Handle value)
101 {
102         assert(i>=0 && i<link_count());
103
104         switch(i)
105         {
106         case 0: CHECK_TYPE_AND_SET_VALUE(gradient_,     ValueBase::TYPE_GRADIENT);
107         case 1: CHECK_TYPE_AND_SET_VALUE(index_,        ValueBase::TYPE_REAL);
108         case 2: CHECK_TYPE_AND_SET_VALUE(loop_,         ValueBase::TYPE_BOOL);
109         }
110         return false;
111 }
112
113 ValueNode::LooseHandle
114 ValueNode_GradientColor::get_link_vfunc(int i)const
115 {
116         assert(i>=0 && i<link_count());
117
118         switch(i)
119         {
120                 case 0: return gradient_;
121                 case 1: return index_;
122                 case 2: return loop_;
123         }
124
125         return 0;
126 }
127
128 int
129 ValueNode_GradientColor::link_count()const
130 {
131         return 3;
132 }
133
134 String
135 ValueNode_GradientColor::link_local_name(int i)const
136 {
137         assert(i>=0 && i<link_count());
138
139         switch(i)
140         {
141                 case 0: return _("Gradient");
142                 case 1: return _("Index");
143                 case 2: return _("Loop");
144         }
145         return String();
146 }
147
148 String
149 ValueNode_GradientColor::link_name(int i)const
150 {
151         assert(i>=0 && i<link_count());
152
153         switch(i)
154         {
155                 case 0: return "gradient";
156                 case 1: return "index";
157                 case 2: return "loop";
158         }
159         return String();
160 }
161
162 int
163 ValueNode_GradientColor::get_link_index_from_name(const String &name)const
164 {
165         if (name=="gradient") return 0;
166         if (name=="index")        return 1;
167         if (name=="loop")         return 2;
168         throw Exception::BadLinkName(name);
169 }
170
171 String
172 ValueNode_GradientColor::get_name()const
173 {
174         return "gradientcolor";
175 }
176
177 String
178 ValueNode_GradientColor::get_local_name()const
179 {
180         return _("Gradient Color");
181 }
182
183 bool
184 ValueNode_GradientColor::check_type(ValueBase::Type type)
185 {
186         return type==ValueBase::TYPE_COLOR;
187 }
188
189 LinkableValueNode::Vocab
190 ValueNode_GradientColor::get_param_vocab()const
191 {
192         LinkableValueNode::Vocab ret;
193
194         ret.push_back(ParamDesc(ValueBase(),"gradient")
195                 .set_local_name(_("Gradient"))
196                 .set_description(_("The gradient where the color is picked from"))
197         );
198
199         ret.push_back(ParamDesc(ValueBase(),"index")
200                 .set_local_name(_("Index"))
201                 .set_description(_("The position of the color at the gradient (0,1]"))
202         );
203
204         ret.push_back(ParamDesc(ValueBase(),"loop")
205                 .set_local_name(_("Loop"))
206                 .set_description(_("When checked, the index would loop"))
207         );
208
209         return ret;
210 }