Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / synfig / valuenode_linear.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_linear.cpp
3 **      \brief Template File
4 **
5 **      $Id: valuenode_linear.cpp,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
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
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43
44 /* === M A C R O S ========================================================= */
45
46 /* === G L O B A L S ======================================================= */
47
48 /* === P R O C E D U R E S ================================================= */
49
50 /* === M E T H O D S ======================================================= */
51
52 ValueNode_Linear::ValueNode_Linear(const ValueBase::Type &x):
53         LinkableValueNode(x)
54 {
55         switch(x)
56         {
57         case ValueBase::TYPE_REAL:
58                 set_link("slope",ValueNode_Const::create(Real(1)));
59                 set_link("offset",ValueNode_Const::create(Real(0)));
60                 break;
61         case ValueBase::TYPE_TIME:
62                 set_link("slope",ValueNode_Const::create(Time(1)));
63                 set_link("offset",ValueNode_Const::create(Time(0)));
64                 break;
65         case ValueBase::TYPE_VECTOR:
66                 set_link("slope",ValueNode_Const::create(Vector(1.0,1.0)));
67                 set_link("offset",ValueNode_Const::create(Vector(0.0,0.0)));
68                 break;
69         case ValueBase::TYPE_ANGLE:
70                 set_link("slope",ValueNode_Const::create(Angle::deg(90)));
71                 set_link("offset",ValueNode_Const::create(Angle::deg(0)));
72                 break;
73         default:
74                 throw Exception::BadType(ValueBase::type_name(x));
75         }
76
77         DCAST_HACK_ENABLE();
78 }
79
80 ValueNode_Linear*
81 ValueNode_Linear::create(const ValueBase &x)
82 {
83         return new ValueNode_Linear(x.get_type());
84 }
85
86 ValueNode_Linear::~ValueNode_Linear()
87 {
88         unlink_all();
89 }
90
91 ValueBase
92 ValueNode_Linear::operator()(Time t)const
93 {
94         switch(get_type())
95         {
96         case ValueBase::TYPE_TIME:
97                 return (*m_)(t).get(Time())*t+(*b_)(t).get(Time());
98         case ValueBase::TYPE_REAL:
99                 return (*m_)(t).get(Real())*t+(*b_)(t).get(Real());
100         case ValueBase::TYPE_VECTOR:
101                 return (*m_)(t).get(Vector())*t+(*b_)(t).get(Vector());
102         case ValueBase::TYPE_ANGLE:
103                 return (*m_)(t).get(Angle())*t+(*b_)(t).get(Angle());
104         default:
105                 assert(0);
106                 break;
107         }
108         return ValueBase();
109 }
110
111
112 String
113 ValueNode_Linear::get_name()const
114 {
115         return "linear";
116 }
117
118 String
119 ValueNode_Linear::get_local_name()const
120 {
121         return _("Linear");
122 }
123                 
124 bool
125 ValueNode_Linear::check_type(ValueBase::Type type)
126 {
127         return type==ValueBase::TYPE_REAL
128                 || type==ValueBase::TYPE_VECTOR
129                 || type==ValueBase::TYPE_TIME
130                 || type==ValueBase::TYPE_ANGLE;
131 }
132
133 bool
134 ValueNode_Linear::set_link_vfunc(int i,ValueNode::Handle x)
135 {
136         assert(i==0 || i==1);
137         if(i==0)
138         {
139                 m_=x;
140                 signal_child_changed()(i);signal_value_changed()();
141                 return true;
142         }
143         if(i==1)
144         {
145                 b_=x;
146                 signal_child_changed()(i);signal_value_changed()();
147                 return true;
148         }
149         return false;
150 }
151
152 ValueNode::LooseHandle
153 ValueNode_Linear::get_link_vfunc(int i)const
154 {
155         assert(i==0 || i==1);
156         if(i==0)
157                 return m_;
158         if(i==1)
159                 return b_;
160
161         return 0;
162 }
163
164 int
165 ValueNode_Linear::link_count()const
166 {
167         return 2;
168 }
169
170 String
171 ValueNode_Linear::link_name(int i)const
172 {
173         assert(i==0 || i==1);
174         if(i==0)
175                 return "slope";
176         if(i==1)
177                 return "offset";
178         return String();
179 }
180
181 String
182 ValueNode_Linear::link_local_name(int i)const
183 {
184         assert(i==0 || i==1);
185         if(i==0)
186                 switch(get_type())
187                 {
188                 case ValueBase::TYPE_REAL:
189                 case ValueBase::TYPE_TIME:
190                 case ValueBase::TYPE_ANGLE:
191                         return _("Rate");
192                 default:
193                         return _("Slope");
194                 }
195         if(i==1)
196                 return _("Offset");
197         return String();
198 }
199
200 int
201 ValueNode_Linear::get_link_index_from_name(const String &name)const
202 {
203         if(name=="slope")
204                 return 0;
205         if(name=="offset")
206                 return 1;
207         
208         throw Exception::BadLinkName(name);
209 }
210
211 LinkableValueNode*
212 ValueNode_Linear::create_new()const
213 {
214         return new ValueNode_Linear(get_type());
215 }