Add ValueNode "Gradient Color" for selecting individual colors from a gradient.
[synfig.git] / synfig-core / trunk / 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                 break;
63         default:
64                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
65         }
66
67         DCAST_HACK_ENABLE();
68 }
69
70 LinkableValueNode*
71 ValueNode_GradientColor::create_new()const
72 {
73         return new ValueNode_GradientColor(get_type());
74 }
75
76 ValueNode_GradientColor*
77 ValueNode_GradientColor::create(const ValueBase &x)
78 {
79         return new ValueNode_GradientColor(x);
80 }
81
82 ValueNode_GradientColor::~ValueNode_GradientColor()
83 {
84         unlink_all();
85 }
86
87 ValueBase
88 ValueNode_GradientColor::operator()(Time t)const
89 {
90         Real index((*index_)(t).get(Real()));
91         return (*gradient_)(t).get(Gradient())(index);
92 }
93
94
95 bool
96 ValueNode_GradientColor::set_link_vfunc(int i,ValueNode::Handle value)
97 {
98         assert(i>=0 && i<link_count());
99
100         switch(i)
101         {
102         case 0: CHECK_TYPE_AND_SET_VALUE(gradient_, ValueBase::TYPE_GRADIENT);
103         case 1: CHECK_TYPE_AND_SET_VALUE(index_, ValueBase::TYPE_REAL);
104         }
105         return false;
106 }
107
108 ValueNode::LooseHandle
109 ValueNode_GradientColor::get_link_vfunc(int i)const
110 {
111         assert(i>=0 && i<link_count());
112
113         switch(i)
114         {
115                 case 0: return gradient_;
116                 case 1: return index_;
117         }
118
119         return 0;
120 }
121
122 int
123 ValueNode_GradientColor::link_count()const
124 {
125         return 2;
126 }
127
128 String
129 ValueNode_GradientColor::link_local_name(int i)const
130 {
131         assert(i>=0 && i<link_count());
132
133         switch(i)
134         {
135                 case 0: return _("Gradient");
136                 case 1: return _("Index");
137         }
138         return String();
139 }
140
141 String
142 ValueNode_GradientColor::link_name(int i)const
143 {
144         assert(i>=0 && i<link_count());
145
146         switch(i)
147         {
148                 case 0: return "gradient";
149                 case 1: return "index";
150         }
151         return String();
152 }
153
154 int
155 ValueNode_GradientColor::get_link_index_from_name(const String &name)const
156 {
157         if (name=="gradient") return 0;
158         if (name=="index")        return 1;
159         throw Exception::BadLinkName(name);
160 }
161
162 String
163 ValueNode_GradientColor::get_name()const
164 {
165         return "gradientcolor";
166 }
167
168 String
169 ValueNode_GradientColor::get_local_name()const
170 {
171         return _("Gradient Color");
172 }
173
174 bool
175 ValueNode_GradientColor::check_type(ValueBase::Type type)
176 {
177         return type==ValueBase::TYPE_COLOR;
178 }