1 /* This is a small demo of the high-performance GUIX graphics framework. */
2 
3 #include <stdio.h>
4 #include "gx_api.h"
5 
6 #include "guix_shapes_resources.h"
7 #include "guix_shapes_specifications.h"
8 
9 extern UINT string_length_get(GX_CONST GX_CHAR* input_string, UINT max_string_length);
10 
11 #define           ROTATION_TIMER 1
12 #define           SHIFT_TIMER    2
13 #define           COUNT_TIMER    3
14 #define           MAX_DYNAMIC_TEXT_LENGTH 20
15 
16 GX_RESOURCE_ID    text_color = GX_COLOR_ID_RED;
17 GX_BOOL           auto_rotate = GX_FALSE;
18 GX_BOOL           auto_shift = GX_FALSE;
19 GX_PIXELMAP       Alphamap_8bit_font;
20 GX_PIXELMAP       Alphamap_4bit_font;
21 GX_PIXELMAP       Alphamap_1bit_font;
22 GX_PIXELMAP       Alphamap_8bit_dynamic;
23 INT               text_angle = 30;
24 INT               xshift_8bpp = 10;
25 INT               yshift_8bpp = 10;
26 INT               xshift_4bpp = 5;
27 INT               yshift_4bpp = 5;
28 INT               xshift_1bpp = 5;
29 INT               yshift_1bpp = 5;
30 INT               xshift_dynamic = 1;
31 INT               yshift_dynamic = 1;
32 INT               dynamic_count = 123456789;
33 GX_CHAR           dynamic_text[MAX_DYNAMIC_TEXT_LENGTH + 1];
34 extern GX_WINDOW *pTextWindow;
35 extern VOID       memory_free(VOID *mem);
36 
RenderTextToAlphamaps(GX_WIDGET * widget)37 VOID RenderTextToAlphamaps(GX_WIDGET *widget)
38 {
39     GX_FONT *font;
40     GX_STRING string;
41 
42     gx_widget_font_get(widget, GX_FONT_ID_SCREEN_LABEL, &font);
43     gx_widget_string_get_ext(widget, GX_STRING_ID_HELLO_WORLD, &string);
44     gx_utility_string_to_alphamap_ext(&string, font, &Alphamap_8bit_font);
45 
46     gx_widget_font_get(widget, GX_FONT_ID_SMALL_BOLD, &font);
47     gx_widget_string_get_ext(widget, GX_STRING_ID_FONT_4BPP, &string);
48     gx_utility_string_to_alphamap_ext(&string, font, &Alphamap_4bit_font);
49 
50     gx_widget_font_get(widget, GX_FONT_ID_MONO_FONT, &font);
51     gx_widget_string_get_ext(widget, GX_STRING_ID_FONT_1BPP, &string);
52     gx_utility_string_to_alphamap_ext(&string, font, &Alphamap_1bit_font);
53 }
54 
text_window_draw(GX_WINDOW * window)55 void text_window_draw(GX_WINDOW *window)
56 {
57     INT xpos = 0;
58     INT ypos = 0;
59 
60     GX_PIXELMAP rotated_text;
61     GX_PIXELMAP *source_map = GX_NULL;
62     GX_STRING string;
63 
64     gx_window_draw(window);
65 
66     gx_context_line_color_set(text_color);
67     gx_context_fill_color_set(text_color);
68 
69     switch (window->gx_widget_id)
70     {
71     case TEXT_WIN_8BPP:
72         source_map = &Alphamap_8bit_font;
73         break;
74 
75     case TEXT_WIN_4BPP:
76         source_map = &Alphamap_4bit_font;
77         break;
78 
79     case TEXT_WIN_1BPP:
80         source_map = &Alphamap_1bit_font;
81         break;
82     }
83 
84     xpos = (window -> gx_widget_size.gx_rectangle_left + window -> gx_widget_size.gx_rectangle_right) / 2;
85     ypos = (window -> gx_widget_size.gx_rectangle_top + window -> gx_widget_size.gx_rectangle_bottom) / 2;
86 
87     if (source_map)
88     {
89         if (text_angle)
90         {
91             if (gx_utility_pixelmap_rotate(source_map, text_angle, &rotated_text, &xpos, &ypos) == GX_SUCCESS)
92             {
93                 xpos = (window -> gx_widget_size.gx_rectangle_left + window -> gx_widget_size.gx_rectangle_right) / 2;
94                 ypos = (window -> gx_widget_size.gx_rectangle_top + window -> gx_widget_size.gx_rectangle_bottom) / 2;
95                 xpos -= rotated_text.gx_pixelmap_width >> 1;
96                 ypos -= rotated_text.gx_pixelmap_height >> 1;
97                 gx_canvas_pixelmap_draw(xpos, ypos, &rotated_text);
98                 memory_free((VOID *)rotated_text.gx_pixelmap_data);
99             }
100         }
101         else
102         {
103             xpos -= source_map->gx_pixelmap_width >> 1;
104             ypos -= source_map->gx_pixelmap_height >> 1;
105             gx_canvas_pixelmap_draw(xpos, ypos, source_map);
106         }
107     }
108     else
109     {
110         gx_context_font_set(GX_FONT_ID_SMALL_BOLD);
111         gx_utility_ltoa(dynamic_count, dynamic_text, MAX_DYNAMIC_TEXT_LENGTH + 1);
112         string.gx_string_ptr = dynamic_text;
113         string.gx_string_length = string_length_get(dynamic_text, MAX_DYNAMIC_TEXT_LENGTH);
114         gx_canvas_rotated_text_draw_ext(&string, xpos, ypos, text_angle);
115     }
116 }
117 
118 
ShiftText(GX_WINDOW * window)119 VOID ShiftText(GX_WINDOW *window)
120 {
121 INT         *xshift;
122 INT         *yshift;
123 GX_RECTANGLE size = Text_Rotaion_Window.Text_Rotaion_Window_Text_Window.gx_widget_size;
124 
125     switch (window -> gx_widget_id)
126     {
127     case TEXT_WIN_8BPP:
128         xshift = &xshift_8bpp;
129         yshift = &yshift_8bpp;
130         break;
131 
132     case TEXT_WIN_4BPP:
133         xshift = &xshift_4bpp;
134         yshift = &yshift_4bpp;
135         break;
136 
137     case TEXT_WIN_1BPP:
138         xshift = &xshift_1bpp;
139         yshift = &yshift_1bpp;
140         break;
141 
142     case DYNAMIC_TEXT_WINDOW:
143         xshift = &xshift_dynamic;
144         yshift = &yshift_dynamic;
145         gx_system_dirty_mark(&Text_Rotaion_Window.Text_Rotaion_Window_dynamic_text_window);
146         break;
147     }
148 
149     if (*xshift || *yshift)
150     {
151         gx_widget_shift(window, *xshift, *yshift, GX_TRUE);
152 
153         if (*xshift > 0)
154         {
155             if (window->gx_widget_size.gx_rectangle_right >= size.gx_rectangle_right)
156             {
157                 *xshift = -*xshift;
158             }
159         }
160         else
161         {
162             if (window->gx_widget_size.gx_rectangle_left <= 0)
163             {
164                 *xshift = -*xshift;
165             }
166         }
167         if (*yshift > 0)
168         {
169             if (window->gx_widget_size.gx_rectangle_bottom >= size.gx_rectangle_bottom)
170             {
171                 *yshift = -*yshift;
172             }
173         }
174         else
175         {
176             if (window->gx_widget_size.gx_rectangle_top <= 0)
177             {
178                 *yshift = -*yshift;
179             }
180         }
181     }
182 }
183 
ShiftAll(VOID)184 VOID ShiftAll(VOID)
185 {
186     if (auto_shift)
187     {
188         ShiftText(&Text_Rotaion_Window.Text_Rotaion_Window_text_window_8bpp);
189         ShiftText(&Text_Rotaion_Window.Text_Rotaion_Window_text_window_4bpp);
190         ShiftText(&Text_Rotaion_Window.Text_Rotaion_Window_text_window_1bpp);
191         ShiftText(&Text_Rotaion_Window.Text_Rotaion_Window_dynamic_text_window);
192     }
193 }
194 
update_text_value(INT value)195 VOID update_text_value(INT value)
196 {
197 static GX_CHAR str_value[8];
198 GX_STRING string;
199 GX_PROMPT *prompt;
200 
201     prompt = (GX_PROMPT *)&Text_Rotaion_Window.Text_Rotaion_Window_rotation_angle_value;
202     gx_utility_ltoa(value, str_value, 10);
203     string.gx_string_ptr = str_value;
204     string.gx_string_length = string_length_get(str_value, sizeof(str_value) - 1);
205     gx_prompt_text_set_ext((GX_PROMPT *)prompt, &string);
206 }
207 
Text_Rotation_Window_event_handler(GX_WINDOW * window,GX_EVENT * myevent)208 UINT Text_Rotation_Window_event_handler(GX_WINDOW *window, GX_EVENT *myevent)
209 {
210 INT pos;
211 GX_PIXELMAP_SLIDER *slider;
212 GX_SLIDER_INFO     *info;
213 
214     switch (myevent->gx_event_type)
215     {
216     case GX_EVENT_SHOW:
217         gx_system_timer_start(&Text_Rotaion_Window.Text_Rotaion_Window_dynamic_text_window, COUNT_TIMER, 5, 5);
218         gx_window_event_process(window, myevent);
219         RenderTextToAlphamaps((GX_WIDGET *)window);
220         break;
221 
222     case GX_EVENT_HIDE:
223         gx_system_timer_stop(&Text_Rotaion_Window.Text_Rotaion_Window_dynamic_text_window, COUNT_TIMER);
224         gx_window_event_process(window, myevent);
225         break;
226 
227     case GX_SIGNAL(ID_RED_TEXT, GX_EVENT_RADIO_SELECT):
228         text_color = GX_COLOR_ID_RED;
229         gx_system_dirty_mark(&Text_Rotaion_Window.Text_Rotaion_Window_Text_Window);
230         break;
231 
232     case GX_SIGNAL(ID_BLUE_TEXT, GX_EVENT_RADIO_SELECT):
233         text_color = GX_COLOR_ID_BLUE;
234         gx_system_dirty_mark(&Text_Rotaion_Window.Text_Rotaion_Window_Text_Window);
235         break;
236 
237     case GX_SIGNAL(ID_GREEN_TEXT, GX_EVENT_RADIO_SELECT):
238         text_color = GX_COLOR_ID_GREEN;
239         gx_system_dirty_mark(&Text_Rotaion_Window.Text_Rotaion_Window_Text_Window);
240         break;
241 
242     case GX_SIGNAL(ID_AUTO_ROTATE, GX_EVENT_TOGGLE_ON):
243         auto_rotate= GX_TRUE;
244         gx_system_timer_start(window, ROTATION_TIMER, 5, 5);
245         break;
246 
247     case GX_SIGNAL(ID_AUTO_ROTATE, GX_EVENT_TOGGLE_OFF):
248         auto_rotate = GX_FALSE;
249         gx_system_timer_stop(window, ROTATION_TIMER);
250         break;
251 
252     case GX_SIGNAL(ID_AUTO_SHIFT, GX_EVENT_TOGGLE_ON):
253         auto_shift = GX_TRUE;
254         gx_system_timer_start(window, SHIFT_TIMER, 5, 5);
255         break;
256 
257     case GX_SIGNAL(ID_AUTO_SHIFT, GX_EVENT_TOGGLE_OFF):
258         auto_shift = GX_FALSE;
259         gx_system_timer_stop(window, SHIFT_TIMER);
260         break;
261 
262     case GX_SIGNAL(ID_ROTATION_ANGLE_SLIDER, GX_EVENT_SLIDER_VALUE):
263         pos = myevent->gx_event_payload.gx_event_longdata;
264         text_angle = pos;
265         update_text_value(pos);
266         gx_system_dirty_mark(&Text_Rotaion_Window.Text_Rotaion_Window_Text_Window);
267         break;
268 
269     case GX_EVENT_TIMER:
270         switch (myevent->gx_event_payload.gx_event_timer_id)
271         {
272         case COUNT_TIMER:
273             dynamic_count += 17;
274             break;
275 
276         case ROTATION_TIMER:
277             text_angle += 5;
278             if (text_angle >= 360)
279             {
280                 text_angle = 0;
281             }
282             slider = &Text_Rotaion_Window.Text_Rotaion_Window_Rotation_Angle_slider;
283             info = &slider -> gx_slider_info;
284             update_text_value(text_angle);
285             gx_slider_value_set((GX_SLIDER *)slider, info, text_angle);
286 
287             if (auto_shift)
288             {
289                 ShiftAll();
290             }
291             break;
292 
293         case SHIFT_TIMER:
294             if (auto_rotate) return 0;
295             else             ShiftAll();
296             break;
297         }
298         gx_system_dirty_mark(&Text_Rotaion_Window.Text_Rotaion_Window_Text_Window);
299         break;
300 
301     default:
302         return gx_window_event_process(window, myevent);
303     }
304     return 0;
305 }
306