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