65ae87fdc7c36ed94451742971cf1826adc64721
[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         );
106
107         ret.push_back(ParamDesc("center")
108                 .set_local_name(_("Center"))
109         );
110
111         return ret;
112 }
113
114 synfig::Layer::Handle
115 Layer_Stretch::hit_check(synfig::Context context, const synfig::Point &pos)const
116 {
117         Point npos(pos);
118         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
119         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
120         return context.hit_check(npos);
121 }
122
123 Color
124 Layer_Stretch::get_color(Context context, const Point &pos)const
125 {
126         Point npos(pos);
127         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
128         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
129         return context.get_color(npos);
130 }
131
132 class Stretch_Trans : public Transform
133 {
134         etl::handle<const Layer_Stretch> layer;
135 public:
136         Stretch_Trans(const Layer_Stretch* x):Transform(x->get_guid()),layer(x) { }
137
138         synfig::Vector perform(const synfig::Vector& x)const
139         {
140                 return Vector((x[0]-layer->center[0])*layer->amount[0]+layer->center[0],
141                                           (x[1]-layer->center[1])*layer->amount[1]+layer->center[1]);
142         }
143
144         synfig::Vector unperform(const synfig::Vector& x)const
145         {
146                 return Vector((x[0]-layer->center[0])/layer->amount[0]+layer->center[0],
147                                           (x[1]-layer->center[1])/layer->amount[1]+layer->center[1]);
148         }
149 };
150 etl::handle<Transform>
151 Layer_Stretch::get_transform()const
152 {
153         return new Stretch_Trans(this);
154 }
155
156 bool
157 Layer_Stretch::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
158 {
159         if (amount[0] == 0 || amount[1] == 0)
160         {
161                 surface->set_wh(renddesc.get_w(), renddesc.get_h());
162                 surface->clear();
163                 return true;
164         }
165
166         RendDesc desc(renddesc);
167         desc.clear_flags();
168     // Adjust the top_left and bottom_right points
169         // for our zoom amount
170         Point npos;
171         npos[0]=(desc.get_tl()[0]-center[0])/amount[0]+center[0];
172         npos[1]=(desc.get_tl()[1]-center[1])/amount[1]+center[1];
173         desc.set_tl(npos);
174         npos[0]=(desc.get_br()[0]-center[0])/amount[0]+center[0];
175         npos[1]=(desc.get_br()[1]-center[1])/amount[1]+center[1];
176         desc.set_br(npos);
177
178         // Render the scene
179         return context.accelerated_render(surface,quality,desc,cb);
180 }
181
182 Rect
183 Layer_Stretch::get_full_bounding_rect(Context context)const
184 {
185         Rect rect(context.get_full_bounding_rect());
186         Point min(rect.get_min()), max(rect.get_max());
187
188         return Rect(Point((min[0]-center[0])*amount[0]+center[0],
189                                           (min[1]-center[1])*amount[1]+center[1]),
190                                 Point((max[0]-center[0])*amount[0]+center[0],
191                                           (max[1]-center[1])*amount[1]+center[1]));
192 }