moreupdates
[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: valuenode_linear.cpp,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "valuenode_linear.h"
32 #include "valuenode_const.h"
33 #include "general.h"
34
35 #endif
36
37 /* === U S I N G =========================================================== */
38
39 using namespace std;
40 using namespace etl;
41 using namespace synfig;
42
43 /* === M A C R O S ========================================================= */
44
45 /* === G L O B A L S ======================================================= */
46
47 /* === P R O C E D U R E S ================================================= */
48
49 /* === M E T H O D S ======================================================= */
50
51 ValueNode_Linear::ValueNode_Linear(const ValueBase::Type &x):
52         LinkableValueNode(x)
53 {
54         switch(x)
55         {
56         case ValueBase::TYPE_REAL:
57                 set_link("slope",ValueNode_Const::create(Real(1)));
58                 set_link("offset",ValueNode_Const::create(Real(0)));
59                 break;
60         case ValueBase::TYPE_TIME:
61                 set_link("slope",ValueNode_Const::create(Time(1)));
62                 set_link("offset",ValueNode_Const::create(Time(0)));
63                 break;
64         case ValueBase::TYPE_VECTOR:
65                 set_link("slope",ValueNode_Const::create(Vector(1.0,1.0)));
66                 set_link("offset",ValueNode_Const::create(Vector(0.0,0.0)));
67                 break;
68         case ValueBase::TYPE_ANGLE:
69                 set_link("slope",ValueNode_Const::create(Angle::deg(90)));
70                 set_link("offset",ValueNode_Const::create(Angle::deg(0)));
71                 break;
72         default:
73                 throw Exception::BadType(ValueBase::type_name(x));
74         }
75
76         DCAST_HACK_ENABLE();
77 }
78
79 ValueNode_Linear*
80 ValueNode_Linear::create(const ValueBase &x)
81 {
82         return new ValueNode_Linear(x.get_type());
83 }
84
85 ValueNode_Linear::~ValueNode_Linear()
86 {
87         unlink_all();
88 }
89
90 ValueBase
91 ValueNode_Linear::operator()(Time t)const
92 {
93         switch(get_type())
94         {
95         case ValueBase::TYPE_TIME:
96                 return (*m_)(t).get(Time())*t+(*b_)(t).get(Time());
97         case ValueBase::TYPE_REAL:
98                 return (*m_)(t).get(Real())*t+(*b_)(t).get(Real());
99         case ValueBase::TYPE_VECTOR:
100                 return (*m_)(t).get(Vector())*t+(*b_)(t).get(Vector());
101         case ValueBase::TYPE_ANGLE:
102                 return (*m_)(t).get(Angle())*t+(*b_)(t).get(Angle());
103         default:
104                 assert(0);
105                 break;
106         }
107         return ValueBase();
108 }
109
110
111 String
112 ValueNode_Linear::get_name()const
113 {
114         return "linear";
115 }
116
117 String
118 ValueNode_Linear::get_local_name()const
119 {
120         return _("Linear");
121 }
122                 
123 bool
124 ValueNode_Linear::check_type(ValueBase::Type type)
125 {
126         return type==ValueBase::TYPE_REAL
127                 || type==ValueBase::TYPE_VECTOR
128                 || type==ValueBase::TYPE_TIME
129                 || type==ValueBase::TYPE_ANGLE;
130 }
131
132 bool
133 ValueNode_Linear::set_link_vfunc(int i,ValueNode::Handle x)
134 {
135         assert(i==0 || i==1);
136         if(i==0)
137         {
138                 m_=x;
139                 signal_child_changed()(i);signal_value_changed()();
140                 return true;
141         }
142         if(i==1)
143         {
144                 b_=x;
145                 signal_child_changed()(i);signal_value_changed()();
146                 return true;
147         }
148         return false;
149 }
150
151 ValueNode::LooseHandle
152 ValueNode_Linear::get_link_vfunc(int i)const
153 {
154         assert(i==0 || i==1);
155         if(i==0)
156                 return m_;
157         if(i==1)
158                 return b_;
159
160         return 0;
161 }
162
163 int
164 ValueNode_Linear::link_count()const
165 {
166         return 2;
167 }
168
169 String
170 ValueNode_Linear::link_name(int i)const
171 {
172         assert(i==0 || i==1);
173         if(i==0)
174                 return "slope";
175         if(i==1)
176                 return "offset";
177         return String();
178 }
179
180 String
181 ValueNode_Linear::link_local_name(int i)const
182 {
183         assert(i==0 || i==1);
184         if(i==0)
185                 switch(get_type())
186                 {
187                 case ValueBase::TYPE_REAL:
188                 case ValueBase::TYPE_TIME:
189                 case ValueBase::TYPE_ANGLE:
190                         return _("Rate");
191                 default:
192                         return _("Slope");
193                 }
194         if(i==1)
195                 return _("Offset");
196         return String();
197 }
198
199 int
200 ValueNode_Linear::get_link_index_from_name(const String &name)const
201 {
202         if(name=="slope")
203                 return 0;
204         if(name=="offset")
205                 return 1;
206         
207         throw Exception::BadLinkName(name);
208 }
209
210 LinkableValueNode*
211 ValueNode_Linear::create_new()const
212 {
213         return new ValueNode_Linear(get_type());
214 }