Rename get_param_vocab to get_children_vocab and use a wrapper for the pure virtual...
[synfig.git] / synfig-core / src / synfig / valuenode_dotproduct.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_dotproduct.cpp
3 **      \brief Implementation of the "DotProduct" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 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_dotproduct.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_DotProduct::ValueNode_DotProduct(const ValueBase &value):
54         LinkableValueNode(value.get_type())
55 {
56         switch(value.get_type())
57         {
58         case ValueBase::TYPE_REAL:
59                 set_link("lhs",ValueNode_Const::create(Vector(value.get(Real()),0)));
60                 set_link("rhs",ValueNode_Const::create(Vector(1,0)));
61                 break;
62         case ValueBase::TYPE_ANGLE:
63                 set_link("lhs",ValueNode_Const::create(Vector(Angle::cos(value.get(Angle())).get(), Angle::sin(value.get(Angle())).get())));
64                 set_link("rhs",ValueNode_Const::create(Vector(1,0)));
65                 break;
66         default:
67                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
68         }
69 }
70
71 LinkableValueNode*
72 ValueNode_DotProduct::create_new()const
73 {
74         return new ValueNode_DotProduct(get_type());
75 }
76
77 ValueNode_DotProduct*
78 ValueNode_DotProduct::create(const ValueBase &x)
79 {
80         return new ValueNode_DotProduct(x);
81 }
82
83 ValueNode_DotProduct::~ValueNode_DotProduct()
84 {
85         unlink_all();
86 }
87
88 ValueBase
89 ValueNode_DotProduct::operator()(Time t)const
90 {
91         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
92                 printf("%s:%d operator()\n", __FILE__, __LINE__);
93
94         Vector lhs((*lhs_)(t).get(Vector()));
95         Vector rhs((*rhs_)(t).get(Vector()));
96
97         switch (get_type())
98         {
99         case ValueBase::TYPE_ANGLE:
100                 return Angle::cos(lhs * rhs / lhs.mag() / rhs.mag()).mod();
101         case ValueBase::TYPE_REAL:
102                 return lhs * rhs;
103         default:
104                 break;
105         }
106
107         assert(0);
108         return ValueBase();
109 }
110
111 String
112 ValueNode_DotProduct::get_name()const
113 {
114         return "dotproduct";
115 }
116
117 String
118 ValueNode_DotProduct::get_local_name()const
119 {
120         return _("Dot Product");
121 }
122
123 bool
124 ValueNode_DotProduct::set_link_vfunc(int i,ValueNode::Handle value)
125 {
126         assert(i>=0 && i<link_count());
127
128         switch(i)
129         {
130         case 0: CHECK_TYPE_AND_SET_VALUE(lhs_, ValueBase::TYPE_VECTOR);
131         case 1: CHECK_TYPE_AND_SET_VALUE(rhs_, ValueBase::TYPE_VECTOR);
132         }
133         return false;
134 }
135
136 ValueNode::LooseHandle
137 ValueNode_DotProduct::get_link_vfunc(int i)const
138 {
139         assert(i>=0 && i<link_count());
140
141         switch(i)
142         {
143         case 0: return lhs_;
144         case 1: return rhs_;
145         }
146
147         return 0;
148 }
149
150 int
151 ValueNode_DotProduct::link_count()const
152 {
153         return 2;
154 }
155
156 String
157 ValueNode_DotProduct::link_name(int i)const
158 {
159         assert(i>=0 && i<link_count());
160
161         switch(i)
162         {
163                 case 0: return "lhs";
164                 case 1: return "rhs";
165         }
166         return String();
167 }
168
169 String
170 ValueNode_DotProduct::link_local_name(int i)const
171 {
172         assert(i>=0 && i<link_count());
173
174         switch(i)
175         {
176                 case 0: return _("LHS");
177                 case 1: return _("RHS");
178         }
179         return String();
180 }
181
182 int
183 ValueNode_DotProduct::get_link_index_from_name(const String &name)const
184 {
185         if (name=="lhs") return 0;
186         if (name=="rhs") return 1;
187
188         throw Exception::BadLinkName(name);
189 }
190
191 bool
192 ValueNode_DotProduct::check_type(ValueBase::Type type)
193 {
194         return
195                 type==ValueBase::TYPE_ANGLE ||
196                 type==ValueBase::TYPE_REAL;
197 }
198
199 LinkableValueNode::Vocab
200 ValueNode_DotProduct::get_children_vocab_vfunc()const
201 {
202         LinkableValueNode::Vocab ret;
203
204         ret.push_back(ParamDesc(ValueBase(),"lhs")
205                 .set_local_name(_("LHS"))
206                 .set_description(_("The left side of the dot product"))
207         );
208
209         ret.push_back(ParamDesc(ValueBase(),"rhs")
210                 .set_local_name(_("RHS"))
211                 .set_description(_("The right side of the dot product"))
212         );
213
214         return ret;
215 }