f6f842f354539c2c0da25bb50ac0a70d054bf438
[synfig.git] / synfig-core / trunk / src / modules / example / filledrect.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file filledrect.cpp
3 **      \brief Implementation of the "Rectangle" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 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 <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
41 #include <ETL/pen>
42
43 #include "filledrect.h"
44
45 #endif
46
47 /* === U S I N G =========================================================== */
48
49 using namespace etl;
50 using namespace std;
51 using namespace synfig;
52
53 /* === G L O B A L S ======================================================= */
54
55 SYNFIG_LAYER_INIT(FilledRect);
56 SYNFIG_LAYER_SET_NAME(FilledRect,"filled_rectangle");
57 SYNFIG_LAYER_SET_LOCAL_NAME(FilledRect,N_("Filled Rectangle"));
58 SYNFIG_LAYER_SET_CATEGORY(FilledRect,N_("Example"));
59 SYNFIG_LAYER_SET_VERSION(FilledRect,"0.1");
60 SYNFIG_LAYER_SET_CVS_ID(FilledRect,"$Id$");
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 /* === E N T R Y P O I N T ================================================= */
67
68 FilledRect::FilledRect():
69         Layer_Composite(1.0,Color::BLEND_STRAIGHT),
70         color(Color::black()),
71         point1(0,0),
72         point2(1,1),
73         feather_x(0),
74         feather_y(0),
75         bevel(0),
76         bevCircle(0)
77 {
78 }
79
80 bool
81 FilledRect::set_param(const String & param, const ValueBase &value)
82 {
83         IMPORT(color);
84         IMPORT(point1);
85         IMPORT(point2);
86         IMPORT_PLUS(feather_x, if(feather_x<0)feather_x=0;);
87         IMPORT_PLUS(feather_y, if(feather_y<0)feather_y=0;);
88         IMPORT(bevel);
89         IMPORT(bevCircle);
90
91         return Layer_Composite::set_param(param,value);
92 }
93
94 ValueBase
95 FilledRect::get_param(const String &param)const
96 {
97         EXPORT(color);
98         EXPORT(point1);
99         EXPORT(point2);
100         EXPORT(feather_x);
101         EXPORT(feather_y);
102         EXPORT(bevel);
103         EXPORT(bevCircle);
104
105         EXPORT_NAME();
106         EXPORT_VERSION();
107
108         return Layer_Composite::get_param(param);
109 }
110
111 Layer::Vocab
112 FilledRect::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         );
119
120         ret.push_back(ParamDesc("point1")
121                 .set_local_name(_("Point 1"))
122         );
123
124         ret.push_back(ParamDesc("point2")
125                 .set_local_name(_("Point 2"))
126         );
127
128         ret.push_back(ParamDesc("feather_x")
129                 .set_local_name(_("Feather X"))
130         );
131
132         ret.push_back(ParamDesc("feather_y")
133                 .set_local_name(_("Feather Y"))
134         );
135
136         ret.push_back(ParamDesc("bevel")
137                 .set_local_name(_("Bevel"))
138         );
139
140         ret.push_back(ParamDesc("bevCircle")
141                 .set_local_name(_("Keep Bevel Circular"))
142         );
143
144         return ret;
145 }
146
147 synfig::Layer::Handle
148 FilledRect::hit_check(synfig::Context context, const synfig::Point &point)const
149 {
150         Color   clr;
151         Real    amt;
152
153         if (!get_color(point,clr,amt))
154                 return context.hit_check(point);
155
156         synfig::Layer::Handle tmp;
157
158         if (get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
159                 return tmp;
160
161         if (Color::is_onto(get_blend_method()) && !(context.hit_check(point)))
162                 return 0;
163
164         return const_cast<FilledRect*>(this);
165 }
166
167 bool
168 FilledRect::get_color(const Point &pos, Color &out, Real &outamount) const
169 {
170         Point p[2] = {point1,point2};
171         Real swap;
172
173         if(p[0][0] > p[1][0])
174         {
175                 swap = p[0][0];
176                 p[0][0] = p[1][0];
177                 p[1][0] = swap;
178         }
179
180         if(p[0][1] > p[1][1])
181         {
182                 swap = p[0][1];
183                 p[0][1] = p[1][1];
184                 p[1][1] = swap;
185         }
186
187         /*
188         p[0][0] -= feather_x;
189         p[1][0] += feather_x;
190         p[0][1] -= feather_y;
191         p[1][1] += feather_y;*/
192         const Real      w = p[1][0] - p[0][0];
193         const Real      h = p[1][1] - p[0][1];
194
195         if(pos[0] >= p[0][0] && pos[0] <= p[1][0] && pos[1] >= p[0][1] && pos[1] <= p[1][1])
196         {
197                 Real value = 1;
198
199                 if(feather_x > 0)
200                 {
201                         Real xdist = pos[0] - p[0][0];
202                         xdist = min(xdist,p[1][0]-pos[0]);
203
204                         if(xdist < feather_x)
205                         {
206                                 value = xdist/feather_x;
207                         }
208                 }
209
210                 if(feather_y > 0)
211                 {
212                         Real ydist = pos[1]-p[0][1];
213                         ydist = min(ydist,p[1][1]-pos[1]);
214
215                         if(ydist < feather_y)
216                         {
217                                 value = min(value,(ydist/feather_y));
218                         }
219                 }
220
221                 //if we're beveled then check with ellipse code...
222                 if(bevel > 0)
223                 {
224                         const Real bev = (bevel > 1) ? 1 : bevel;
225                         const Real bevx = bevCircle ? min(w*bev/2,h*bev/2) : w*bev/2;
226                         const Real bevy = bevCircle ? min(w*bev/2,h*bev/2) : h*bev/2;;
227
228                         Vector v(0,0);
229                         bool    in = false;
230
231                         //based on which quarter it is in (and because it's x/y symmetric) get a positive vector (x/y)
232                         if(pos[0] < p[0][0] + bevx)
233                         {
234                                  if(pos[1] < p[0][1] + bevy)
235                                  {
236                                          v[0] = p[0][0] + bevx - pos[0];
237                                          v[1] = p[0][1] + bevy - pos[1];
238                                          in = true;
239                                  }else if(pos[1] > p[1][1] - bevy)
240                                  {
241                                          v[0] = p[0][0] + bevx - pos[0];
242                                          v[1] = pos[1] - (p[1][1] - bevy);
243                                          in = true;
244                                  }
245                         }
246                         else if(pos[0] > p[1][0] - bevx)
247                         {
248                                 if(pos[1] < p[0][1] + bevy)
249                                  {
250                                          v[0] = pos[0] - (p[1][0] - bevx);
251                                          v[1] = p[0][1] + bevy - pos[1];
252                                          in = true;
253                                  }else if(pos[1] > p[1][1] - bevy)
254                                  {
255                                          v[0] = pos[0] - (p[1][0] - bevx);
256                                          v[1] = pos[1] - (p[1][1] - bevy);
257                                          in = true;
258                                  }
259                         }
260
261                         //if it's inside a bevelled block
262                         if(in)
263                         {
264                                 const Vector scale(bevx,bevy);
265
266                                 Vector vc(v[0]/scale[0],v[1]/scale[1]);
267                                 Real    d = vc.mag();
268
269                                 //if it's inside the ellipse
270                                 if(d < 1)
271                                 {
272                                         Real val = atan2(vc[1],vc[0]);
273                                         val /= (PI/2); //< will always be (0,pi/2) because both components are positive
274
275                                         Real fthx=1,fthy=1;
276
277                                         //change d into distance away from edge
278                                         d = 1 - d;
279
280                                         if(feather_x > 0)
281                                         {
282                                                 if(scale[0] < feather_x)
283                                                 {
284                                                         fthy = scale[0]/feather_x;
285                                                 }
286
287                                                 if(d*scale[0] < feather_x)
288                                                 {
289                                                         fthx = d*scale[0]/feather_x;
290                                                 }
291                                         }
292
293                                         if(feather_y > 0)
294                                         {
295                                                 if(scale[1] < feather_y)
296                                                 {
297                                                         fthx = min(fthx,scale[1]/feather_y);
298                                                 }
299
300                                                 if(d*scale[1] < feather_y)
301                                                 {
302                                                         fthy = min(fthy,d*scale[1]/feather_y);
303                                                 }
304                                         }
305
306                                         //interpolate
307                                         outamount = min(value,((1-val)*fthx + val*fthy)) * get_amount();
308                                         out = color;
309                                         return true;
310
311                                 }else return false;
312                         }
313                 }
314
315                 outamount = value * get_amount();
316                 out = color;
317
318                 return true;
319         }else
320                 return false;
321 }
322
323 Color
324 FilledRect::get_color(Context context, const Point &pos)const
325 {
326         Color   clr;
327         Real    amt;
328
329         if(get_color(pos,clr,amt))
330         {
331                 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
332                         return clr;
333                 else
334                         return Color::blend(clr,context.get_color(pos),amt,get_blend_method());
335         }
336         else
337                 return context.get_color(pos);
338 }
339
340 bool
341 FilledRect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
342 {
343         // Width and Height of a pixel
344         const Point br(renddesc.get_br()), tl(renddesc.get_tl());
345         const int w = renddesc.get_w(), h = renddesc.get_h();
346
347         Real    wpp = (br[0]-tl[0])/w;
348         Real    hpp = (br[1]-tl[1])/h;
349         //const Real    xneg = wpp<0?-1:1;
350         //const Real    yneg = hpp<0?-1:1;
351
352         //the bounds of the rectangle
353         Point p[2] = {point1,point2};
354
355         if((p[0][0] > p[1][0]) ^ (wpp < 0))
356         {
357                 swap(p[0][0],p[1][0]);
358         }
359
360         if((p[0][1] > p[1][1]) ^ (hpp < 0))
361         {
362                 swap(p[0][1],p[1][1]);
363         }
364
365         /*p[0][0] -= xneg*feather_x;
366         p[1][0] += xneg*feather_x;
367         p[0][1] -= yneg*feather_y;
368         p[1][1] += yneg*feather_y;*/
369
370         //the integer coordinates
371         int y_start = (int)((p[0][1] - tl[1])/hpp +.5);         //round start up
372         int x_start = (int)((p[0][0] - tl[0])/wpp +.5);
373         int y_end = (int)((p[1][1] - tl[1])/hpp +.5);   //and ends up
374         int x_end =     (int)((p[1][0] - tl[0])/wpp +.5);
375
376         y_start = max(0,y_start);
377         x_start = max(0,x_start);
378         y_end = min(h,y_end);
379         x_end = min(w,x_end);
380
381         SuperCallback supercb(cb,0,9000,10000);
382
383         if(y_start >= h || x_start > w  || x_end < 0 || y_end < 0)
384         {
385                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
386                 {
387                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
388                         return false;
389                 }
390
391                 return true;
392         }
393
394         Real xf_start = tl[0] + x_start*wpp;
395         Point pos(xf_start,tl[1] + y_start*hpp);
396
397         Color   clr = Color::black();
398         Real    amt;
399
400         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
401         {
402                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
403                 return false;
404         }
405
406         for(int y = y_start; y < y_end; y++, pos[1] += hpp)
407         {
408                 pos[0] = xf_start;
409                 for(int x = x_start; x < x_end; x++, pos[0] += wpp)
410                 {
411                         if(get_color(pos,clr,amt))
412                         {
413                                 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
414                                         (*surface)[y][x] = clr;
415                                 else
416                                         (*surface)[y][x] = Color::blend(clr,(*surface)[y][x],amt,get_blend_method());
417                         }
418                 }
419         }
420
421         return true;
422
423 #if     0
424         //faster version but much more complex
425         //the floating point
426         Real y1,y2,y3;
427         Real x1,x2,x3;
428         Real dx1,dx2; //reversed in 3rd y section, not used in 2nd
429
430         //the transparent
431         Real fx = 0, fy = 0;
432         Real dfx,dfy;
433
434         //Get the slopes of the lines we need to worry about
435         if(feather_x)
436         {
437                 dfx = 1/(fxsize);
438
439                 if(fxsize*2 > xfw)
440                 {
441                         x1 = xfw/2;
442                         x2 = 0;
443                         x3 = xfw;
444                 }else
445                 {
446                         x1 = fxsize;
447                         x2 = xfw - fxsize;
448                         x3 = xfw;
449                 }
450         }else
451         {
452                 fx = 1;
453                 dfx = 0;
454                 x1=0;
455                 x2=xfw;
456                 x3=0;
457         }
458
459         if(feather_y)
460         {
461                 dfy = 1/(fysize);
462
463                 if(fysize*2 > yfh)
464                 {
465                         y1 = yfh/2;
466                         y2 = 0;
467                         y3 = yfh;
468                 }else
469                 {
470                         y1 = fysize;
471                         y2 = yfh - fysize;
472                         y3 = yfh;
473                 }
474
475                 dx1 = ph*feather_x/feather_y;
476                 dx2 = -2*dx1;
477
478         }else
479         {
480                 fy = 1;
481                 dfy = 0;
482                 y1=0;
483                 y2=yfh;
484                 y3=0;
485         }
486
487         fy = yf*dfy;
488
489         int x,y;
490         Real value = 0;
491         SuperCallback supercb(cb,0,9000,10000);
492         Surface::pen    p;
493
494         Real tx1 = 0,tx2 =
495         for(y = y_start;yf < y1; y++,yf++,fy+=dfy, tx1+=dx1)
496         {
497                 fx = xf*dfx;
498
499                 p = surface->get_pen(x_start,y);
500                 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
501                 {
502                         //we are in the x portion... use fx
503                         value = fx*get_amount();
504                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
505                                 p.put_value(color);
506                         else
507                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
508                 }
509
510                 for(;xf < x2; xf++,p.inc_x())
511                 {
512                         //we are now in y... use fy
513                         value = fy*get_amount();
514                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
515                                 p.put_value(color);
516                         else
517                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
518                 }
519
520                 fx = xfw?(xfw - xf)/xfw:1;
521                 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
522                 {
523                         //we are in the x portion... use fx
524                         value = max(0,fx*get_amount());
525                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
526                                 p.put_value(color);
527                         else
528                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
529                 }
530         }
531
532         x1 =
533         for(;fy < 1.0; y++,yf++,fy+=dfy)
534         {
535                 fx = xf*dfx;
536
537                 p = surface->get_pen(x_start,y);
538                 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
539                 {
540                         //we are in the x portion... use fx
541                         value = fx*get_amount();
542                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
543                                 p.put_value(color);
544                         else
545                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
546                 }
547
548                 for(;xf < x2; xf++,p.inc_x())
549                 {
550                         //we are now in y... use fy
551                         value = fy*get_amount();
552                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
553                                 p.put_value(color);
554                         else
555                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
556                 }
557
558                 fx = xfw?(xfw - xf)/xfw:1;
559                 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
560                 {
561                         //we are in the x portion... use fx
562                         value = max(0,fx*get_amount());
563                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
564                                 p.put_value(color);
565                         else
566                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
567                 }
568         }
569
570         for()
571         {
572                 for(int x = x_start; x < x_end; x++)
573                 {
574
575                 }
576         }
577
578 #endif
579
580         // Mark our progress as finished
581         if(cb && !cb->amount_complete(10000,10000))
582                 return false;
583
584         return true;
585 }