c4aecc32ce81aac0a486f4576e3a4a83d49abcbd
[synfig.git] / synfig-core / src / synfig / valuenode_twotone.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_twotone.cpp
3 **      \brief Implementation of the "Two-Tone" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 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_twotone.h"
35 #include "valuenode_const.h"
36 #include <stdexcept>
37 #include "color.h"
38 #include "gradient.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 synfig::ValueNode_TwoTone::ValueNode_TwoTone(const ValueBase &value):LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
57 {
58         switch(value.get_type())
59         {
60         case ValueBase::TYPE_GRADIENT:
61                 set_link("color1",ValueNode_Const::create(value.get(Gradient())(0)));
62                 set_link("color2",ValueNode_Const::create(value.get(Gradient())(1)));
63                 break;
64         default:
65                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
66         }
67 }
68
69 LinkableValueNode*
70 ValueNode_TwoTone::create_new()const
71 {
72         return new ValueNode_TwoTone(get_type());
73 }
74
75 ValueNode_TwoTone*
76 ValueNode_TwoTone::create(const ValueBase& x)
77 {
78         return new ValueNode_TwoTone(x);
79 }
80
81 synfig::ValueNode_TwoTone::~ValueNode_TwoTone()
82 {
83         unlink_all();
84 }
85
86 synfig::ValueBase
87 synfig::ValueNode_TwoTone::operator()(Time t)const
88 {
89         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
90                 printf("%s:%d operator()\n", __FILE__, __LINE__);
91
92         return Gradient((*ref_a)(t).get(Color()),(*ref_b)(t).get(Color()));
93 }
94
95 bool
96 ValueNode_TwoTone::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(ref_a, ValueBase::TYPE_COLOR);
103         case 1: CHECK_TYPE_AND_SET_VALUE(ref_b, ValueBase::TYPE_COLOR);
104         }
105         return false;
106 }
107
108 ValueNode::LooseHandle
109 ValueNode_TwoTone::get_link_vfunc(int i)const
110 {
111         assert(i>=0 && i<link_count());
112
113         switch(i)
114         {
115                 case 0:
116                         return ref_a;
117                 case 1:
118                         return ref_b;
119         }
120         return 0;
121 }
122
123 int
124 ValueNode_TwoTone::link_count()const
125 {
126         return 2;
127 }
128
129 String
130 ValueNode_TwoTone::link_local_name(int i)const
131 {
132         assert(i>=0 && i<link_count());
133
134         switch(i)
135         {
136                 case 0:
137                         return _("Color1");
138                 case 1:
139                         return _("Color2");
140                 default:
141                         return String();
142         }
143 }
144
145 String
146 ValueNode_TwoTone::link_name(int i)const
147 {
148         assert(i>=0 && i<link_count());
149
150         switch(i)
151         {
152                 case 0:
153                         return "color1";
154                 case 1:
155                         return "color2";
156                 default:
157                         return String();
158         }
159 }
160
161 int
162 ValueNode_TwoTone::get_link_index_from_name(const String &name)const
163 {
164         if(name=="color1")
165                 return 0;
166         if(name=="color2")
167                 return 1;
168         throw Exception::BadLinkName(name);
169 }
170
171 String
172 ValueNode_TwoTone::get_name()const
173 {
174         return "twotone";
175 }
176
177 String
178 ValueNode_TwoTone::get_local_name()const
179 {
180         return _("Two-Tone");
181 }
182
183 bool
184 ValueNode_TwoTone::check_type(ValueBase::Type type)
185 {
186         return type==ValueBase::TYPE_GRADIENT;
187 }
188
189 LinkableValueNode::Vocab
190 ValueNode_TwoTone::get_param_vocab()const
191 {
192         LinkableValueNode::Vocab ret;
193
194         ret.push_back(ParamDesc(ValueBase(),"color1")
195                 .set_local_name(_("Color 1"))
196                 .set_description(_("The start color of the gradient"))
197         );
198
199         ret.push_back(ParamDesc(ValueBase(),"color2")
200                 .set_local_name(_("Color 2"))
201                 .set_description(_("The end color of the gradient"))
202         );
203
204         return ret;
205 }