1 /* This is a demo of the high-performance GUIX graphics framework. */
2 
3 #include <stdio.h>
4 #include "gx_api.h"
5 #include "all_widgets_16bpp_resources.h"
6 #include "all_widgets_16bpp_specifications.h"
7 
8 extern GX_WINDOW *pShapesScreen;
9 
10 GX_RESOURCE_ID    line_color = GX_COLOR_ID_GRAY;
11 GX_RESOURCE_ID    fill_color = GX_COLOR_ID_PURPLE;
12 INT               radius = 160;
13 GX_BOOL           anti_aliased = GX_TRUE;
14 GX_BOOL           round_end = GX_TRUE;
15 GX_BOOL           solid_fill = GX_TRUE;
16 GX_BOOL           pixelmap_fill = GX_FALSE;
17 INT               brush_width = 2;
18 INT               start_angle = 60;
19 INT               end_angle = 90;
20 INT               angle_off = 0;
21 INT               ellipse_b = 100;
22 GX_UBYTE          brush_alpha = 255;
23 INT               fill_pixelmap_index = 0;
24 INT               alpha = 0;
25 INT               compress = 0;
26 
27 GX_RESOURCE_ID pixelmap_id[4] = { GX_PIXELMAP_ID_ID_RAW, GX_PIXELMAP_ID_ID_ALPHA,
28                                   GX_PIXELMAP_ID_ID_COMPRESS, GX_PIXELMAP_ID_ID_COMPRESS_ALPHA};
29 
30 
31 
32 #define CIRCLE    0
33 #define ARC       1
34 #define PIE       2
35 #define POLYGON   3
36 #define ELLIPSE   4
37 #define RECTANGLE 5
38 
39 #define ARC_TIMER     5
40 #define PIE_TIMER     6
41 
42 #define ARC_TICKS     2
43 #define PIE_TICKS     10
44 
45 INT   draw_shape = CIRCLE;
46 INT   pre_shape;
47 
update_prompt_value(GX_PROMPT * pp,INT value)48 VOID update_prompt_value(GX_PROMPT *pp, INT value)
49 {
50     static GX_CHAR str[10];
51     GX_STRING string;
52 
53     if (pp)
54     {
55         gx_utility_ltoa(value, str, 10);
56         string.gx_string_ptr = str;
57         string.gx_string_length = strnlen(str, sizeof(str));
58         gx_prompt_text_set_ext(pp, &string);
59     }
60 }
61 
shapesscreen_event_handler(GX_WINDOW * window,GX_EVENT * myevent)62 UINT shapesscreen_event_handler(GX_WINDOW *window, GX_EVENT *myevent)
63 {
64 UINT status = 0;
65 GX_PROMPT *prompt;
66 GX_WINDOW *pGraphicsWin = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->shapes_screen_graphics_window);
67 
68     switch (myevent->gx_event_type)
69     {
70     case GX_EVENT_SHOW:
71         gx_window_event_process(window, myevent);
72         break;
73 
74     case GX_SIGNAL(ID_BRUSH_ALPHA_SLIDER, GX_EVENT_SLIDER_VALUE):
75         brush_alpha = (GX_UBYTE )myevent->gx_event_payload.gx_event_longdata;
76         prompt = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen) ->shapes_screen_prompt_12);
77         update_prompt_value(prompt, brush_alpha);
78         gx_system_dirty_mark(pGraphicsWin);
79         break;
80 
81     case GX_SIGNAL(ID_RADIUS_SLIDER, GX_EVENT_SLIDER_VALUE):
82         radius = myevent->gx_event_payload.gx_event_longdata;
83         prompt = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->shapes_screen_prompt_6);
84         update_prompt_value(prompt, radius);
85         gx_system_dirty_mark(pGraphicsWin);
86         break;
87 
88     case GX_SIGNAL(ID_BRUSH_WIDTH, GX_EVENT_SLIDER_VALUE):
89         brush_width = myevent->gx_event_payload.gx_event_longdata;
90         prompt = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->shapes_screen_prompt_7);
91         update_prompt_value(prompt, brush_width);
92         gx_system_dirty_mark(pGraphicsWin);
93         break;
94 
95     case GX_SIGNAL(ID_ANTI_ALIASED, GX_EVENT_TOGGLE_ON):
96         anti_aliased = GX_TRUE;
97         gx_system_dirty_mark(pGraphicsWin);
98         break;
99 
100     case GX_SIGNAL(ID_ANTI_ALIASED, GX_EVENT_TOGGLE_OFF):
101         anti_aliased = GX_FALSE;
102         gx_system_dirty_mark(pGraphicsWin);
103         break;
104 
105     case GX_SIGNAL(ID_ROUND_END, GX_EVENT_TOGGLE_ON):
106         round_end = GX_TRUE;
107         gx_system_dirty_mark(pGraphicsWin);
108         break;
109 
110     case GX_SIGNAL(ID_ROUND_END, GX_EVENT_TOGGLE_OFF):
111         round_end = GX_FALSE;
112         gx_system_dirty_mark(pGraphicsWin);
113         break;
114 
115     case GX_SIGNAL(ID_SOLID_FILL, GX_EVENT_TOGGLE_ON):
116         solid_fill = GX_TRUE;
117         gx_system_dirty_mark(pGraphicsWin);
118         break;
119 
120     case GX_SIGNAL(ID_SOLID_FILL, GX_EVENT_TOGGLE_OFF):
121         solid_fill = GX_FALSE;
122         gx_system_dirty_mark(pGraphicsWin);
123         break;
124 
125     case GX_SIGNAL(ID_PIXELMAP_FILL, GX_EVENT_TOGGLE_ON):
126         pixelmap_fill = GX_TRUE;
127         gx_system_dirty_mark(pGraphicsWin);
128         break;
129 
130     case GX_SIGNAL(ID_PIXELMAP_FILL, GX_EVENT_TOGGLE_OFF):
131         pixelmap_fill = GX_FALSE;
132         gx_system_dirty_mark(pGraphicsWin);
133         break;
134 
135     case GX_SIGNAL(ID_COMPRESS, GX_EVENT_TOGGLE_ON):
136         compress = 2;
137         gx_system_dirty_mark(pGraphicsWin);
138         break;
139 
140     case GX_SIGNAL(ID_COMPRESS, GX_EVENT_TOGGLE_OFF):
141         compress = 0;
142         gx_system_dirty_mark(pGraphicsWin);
143         break;
144 
145     case GX_SIGNAL(ID_ALPHA, GX_EVENT_TOGGLE_ON):
146         alpha = 1;
147         gx_system_dirty_mark(pGraphicsWin);
148         break;
149 
150     case GX_SIGNAL(ID_ALPHA, GX_EVENT_TOGGLE_OFF):
151         alpha = 0;
152         gx_system_dirty_mark(pGraphicsWin);
153         break;
154 
155     case GX_SIGNAL(ID_CIRCLE, GX_EVENT_RADIO_SELECT):
156         draw_shape = CIRCLE;
157         gx_system_dirty_mark(pGraphicsWin);
158         break;
159 
160     case GX_SIGNAL(ID_ELLIPSE, GX_EVENT_RADIO_SELECT):
161         draw_shape = ELLIPSE;
162         gx_system_dirty_mark(pGraphicsWin);
163         break;
164 
165     case GX_SIGNAL(ID_ARC, GX_EVENT_RADIO_SELECT):
166         draw_shape = ARC;
167         end_angle = start_angle;
168         gx_system_timer_start(pGraphicsWin, ARC_TIMER, ARC_TICKS, ARC_TICKS);
169         gx_system_dirty_mark(pGraphicsWin);
170         break;
171 
172     case GX_SIGNAL(ID_PIE, GX_EVENT_RADIO_SELECT):
173         draw_shape = PIE;
174         gx_system_timer_start(pGraphicsWin, PIE_TIMER, PIE_TICKS, PIE_TICKS);
175         gx_system_dirty_mark(pGraphicsWin);
176         break;
177 
178     case GX_SIGNAL(ID_POLYGON, GX_EVENT_RADIO_SELECT):
179         draw_shape = POLYGON;
180         gx_system_dirty_mark(pGraphicsWin);
181         break;
182 
183     case GX_SIGNAL(ID_RECTANGLE, GX_EVENT_RADIO_SELECT):
184         draw_shape = RECTANGLE;
185         gx_system_dirty_mark(pGraphicsWin);
186         break;
187 
188     case GX_EVENT_TIMER:
189         if (myevent->gx_event_payload.gx_event_timer_id == ARC_TIMER)
190         {
191             /* Update arc parameter.  */
192             end_angle += 2;
193             if (end_angle == 360)
194             {
195                 gx_system_timer_stop(pGraphicsWin, ARC_TIMER);
196             }
197         }
198         else if (myevent->gx_event_payload.gx_event_timer_id == PIE_TIMER)
199         {
200             /* Update pie parameter.  */
201             angle_off += 2;
202             if (angle_off >= 360)
203             {
204                 angle_off = 0;
205             }
206             gx_system_dirty_mark(pGraphicsWin);
207         }
208         break;
209 
210         default:
211             return next_button_handler(window, myevent);
212          break;
213     }
214     fill_pixelmap_index = alpha + compress;
215     return status;
216 }
217 
218 
219 
graphics_draw(GX_WINDOW * window)220 VOID graphics_draw(GX_WINDOW *window)
221 {
222     ULONG brush_style = 0;
223     GX_RECTANGLE rect;
224     GX_POINT rectangle[4] = { { 188, 50 },{ 254, 50 },{ 254, 150 },{ 188, 150 } };
225     GX_POINT pentagon[5] = { { 290, 90 },{ 335, 50 },{ 380, 90 },{ 360, 150 },{ 310, 150 } };
226     GX_POINT concave[6] = { { 50, 50 },{ 90, 80 },{ 130, 50 },{ 130, 150 },{ 90, 110 },{ 50, 150 } };
227     GX_POINT star[10] = { { 173, 227 },{ 212, 227 },{ 223, 187 },{ 237, 227 },{ 273, 227 },{ 244, 253 },{ 256, 294 },{ 226, 270 },{ 192, 293 },{ 203, 253 } };
228     GX_POINT self_intersection[8] = { { 110, 321 },{ 189, 415 },{ 266, 321 },{ 334, 415 },{ 334, 321 },{ 264, 415 },{ 189, 321 },{ 110, 415 } };
229     INT      xcenter = 213;
230     INT      ycenter = 230;
231     GX_BRUSH *brush;
232 
233     gx_context_brush_get(&brush);
234     gx_window_draw((GX_WINDOW*)window);
235     brush->gx_brush_alpha = brush_alpha;
236 
237     if (anti_aliased)
238     {
239         brush_style |= GX_BRUSH_ALIAS;
240     }
241 
242     if (solid_fill)
243     {
244         brush_style |= GX_BRUSH_SOLID_FILL;
245     }
246 
247     if (pixelmap_fill)
248     {
249         brush_style |= GX_BRUSH_PIXELMAP_FILL;
250         gx_context_pixelmap_set(pixelmap_id[fill_pixelmap_index]);
251     }
252 
253     if (round_end)
254     {
255         brush_style |= GX_BRUSH_ROUND;
256     }
257 
258     gx_context_brush_define(line_color, fill_color, brush_style);
259     gx_context_brush_width_set(brush_width);
260 
261 
262     switch (draw_shape)
263     {
264 
265     case CIRCLE:
266         gx_canvas_circle_draw(xcenter, ycenter, radius);
267         break;
268 
269     case ARC:
270 
271         gx_canvas_arc_draw(xcenter, ycenter, radius, start_angle, end_angle);
272         break;
273 
274     case PIE:
275         gx_context_brush_define(GX_COLOR_ID_GRAY, GX_COLOR_ID_INDIAN_RED, brush_style);
276         gx_canvas_pie_draw(xcenter, ycenter, radius, 60 + angle_off, 150 + angle_off);
277 
278         gx_context_brush_define(GX_COLOR_ID_GRAY, GX_COLOR_ID_YELLOW, brush_style);
279         gx_canvas_pie_draw(xcenter, ycenter, radius, 150 + angle_off, 200 + angle_off);
280 
281         gx_context_brush_define(GX_COLOR_ID_GRAY, GX_COLOR_ID_PINK, brush_style);
282         gx_canvas_pie_draw(xcenter, ycenter, radius, 200 + angle_off, 280 + angle_off);
283 
284         gx_context_brush_define(GX_COLOR_ID_GRAY, GX_COLOR_ID_PURPLE, brush_style);
285         gx_canvas_pie_draw(xcenter, ycenter, radius, 280 + angle_off, 60 + angle_off);
286         break;
287 
288     case RECTANGLE:
289         gx_context_line_color_set(GX_COLOR_ID_INDIAN_RED);
290         gx_context_fill_color_set(GX_COLOR_ID_YELLOW);
291         rect = window->gx_widget_size;
292         gx_utility_rectangle_resize(&rect, -10);
293         gx_canvas_rectangle_draw(&rect);
294 
295         gx_utility_rectangle_resize(&rect, -30);
296         gx_context_fill_color_set(GX_COLOR_ID_BROWN);
297         gx_canvas_rectangle_draw(&rect);
298 
299         gx_utility_rectangle_resize(&rect, -30);
300         gx_context_line_color_set(GX_COLOR_ID_WHITE);
301         gx_context_fill_color_set(GX_COLOR_ID_BLACK);
302         gx_canvas_rectangle_draw(&rect);
303         break;
304 
305     case POLYGON:
306         gx_context_brush_define(GX_COLOR_ID_GREEN, fill_color, brush_style);
307         gx_canvas_polygon_draw(rectangle, 4);
308         gx_canvas_polygon_draw(pentagon, 5);
309         gx_canvas_polygon_draw(concave, 6);
310         gx_canvas_polygon_draw(star, 10);
311         gx_canvas_polygon_draw(self_intersection, 8);
312         break;
313 
314     case ELLIPSE:
315         gx_context_brush_define(GX_COLOR_ID_BROWN, GX_COLOR_ID_YELLOW, brush_style);
316         gx_canvas_ellipse_draw(xcenter, ycenter, radius, ellipse_b);
317 
318         if (radius > 50)
319         {
320             gx_context_brush_define(GX_COLOR_ID_BROWN, GX_COLOR_ID_WHITE, brush_style);
321             gx_canvas_ellipse_draw(xcenter, ycenter, radius - 50, ellipse_b);
322         }
323 
324         if (radius > 100)
325         {
326             gx_context_brush_define(GX_COLOR_ID_BROWN, GX_COLOR_ID_YELLOW, brush_style);
327             gx_canvas_ellipse_draw(xcenter, ycenter, radius - 100, ellipse_b);
328         }
329         break;
330     }
331 
332 }