Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc1 / src / modules / lyr_std / stretch.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file stretch.cpp
3 **      \brief Template Header
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 #define SYNFIG_NO_ANGLE
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "stretch.h"
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44 #include <synfig/transform.h>
45
46 #endif
47
48 /* === U S I N G =========================================================== */
49
50 using namespace etl;
51 using namespace std;
52 using namespace synfig;
53
54 /* === G L O B A L S ======================================================= */
55
56 SYNFIG_LAYER_INIT(Layer_Stretch);
57 SYNFIG_LAYER_SET_NAME(Layer_Stretch,"stretch");
58 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Stretch,_("Stretch"));
59 SYNFIG_LAYER_SET_CATEGORY(Layer_Stretch,_("Distortions"));
60 SYNFIG_LAYER_SET_VERSION(Layer_Stretch,"0.1");
61 SYNFIG_LAYER_SET_CVS_ID(Layer_Stretch,"$Id$");
62
63 /* === P R O C E D U R E S ================================================= */
64
65 /* === M E T H O D S ======================================================= */
66
67 /* === E N T R Y P O I N T ================================================= */
68
69 Layer_Stretch::Layer_Stretch():
70         amount(1,1),
71         center(0,0)
72 {
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         );
105
106         ret.push_back(ParamDesc("center")
107                 .set_local_name(_("Center"))
108         );
109
110         return ret;
111 }
112
113 synfig::Layer::Handle
114 Layer_Stretch::hit_check(synfig::Context context, const synfig::Point &pos)const
115 {
116         Point npos(pos);
117         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
118         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
119         return context.hit_check(npos);
120 }
121
122 Color
123 Layer_Stretch::get_color(Context context, const Point &pos)const
124 {
125         Point npos(pos);
126         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
127         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
128         return context.get_color(npos);
129 }
130
131 class Stretch_Trans : public Transform
132 {
133         etl::handle<const Layer_Stretch> layer;
134 public:
135         Stretch_Trans(const Layer_Stretch* x):Transform(x->get_guid()),layer(x) { }
136
137         synfig::Vector perform(const synfig::Vector& x)const
138         {
139                 return Vector((x[0]-layer->center[0])*layer->amount[0]+layer->center[0],
140                                           (x[1]-layer->center[1])*layer->amount[1]+layer->center[1]);
141         }
142
143         synfig::Vector unperform(const synfig::Vector& x)const
144         {
145                 return Vector((x[0]-layer->center[0])/layer->amount[0]+layer->center[0],
146                                           (x[1]-layer->center[1])/layer->amount[1]+layer->center[1]);
147         }
148 };
149 etl::handle<Transform>
150 Layer_Stretch::get_transform()const
151 {
152         return new Stretch_Trans(this);
153 }
154
155 bool
156 Layer_Stretch::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
157 {
158         RendDesc desc(renddesc);
159         desc.clear_flags();
160     // Adjust the top_left and bottom_right points
161         // for our zoom amount
162         Point npos;
163         npos[0]=(desc.get_tl()[0]-center[0])/amount[0]+center[0];
164         npos[1]=(desc.get_tl()[1]-center[1])/amount[1]+center[1];
165         desc.set_tl(npos);
166         npos[0]=(desc.get_br()[0]-center[0])/amount[0]+center[0];
167         npos[1]=(desc.get_br()[1]-center[1])/amount[1]+center[1];
168         desc.set_br(npos);
169
170         // Render the scene
171         return context.accelerated_render(surface,quality,desc,cb);
172 }
173
174 Rect
175 Layer_Stretch::get_full_bounding_rect(Context context)const
176 {
177         Rect rect(context.get_full_bounding_rect());
178         Point min(rect.get_min()), max(rect.get_max());
179
180         return Rect(Point((min[0]-center[0])*amount[0]+center[0],
181                                           (min[1]-center[1])*amount[1]+center[1]),
182                                 Point((max[0]-center[0])*amount[0]+center[0],
183                                           (max[1]-center[1])*amount[1]+center[1]));
184 }