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