font.c was created, and the Makefiles updated to reflect this. It contains reload_fon...
[xuni.git] / options.c
blob6d29f6c6a2fa552988df72986b6853da8fdad4a7
1 /*! \file options.c
3 */
5 #include "SDL_image.h"
6 #include "SDL_rotozoom.h"
7 #include "options.h"
8 #include "font.h"
9 #include "graphics.h"
10 #include "gui.h"
11 #include "loop.h"
12 #include "menu.h"
13 #include "xuni.h"
15 static void resize_options_images(struct options_data_t *data,
16 struct smode_t *smode, struct font_t *font, struct theme_t *theme,
17 struct gui_t *gui);
18 static void resize_theme_fontsample(struct options_data_t *data,
19 struct font_t *font);
20 static int set_options_widget_sel(int xp, int yp, int click,
21 struct options_data_t *data, struct smode_t *smode,
22 struct theme_t *theme, struct gui_t *gui);
23 static int options_perform_click(int id, enum loop_mode_t *mode,
24 enum options_mode_t *omode, struct options_data_t *data,
25 struct smode_t *smode, struct gui_t *gui);
26 static void paint_theme_fontsample(struct smode_t *smode,
27 struct options_data_t *data);
28 static void free_resize_options(struct options_data_t *data,
29 struct gui_t *gui);
31 void init_options(struct options_data_t *data, struct smode_t *smode,
32 struct font_t *font, struct theme_t *theme, struct gui_t *gui) {
34 data->mode = OMODE_NORMAL;
36 data->loaded = 0;
37 resize_options_images(data, smode, font, theme, gui);
38 data->loaded = 1;
41 static void resize_options_images(struct options_data_t *data,
42 struct smode_t *smode, struct font_t *font, struct theme_t *theme,
43 struct gui_t *gui) {
45 if(data->loaded) free_resize_options(data, gui);
47 /* Initialize the options menu widgets. */
48 add_widget(gui->panel[PANEL_OPTIONS],
49 allocate_button(0, font->sans, "Graphics",
50 (smode->width - smode->width / 4) / 2,
51 smode->height / 8,
52 smode->width / 4, smode->height / 15));
53 add_widget(gui->panel[PANEL_OPTIONS],
54 allocate_button(1, font->sans, "Theme",
55 (smode->width - smode->width / 4) / 2,
56 smode->height / 8 + smode->height / 15 * 1.3,
57 smode->width / 4, smode->height / 15));
58 add_widget(gui->panel[PANEL_OPTIONS],
59 allocate_button(2, font->sans, "Return to main menu",
60 (smode->width - smode->width / 4) / 2,
61 smode->height / 8 + smode->height / 15 * 1.3 * 2,
62 smode->width / 4, smode->height / 15));
64 /* Initialize the graphics widgets. */
65 add_widget(gui->panel[PANEL_OPTIONS_GRAPHICS],
66 allocate_button(100, font->sans, "Return to options",
67 (smode->width - smode->width / 4) / 2,
68 smode->height / 8 + smode->height / 15 * 1.3,
69 smode->width / 4, smode->height / 15));
70 add_widget(gui->panel[PANEL_OPTIONS_GRAPHICS],
71 allocate_checkbox(101, font->sans, "Fullscreen",
72 theme, smode->fullscreen, (smode->width - smode->width / 4) / 2,
73 smode->height / 8 + smode->height / 15 * 1.3 * 2,
74 smode->width / 3, smode->height / 15, smode->width / 20));
75 add_widget(gui->panel[PANEL_OPTIONS_GRAPHICS],
76 allocate_checkbox(102, font->sans, "Restrict cursor",
77 theme, smode->fullscreen, (smode->width - smode->width / 4) / 2,
78 smode->height / 8 + smode->height / 15 * 1.3 * 3,
79 smode->width / 3, smode->height / 15, smode->width / 20));
81 char buffer[BUFSIZ];
82 sprintf(buffer, "%ix%ix%i", smode->width, smode->height, smode->depth);
84 add_widget(gui->panel[PANEL_OPTIONS_GRAPHICS],
85 allocate_textbox(103, font->sans, buffer, strlen(buffer),
86 theme, (smode->width - smode->width / 4) / 2,
87 smode->height / 8 + smode->height / 15 * 1.3 * 4,
88 smode->width / 3, smode->height / 15));
91 /* Initialize the theme widgets. */
92 add_widget(gui->panel[PANEL_OPTIONS_THEME],
93 allocate_button(200, font->sans, "Return to options",
94 (smode->width - smode->width / 4) / 2,
95 smode->height / 8 + smode->height / 15 * 1.3,
96 smode->width / 4, smode->height / 15));
98 resize_theme_fontsample(data, font);
100 /*data->text.menu = TTF_RenderText_Blended(font->sans, "Menu", white);*/
103 static void resize_theme_fontsample(struct options_data_t *data,
104 struct font_t *font) {
106 static const char *fontsample_text[] = {
107 "1234567890",
108 "abcdefghijklmnopqrstuvwxyz",
109 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
110 "How quickly daft jumping zebras vex.",
111 "!@#$%^&*()`~-_=+[{]}\\|;:'\",<.>/?"
113 size_t x;
115 for(x = 0; x < sizeof(fontsample_text)/sizeof(*fontsample_text); x ++) {
116 data->gui.theme.fontsample[x] = render_white_text(font->sans,
117 fontsample_text[x]);
121 void start_options(struct options_data_t *data) {
122 set_caption("Options and preferences");
124 data->mode = OMODE_NORMAL;
127 int options_event(enum loop_mode_t *mode, SDL_Event *event,
128 struct options_data_t *data, struct smode_t *smode, struct font_t *font,
129 struct theme_t *theme, struct gui_t *gui) {
131 int repaint = 0;
133 switch(event->type) {
134 case SDL_KEYUP:
135 switch(event->key.keysym.sym) {
136 case SDLK_ESCAPE:
137 if(gui->active.widget) {
138 repaint = 1;
140 revert_widget(gui->active.widget);
142 clear_active(&gui->active);
144 else if(data->mode != OMODE_NORMAL) {
145 data->mode = OMODE_NORMAL;
146 repaint = 1;
147 clear_gui(gui);
149 else *mode = MODE_MENU;
151 break;
152 default: break;
154 break;
155 case SDL_MOUSEBUTTONDOWN:
156 if(event->button.button != 1) break;
158 repaint = set_options_widget_sel(event->button.x, event->button.y, 1,
159 data, smode, theme, gui);
160 break;
161 case SDL_MOUSEBUTTONUP:
162 if(event->button.button != 1) break;
164 if(gui->sel.p.widget
165 && in_sdl_rect(event->button.x, event->button.y,
166 &gui->sel.p.widget->pos)) {
168 if(gui->sel.clickin) {
169 options_perform_click(gui->sel.p.widget->id, mode,
170 &data->mode, data, smode, gui);
172 gui->sel.clickin = 0;
175 gui->sel.wasin = 1;
176 repaint = 1;
178 if(gui->sel.p.widget
179 && widget_can_be_active(gui->sel.p.widget->type)) {
181 gui->active = gui->sel.p;
182 activate_widget(gui->sel.p.widget);
183 enable_unicode(gui->active.widget->type);
186 else {
187 repaint = set_options_widget_sel(event->button.x, event->button.y,
188 0, data, smode, theme, gui);
190 if(gui->active.widget) {
191 repaint = 1;
193 deactivate_widget(gui->active.widget);
195 clear_active(&gui->active);
199 break;
200 case SDL_MOUSEMOTION:
201 repaint = set_options_widget_sel(event->button.x, event->button.y,
202 gui->sel.clickin, data, smode, theme, gui);
203 /*t = options_handle_click(event->motion.x, event->motion.y, data,
204 smode, theme);
205 if(data->gui.lastid != t) {
206 data->gui.lastid = t;
207 repaint = 1;
209 break;
210 case SDL_QUIT:
211 *mode = MODE_MENU;
212 break;
213 case SDL_VIDEORESIZE:
214 resize_options_images(data, smode, font, theme, gui);
215 break;
216 default:
217 break;
220 return repaint;
223 static int set_options_widget_sel(int xp, int yp, int click,
224 struct options_data_t *data, struct smode_t *smode,
225 struct theme_t *theme, struct gui_t *gui) {
227 if(data->mode == OMODE_NORMAL) {
228 if(set_widget_sel_repaint(&gui->sel, xp, yp, click,
229 gui->panel[PANEL_OPTIONS]->widget,
230 gui->panel[PANEL_OPTIONS]->widgets)) {
232 return 1;
235 else if(data->mode == OMODE_GRAPHICS) {
236 if(set_widget_sel_repaint(&gui->sel, xp, yp, click,
237 gui->panel[PANEL_OPTIONS_GRAPHICS]->widget,
238 gui->panel[PANEL_OPTIONS_GRAPHICS]->widgets)) {
240 return 1;
243 else if(data->mode == OMODE_THEME) {
244 if(set_widget_sel_repaint(&gui->sel, xp, yp, click,
245 gui->panel[PANEL_OPTIONS_THEME]->widget,
246 gui->panel[PANEL_OPTIONS_THEME]->widgets)) {
248 return 1;
252 return 0;
255 static int options_perform_click(int id, enum loop_mode_t *mode,
256 enum options_mode_t *omode, struct options_data_t *data,
257 struct smode_t *smode, struct gui_t *gui) {
259 if(id < 100) {
260 perform_widget_click(gui->panel[PANEL_OPTIONS]->widget[id]);
262 else if(id < 200) {
263 perform_widget_click(
264 gui->panel[PANEL_OPTIONS_GRAPHICS]->widget[id-100]);
266 else if(id < 300) {
267 perform_widget_click(gui->panel[PANEL_OPTIONS_THEME]->widget[id-200]);
270 switch(id) {
271 case 0:
272 *omode = OMODE_GRAPHICS;
273 clear_gui(gui);
274 break;
275 case 1:
276 *omode = OMODE_THEME;
277 clear_gui(gui);
278 break;
279 case 2:
280 *mode = MODE_MENU;
281 break;
282 case 100:
283 *omode = OMODE_NORMAL;
284 clear_gui(gui);
285 break;
286 case 101: /* paint_widget() !!! */
287 /*data->gui.graphics.widget[1].p.checkbox->checked
288 = !data->gui.graphics.widget[1].p.checkbox->checked;*/
289 toggle_fullscreen(smode); /* !!! Use return value for repaint */
290 break;
291 case 102: /* toggle restrict cursor */
292 smode->restrictfocus = set_focus(!smode->restrictfocus);
293 break;
294 case 200:
295 *omode = OMODE_NORMAL;
296 clear_gui(gui);
297 default:
298 break;
301 return 0;
304 void paint_options(struct options_data_t *data, struct smode_t *smode,
305 struct font_t *font, struct theme_t *theme, struct gui_t *gui) {
307 /*if(gui->active.widget) printf("%s\n", gui->active.widget->p.textbox->data);*/
309 gui->panel[PANEL_OPTIONS_GRAPHICS]->widget[1]->p.checkbox->checked
310 = smode->fullscreen;
311 gui->panel[PANEL_OPTIONS_GRAPHICS]->widget[2]->p.checkbox->checked
312 = (smode->restrictfocus == SDL_GRAB_ON);
314 SDL_FillRect(smode->screen, NULL,
315 SDL_MapRGB(smode->screen->format, 0, 0, 0));
317 if(data->mode == OMODE_NORMAL) {
318 paint_widget_array(smode, font, theme, gui,
319 gui->panel[PANEL_OPTIONS]->widget,
320 gui->panel[PANEL_OPTIONS]->widgets);
322 else if(data->mode == OMODE_GRAPHICS) {
323 paint_widget_array(smode, font, theme, gui,
324 gui->panel[PANEL_OPTIONS_GRAPHICS]->widget,
325 gui->panel[PANEL_OPTIONS_GRAPHICS]->widgets);
327 else if(data->mode == OMODE_THEME) {
328 paint_widget_array(smode, font, theme, gui,
329 gui->panel[PANEL_OPTIONS_THEME]->widget,
330 gui->panel[PANEL_OPTIONS_THEME]->widgets);
332 paint_theme_fontsample(smode, data);
335 paint_menu_fps(smode->screen, font);
337 SDL_UpdateRect(smode->screen, 0, 0, 0, 0);
340 static void paint_theme_fontsample(struct smode_t *smode,
341 struct options_data_t *data) {
343 size_t x;
344 int maxw = 0;
346 for(x = 0; x < sizeof(data->gui.theme.fontsample)
347 / sizeof(*data->gui.theme.fontsample); x ++) {
349 if(data->gui.theme.fontsample[x]->w > maxw) {
350 maxw = data->gui.theme.fontsample[x]->w;
354 for(x = 0; x < sizeof(data->gui.theme.fontsample)
355 / sizeof(*data->gui.theme.fontsample); x ++) {
357 blit_surface(smode->screen, data->gui.theme.fontsample[x],
358 (smode->width - maxw) / 2,
359 200 + data->gui.theme.fontsample[x]->h * x);
363 static void free_resize_options(struct options_data_t *data,
364 struct gui_t *gui) {
366 size_t x;
368 /*free_panel(gui->panel[PANEL_OPTIONS]);
369 free_panel(gui->panel[PANEL_OPTIONS_GRAPHICS]);
370 free_panel(gui->panel[PANEL_OPTIONS_THEME]);*/
372 for(x = 0; x < sizeof(data->gui.theme.fontsample)
373 / sizeof(*data->gui.theme.fontsample); x ++) {
375 SDL_FreeSurface(data->gui.theme.fontsample[x]);
379 void free_options(struct options_data_t *data, struct gui_t *gui) {
380 free_resize_options(data, gui);