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