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