Use LinkableValueNode members functions when possible in the derived valuenodes.
[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         Vocab ret(get_children_vocab());
59         set_children_vocab(ret);
60         switch(get_type())
61         {
62         case ValueBase::TYPE_ANGLE:
63                 set_link("slope",ValueNode_Const::create(Angle::deg(0)));
64                 set_link("offset",ValueNode_Const::create(value.get(Angle())));
65                 break;
66         case ValueBase::TYPE_COLOR:
67                 set_link("slope",ValueNode_Const::create(Color(0,0,0,0)));
68                 set_link("offset",ValueNode_Const::create(value.get(Color())));
69                 break;
70         case ValueBase::TYPE_INTEGER:
71                 set_link("slope",ValueNode_Const::create(int(0)));
72                 set_link("offset",ValueNode_Const::create(value.get(int())));
73                 break;
74         case ValueBase::TYPE_REAL:
75                 set_link("slope",ValueNode_Const::create(Real(0)));
76                 set_link("offset",ValueNode_Const::create(value.get(Real())));
77                 break;
78         case ValueBase::TYPE_TIME:
79                 set_link("slope",ValueNode_Const::create(Time(0)));
80                 set_link("offset",ValueNode_Const::create(value.get(Time())));
81                 break;
82         case ValueBase::TYPE_VECTOR:
83                 set_link("slope",ValueNode_Const::create(Vector(0,0)));
84                 set_link("offset",ValueNode_Const::create(value.get(Vector())));
85                 break;
86         default:
87                 throw Exception::BadType(ValueBase::type_local_name(get_type()));
88         }
89 }
90
91 LinkableValueNode*
92 ValueNode_Linear::create_new()const
93 {
94         return new ValueNode_Linear(get_type());
95 }
96
97 ValueNode_Linear*
98 ValueNode_Linear::create(const ValueBase &x)
99 {
100         return new ValueNode_Linear(x);
101 }
102
103 ValueNode_Linear::~ValueNode_Linear()
104 {
105         unlink_all();
106 }
107
108 ValueBase
109 ValueNode_Linear::operator()(Time t)const
110 {
111         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
112                 printf("%s:%d operator()\n", __FILE__, __LINE__);
113
114         switch(get_type())
115         {
116         case ValueBase::TYPE_ANGLE:
117                 return (*m_)(t).get( Angle())*t+(*b_)(t).get( Angle());
118         case ValueBase::TYPE_COLOR:
119                 return (*m_)(t).get( Color())*t+(*b_)(t).get( Color());
120         case ValueBase::TYPE_INTEGER:
121                 return round_to_int((*m_)(t).get(int())*t+(*b_)(t).get(int()));
122         case ValueBase::TYPE_REAL:
123                 return (*m_)(t).get(  Real())*t+(*b_)(t).get(  Real());
124         case ValueBase::TYPE_TIME:
125                 return (*m_)(t).get(  Time())*t+(*b_)(t).get(  Time());
126         case ValueBase::TYPE_VECTOR:
127                 return (*m_)(t).get(Vector())*t+(*b_)(t).get(Vector());
128         default:
129                 assert(0);
130                 break;
131         }
132         return ValueBase();
133 }
134
135
136 String
137 ValueNode_Linear::get_name()const
138 {
139         return "linear";
140 }
141
142 String
143 ValueNode_Linear::get_local_name()const
144 {
145         return _("Linear");
146 }
147
148 bool
149 ValueNode_Linear::check_type(ValueBase::Type type)
150 {
151         return
152                 type==ValueBase::TYPE_ANGLE             ||
153                 type==ValueBase::TYPE_COLOR             ||
154                 type==ValueBase::TYPE_INTEGER   ||
155                 type==ValueBase::TYPE_REAL              ||
156                 type==ValueBase::TYPE_TIME              ||
157                 type==ValueBase::TYPE_VECTOR    ;
158 }
159
160 bool
161 ValueNode_Linear::set_link_vfunc(int i,ValueNode::Handle value)
162 {
163         assert(i>=0 && i<link_count());
164
165         switch(i)
166         {
167         case 0: CHECK_TYPE_AND_SET_VALUE(m_, get_type());
168         case 1: CHECK_TYPE_AND_SET_VALUE(b_, get_type());
169         }
170         return false;
171 }
172
173 ValueNode::LooseHandle
174 ValueNode_Linear::get_link_vfunc(int i)const
175 {
176         assert(i>=0 && i<link_count());
177
178         if(i==0) return m_;
179         if(i==1) return b_;
180         return 0;
181 }
182
183 LinkableValueNode::Vocab
184 ValueNode_Linear::get_children_vocab_vfunc()const
185 {
186         if(children_vocab.size())
187                 return children_vocab;
188
189         LinkableValueNode::Vocab ret;
190
191         switch(get_type())
192         {
193         case ValueBase::TYPE_ANGLE:
194         case ValueBase::TYPE_COLOR:
195         case ValueBase::TYPE_INTEGER:
196         case ValueBase::TYPE_REAL:
197         case ValueBase::TYPE_TIME:
198                 ret.push_back(ParamDesc(ValueBase(),"slope")
199                         .set_local_name(_("Rate"))
200                         .set_description(_("Value that is multiplied by the current time (in seconds)"))
201                 );
202         break;
203         case ValueBase::TYPE_VECTOR:
204         default:
205                 ret.push_back(ParamDesc(ValueBase(),"slope")
206                         .set_local_name(_("Slope"))
207                         .set_description(_("Value that is multiplied by the current time (in seconds)"))
208                 );
209         }
210
211         ret.push_back(ParamDesc(ValueBase(),"offset")
212                 .set_local_name(_("Offset"))
213                 .set_description(_("Returned value when the current time is zero"))
214         );
215
216         return ret;
217 }