Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / src / synfig / valuenode_anglestring.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_anglestring.cpp
3 **      \brief Implementation of the "AngleString" 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_anglestring.h"
34 #include "valuenode_const.h"
35 #include "canvas.h"
36 #include "general.h"
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 ValueNode_AngleString::ValueNode_AngleString(const ValueBase &value):
55         LinkableValueNode(value.get_type())
56 {
57         Vocab ret(get_children_vocab());
58         set_children_vocab(ret);
59         switch(value.get_type())
60         {
61         case ValueBase::TYPE_STRING:
62                 set_link("angle",ValueNode_Const::create(Angle::deg(0)));
63                 set_link("width",ValueNode_Const::create(int(0)));
64                 set_link("precision",ValueNode_Const::create(int(3)));
65                 set_link("zero_pad",ValueNode_Const::create(bool(false)));
66                 break;
67         default:
68                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
69         }
70 }
71
72 LinkableValueNode*
73 ValueNode_AngleString::create_new()const
74 {
75         return new ValueNode_AngleString(get_type());
76 }
77
78 ValueNode_AngleString*
79 ValueNode_AngleString::create(const ValueBase &x)
80 {
81         return new ValueNode_AngleString(x);
82 }
83
84 ValueNode_AngleString::~ValueNode_AngleString()
85 {
86         unlink_all();
87 }
88
89 ValueBase
90 ValueNode_AngleString::operator()(Time t)const
91 {
92         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
93                 printf("%s:%d operator()\n", __FILE__, __LINE__);
94
95         Real angle(Angle::deg((*angle_)(t).get(Angle())).get());
96         int width((*width_)(t).get(int()));
97         int precision((*precision_)(t).get(int()));
98         int zero_pad((*zero_pad_)(t).get(bool()));
99
100         if(precision<0) precision=0;
101         switch (get_type())
102         {
103         case ValueBase::TYPE_STRING:
104                 return strprintf(strprintf("%%%s%d.%df",
105                                                                    zero_pad ? "0" : "",
106                                                                    width,
107                                                                    precision).c_str(), angle)+"°";
108         default:
109                 break;
110         }
111
112         assert(0);
113         return ValueBase();
114 }
115
116 String
117 ValueNode_AngleString::get_name()const
118 {
119         return "anglestring";
120 }
121
122 String
123 ValueNode_AngleString::get_local_name()const
124 {
125         return _("Angle String");
126 }
127
128 bool
129 ValueNode_AngleString::set_link_vfunc(int i,ValueNode::Handle value)
130 {
131         assert(i>=0 && i<link_count());
132
133         switch(i)
134         {
135         case 0: CHECK_TYPE_AND_SET_VALUE(angle_, ValueBase::TYPE_ANGLE);
136         case 1: CHECK_TYPE_AND_SET_VALUE(width_, ValueBase::TYPE_INTEGER);
137         case 2: CHECK_TYPE_AND_SET_VALUE(precision_, ValueBase::TYPE_INTEGER);
138         case 3: CHECK_TYPE_AND_SET_VALUE(zero_pad_, ValueBase::TYPE_BOOL);
139         }
140         return false;
141 }
142
143 ValueNode::LooseHandle
144 ValueNode_AngleString::get_link_vfunc(int i)const
145 {
146         assert(i>=0 && i<link_count());
147
148         switch(i)
149         {
150         case 0: return angle_;
151         case 1: return width_;
152         case 2: return precision_;
153         case 3: return zero_pad_;
154         }
155
156         return 0;
157 }
158
159 bool
160 ValueNode_AngleString::check_type(ValueBase::Type type)
161 {
162         return
163                 type==ValueBase::TYPE_STRING;
164 }
165
166 LinkableValueNode::Vocab
167 ValueNode_AngleString::get_children_vocab_vfunc()const
168 {
169         LinkableValueNode::Vocab ret;
170
171         ret.push_back(ParamDesc(ValueBase(),"angle")
172                 .set_local_name(_("Angle"))
173                 .set_description(_("Value to convert to string"))
174         );
175
176         ret.push_back(ParamDesc(ValueBase(),"width")
177                 .set_local_name(_("Width"))
178                 .set_description(_("Width of the string"))
179         );
180
181         ret.push_back(ParamDesc(ValueBase(),"precision")
182                 .set_local_name(_("Precision"))
183                 .set_description(_("Number of decimal places"))
184         );
185
186         ret.push_back(ParamDesc(ValueBase(),"zero_pad")
187                 .set_local_name(_("Zero Padded"))
188                 .set_description(_("When checked, the string is left filled with zeros to match the width"))
189         );
190
191         return ret;
192 }