Round integers towards zero in linear convert type.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_linear.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_linear.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "valuenode_linear.h"
33 #include "valuenode_const.h"
34 #include "general.h"
35 #include "color.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44
45 /* === M A C R O S ========================================================= */
46
47 /* === G L O B A L S ======================================================= */
48
49 /* === P R O C E D U R E S ================================================= */
50
51 /* === M E T H O D S ======================================================= */
52
53 ValueNode_Linear::ValueNode_Linear(const ValueBase &value):
54         LinkableValueNode(value.get_type())
55 {
56         switch(get_type())
57         {
58         case ValueBase::TYPE_ANGLE:
59                 set_link("slope",ValueNode_Const::create(Angle::deg(0)));
60                 set_link("offset",ValueNode_Const::create(value.get(Angle())));
61                 break;
62         case ValueBase::TYPE_COLOR:
63                 set_link("slope",ValueNode_Const::create(Color(0,0,0,0)));
64                 set_link("offset",ValueNode_Const::create(value.get(Color())));
65                 break;
66         case ValueBase::TYPE_INTEGER:
67                 set_link("slope",ValueNode_Const::create(int(0)));
68                 set_link("offset",ValueNode_Const::create(value.get(int())));
69                 break;
70         case ValueBase::TYPE_REAL:
71                 set_link("slope",ValueNode_Const::create(Real(0)));
72                 set_link("offset",ValueNode_Const::create(value.get(Real())));
73                 break;
74         case ValueBase::TYPE_TIME:
75                 set_link("slope",ValueNode_Const::create(Time(0)));
76                 set_link("offset",ValueNode_Const::create(value.get(Time())));
77                 break;
78         case ValueBase::TYPE_VECTOR:
79                 set_link("slope",ValueNode_Const::create(Vector(0,0)));
80                 set_link("offset",ValueNode_Const::create(value.get(Vector())));
81                 break;
82         default:
83                 throw Exception::BadType(ValueBase::type_name(get_type()));
84         }
85
86         DCAST_HACK_ENABLE();
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         switch(get_type())
110         {
111         case ValueBase::TYPE_ANGLE:
112                 return (*m_)(t).get( Angle())*t+(*b_)(t).get( Angle());
113         case ValueBase::TYPE_COLOR:
114                 return (*m_)(t).get( Color())*t+(*b_)(t).get( Color());
115         case ValueBase::TYPE_INTEGER:
116         {
117                 Real ret = (*m_)(t).get(int())*t+(*b_)(t).get(int()) + 0.5f;
118                 if (ret < 0) return static_cast<int>(ret-1);
119                 return static_cast<int>(ret);
120         }
121         case ValueBase::TYPE_REAL:
122                 return (*m_)(t).get(  Real())*t+(*b_)(t).get(  Real());
123         case ValueBase::TYPE_TIME:
124                 return (*m_)(t).get(  Time())*t+(*b_)(t).get(  Time());
125         case ValueBase::TYPE_VECTOR:
126                 return (*m_)(t).get(Vector())*t+(*b_)(t).get(Vector());
127         default:
128                 assert(0);
129                 break;
130         }
131         return ValueBase();
132 }
133
134
135 String
136 ValueNode_Linear::get_name()const
137 {
138         return "linear";
139 }
140
141 String
142 ValueNode_Linear::get_local_name()const
143 {
144         return _("Linear");
145 }
146
147 bool
148 ValueNode_Linear::check_type(ValueBase::Type type)
149 {
150         return
151                 type==ValueBase::TYPE_ANGLE             ||
152                 type==ValueBase::TYPE_COLOR             ||
153                 type==ValueBase::TYPE_INTEGER   ||
154                 type==ValueBase::TYPE_REAL              ||
155                 type==ValueBase::TYPE_TIME              ||
156                 type==ValueBase::TYPE_VECTOR    ;
157 }
158
159 bool
160 ValueNode_Linear::set_link_vfunc(int i,ValueNode::Handle x)
161 {
162         assert(i==0 || i==1);
163         if(i==0)
164         {
165                 m_=x;
166                 signal_child_changed()(i);signal_value_changed()();
167                 return true;
168         }
169         if(i==1)
170         {
171                 b_=x;
172                 signal_child_changed()(i);signal_value_changed()();
173                 return true;
174         }
175         return false;
176 }
177
178 ValueNode::LooseHandle
179 ValueNode_Linear::get_link_vfunc(int i)const
180 {
181         assert(i==0 || i==1);
182         if(i==0) return m_;
183         if(i==1) return b_;
184         return 0;
185 }
186
187 int
188 ValueNode_Linear::link_count()const
189 {
190         return 2;
191 }
192
193 String
194 ValueNode_Linear::link_name(int i)const
195 {
196         assert(i==0 || i==1);
197         if(i==0) return "slope";
198         if(i==1) return "offset";
199         return String();
200 }
201
202 String
203 ValueNode_Linear::link_local_name(int i)const
204 {
205         assert(i==0 || i==1);
206         if(i==0)
207                 switch(get_type())
208                 {
209                 case ValueBase::TYPE_ANGLE:
210                 case ValueBase::TYPE_COLOR:
211                 case ValueBase::TYPE_INTEGER:
212                 case ValueBase::TYPE_REAL:
213                 case ValueBase::TYPE_TIME:
214                         return _("Rate");
215                 case ValueBase::TYPE_VECTOR:
216                 default:
217                         return _("Slope");
218                 }
219         if(i==1)
220                 return _("Offset");
221         return String();
222 }
223
224 int
225 ValueNode_Linear::get_link_index_from_name(const String &name)const
226 {
227         if(name=="slope")  return 0;
228         if(name=="offset") return 1;
229
230         throw Exception::BadLinkName(name);
231 }