Use LinkableValueNode members functions when possible in the derived valuenodes.
[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         Vocab ret(get_children_vocab());
58         set_children_vocab(ret);
59         switch(value.get_type())
60         {
61         case ValueBase::TYPE_COLOR:
62                 set_link("gradient", ValueNode_Const::create(Gradient(value.get(Color()),value.get(Color()))));
63                 set_link("index",    ValueNode_Const::create(Real(0.5)));
64                 set_link("loop",    ValueNode_Const::create(bool(false)));
65                 break;
66         default:
67                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
68         }
69 }
70
71 LinkableValueNode*
72 ValueNode_GradientColor::create_new()const
73 {
74         return new ValueNode_GradientColor(get_type());
75 }
76
77 ValueNode_GradientColor*
78 ValueNode_GradientColor::create(const ValueBase &x)
79 {
80         return new ValueNode_GradientColor(x);
81 }
82
83 ValueNode_GradientColor::~ValueNode_GradientColor()
84 {
85         unlink_all();
86 }
87
88 ValueBase
89 ValueNode_GradientColor::operator()(Time t)const
90 {
91         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
92                 printf("%s:%d operator()\n", __FILE__, __LINE__);
93
94         Real index((*index_)(t).get(Real()));
95         bool loop((*loop_)(t).get(bool()));
96         if (loop) index -= floor(index);
97         return (*gradient_)(t).get(Gradient())(index);
98 }
99
100
101 bool
102 ValueNode_GradientColor::set_link_vfunc(int i,ValueNode::Handle value)
103 {
104         assert(i>=0 && i<link_count());
105
106         switch(i)
107         {
108         case 0: CHECK_TYPE_AND_SET_VALUE(gradient_,     ValueBase::TYPE_GRADIENT);
109         case 1: CHECK_TYPE_AND_SET_VALUE(index_,        ValueBase::TYPE_REAL);
110         case 2: CHECK_TYPE_AND_SET_VALUE(loop_,         ValueBase::TYPE_BOOL);
111         }
112         return false;
113 }
114
115 ValueNode::LooseHandle
116 ValueNode_GradientColor::get_link_vfunc(int i)const
117 {
118         assert(i>=0 && i<link_count());
119
120         switch(i)
121         {
122                 case 0: return gradient_;
123                 case 1: return index_;
124                 case 2: return loop_;
125         }
126
127         return 0;
128 }
129
130 String
131 ValueNode_GradientColor::get_name()const
132 {
133         return "gradientcolor";
134 }
135
136 String
137 ValueNode_GradientColor::get_local_name()const
138 {
139         return _("Gradient Color");
140 }
141
142 bool
143 ValueNode_GradientColor::check_type(ValueBase::Type type)
144 {
145         return type==ValueBase::TYPE_COLOR;
146 }
147
148 LinkableValueNode::Vocab
149 ValueNode_GradientColor::get_children_vocab_vfunc()const
150 {
151         if(children_vocab.size())
152                 return children_vocab;
153
154         LinkableValueNode::Vocab ret;
155
156         ret.push_back(ParamDesc(ValueBase(),"gradient")
157                 .set_local_name(_("Gradient"))
158                 .set_description(_("The gradient where the color is picked from"))
159         );
160
161         ret.push_back(ParamDesc(ValueBase(),"index")
162                 .set_local_name(_("Index"))
163                 .set_description(_("The position of the color at the gradient (0,1]"))
164         );
165
166         ret.push_back(ParamDesc(ValueBase(),"loop")
167                 .set_local_name(_("Loop"))
168                 .set_description(_("When checked, the index would loop"))
169         );
170
171         return ret;
172 }