ceec7a77fcc76d888ccd05275a77e3ff7e82b647
[synfig.git] / synfig-core / src / synfig / valuenode_atan2.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_atan2.cpp
3 **      \brief Implementation of the "aTan2" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 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_atan2.h"
34 #include "valuenode_const.h"
35 #include "general.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_Atan2::ValueNode_Atan2(const ValueBase &value):
54         LinkableValueNode(value.get_type())
55 {
56         switch(value.get_type())
57         {
58         case ValueBase::TYPE_ANGLE:
59                 set_link("x",ValueNode_Const::create(Angle::cos(value.get(Angle())).get()));
60                 set_link("y",ValueNode_Const::create(Angle::sin(value.get(Angle())).get()));
61                 break;
62         default:
63                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
64         }
65 }
66
67 LinkableValueNode*
68 ValueNode_Atan2::create_new()const
69 {
70         return new ValueNode_Atan2(get_type());
71 }
72
73 ValueNode_Atan2*
74 ValueNode_Atan2::create(const ValueBase &x)
75 {
76         return new ValueNode_Atan2(x);
77 }
78
79 ValueNode_Atan2::~ValueNode_Atan2()
80 {
81         unlink_all();
82 }
83
84 ValueBase
85 ValueNode_Atan2::operator()(Time t)const
86 {
87         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
88                 printf("%s:%d operator()\n", __FILE__, __LINE__);
89
90         return Angle::tan((*y_)(t).get(Real()),
91                                           (*x_)(t).get(Real()));
92 }
93
94
95 String
96 ValueNode_Atan2::get_name()const
97 {
98         return "atan2";
99 }
100
101 String
102 ValueNode_Atan2::get_local_name()const
103 {
104         return _("aTan2");
105 }
106
107 bool
108 ValueNode_Atan2::check_type(ValueBase::Type type)
109 {
110         return type==ValueBase::TYPE_ANGLE;
111 }
112
113 bool
114 ValueNode_Atan2::set_link_vfunc(int i,ValueNode::Handle value)
115 {
116         assert(i>=0 && i<link_count());
117
118         switch(i)
119         {
120         case 0: CHECK_TYPE_AND_SET_VALUE(x_, ValueBase::TYPE_REAL);
121         case 1: CHECK_TYPE_AND_SET_VALUE(y_, ValueBase::TYPE_REAL);
122         }
123         return false;
124 }
125
126 ValueNode::LooseHandle
127 ValueNode_Atan2::get_link_vfunc(int i)const
128 {
129         assert(i>=0 && i<link_count());
130
131         if(i==0)
132                 return x_;
133         if(i==1)
134                 return y_;
135
136         return 0;
137 }
138
139 int
140 ValueNode_Atan2::link_count()const
141 {
142         return 2;
143 }
144
145 String
146 ValueNode_Atan2::link_name(int i)const
147 {
148         assert(i>=0 && i<link_count());
149
150         if(i==0)
151                 return "x";
152         if(i==1)
153                 return "y";
154         return String();
155 }
156
157 String
158 ValueNode_Atan2::link_local_name(int i)const
159 {
160         assert(i>=0 && i<link_count());
161
162         if(i==0)
163                 return _("X");
164         if(i==1)
165                 return _("Y");
166         return String();
167 }
168
169 int
170 ValueNode_Atan2::get_link_index_from_name(const String &name)const
171 {
172         if(name=="x")
173                 return 0;
174         if(name=="y")
175                 return 1;
176
177         throw Exception::BadLinkName(name);
178 }
179
180 LinkableValueNode::Vocab
181 ValueNode_Atan2::get_children_vocab_vfunc()const
182 {
183         LinkableValueNode::Vocab ret;
184
185         ret.push_back(ParamDesc(ValueBase(),"x")
186                 .set_local_name(_("X"))
187                 .set_description(_("Cosine of the angle"))
188         );
189
190         ret.push_back(ParamDesc(ValueBase(),"y")
191                 .set_local_name(_("Y"))
192                 .set_description(_("Sine of the angle"))
193         );
194
195         return ret;
196 }