1 /*
2     GUILIB:  An example GUI framework library for use with SDL
3 */
4 
5 #include "GUI_area.h"
6 #include "math.h"
7 //#include "GUI_utils.h"
8 
GUI_Area(int x,int y,int w,int h,Uint8 r,Uint8 g,Uint8 b,int aShape)9 GUI_Area:: GUI_Area(int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, int aShape)
10  : GUI_Widget(NULL, x, y, w, h)
11 {
12 	R = r;
13 	G = g;
14 	B = b;
15 	color = 0;
16 	useFrame=0;
17 	shape=aShape;
18 }
19 
GUI_Area(int x,int y,int w,int h,Uint8 r,Uint8 g,Uint8 b,Uint8 fr,Uint8 fg,Uint8 fb,int fthick,int aShape)20 GUI_Area:: GUI_Area(int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b,
21 			Uint8 fr, Uint8 fg, Uint8 fb, int fthick, int aShape)
22  : GUI_Widget(NULL, x, y, w, h)
23 {
24 	R = r;
25 	G = g;
26 	B = b;
27 	color = 0;
28 	useFrame=1;
29 	fR = fr;
30 	fG = fg;
31 	fB = fb;
32 	frameColor=0;
33 	frameThickness=fthick;
34 	shape=aShape;
35 }
36 
37 /* Map the color to the display */
38 void
SetDisplay(SDL_Surface * display)39 GUI_Area:: SetDisplay(SDL_Surface *display)
40 {
41 	GUI_Widget::SetDisplay(display);
42 	color = SDL_MapRGB(screen->format, R, G, B);
43 	if (useFrame)
44 	  frameColor = SDL_MapRGB(screen->format, fR, fG, fB);
45 }
46 
47 /* Show the widget  */
48 void
Display(void)49 GUI_Area:: Display(void)
50 {
51 	SDL_Rect framerect;
52 	int x,dy,r1,r2,x0,y0;
53 
54 	switch (shape)
55 	{
56 	case AREA_ROUND:
57 
58 	  r1=area.w >> 1;
59 	  r2=area.h >> 1;
60 	  x0=area.x+r1;
61 	  y0=area.y+r2;
62 	  for (x=area.x;x<area.x+area.w;x++)
63 	  {
64 	    dy=(int)((double) r2*sin(acos((double) (x-x0)/(double) r1)));
65 	    framerect.x=x; framerect.y=y0-dy;
66 	    framerect.w=1; framerect.h=dy << 1;
67 	    SDL_FillRect(screen,&framerect,color);
68 	    if (useFrame)
69 	    {
70 	      if ((x==area.x) || (x==area.x+area.w-1))
71 	      {
72 	        SDL_FillRect(screen,&framerect,frameColor);
73 	      }
74 	      framerect.h=frameThickness;
75 	      SDL_FillRect(screen,&framerect,frameColor);
76 	      framerect.y=y0+dy-frameThickness;
77 	      SDL_FillRect(screen,&framerect,frameColor);
78 	    }
79 	  }
80 /*
81 ******** GUI_FillEllipse is not ready yet, GUI_BoundaryFill either *****
82 	    framerect=area;
83 	    if (useFrame)
84 	    {
85 	      GUI_FillEllipse(screen,&framerect,frameColor);
86 	      area.x+=frameThickness; area.w-=frameThickness << 1;
87 	      area.y+=frameThickness; area.h-=frameThickness << 1;
88 	    }
89 	    GUI_FillEllipse(screen,&framerect,color);
90 */
91 	  break;
92 
93 	case AREA_ANGULAR:
94 	  SDL_FillRect(screen, &area, color);
95 
96 	  /* draw frame */
97 	  if (useFrame)
98 	  {
99 	    framerect=area;
100 	    framerect.h=frameThickness;
101 	    SDL_FillRect(screen, &framerect, frameColor);
102 	    framerect.y+=area.h-frameThickness;
103 	    SDL_FillRect(screen, &framerect, frameColor);
104 	    framerect.y=area.y;
105 	    framerect.h=area.h;
106 	    framerect.w=frameThickness;
107 	    SDL_FillRect(screen, &framerect, frameColor);
108 	    framerect.x+=area.w-frameThickness;
109 	    SDL_FillRect(screen, &framerect, frameColor);
110 	  }
111 	  break;
112 	}
113 }
114