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