Rename get_param_vocab to get_children_vocab and use a wrapper for the pure virtual...
[synfig.git] / synfig-core / src / synfig / valuenode_linear.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_linear.cpp
3 **      \brief Implementation of the "Linear" 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_linear.h"
34 #include "valuenode_const.h"
35 #include "general.h"
36 #include "color.h"
37 #include <ETL/misc>
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 ValueNode_Linear::ValueNode_Linear(const ValueBase &value):
56         LinkableValueNode(value.get_type())
57 {
58         switch(get_type())
59         {
60         case ValueBase::TYPE_ANGLE:
61                 set_link("slope",ValueNode_Const::create(Angle::deg(0)));
62                 set_link("offset",ValueNode_Const::create(value.get(Angle())));
63                 break;
64         case ValueBase::TYPE_COLOR:
65                 set_link("slope",ValueNode_Const::create(Color(0,0,0,0)));
66                 set_link("offset",ValueNode_Const::create(value.get(Color())));
67                 break;
68         case ValueBase::TYPE_INTEGER:
69                 set_link("slope",ValueNode_Const::create(int(0)));
70                 set_link("offset",ValueNode_Const::create(value.get(int())));
71                 break;
72         case ValueBase::TYPE_REAL:
73                 set_link("slope",ValueNode_Const::create(Real(0)));
74                 set_link("offset",ValueNode_Const::create(value.get(Real())));
75                 break;
76         case ValueBase::TYPE_TIME:
77                 set_link("slope",ValueNode_Const::create(Time(0)));
78                 set_link("offset",ValueNode_Const::create(value.get(Time())));
79                 break;
80         case ValueBase::TYPE_VECTOR:
81                 set_link("slope",ValueNode_Const::create(Vector(0,0)));
82                 set_link("offset",ValueNode_Const::create(value.get(Vector())));
83                 break;
84         default:
85                 throw Exception::BadType(ValueBase::type_local_name(get_type()));
86         }
87 }
88
89 LinkableValueNode*
90 ValueNode_Linear::create_new()const
91 {
92         return new ValueNode_Linear(get_type());
93 }
94
95 ValueNode_Linear*
96 ValueNode_Linear::create(const ValueBase &x)
97 {
98         return new ValueNode_Linear(x);
99 }
100
101 ValueNode_Linear::~ValueNode_Linear()
102 {
103         unlink_all();
104 }
105
106 ValueBase
107 ValueNode_Linear::operator()(Time t)const
108 {
109         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
110                 printf("%s:%d operator()\n", __FILE__, __LINE__);
111
112         switch(get_type())
113         {
114         case ValueBase::TYPE_ANGLE:
115                 return (*m_)(t).get( Angle())*t+(*b_)(t).get( Angle());
116         case ValueBase::TYPE_COLOR:
117                 return (*m_)(t).get( Color())*t+(*b_)(t).get( Color());
118         case ValueBase::TYPE_INTEGER:
119                 return round_to_int((*m_)(t).get(int())*t+(*b_)(t).get(int()));
120         case ValueBase::TYPE_REAL:
121                 return (*m_)(t).get(  Real())*t+(*b_)(t).get(  Real());
122         case ValueBase::TYPE_TIME:
123                 return (*m_)(t).get(  Time())*t+(*b_)(t).get(  Time());
124         case ValueBase::TYPE_VECTOR:
125                 return (*m_)(t).get(Vector())*t+(*b_)(t).get(Vector());
126         default:
127                 assert(0);
128                 break;
129         }
130         return ValueBase();
131 }
132
133
134 String
135 ValueNode_Linear::get_name()const
136 {
137         return "linear";
138 }
139
140 String
141 ValueNode_Linear::get_local_name()const
142 {
143         return _("Linear");
144 }
145
146 bool
147 ValueNode_Linear::check_type(ValueBase::Type type)
148 {
149         return
150                 type==ValueBase::TYPE_ANGLE             ||
151                 type==ValueBase::TYPE_COLOR             ||
152                 type==ValueBase::TYPE_INTEGER   ||
153                 type==ValueBase::TYPE_REAL              ||
154                 type==ValueBase::TYPE_TIME              ||
155                 type==ValueBase::TYPE_VECTOR    ;
156 }
157
158 bool
159 ValueNode_Linear::set_link_vfunc(int i,ValueNode::Handle value)
160 {
161         assert(i>=0 && i<link_count());
162
163         switch(i)
164         {
165         case 0: CHECK_TYPE_AND_SET_VALUE(m_, get_type());
166         case 1: CHECK_TYPE_AND_SET_VALUE(b_, get_type());
167         }
168         return false;
169 }
170
171 ValueNode::LooseHandle
172 ValueNode_Linear::get_link_vfunc(int i)const
173 {
174         assert(i>=0 && i<link_count());
175
176         if(i==0) return m_;
177         if(i==1) return b_;
178         return 0;
179 }
180
181 int
182 ValueNode_Linear::link_count()const
183 {
184         return 2;
185 }
186
187 String
188 ValueNode_Linear::link_name(int i)const
189 {
190         assert(i>=0 && i<link_count());
191
192         if(i==0) return "slope";
193         if(i==1) return "offset";
194         return String();
195 }
196
197 String
198 ValueNode_Linear::link_local_name(int i)const
199 {
200         assert(i>=0 && i<link_count());
201
202         if(i==0)
203                 switch(get_type())
204                 {
205                 case ValueBase::TYPE_ANGLE:
206                 case ValueBase::TYPE_COLOR:
207                 case ValueBase::TYPE_INTEGER:
208                 case ValueBase::TYPE_REAL:
209                 case ValueBase::TYPE_TIME:
210                         return _("Rate");
211                 case ValueBase::TYPE_VECTOR:
212                 default:
213                         return _("Slope");
214                 }
215         if(i==1)
216                 return _("Offset");
217         return String();
218 }
219
220 int
221 ValueNode_Linear::get_link_index_from_name(const String &name)const
222 {
223         if(name=="slope")  return 0;
224         if(name=="offset") return 1;
225
226         throw Exception::BadLinkName(name);
227 }
228
229
230 LinkableValueNode::Vocab
231 ValueNode_Linear::get_children_vocab_vfunc()const
232 {
233         LinkableValueNode::Vocab ret;
234
235         switch(get_type())
236         {
237         case ValueBase::TYPE_ANGLE:
238         case ValueBase::TYPE_COLOR:
239         case ValueBase::TYPE_INTEGER:
240         case ValueBase::TYPE_REAL:
241         case ValueBase::TYPE_TIME:
242                 ret.push_back(ParamDesc(ValueBase(),"slope")
243                         .set_local_name(_("Rate"))
244                         .set_description(_("Value that is multiplied by the current time (in seconds)"))
245                 );
246         break;
247         case ValueBase::TYPE_VECTOR:
248         default:
249                 ret.push_back(ParamDesc(ValueBase(),"slope")
250                         .set_local_name(_("Slope"))
251                         .set_description(_("Value that is multiplied by the current time (in seconds)"))
252                 );
253         }
254
255         ret.push_back(ParamDesc(ValueBase(),"offset")
256                 .set_local_name(_("Offset"))
257                 .set_description(_("Returned value when the current time is zero"))
258         );
259
260         return ret;
261 }