Set the description of some parameters
[synfig.git] / synfig-core / src / modules / lyr_std / stretch.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file stretch.cpp
3 **      \brief Implementation of the "Stretch" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 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 "stretch.h"
34 #include <synfig/string.h>
35 #include <synfig/time.h>
36 #include <synfig/context.h>
37 #include <synfig/paramdesc.h>
38 #include <synfig/renddesc.h>
39 #include <synfig/surface.h>
40 #include <synfig/value.h>
41 #include <synfig/valuenode.h>
42 #include <synfig/transform.h>
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace etl;
49 using namespace std;
50 using namespace synfig;
51
52 /* === G L O B A L S ======================================================= */
53
54 SYNFIG_LAYER_INIT(Layer_Stretch);
55 SYNFIG_LAYER_SET_NAME(Layer_Stretch,"stretch");
56 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Stretch,N_("Stretch"));
57 SYNFIG_LAYER_SET_CATEGORY(Layer_Stretch,N_("Distortions"));
58 SYNFIG_LAYER_SET_VERSION(Layer_Stretch,"0.1");
59 SYNFIG_LAYER_SET_CVS_ID(Layer_Stretch,"$Id$");
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65 /* === E N T R Y P O I N T ================================================= */
66
67 Layer_Stretch::Layer_Stretch():
68         amount(1,1),
69         center(0,0)
70 {
71         Layer::Vocab voc(get_param_vocab());
72         Layer::fill_static(voc);
73 }
74
75
76 bool
77 Layer_Stretch::set_param(const String & param, const ValueBase &value)
78 {
79         IMPORT(amount);
80         IMPORT(center);
81
82         return false;
83 }
84
85 ValueBase
86 Layer_Stretch::get_param(const String &param)const
87 {
88         EXPORT(amount);
89         EXPORT(center);
90
91         EXPORT_NAME();
92         EXPORT_VERSION();
93
94         return ValueBase();
95 }
96
97 Layer::Vocab
98 Layer_Stretch::get_param_vocab()const
99 {
100         Layer::Vocab ret;
101
102         ret.push_back(ParamDesc("amount")
103                 .set_local_name(_("Amount"))
104                 .set_origin("center")
105                 .set_description(_("Size of the stretch relative to its Center"))
106         );
107
108         ret.push_back(ParamDesc("center")
109                 .set_local_name(_("Center"))
110                 .set_description(_("Where the stretch distortion is centered"))
111         );
112
113         return ret;
114 }
115
116 synfig::Layer::Handle
117 Layer_Stretch::hit_check(synfig::Context context, const synfig::Point &pos)const
118 {
119         Point npos(pos);
120         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
121         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
122         return context.hit_check(npos);
123 }
124
125 Color
126 Layer_Stretch::get_color(Context context, const Point &pos)const
127 {
128         Point npos(pos);
129         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
130         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
131         return context.get_color(npos);
132 }
133
134 class Stretch_Trans : public Transform
135 {
136         etl::handle<const Layer_Stretch> layer;
137 public:
138         Stretch_Trans(const Layer_Stretch* x):Transform(x->get_guid()),layer(x) { }
139
140         synfig::Vector perform(const synfig::Vector& x)const
141         {
142                 return Vector((x[0]-layer->center[0])*layer->amount[0]+layer->center[0],
143                                           (x[1]-layer->center[1])*layer->amount[1]+layer->center[1]);
144         }
145
146         synfig::Vector unperform(const synfig::Vector& x)const
147         {
148                 return Vector((x[0]-layer->center[0])/layer->amount[0]+layer->center[0],
149                                           (x[1]-layer->center[1])/layer->amount[1]+layer->center[1]);
150         }
151 };
152 etl::handle<Transform>
153 Layer_Stretch::get_transform()const
154 {
155         return new Stretch_Trans(this);
156 }
157
158 bool
159 Layer_Stretch::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
160 {
161         if (amount[0] == 0 || amount[1] == 0)
162         {
163                 surface->set_wh(renddesc.get_w(), renddesc.get_h());
164                 surface->clear();
165                 return true;
166         }
167
168         RendDesc desc(renddesc);
169         desc.clear_flags();
170     // Adjust the top_left and bottom_right points
171         // for our zoom amount
172         Point npos;
173         npos[0]=(desc.get_tl()[0]-center[0])/amount[0]+center[0];
174         npos[1]=(desc.get_tl()[1]-center[1])/amount[1]+center[1];
175         desc.set_tl(npos);
176         npos[0]=(desc.get_br()[0]-center[0])/amount[0]+center[0];
177         npos[1]=(desc.get_br()[1]-center[1])/amount[1]+center[1];
178         desc.set_br(npos);
179
180         // Render the scene
181         return context.accelerated_render(surface,quality,desc,cb);
182 }
183
184 Rect
185 Layer_Stretch::get_full_bounding_rect(Context context)const
186 {
187         Rect rect(context.get_full_bounding_rect());
188         Point min(rect.get_min()), max(rect.get_max());
189
190         return Rect(Point((min[0]-center[0])*amount[0]+center[0],
191                                           (min[1]-center[1])*amount[1]+center[1]),
192                                 Point((max[0]-center[0])*amount[0]+center[0],
193                                           (max[1]-center[1])*amount[1]+center[1]));
194 }