21a960f6f665325ed19844f4df51971856c574c8
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_sine.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_sine.cpp
3 **      \brief Template File
4 **
5 **      $Id$
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_sine.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_Sine::ValueNode_Sine(const ValueBase &value):
53         LinkableValueNode(value.get_type())
54 {
55         switch(value.get_type())
56         {
57         case ValueBase::TYPE_REAL:
58                 set_link("angle",ValueNode_Const::create(Angle::deg(90)));
59                 set_link("amp",ValueNode_Const::create(value.get(Real())));
60                 break;
61         default:
62                 throw Exception::BadType(ValueBase::type_name(value.get_type()));
63         }
64
65         DCAST_HACK_ENABLE();
66 }
67
68 LinkableValueNode*
69 ValueNode_Sine::create_new()const
70 {
71         return new ValueNode_Sine(get_type());
72 }
73
74 ValueNode_Sine*
75 ValueNode_Sine::create(const ValueBase &x)
76 {
77         return new ValueNode_Sine(x);
78 }
79
80 ValueNode_Sine::~ValueNode_Sine()
81 {
82         unlink_all();
83 }
84
85 ValueBase
86 ValueNode_Sine::operator()(Time t)const
87 {
88         return
89                 Angle::sin(
90                         (*angle_)(t).get(Angle())
91                 ).get() * (*amp_)(t).get(Real())
92         ;
93 }
94
95
96 String
97 ValueNode_Sine::get_name()const
98 {
99         return "sine";
100 }
101
102 String
103 ValueNode_Sine::get_local_name()const
104 {
105         return _("Sine");
106 }
107
108 bool
109 ValueNode_Sine::check_type(ValueBase::Type type)
110 {
111         return type==ValueBase::TYPE_REAL;
112 }
113
114 bool
115 ValueNode_Sine::set_link_vfunc(int i,ValueNode::Handle x)
116 {
117         assert(i==0 || i==1);
118         if(i==0)
119         {
120                 angle_=x;
121                 signal_child_changed()(i);signal_value_changed()();
122                 return true;
123         }
124         if(i==1)
125         {
126                 amp_=x;
127                 signal_child_changed()(i);signal_value_changed()();
128                 return true;
129         }
130         return false;
131 }
132
133 ValueNode::LooseHandle
134 ValueNode_Sine::get_link_vfunc(int i)const
135 {
136         assert(i==0 || i==1);
137         if(i==0)
138                 return angle_;
139         if(i==1)
140                 return amp_;
141
142         return 0;
143 }
144
145 int
146 ValueNode_Sine::link_count()const
147 {
148         return 2;
149 }
150
151 String
152 ValueNode_Sine::link_name(int i)const
153 {
154         assert(i==0 || i==1);
155         if(i==0)
156                 return "angle";
157         if(i==1)
158                 return "amp";
159         return String();
160 }
161
162 String
163 ValueNode_Sine::link_local_name(int i)const
164 {
165         assert(i==0 || i==1);
166         if(i==0)
167                 return _("Angle");
168         if(i==1)
169                 return _("Amplitude");
170         return String();
171 }
172
173 int
174 ValueNode_Sine::get_link_index_from_name(const String &name)const
175 {
176         if(name=="angle")
177                 return 0;
178         if(name=="amp")
179                 return 1;
180
181         throw Exception::BadLinkName(name);
182 }