Fixed a problem with Makefile.mingw where make wasn't rebuilding targets
[neverball] / share / st_lang.c
1 /*
2  * Copyright (C) 2003 Robert Kooima - 2006 Jean Privat
3  * Part of the Neverball Project http://icculus.org/neverball/
4  *
5  * NEVERBALL is  free software; you can redistribute  it and/or modify
6  * it under the  terms of the GNU General  Public License as published
7  * by the Free  Software Foundation; either version 2  of the License,
8  * or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
12  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
13  * General Public License for more details.
14  */
15
16 #include <string.h>
17
18 #include "gui.h"
19 #include "back.h"
20 #include "geom.h"
21 #include "part.h"
22 #include "audio.h"
23 #include "config.h"
24
25 #include "st_lang.h"
26 #include "i18n.h"
27
28 extern struct state st_conf;
29 extern struct state st_null;
30
31 static char **lang_names;
32
33 /*---------------------------------------------------------------------------*/
34
35 #define LANG_BACK 100
36
37 static int lang_action(int i)
38 {
39     int r = 1;
40
41     switch (i)
42     {
43     case LANG_BACK:
44         goto_state(&st_conf);
45         break;
46
47     default:
48         goto_state(&st_null);
49         language_set(i - 1);
50         config_set_s(CONFIG_LANG, language_code(i - 1));
51
52         goto_state(&st_conf);
53         break;
54     }
55
56     return r;
57 }
58
59 static int lang_enter(void)
60 {
61     int id, jd, kd;
62     int i;
63     int l;
64     char *ln, *ln1, *ln2;
65
66     back_init("back/gui.png", config_get_d(CONFIG_GEOMETRY));
67
68     /* Initialize the language selection GUI. */
69     l = language_from_code(config_simple_get_s(CONFIG_LANG));
70
71     if ((id = gui_harray(0)))
72     {
73         if ((jd = gui_varray(id)))
74         {
75             if ((kd = gui_harray(jd)))
76             {
77                 gui_label(kd, _("Language"), GUI_SML, GUI_ALL, 0, 0);
78                 gui_filler(kd);
79                 gui_start(kd, _("Back"), GUI_SML, LANG_BACK, 0);
80             }
81
82             lang_names = calloc(language_count(), sizeof(char *));
83
84             gui_state(jd, _(language_name(0)), GUI_SML, 1, (l == 0));
85             for (i = 1; i <= language_count(); i++)
86             {
87                 language_set(i);
88                 ln1 = _(language_name(i));
89                 language_set(l);
90                 ln2 = _(language_name(i));
91                 if (strcmp(ln1, ln2) == 0)
92                     ln = ln1;
93                 else
94                 {
95                     ln = malloc(sizeof(char) * (strlen(ln1) + strlen(ln2) + 4));
96                     lang_names[i - 1] = ln;
97                     strcpy(ln, ln1);
98                     strcat(ln, " (");
99                     strcat(ln, ln2);
100                     strcat(ln, ")");
101                 }
102
103                 gui_state(jd, ln, GUI_SML, i + 1, (l == i));
104             }
105         }
106         gui_layout(id, 0, 0);
107     }
108
109     audio_music_fade_to(0.5f, "bgm/inter.ogg");
110
111     return id;
112 }
113
114 static void lang_leave(int id)
115 {
116     int i;
117
118     for (i = 0; i < language_count(); i++)
119         if (lang_names[i] != NULL)
120             free(lang_names[i]);
121     free(lang_names);
122     back_free();
123     gui_delete(id);
124 }
125
126 static void lang_paint(int id, float st)
127 {
128     config_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
129     {
130         back_draw(0);
131     }
132     config_pop_matrix();
133     gui_paint(id);
134 }
135
136 static void lang_timer(int id, float dt)
137 {
138     gui_timer(id, dt);
139     audio_timer(dt);
140 }
141
142 static void lang_point(int id, int x, int y, int dx, int dy)
143 {
144     gui_pulse(gui_point(id, x, y), 1.2f);
145 }
146
147 static void lang_stick(int id, int a, int v)
148 {
149     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
150         gui_pulse(gui_stick(id, v, 0), 1.2f);
151     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
152         gui_pulse(gui_stick(id, 0, v), 1.2f);
153 }
154
155 static int lang_click(int b, int d)
156 {
157     if (b < 0 && d == 1)
158         return lang_action(gui_token(gui_click()));
159     return 1;
160 }
161
162 static int lang_keybd(int c, int d)
163 {
164     return (d && c == SDLK_ESCAPE) ? goto_state(&st_conf) : 1;
165 }
166
167 static int lang_buttn(int b, int d)
168 {
169     if (d)
170     {
171         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
172             return lang_action(gui_token(gui_click()));
173         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
174             return goto_state(&st_conf);
175         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
176             return goto_state(&st_conf);
177     }
178     return 1;
179 }
180
181 /*---------------------------------------------------------------------------*/
182
183
184 struct state st_lang = {
185     lang_enter,
186     lang_leave,
187     lang_paint,
188     lang_timer,
189     lang_point,
190     lang_stick,
191     lang_click,
192     lang_keybd,
193     lang_buttn,
194     1, 0
195 };
196