Use LinkableValueNode members functions when possible in the derived valuenodes.
[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         Vocab ret(get_children_vocab());
57         set_children_vocab(ret);
58         switch(value.get_type())
59         {
60         case ValueBase::TYPE_REAL:
61                 set_link("lhs",ValueNode_Const::create(Vector(value.get(Real()),0)));
62                 set_link("rhs",ValueNode_Const::create(Vector(1,0)));
63                 break;
64         case ValueBase::TYPE_ANGLE:
65                 set_link("lhs",ValueNode_Const::create(Vector(Angle::cos(value.get(Angle())).get(), Angle::sin(value.get(Angle())).get())));
66                 set_link("rhs",ValueNode_Const::create(Vector(1,0)));
67                 break;
68         default:
69                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
70         }
71 }
72
73 LinkableValueNode*
74 ValueNode_DotProduct::create_new()const
75 {
76         return new ValueNode_DotProduct(get_type());
77 }
78
79 ValueNode_DotProduct*
80 ValueNode_DotProduct::create(const ValueBase &x)
81 {
82         return new ValueNode_DotProduct(x);
83 }
84
85 ValueNode_DotProduct::~ValueNode_DotProduct()
86 {
87         unlink_all();
88 }
89
90 ValueBase
91 ValueNode_DotProduct::operator()(Time t)const
92 {
93         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
94                 printf("%s:%d operator()\n", __FILE__, __LINE__);
95
96         Vector lhs((*lhs_)(t).get(Vector()));
97         Vector rhs((*rhs_)(t).get(Vector()));
98
99         switch (get_type())
100         {
101         case ValueBase::TYPE_ANGLE:
102                 return Angle::cos(lhs * rhs / lhs.mag() / rhs.mag()).mod();
103         case ValueBase::TYPE_REAL:
104                 return lhs * rhs;
105         default:
106                 break;
107         }
108
109         assert(0);
110         return ValueBase();
111 }
112
113 String
114 ValueNode_DotProduct::get_name()const
115 {
116         return "dotproduct";
117 }
118
119 String
120 ValueNode_DotProduct::get_local_name()const
121 {
122         return _("Dot Product");
123 }
124
125 bool
126 ValueNode_DotProduct::set_link_vfunc(int i,ValueNode::Handle value)
127 {
128         assert(i>=0 && i<link_count());
129
130         switch(i)
131         {
132         case 0: CHECK_TYPE_AND_SET_VALUE(lhs_, ValueBase::TYPE_VECTOR);
133         case 1: CHECK_TYPE_AND_SET_VALUE(rhs_, ValueBase::TYPE_VECTOR);
134         }
135         return false;
136 }
137
138 ValueNode::LooseHandle
139 ValueNode_DotProduct::get_link_vfunc(int i)const
140 {
141         assert(i>=0 && i<link_count());
142
143         switch(i)
144         {
145         case 0: return lhs_;
146         case 1: return rhs_;
147         }
148
149         return 0;
150 }
151
152 bool
153 ValueNode_DotProduct::check_type(ValueBase::Type type)
154 {
155         return
156                 type==ValueBase::TYPE_ANGLE ||
157                 type==ValueBase::TYPE_REAL;
158 }
159
160 LinkableValueNode::Vocab
161 ValueNode_DotProduct::get_children_vocab_vfunc()const
162 {
163         if(children_vocab.size())
164                 return children_vocab;
165
166         LinkableValueNode::Vocab ret;
167
168         ret.push_back(ParamDesc(ValueBase(),"lhs")
169                 .set_local_name(_("LHS"))
170                 .set_description(_("The left side of the dot product"))
171         );
172
173         ret.push_back(ParamDesc(ValueBase(),"rhs")
174                 .set_local_name(_("RHS"))
175                 .set_description(_("The right side of the dot product"))
176         );
177
178         return ret;
179 }