more updates
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_scale.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file valuenode_scale.cpp
3 **      \brief Template File
4 **
5 **      $Id: valuenode_scale.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 "general.h"
32 #include "valuenode_scale.h"
33 #include "valuenode_const.h"
34 #include <stdexcept>
35 #include <cassert>
36 #include "color.h"
37 #include "vector.h"
38 #include "time.h"
39 #include "angle.h"
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace sinfg;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 ValueNode_Scale::ValueNode_Scale():LinkableValueNode(sinfg::ValueBase::TYPE_NIL)
58 {
59         set_scalar(1.0);
60 }
61
62 ValueNode_Scale*
63 ValueNode_Scale::create(const ValueBase& x)
64 {
65         ValueNode_Scale* value_node;
66         switch(x.get_type())
67         {
68         case ValueBase::TYPE_VECTOR:
69         case ValueBase::TYPE_REAL:
70         case ValueBase::TYPE_TIME:
71         case ValueBase::TYPE_INTEGER:
72         case ValueBase::TYPE_ANGLE:
73         case ValueBase::TYPE_COLOR:
74                 value_node=new ValueNode_Scale();
75                 if(!value_node->set_value_node(ValueNode_Const::create(x)))
76                         return 0;
77                 assert(value_node->get_value_node()->get_type()==x.get_type());
78                 break;
79         default:
80                 assert(0);
81                 throw runtime_error("sinfg::ValueNode_Scale:Bad type "+ValueBase::type_name(x.get_type()));                     
82         }
83         assert(value_node);
84         assert(value_node->get_type()==x.get_type());
85         
86         return value_node;
87 }
88
89 LinkableValueNode*
90 ValueNode_Scale::create_new()const
91 {
92         return new ValueNode_Scale();
93 }
94
95 sinfg::ValueNode_Scale::~ValueNode_Scale()
96 {
97         unlink_all();
98 }
99
100 void
101 ValueNode_Scale::set_scalar(Real x)
102 {
103         set_link("scalar",ValueNode::Handle(ValueNode_Const::create(x)));
104 }
105
106 bool
107 ValueNode_Scale::set_scalar(const ValueNode::Handle &x)
108 {
109         if(!x
110                 || x->get_type()!=ValueBase::TYPE_REAL
111                 && !PlaceholderValueNode::Handle::cast_dynamic(x)
112         )
113                 return false;
114         scalar=x;
115         return true;
116 }
117
118 ValueNode::Handle
119 ValueNode_Scale::get_scalar()const
120 {
121         return scalar;
122 }
123
124 bool
125 ValueNode_Scale::set_value_node(const ValueNode::Handle &x)
126 {
127         if(!x
128                 || ( get_type()==ValueBase::TYPE_NIL
129                         && !check_type(x->get_type()) )
130                 || ( get_type()!=ValueBase::TYPE_NIL
131                         && x->get_type()!=get_type() ) &&
132                 !PlaceholderValueNode::Handle::cast_dynamic(x)
133         )
134                 return false;
135
136         assert(!(PlaceholderValueNode::Handle::cast_dynamic(x) && !get_type()));
137
138         value_node=x;
139
140         if(!get_type())
141                 set_type(x->get_type());
142
143         return true;
144 }
145
146 ValueNode::Handle
147 ValueNode_Scale::get_value_node()const
148 {
149         return value_node;
150 }
151
152
153 sinfg::ValueBase
154 sinfg::ValueNode_Scale::operator()(Time t)const
155 {
156         if(!value_node || !scalar)
157                 throw runtime_error(strprintf("ValueNode_Scale: %s",_("One or both of my parameters aren't set!")));
158         else
159         if(get_type()==ValueBase::TYPE_VECTOR)
160                 return (*value_node)(t).get(Vector())*(*scalar)(t).get(Real());
161         else
162         if(get_type()==ValueBase::TYPE_REAL)
163                 return (*value_node)(t).get(Real())*(*scalar)(t).get(Real());
164         else
165         if(get_type()==ValueBase::TYPE_TIME)
166                 return (*value_node)(t).get(Time())*(*scalar)(t).get(Time());
167         else
168         if(get_type()==ValueBase::TYPE_INTEGER)
169                 return (*value_node)(t).get(int())*(*scalar)(t).get(Real());
170         else
171         if(get_type()==ValueBase::TYPE_ANGLE)
172                 return (*value_node)(t).get(Angle())*(*scalar)(t).get(Real());
173         else
174         if(get_type()==ValueBase::TYPE_COLOR)
175         {
176                 Color ret((*value_node)(t).get(Color()));
177                 Real s((*scalar)(t).get(Real()));
178                 ret.set_r(ret.get_r()*s);
179                 ret.set_g(ret.get_r()*s);
180                 ret.set_b(ret.get_r()*s);
181                 return ret;
182         }
183
184         assert(0);
185         return ValueBase();
186 }
187
188
189 bool
190 ValueNode_Scale::set_link_vfunc(int i,ValueNode::Handle x)
191 {
192         if(!(i==0 || i==1))
193                 return false;
194         
195         if(i==0 && !set_value_node(x))
196                 return false;
197         else
198         if(i==1 && !set_scalar(x))
199                 return false;
200         
201         signal_child_changed()(i);signal_value_changed()();
202
203         return true;
204 }
205
206 ValueNode::LooseHandle
207 ValueNode_Scale::get_link_vfunc(int i)const
208 {
209         assert(i==0 || i==1);
210         if(i==0)
211                 return value_node;
212         else if(i==1)
213                 return scalar;
214         return 0;
215 }
216
217 int
218 ValueNode_Scale::link_count()const
219 {
220         return 2;
221 }
222
223 String
224 ValueNode_Scale::link_local_name(int i)const
225 {
226         assert(i==0 || i==1);
227         if(i==0)
228                 return _("Link");
229         else if(i==1)
230                 return _("Scalar");
231         return String();
232 }       
233
234 String
235 ValueNode_Scale::link_name(int i)const
236 {
237         assert(i==0 || i==1);
238         if(i==0)
239                 return "link";
240         else if(i==1)
241                 return "scalar";
242         return String();
243 }
244
245 int
246 ValueNode_Scale::get_link_index_from_name(const String &name)const
247 {
248         if(name=="link")
249                 return 0;
250         if(name=="scalar")
251                 return 1;
252         
253         throw Exception::BadLinkName(name);
254 }
255
256 String
257 ValueNode_Scale::get_name()const
258 {
259         return "scale";
260 }
261
262 String
263 ValueNode_Scale::get_local_name()const
264 {
265         return _("Scale");
266 }
267
268 bool
269 ValueNode_Scale::check_type(ValueBase::Type type)
270 {
271         return
272                 type==ValueBase::TYPE_VECTOR ||
273                 type==ValueBase::TYPE_REAL ||
274                 type==ValueBase::TYPE_INTEGER ||
275                 type==ValueBase::TYPE_COLOR ||
276                 type==ValueBase::TYPE_ANGLE;
277 }