Rearrange conditions for export action and fix bug 2956710
[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 }
72
73
74 bool
75 Layer_Stretch::set_param(const String & param, const ValueBase &value)
76 {
77         IMPORT(amount);
78         IMPORT(center);
79
80         return false;
81 }
82
83 ValueBase
84 Layer_Stretch::get_param(const String &param)const
85 {
86         EXPORT(amount);
87         EXPORT(center);
88
89         EXPORT_NAME();
90         EXPORT_VERSION();
91
92         return ValueBase();
93 }
94
95 Layer::Vocab
96 Layer_Stretch::get_param_vocab()const
97 {
98         Layer::Vocab ret;
99
100         ret.push_back(ParamDesc("amount")
101                 .set_local_name(_("Amount"))
102                 .set_origin("center")
103         );
104
105         ret.push_back(ParamDesc("center")
106                 .set_local_name(_("Center"))
107         );
108
109         return ret;
110 }
111
112 synfig::Layer::Handle
113 Layer_Stretch::hit_check(synfig::Context context, const synfig::Point &pos)const
114 {
115         Point npos(pos);
116         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
117         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
118         return context.hit_check(npos);
119 }
120
121 Color
122 Layer_Stretch::get_color(Context context, const Point &pos)const
123 {
124         Point npos(pos);
125         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
126         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
127         return context.get_color(npos);
128 }
129
130 class Stretch_Trans : public Transform
131 {
132         etl::handle<const Layer_Stretch> layer;
133 public:
134         Stretch_Trans(const Layer_Stretch* x):Transform(x->get_guid()),layer(x) { }
135
136         synfig::Vector perform(const synfig::Vector& x)const
137         {
138                 return Vector((x[0]-layer->center[0])*layer->amount[0]+layer->center[0],
139                                           (x[1]-layer->center[1])*layer->amount[1]+layer->center[1]);
140         }
141
142         synfig::Vector unperform(const synfig::Vector& x)const
143         {
144                 return Vector((x[0]-layer->center[0])/layer->amount[0]+layer->center[0],
145                                           (x[1]-layer->center[1])/layer->amount[1]+layer->center[1]);
146         }
147 };
148 etl::handle<Transform>
149 Layer_Stretch::get_transform()const
150 {
151         return new Stretch_Trans(this);
152 }
153
154 bool
155 Layer_Stretch::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
156 {
157         if (amount[0] == 0 || amount[1] == 0)
158         {
159                 surface->set_wh(renddesc.get_w(), renddesc.get_h());
160                 surface->clear();
161                 return true;
162         }
163
164         RendDesc desc(renddesc);
165         desc.clear_flags();
166     // Adjust the top_left and bottom_right points
167         // for our zoom amount
168         Point npos;
169         npos[0]=(desc.get_tl()[0]-center[0])/amount[0]+center[0];
170         npos[1]=(desc.get_tl()[1]-center[1])/amount[1]+center[1];
171         desc.set_tl(npos);
172         npos[0]=(desc.get_br()[0]-center[0])/amount[0]+center[0];
173         npos[1]=(desc.get_br()[1]-center[1])/amount[1]+center[1];
174         desc.set_br(npos);
175
176         // Render the scene
177         return context.accelerated_render(surface,quality,desc,cb);
178 }
179
180 Rect
181 Layer_Stretch::get_full_bounding_rect(Context context)const
182 {
183         Rect rect(context.get_full_bounding_rect());
184         Point min(rect.get_min()), max(rect.get_max());
185
186         return Rect(Point((min[0]-center[0])*amount[0]+center[0],
187                                           (min[1]-center[1])*amount[1]+center[1]),
188                                 Point((max[0]-center[0])*amount[0]+center[0],
189                                           (max[1]-center[1])*amount[1]+center[1]));
190 }