Use ancestor's link_local_name(int i), link_name(i) and get_link_index_from_name...
[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         switch(value.get_type())
58         {
59         case ValueBase::TYPE_STRING:
60                 set_link("angle",ValueNode_Const::create(Angle::deg(0)));
61                 set_link("width",ValueNode_Const::create(int(0)));
62                 set_link("precision",ValueNode_Const::create(int(3)));
63                 set_link("zero_pad",ValueNode_Const::create(bool(false)));
64                 break;
65         default:
66                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
67         }
68 }
69
70 LinkableValueNode*
71 ValueNode_AngleString::create_new()const
72 {
73         return new ValueNode_AngleString(get_type());
74 }
75
76 ValueNode_AngleString*
77 ValueNode_AngleString::create(const ValueBase &x)
78 {
79         return new ValueNode_AngleString(x);
80 }
81
82 ValueNode_AngleString::~ValueNode_AngleString()
83 {
84         unlink_all();
85 }
86
87 ValueBase
88 ValueNode_AngleString::operator()(Time t)const
89 {
90         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
91                 printf("%s:%d operator()\n", __FILE__, __LINE__);
92
93         Real angle(Angle::deg((*angle_)(t).get(Angle())).get());
94         int width((*width_)(t).get(int()));
95         int precision((*precision_)(t).get(int()));
96         int zero_pad((*zero_pad_)(t).get(bool()));
97
98         if(precision<0) precision=0;
99         switch (get_type())
100         {
101         case ValueBase::TYPE_STRING:
102                 return strprintf(strprintf("%%%s%d.%df",
103                                                                    zero_pad ? "0" : "",
104                                                                    width,
105                                                                    precision).c_str(), angle)+"°";
106         default:
107                 break;
108         }
109
110         assert(0);
111         return ValueBase();
112 }
113
114 String
115 ValueNode_AngleString::get_name()const
116 {
117         return "anglestring";
118 }
119
120 String
121 ValueNode_AngleString::get_local_name()const
122 {
123         return _("Angle String");
124 }
125
126 bool
127 ValueNode_AngleString::set_link_vfunc(int i,ValueNode::Handle value)
128 {
129         assert(i>=0 && i<link_count());
130
131         switch(i)
132         {
133         case 0: CHECK_TYPE_AND_SET_VALUE(angle_, ValueBase::TYPE_ANGLE);
134         case 1: CHECK_TYPE_AND_SET_VALUE(width_, ValueBase::TYPE_INTEGER);
135         case 2: CHECK_TYPE_AND_SET_VALUE(precision_, ValueBase::TYPE_INTEGER);
136         case 3: CHECK_TYPE_AND_SET_VALUE(zero_pad_, ValueBase::TYPE_BOOL);
137         }
138         return false;
139 }
140
141 ValueNode::LooseHandle
142 ValueNode_AngleString::get_link_vfunc(int i)const
143 {
144         assert(i>=0 && i<link_count());
145
146         switch(i)
147         {
148         case 0: return angle_;
149         case 1: return width_;
150         case 2: return precision_;
151         case 3: return zero_pad_;
152         }
153
154         return 0;
155 }
156
157 int
158 ValueNode_AngleString::link_count()const
159 {
160         return 4;
161 }
162
163 bool
164 ValueNode_AngleString::check_type(ValueBase::Type type)
165 {
166         return
167                 type==ValueBase::TYPE_STRING;
168 }
169
170 LinkableValueNode::Vocab
171 ValueNode_AngleString::get_param_vocab()const
172 {
173         LinkableValueNode::Vocab ret;
174
175         ret.push_back(ParamDesc(ValueBase(),"angle")
176                 .set_local_name(_("Angle"))
177                 .set_description(_("Value to convert to string"))
178         );
179
180         ret.push_back(ParamDesc(ValueBase(),"width")
181                 .set_local_name(_("Width"))
182                 .set_description(_("Width of the string"))
183         );
184
185         ret.push_back(ParamDesc(ValueBase(),"precision")
186                 .set_local_name(_("Precision"))
187                 .set_description(_("Number of decimal places"))
188         );
189
190         ret.push_back(ParamDesc(ValueBase(),"zero_pad")
191                 .set_local_name(_("Zero Padded"))
192                 .set_description(_("When checked, the string is left filled with zeros to match the width"))
193         );
194
195         return ret;
196 }