Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc1 / 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 **      Copyright (c) 2007 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
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_Linear::ValueNode_Linear(const ValueBase &value):
55         LinkableValueNode(value.get_type())
56 {
57         switch(get_type())
58         {
59         case ValueBase::TYPE_ANGLE:
60                 set_link("slope",ValueNode_Const::create(Angle::deg(0)));
61                 set_link("offset",ValueNode_Const::create(value.get(Angle())));
62                 break;
63         case ValueBase::TYPE_COLOR:
64                 set_link("slope",ValueNode_Const::create(Color(0,0,0,0)));
65                 set_link("offset",ValueNode_Const::create(value.get(Color())));
66                 break;
67         case ValueBase::TYPE_INTEGER:
68                 set_link("slope",ValueNode_Const::create(int(0)));
69                 set_link("offset",ValueNode_Const::create(value.get(int())));
70                 break;
71         case ValueBase::TYPE_REAL:
72                 set_link("slope",ValueNode_Const::create(Real(0)));
73                 set_link("offset",ValueNode_Const::create(value.get(Real())));
74                 break;
75         case ValueBase::TYPE_TIME:
76                 set_link("slope",ValueNode_Const::create(Time(0)));
77                 set_link("offset",ValueNode_Const::create(value.get(Time())));
78                 break;
79         case ValueBase::TYPE_VECTOR:
80                 set_link("slope",ValueNode_Const::create(Vector(0,0)));
81                 set_link("offset",ValueNode_Const::create(value.get(Vector())));
82                 break;
83         default:
84                 throw Exception::BadType(ValueBase::type_name(get_type()));
85         }
86
87         DCAST_HACK_ENABLE();
88 }
89
90 LinkableValueNode*
91 ValueNode_Linear::create_new()const
92 {
93         return new ValueNode_Linear(get_type());
94 }
95
96 ValueNode_Linear*
97 ValueNode_Linear::create(const ValueBase &x)
98 {
99         return new ValueNode_Linear(x);
100 }
101
102 ValueNode_Linear::~ValueNode_Linear()
103 {
104         unlink_all();
105 }
106
107 ValueBase
108 ValueNode_Linear::operator()(Time t)const
109 {
110         switch(get_type())
111         {
112         case ValueBase::TYPE_ANGLE:
113                 return (*m_)(t).get( Angle())*t+(*b_)(t).get( Angle());
114         case ValueBase::TYPE_COLOR:
115                 return (*m_)(t).get( Color())*t+(*b_)(t).get( Color());
116         case ValueBase::TYPE_INTEGER:
117         {
118                 Real ret = (*m_)(t).get(int())*t+(*b_)(t).get(int()) + 0.5f;
119                 if (ret < 0) return static_cast<int>(ret-1);
120                 return static_cast<int>(ret);
121         }
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 x)
162 {
163         assert(i==0 || i==1);
164         if(i==0)
165         {
166                 m_=x;
167                 signal_child_changed()(i);signal_value_changed()();
168                 return true;
169         }
170         if(i==1)
171         {
172                 b_=x;
173                 signal_child_changed()(i);signal_value_changed()();
174                 return true;
175         }
176         return false;
177 }
178
179 ValueNode::LooseHandle
180 ValueNode_Linear::get_link_vfunc(int i)const
181 {
182         assert(i==0 || i==1);
183         if(i==0) return m_;
184         if(i==1) return b_;
185         return 0;
186 }
187
188 int
189 ValueNode_Linear::link_count()const
190 {
191         return 2;
192 }
193
194 String
195 ValueNode_Linear::link_name(int i)const
196 {
197         assert(i==0 || i==1);
198         if(i==0) return "slope";
199         if(i==1) return "offset";
200         return String();
201 }
202
203 String
204 ValueNode_Linear::link_local_name(int i)const
205 {
206         assert(i==0 || i==1);
207         if(i==0)
208                 switch(get_type())
209                 {
210                 case ValueBase::TYPE_ANGLE:
211                 case ValueBase::TYPE_COLOR:
212                 case ValueBase::TYPE_INTEGER:
213                 case ValueBase::TYPE_REAL:
214                 case ValueBase::TYPE_TIME:
215                         return _("Rate");
216                 case ValueBase::TYPE_VECTOR:
217                 default:
218                         return _("Slope");
219                 }
220         if(i==1)
221                 return _("Offset");
222         return String();
223 }
224
225 int
226 ValueNode_Linear::get_link_index_from_name(const String &name)const
227 {
228         if(name=="slope")  return 0;
229         if(name=="offset") return 1;
230
231         throw Exception::BadLinkName(name);
232 }