Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / lyr_std / stretch.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file stretch.cpp
3 **      \brief Template Header
4 **
5 **      $Id: stretch.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #define SINFG_NO_ANGLE
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 <sinfg/string.h>
35 #include <sinfg/time.h>
36 #include <sinfg/context.h>
37 #include <sinfg/paramdesc.h>
38 #include <sinfg/renddesc.h>
39 #include <sinfg/surface.h>
40 #include <sinfg/value.h>
41 #include <sinfg/valuenode.h>
42 #include <sinfg/transform.h>
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace etl;
49 using namespace std;
50 using namespace sinfg;
51
52 /* === G L O B A L S ======================================================= */
53
54 SINFG_LAYER_INIT(Layer_Stretch);
55 SINFG_LAYER_SET_NAME(Layer_Stretch,"stretch");
56 SINFG_LAYER_SET_LOCAL_NAME(Layer_Stretch,_("Stretch"));
57 SINFG_LAYER_SET_CATEGORY(Layer_Stretch,_("Distortions"));
58 SINFG_LAYER_SET_VERSION(Layer_Stretch,"0.1");
59 SINFG_LAYER_SET_CVS_ID(Layer_Stretch,"$Id: stretch.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
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         );
103
104         ret.push_back(ParamDesc("center")
105                 .set_local_name(_("Center"))
106         );
107         
108         return ret;
109 }
110
111 sinfg::Layer::Handle
112 Layer_Stretch::hit_check(sinfg::Context context, const sinfg::Point &pos)const
113 {
114         Point npos(pos);
115         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
116         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
117         return context.hit_check(npos);
118 }
119
120 Color
121 Layer_Stretch::get_color(Context context, const Point &pos)const
122 {
123         Point npos(pos);
124         npos[0]=(npos[0]-center[0])/amount[0]+center[0];
125         npos[1]=(npos[1]-center[1])/amount[1]+center[1];
126         return context.get_color(npos);
127 }
128
129 class  Stretch_Trans : public Transform
130 {
131         etl::handle<const Layer_Stretch> layer;
132 public:
133         Stretch_Trans(const Layer_Stretch* x):Transform(x->get_guid()),layer(x) { }
134         
135         sinfg::Vector perform(const sinfg::Vector& x)const
136         {
137                 return Vector((x[0]-layer->center[0])*layer->amount[0]+layer->center[0],(x[1]-layer->center[1])*layer->amount[1]+layer->center[1]);
138         }
139         
140         sinfg::Vector unperform(const sinfg::Vector& x)const
141         {
142                 return Vector((x[0]-layer->center[0])/layer->amount[0]+layer->center[0],(x[1]-layer->center[1])/layer->amount[1]+layer->center[1]);
143         }
144 };
145 etl::handle<Transform>
146 Layer_Stretch::get_transform()const
147 {
148         return new Stretch_Trans(this);
149 }
150
151 bool
152 Layer_Stretch::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
153 {
154         RendDesc desc(renddesc);
155         desc.clear_flags();
156     // Adjust the top_left and bottom_right points
157         // for our zoom amount
158         Point npos;
159         npos[0]=(desc.get_tl()[0]-center[0])/amount[0]+center[0];
160         npos[1]=(desc.get_tl()[1]-center[1])/amount[1]+center[1];
161         desc.set_tl(npos);
162         npos[0]=(desc.get_br()[0]-center[0])/amount[0]+center[0];
163         npos[1]=(desc.get_br()[1]-center[1])/amount[1]+center[1];
164         desc.set_br(npos);
165
166         // Render the scene
167         return context.accelerated_render(surface,quality,desc,cb);
168 }