Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / modules / mod_geometry / checkerboard.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file checkerboard.cpp
3 **      \brief Template Header
4 **
5 **      $Id: checkerboard.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "checkerboard.h"
33
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/segment.h>
43
44 #endif
45
46 using namespace synfig;
47 using namespace std;
48 using namespace etl;
49
50 /* === M A C R O S ========================================================= */
51
52 /* === G L O B A L S ======================================================= */
53
54 SYNFIG_LAYER_INIT(CheckerBoard);
55 SYNFIG_LAYER_SET_NAME(CheckerBoard,"checker_board");
56 SYNFIG_LAYER_SET_LOCAL_NAME(CheckerBoard,_("CheckerBoard"));
57 SYNFIG_LAYER_SET_CATEGORY(CheckerBoard,_("Geometry"));
58 SYNFIG_LAYER_SET_VERSION(CheckerBoard,"0.1");
59 SYNFIG_LAYER_SET_CVS_ID(CheckerBoard,"$Id: checkerboard.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 CheckerBoard::CheckerBoard():
66         Layer_Composite (1.0,Color::BLEND_STRAIGHT),
67         color                   (Color::black()),
68         pos                             (Point(0.125,0.125)),
69         size                    (Point(0.25,0.25))
70 {
71
72         set_blend_method(Color::BLEND_STRAIGHT);
73 }
74
75 inline bool
76 CheckerBoard::point_test(const synfig::Point& getpos)const
77 {
78         int val=((int)((getpos[0]-pos[0])/size[0])+(int)((getpos[1]-pos[1])/size[1]));
79         if(getpos[0]-pos[0] < 0.0)
80                 val++;
81         if(getpos[1]-pos[1] < 0.0)
82                 val++;
83         return val&1;
84 }
85
86 bool
87 CheckerBoard::set_param(const String &param, const ValueBase &value)
88 {
89         IMPORT(color);
90         IMPORT(pos);
91         IMPORT(pos[0]);
92         IMPORT(pos[1]);
93         IMPORT(size);
94         
95         return Layer_Composite::set_param(param,value);
96 }
97
98 ValueBase
99 CheckerBoard::get_param(const String &param)const
100 {
101         EXPORT(color);
102         EXPORT(pos);
103         EXPORT(pos[0]);
104         EXPORT(pos[1]); 
105         EXPORT(size);
106         EXPORT_NAME();
107         EXPORT_VERSION();
108                 
109         return Layer_Composite::get_param(param);       
110 }
111
112 Layer::Vocab
113 CheckerBoard::get_param_vocab()const
114 {
115         Layer::Vocab ret(Layer_Composite::get_param_vocab());
116         
117         ret.push_back(ParamDesc("color")
118                 .set_local_name(_("Color"))
119                 .set_description(_("Color of checkers"))
120         );
121         ret.push_back(ParamDesc("pos")
122                 .set_local_name(_("Offset"))
123         );
124         ret.push_back(ParamDesc("size")
125                 .set_local_name(_("Size"))
126                 .set_description(_("Size of checkers"))
127                 .set_origin("pos")
128         );
129
130         return ret;
131 }
132
133 synfig::Layer::Handle
134 CheckerBoard::hit_check(synfig::Context context, const synfig::Point &getpos)const
135 {
136         if(get_amount()!=0.0 && point_test(getpos))
137         {
138                 synfig::Layer::Handle tmp;
139                 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(getpos)))
140                         return tmp;
141                 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(getpos)))
142                         return 0;
143                 return const_cast<CheckerBoard*>(this);
144         }
145         else
146                 return context.hit_check(getpos);
147 }
148
149 Color
150 CheckerBoard::get_color(Context context, const Point &getpos)const
151 {
152         if(get_amount()!=0.0 && point_test(getpos))
153         {
154                 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
155                         return color;
156                 else
157                         return Color::blend(color,context.get_color(getpos),get_amount(),get_blend_method());
158         }
159         else
160                 return context.get_color(getpos);
161 }
162
163 bool
164 CheckerBoard::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
165 {
166         SuperCallback supercb(cb,0,9500,10000);
167
168         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
169                 return false;
170         if(get_amount()==0)
171                 return true;
172
173         int x,y;
174
175         const Point tl(renddesc.get_tl());
176         Point pos;
177         const int w(surface->get_w());
178         const int h(surface->get_h());
179         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
180
181         Surface::alpha_pen apen(surface->begin());
182
183         apen.set_alpha(get_amount());
184         apen.set_blend_method(get_blend_method());
185         apen.set_value(color);
186
187         for(y=0,pos[1]=tl[1];y<h;y++,apen.inc_y(),apen.dec_x(x),pos[1]+=ph)
188                 for(x=0,pos[0]=tl[0];x<w;x++,apen.inc_x(),pos[0]+=pw)
189                         if(point_test(pos))
190                                 apen.put_value();
191
192         // Mark our progress as finished
193         if(cb && !cb->amount_complete(10000,10000))
194                 return false;
195
196         return true;
197 }