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