1 /* === S I N F G =========================================================== */
2 /*! \file rectangle.cpp
3 ** \point2ief Template Header
5 ** $Id: rectangle.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quatpoint1ebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include <sinfg/string.h>
32 #include <sinfg/time.h>
33 #include <sinfg/context.h>
34 #include <sinfg/paramdesc.h>
35 #include <sinfg/renddesc.h>
36 #include <sinfg/surface.h>
37 #include <sinfg/value.h>
38 #include <sinfg/valuenode.h>
42 #include "rectangle.h"
46 /* === U S I N G =========================================================== */
50 using namespace sinfg;
52 /* === G L O B A L S ======================================================= */
54 SINFG_LAYER_INIT(Rectangle);
55 SINFG_LAYER_SET_NAME(Rectangle,"rectangle");
56 SINFG_LAYER_SET_LOCAL_NAME(Rectangle,_("Rectangle"));
57 SINFG_LAYER_SET_CATEGORY(Rectangle,_("Geometry"));
58 SINFG_LAYER_SET_VERSION(Rectangle,"0.2");
59 SINFG_LAYER_SET_CVS_ID(Rectangle,"$Id: rectangle.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $");
61 /* === P R O C E D U R E S ================================================= */
64 inline int ceil_to_int(const float x) { return static_cast<int>(ceil(x)); }
65 inline int ceil_to_int(const double x) { return static_cast<int>(ceil(x)); }
67 inline int floor_to_int(const float x) { return static_cast<int>(floor(x)); }
68 inline int floor_to_int(const double x) { return static_cast<int>(floor(x)); }
71 /* === M E T H O D S ======================================================= */
73 /* === E N T R Y P O I N T ================================================= */
75 Rectangle::Rectangle():
76 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
77 color(Color::black()),
86 Rectangle::set_param(const String & param, const ValueBase &value)
94 return Layer_Composite::set_param(param,value);
98 Rectangle::get_param(const String ¶m)const
109 return Layer_Composite::get_param(param);
113 Rectangle::get_param_vocab()const
115 Layer::Vocab ret(Layer_Composite::get_param_vocab());
117 ret.push_back(ParamDesc("color")
118 .set_local_name(_("Color"))
121 ret.push_back(ParamDesc("point1")
122 .set_local_name(_("Point 1"))
126 ret.push_back(ParamDesc("point2")
127 .set_local_name(_("Point 2"))
130 ret.push_back(ParamDesc("expand")
132 .set_local_name(_("Expand amount"))
135 ret.push_back(ParamDesc("invert")
136 .set_local_name(_("Invert the rectangle"))
143 Rectangle::hit_check(sinfg::Context context, const sinfg::Point &pos)const
146 return context.hit_check(pos);
150 max[0]=std::max(point1[0],point2[0])+expand;
151 max[1]=std::max(point1[1],point2[1])+expand;
152 min[0]=std::min(point1[0],point2[0])-expand;
153 min[1]=std::min(point1[1],point2[1])-expand;
155 bool intersect(false);
157 if( pos[0]<max[0] && pos[0]>min[0] &&
158 pos[1]<max[1] && pos[1]>min[1] )
164 intersect=!intersect;
168 sinfg::Layer::Handle tmp;
169 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(pos)))
171 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(pos)))
173 return const_cast<Rectangle*>(this);
176 return context.hit_check(pos);
180 Rectangle::get_color(Context context, const Point &pos)const
183 return context.get_color(pos);
187 max[0]=std::max(point1[0],point2[0])+expand;
188 max[1]=std::max(point1[1],point2[1])+expand;
189 min[0]=std::min(point1[0],point2[0])-expand;
190 min[1]=std::min(point1[1],point2[1])-expand;
192 /**************************
193 // This is darco's old-old-old feathered box code
194 // it produces really nice feathered edges
197 if( pos[0]<=max[0]-feather/2.0 && pos[0]>=min[0]+feather/2.0 &&
198 pos[1]<=max[1]-feather/2.0 && pos[1]>=min[1]+feather/2.0 )
201 return (*context).GetColor(context,pos);
206 if( pos[0]>=max[0]+feather/2.0 || pos[0]<=min[0]-feather/2.0 ||
207 pos[1]>=max[1]+feather/2.0 || pos[1]<=min[1]-feather/2.0 )
212 return (*context).GetColor(context,pos);
215 Color::unit alpha=1000000;
216 Color::unit alpha2=1000000;
218 if(max[0]-pos[0]+feather/2.0<alpha)
219 alpha=max[0]-pos[0]+feather/2.0;
220 if(pos[0]-min[0]+feather/2.0<alpha)
221 alpha=pos[0]-min[0]+feather/2.0;
223 if(max[1]-pos[1]+feather/2.0<alpha2)
224 alpha2=max[1]-pos[1]+feather/2.0;
225 if(pos[1]-min[1]+feather/2.0<alpha2)
226 alpha2=pos[1]-min[1]+feather/2.0;
229 if(alpha<=feather && alpha2<=feather)
232 alpha2=feather-alpha2;
234 alpha=sqrt(alpha*alpha+alpha2*alpha2);
241 return (*context).GetColor(context,pos);
248 alpha=(alpha<alpha2)?alpha:alpha2;
256 return Color::blend(color,context.get_color(pos),alpha,get_blend_method());
261 if( pos[0]<max[0] && pos[0]>min[0] &&
262 pos[1]<max[1] && pos[1]>min[1] )
265 return context.get_color(pos);
271 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
281 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
284 return context.get_color(pos);
288 Rectangle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
291 return context.accelerated_render(surface,quality,renddesc,cb);
293 const Point tl(renddesc.get_tl());
294 const Point br(renddesc.get_br());
296 const int w(renddesc.get_w());
297 const int h(renddesc.get_h());
299 // Width and Height of a pixel
300 const Real pw = (br[0] - tl[0]) / w;
301 const Real ph = (br[1] - tl[1]) / h;
303 Point max(point1),min(point2);
312 max=context.get_bounding_rect().get_max();
313 min=context.get_bounding_rect().get_min();
317 max=context.get_full_bounding_rect().get_max();
318 min=context.get_full_bounding_rect().get_min();
326 if((min[0] > max[0]) ^ (pw < 0))swap(min[0],max[0]);
327 if((min[1] > max[1]) ^ (ph < 0))swap(min[1],max[1]);
353 int left(floor_to_int((min[0]-tl[0])/pw));
354 int right(ceil_to_int((max[0]-tl[0])/pw));
355 int top(floor_to_int((min[1]-tl[1])/ph));
356 int bottom(ceil_to_int((max[1]-tl[1])/ph));
358 float left_edge((min[0]-tl[0])/pw-float(left));
359 float right_edge(float(right)-(max[0]-tl[0])/pw);
360 float top_edge((min[1]-tl[1])/ph-float(top));
361 float bottom_edge(float(bottom)-(max[1]-tl[1])/ph);
363 if(top<0)top=0,top_edge=0;
364 if(left<0)left=0,left_edge=0;
365 if(bottom>h)bottom=h,bottom_edge=0;
366 if(right>w)right=w,right_edge=0;
371 RendDesc desc(renddesc);
374 //fill the surface with the background color initially
375 surface->set_wh(w,h);
376 surface->fill(color);
378 // Check for the case where there is nothing to render
379 if(right-left<=0||bottom-top<=0)
382 desc.set_subwindow(left,top,right-left,bottom-top);
384 // Render what is behind us
385 if(!context.accelerated_render(&subimage,quality,desc,cb))
387 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
391 Surface::pen pen(surface->get_pen(left,top));
393 subimage.blit_to(pen);
397 if(!context.accelerated_render(surface,quality,renddesc,cb))
399 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
404 // Check for the case where there is nothing to render
405 if(!(right-left<=0||bottom-top<=0))
407 subimage.set_wh(right-left,bottom-top);
408 Surface::pen subimage_pen(subimage.begin());
409 surface->blit_to(subimage_pen,left,top,right-left,bottom-top);
412 Surface::alpha_pen surface_pen(surface->begin(),get_amount(),get_blend_method());
414 surface->fill(color,surface_pen,w,h);
418 Surface::pen pen(surface->get_pen(left,top));
419 subimage.blit_to(pen);
423 Surface::alpha_pen pen;
425 if(bottom-1>=0 && bottom_edge)
427 pen=Surface::alpha_pen(surface->get_pen(left,bottom-1),get_amount()*bottom_edge,get_blend_method());
428 surface->fill(color,pen,right-left,1);
431 if(right-1>=0 && right_edge)
433 pen=Surface::alpha_pen(surface->get_pen(right-1,top),get_amount()*right_edge,get_blend_method());
434 surface->fill(color,pen,1,bottom-top);
437 if(left>=0 && left_edge)
439 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*left_edge,get_blend_method());
440 surface->fill(color,pen,1,bottom-top);
443 if(top>=0 && top_edge)
445 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*top_edge,get_blend_method());
446 surface->fill(color,pen,right-left,1);
452 // Render what is behind us
453 if(!context.accelerated_render(surface,quality,renddesc,cb))
455 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
459 int left(ceil_to_int((min[0]-tl[0])/pw));
460 int right(floor_to_int((max[0]-tl[0])/pw));
461 int top(ceil_to_int((min[1]-tl[1])/ph));
462 int bottom(floor_to_int((max[1]-tl[1])/ph));
464 float left_edge(float(left)-(min[0]-tl[0])/pw);
465 float right_edge((max[0]-tl[0])/pw-float(right));
466 float top_edge(float(top)-(min[1]-tl[1])/ph);
467 float bottom_edge((max[1]-tl[1])/ph-float(bottom));
469 if(top<=0)top=0,top_edge=0;
470 if(left<=0)left=0,left_edge=0;
471 if(bottom>=h)bottom=h,bottom_edge=0;
472 if(right>=w)right=w,right_edge=0;
475 top = std::max(0,top);
476 left = std::max(0,left);
477 bottom = std::min(h,bottom);
478 right = std::min(w,right);
481 Surface::alpha_pen pen;
483 // In the case where there is nothing to render...
484 if(right-left<0||bottom-top<0)
487 if(right-left>0&&bottom-top>0)
490 surface->fill(color,left,top,right-left,bottom-top);
493 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount(),get_blend_method());
494 surface->fill(color,pen,right-left,bottom-top);
498 if(bottom<surface->get_h() && bottom_edge>=0.0001)
500 pen=Surface::alpha_pen(surface->get_pen(left,bottom),get_amount()*bottom_edge,get_blend_method());
501 surface->fill(color,pen,right-left,1);
504 if(right<surface->get_w() && right_edge>=0.0001)
506 pen=Surface::alpha_pen(surface->get_pen(right,top),get_amount()*right_edge,get_blend_method());
507 surface->fill(color,pen,1,bottom-top);
510 if(left>0 && left_edge>=0.0001)
512 pen=Surface::alpha_pen(surface->get_pen(left-1,top),get_amount()*left_edge,get_blend_method());
513 surface->fill(color,pen,1,bottom-top);
516 if(top>0 && top_edge>=0.0001)
518 pen=Surface::alpha_pen(surface->get_pen(left,top-1),get_amount()*top_edge,get_blend_method());
519 surface->fill(color,pen,right-left,1);
527 Rectangle::get_bounding_rect()const
530 return Rect::full_plane();
532 Point max(point1),min(point2);
533 if((min[0] > max[0]))swap(min[0],max[0]);
534 if((min[1] > max[1]))swap(min[1],max[1]);
557 Rect bounds(min,max);
563 Rectangle::get_full_bounding_rect(Context context)const
567 if(is_solid_color() && color.get_a()==0)
569 Point max(point1),min(point2);
570 if((min[0] > max[0]))swap(min[0],max[0]);
571 if((min[1] > max[1]))swap(min[1],max[1]);
594 Rect bounds(min,max);
596 return bounds & context.get_full_bounding_rect();
598 return Rect::full_plane();
601 return Layer_Composite::get_full_bounding_rect(context);