Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc2 / src / modules / mod_geometry / checkerboard.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file checkerboard.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 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "checkerboard.h"
34
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
43 #include <synfig/segment.h>
44
45 #endif
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* === M A C R O S ========================================================= */
52
53 /* === G L O B A L S ======================================================= */
54
55 SYNFIG_LAYER_INIT(CheckerBoard);
56 SYNFIG_LAYER_SET_NAME(CheckerBoard,"checker_board");
57 SYNFIG_LAYER_SET_LOCAL_NAME(CheckerBoard,_("Checkerboard"));
58 SYNFIG_LAYER_SET_CATEGORY(CheckerBoard,_("Geometry"));
59 SYNFIG_LAYER_SET_VERSION(CheckerBoard,"0.1");
60 SYNFIG_LAYER_SET_CVS_ID(CheckerBoard,"$Id$");
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 CheckerBoard::CheckerBoard():
67         Layer_Composite (1.0,Color::BLEND_STRAIGHT),
68         color                   (Color::black()),
69         pos                             (Point(0.125,0.125)),
70         size                    (Point(0.25,0.25))
71 {
72
73         set_blend_method(Color::BLEND_STRAIGHT);
74 }
75
76 inline bool
77 CheckerBoard::point_test(const synfig::Point& getpos)const
78 {
79         int val=((int)((getpos[0]-pos[0])/size[0])+(int)((getpos[1]-pos[1])/size[1]));
80         if(getpos[0]-pos[0] < 0.0)
81                 val++;
82         if(getpos[1]-pos[1] < 0.0)
83                 val++;
84         return val&1;
85 }
86
87 bool
88 CheckerBoard::set_param(const String &param, const ValueBase &value)
89 {
90         IMPORT(color);
91         IMPORT(pos);
92         IMPORT(pos[0]);
93         IMPORT(pos[1]);
94         IMPORT(size);
95
96         return Layer_Composite::set_param(param,value);
97 }
98
99 ValueBase
100 CheckerBoard::get_param(const String &param)const
101 {
102         EXPORT(color);
103         EXPORT(pos);
104         EXPORT(pos[0]);
105         EXPORT(pos[1]);
106         EXPORT(size);
107         EXPORT_NAME();
108         EXPORT_VERSION();
109
110         return Layer_Composite::get_param(param);
111 }
112
113 Layer::Vocab
114 CheckerBoard::get_param_vocab()const
115 {
116         Layer::Vocab ret(Layer_Composite::get_param_vocab());
117
118         ret.push_back(ParamDesc("color")
119                 .set_local_name(_("Color"))
120                 .set_description(_("Color of checkers"))
121         );
122         ret.push_back(ParamDesc("pos")
123                 .set_local_name(_("Offset"))
124         );
125         ret.push_back(ParamDesc("size")
126                 .set_local_name(_("Size"))
127                 .set_description(_("Size of checkers"))
128                 .set_origin("pos")
129         );
130
131         return ret;
132 }
133
134 synfig::Layer::Handle
135 CheckerBoard::hit_check(synfig::Context context, const synfig::Point &getpos)const
136 {
137         if(get_amount()!=0.0 && point_test(getpos))
138         {
139                 synfig::Layer::Handle tmp;
140                 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(getpos)))
141                         return tmp;
142                 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(getpos)))
143                         return 0;
144                 return const_cast<CheckerBoard*>(this);
145         }
146         else
147                 return context.hit_check(getpos);
148 }
149
150 Color
151 CheckerBoard::get_color(Context context, const Point &getpos)const
152 {
153         if(get_amount()!=0.0 && point_test(getpos))
154         {
155                 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
156                         return color;
157                 else
158                         return Color::blend(color,context.get_color(getpos),get_amount(),get_blend_method());
159         }
160         else
161                 return context.get_color(getpos);
162 }
163
164 bool
165 CheckerBoard::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
166 {
167         SuperCallback supercb(cb,0,9500,10000);
168
169         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
170                 return false;
171         if(get_amount()==0)
172                 return true;
173
174         int x,y;
175
176         const Point tl(renddesc.get_tl());
177         Point pos;
178         const int w(surface->get_w());
179         const int h(surface->get_h());
180         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
181
182         Surface::alpha_pen apen(surface->begin());
183
184         apen.set_alpha(get_amount());
185         apen.set_blend_method(get_blend_method());
186         apen.set_value(color);
187
188         for(y=0,pos[1]=tl[1];y<h;y++,apen.inc_y(),apen.dec_x(x),pos[1]+=ph)
189                 for(x=0,pos[0]=tl[0];x<w;x++,apen.inc_x(),pos[0]+=pw)
190                         if(point_test(pos))
191                                 apen.put_value();
192
193         // Mark our progress as finished
194         if(cb && !cb->amount_complete(10000,10000))
195                 return false;
196
197         return true;
198 }