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_332rgb_resources.h"
6 #include "all_widgets_332rgb_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               shape_screen_alpha = 0;
24 INT               shape_screen_compress = 0;
25 INT               fill_pixelmap_index = 0;
26 
27 GX_RESOURCE_ID  fill_pixelmap_id[4] = {GX_PIXELMAP_ID_ID_RAW, GX_PIXELMAP_ID_ID_COMPRESS, GX_PIXELMAP_ID_ID_ALPHA, GX_PIXELMAP_ID_ID_COMPRESS_ALPHA};
28 
29 #define CIRCLE    0
30 #define ARC       1
31 #define PIE       2
32 #define POLYGON   3
33 #define ELLIPSE   4
34 #define RECTANGLE 5
35 
36 #define ARC_TIMER     5
37 #define PIE_TIMER     6
38 
39 #define ARC_TICKS     2
40 #define PIE_TICKS     10
41 
42 INT   draw_shape = CIRCLE;
43 INT   pre_shape;
44 
update_prompt_value(GX_PROMPT * pp,INT value)45 VOID update_prompt_value(GX_PROMPT *pp, INT value)
46 {
47     static GX_CHAR str[10];
48     GX_STRING string;
49 
50     if (pp)
51     {
52         gx_utility_ltoa(value, str, 10);
53         string.gx_string_ptr = str;
54         string.gx_string_length = strnlen(str, sizeof(str));
55         gx_prompt_text_set_ext(pp, &string);
56     }
57 }
58 
shapesscreen_event_handler(GX_WINDOW * window,GX_EVENT * myevent)59 UINT shapesscreen_event_handler(GX_WINDOW *window, GX_EVENT *myevent)
60 {
61 UINT status = 0;
62 GX_PROMPT *prompt;
63 GX_WINDOW *pGraphicsWin = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->shapes_screen_graphics_window);
64 
65     switch (myevent->gx_event_type)
66     {
67     case GX_EVENT_SHOW:
68         gx_window_event_process(window, myevent);
69         break;
70 
71     case GX_SIGNAL(ID_BRUSH_ALPHA, GX_EVENT_SLIDER_VALUE):
72         brush_alpha = (GX_UBYTE)myevent->gx_event_payload.gx_event_longdata;
73         prompt = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->shapes_screen_prompt_14);
74         update_prompt_value(prompt, brush_alpha);
75         gx_system_dirty_mark(pGraphicsWin);
76         break;
77 
78     case GX_SIGNAL(ID_RADIUS_SLIDER, GX_EVENT_SLIDER_VALUE):
79         radius = myevent->gx_event_payload.gx_event_longdata;
80         prompt = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->shapes_screen_prompt_6);
81         update_prompt_value(prompt, radius);
82         gx_system_dirty_mark(pGraphicsWin);
83         break;
84 
85     case GX_SIGNAL(ID_BRUSH_WIDTH, GX_EVENT_SLIDER_VALUE):
86         brush_width = myevent->gx_event_payload.gx_event_longdata;
87         prompt = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->shapes_screen_prompt_8);
88         update_prompt_value(prompt, brush_width);
89         gx_system_dirty_mark(pGraphicsWin);
90         break;
91 
92     case GX_SIGNAL(ID_ANTI_ALIASED, GX_EVENT_TOGGLE_ON):
93         anti_aliased = GX_TRUE;
94         gx_system_dirty_mark(pGraphicsWin);
95         break;
96 
97     case GX_SIGNAL(ID_ANTI_ALIASED, GX_EVENT_TOGGLE_OFF):
98         anti_aliased = GX_FALSE;
99         gx_system_dirty_mark(pGraphicsWin);
100         break;
101 
102     case GX_SIGNAL(ID_ROUND_END, GX_EVENT_TOGGLE_ON):
103         round_end = GX_TRUE;
104         gx_system_dirty_mark(pGraphicsWin);
105         break;
106 
107     case GX_SIGNAL(ID_ROUND_END, GX_EVENT_TOGGLE_OFF):
108         round_end = GX_FALSE;
109         gx_system_dirty_mark(pGraphicsWin);
110         break;
111 
112     case GX_SIGNAL(ID_SOLID_FILL, GX_EVENT_TOGGLE_ON):
113         solid_fill = GX_TRUE;
114         gx_system_dirty_mark(pGraphicsWin);
115         break;
116 
117     case GX_SIGNAL(ID_SOLID_FILL, GX_EVENT_TOGGLE_OFF):
118         solid_fill = GX_FALSE;
119         gx_system_dirty_mark(pGraphicsWin);
120         break;
121 
122     case GX_SIGNAL(ID_PIXELMAP_FILL, GX_EVENT_TOGGLE_ON):
123         pixelmap_fill = GX_TRUE;
124         gx_system_dirty_mark(pGraphicsWin);
125         break;
126 
127     case GX_SIGNAL(ID_PIXELMAP_FILL, GX_EVENT_TOGGLE_OFF):
128         pixelmap_fill = GX_FALSE;
129         gx_system_dirty_mark(pGraphicsWin);
130         break;
131 
132     case GX_SIGNAL(ID_ALPHA, GX_EVENT_TOGGLE_ON):
133         shape_screen_alpha = 2;
134         gx_system_dirty_mark(pGraphicsWin);
135         break;
136 
137     case GX_SIGNAL(ID_ALPHA, GX_EVENT_TOGGLE_OFF):
138         shape_screen_alpha = 0;
139         gx_system_dirty_mark(pGraphicsWin);
140         break;
141 
142     case GX_SIGNAL(ID_COMPRESS, GX_EVENT_TOGGLE_ON):
143         shape_screen_compress = 1;
144         gx_system_dirty_mark(pGraphicsWin);
145         break;
146 
147     case GX_SIGNAL(ID_COMPRESS, GX_EVENT_TOGGLE_OFF):
148         shape_screen_compress = 0;
149         gx_system_dirty_mark(pGraphicsWin);
150         break;
151 
152     case GX_SIGNAL(ID_CIRCLE, GX_EVENT_RADIO_SELECT):
153         draw_shape = CIRCLE;
154         gx_system_dirty_mark(pGraphicsWin);
155         break;
156 
157     case GX_SIGNAL(ID_ELLIPSE, GX_EVENT_RADIO_SELECT):
158         draw_shape = ELLIPSE;
159         gx_system_dirty_mark(pGraphicsWin);
160         break;
161 
162     case GX_SIGNAL(ID_ARC, GX_EVENT_RADIO_SELECT):
163         draw_shape = ARC;
164         end_angle = start_angle;
165         gx_system_timer_start(pGraphicsWin, ARC_TIMER, ARC_TICKS, ARC_TICKS);
166         gx_system_dirty_mark(pGraphicsWin);
167         break;
168 
169     case GX_SIGNAL(ID_PIE, GX_EVENT_RADIO_SELECT):
170         draw_shape = PIE;
171         gx_system_timer_start(pGraphicsWin, PIE_TIMER, PIE_TICKS, PIE_TICKS);
172         gx_system_dirty_mark(pGraphicsWin);
173         break;
174 
175     case GX_SIGNAL(ID_POLYGON, GX_EVENT_RADIO_SELECT):
176         draw_shape = POLYGON;
177         gx_system_dirty_mark(pGraphicsWin);
178         break;
179 
180     case GX_SIGNAL(ID_RECTANGLE, GX_EVENT_RADIO_SELECT):
181         draw_shape = RECTANGLE;
182         gx_system_dirty_mark(pGraphicsWin);
183         break;
184 
185     case GX_EVENT_TIMER:
186         if (myevent->gx_event_payload.gx_event_timer_id == ARC_TIMER)
187         {
188             /* Update arc parameter.  */
189             end_angle += 2;
190             if (end_angle == 360)
191             {
192                 gx_system_timer_stop(pGraphicsWin, ARC_TIMER);
193             }
194         }
195         else if (myevent->gx_event_payload.gx_event_timer_id == PIE_TIMER)
196         {
197             /* Update pie parameter.  */
198             angle_off += 2;
199             if (angle_off >= 360)
200             {
201                 angle_off = 0;
202             }
203             gx_system_dirty_mark(pGraphicsWin);
204         }
205         break;
206 
207         default:
208             return next_button_handler(window, myevent);
209          break;
210     }
211     fill_pixelmap_index = shape_screen_alpha + shape_screen_compress;
212     return status;
213 }
214 
215 
216 
graphics_draw(GX_WINDOW * window)217 VOID graphics_draw(GX_WINDOW *window)
218 {
219     ULONG brush_style = 0;
220     GX_RECTANGLE rect;
221     GX_POINT rectangle[4] = { { 188, 50 },{ 254, 50 },{ 254, 150 },{ 188, 150 } };
222     GX_POINT pentagon[5] = { { 290, 90 },{ 335, 50 },{ 380, 90 },{ 360, 150 },{ 310, 150 } };
223     GX_POINT concave[6] = { { 50, 50 },{ 90, 80 },{ 130, 50 },{ 130, 150 },{ 90, 110 },{ 50, 150 } };
224     GX_POINT star[10] = { { 173, 227 },{ 212, 227 },{ 223, 187 },{ 237, 227 },{ 273, 227 },{ 244, 253 },{ 256, 294 },{ 226, 270 },{ 192, 293 },{ 203, 253 } };
225     GX_POINT self_intersection[8] = { { 110, 321 },{ 189, 415 },{ 266, 321 },{ 334, 415 },{ 334, 321 },{ 264, 415 },{ 189, 321 },{ 110, 415 } };
226     INT      xcenter = 213;
227     INT      ycenter = 230;
228     GX_BRUSH *brush;
229 
230     gx_context_brush_get(&brush);
231     gx_window_background_draw((GX_WINDOW*)window);
232     brush -> gx_brush_alpha = brush_alpha;
233     gx_widget_children_draw((GX_WIDGET *)window);
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(fill_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 }