I can't sleep, so fixing bugs #39
[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
29 extern struct state st_conf;
30 extern struct state st_null;
31
32 char ** lang_names;
33
34 /*---------------------------------------------------------------------------*/
35
36 #define LANG_BACK 100
37
38 static int lang_action(int i)
39 {
40     int r = 1;
41
42     switch (i)
43     {
44     case LANG_BACK:
45         goto_state(&st_conf);
46         break;
47
48     default:
49         goto_state(&st_null);
50         language_set(i-1);
51         config_set_s(CONFIG_LANG, language_get_code(i-1));
52         gui_init();
53
54         goto_state(&st_conf);
55         break;
56     }
57
58     return r;
59 }
60
61 static int lang_enter(void)
62 {
63     int id, jd, kd;
64     int i;
65     int l;
66     char * ln, * ln1, * ln2;
67
68     back_init("back/gui.png", config_get_d(CONFIG_GEOMETRY));
69
70     /* Initialize the language selection GUI. */
71     l = language_from_code(config_simple_get_s(CONFIG_LANG));
72
73     if ((id = gui_harray(0)))
74     {
75         if ((jd = gui_varray(id)))
76         {
77             if ((kd = gui_harray(jd)))
78             {
79                 gui_label(kd, _("Language"), GUI_SML, GUI_ALL, 0, 0);
80                 gui_filler(kd);
81                 gui_start(kd, _("Back"),     GUI_SML, LANG_BACK, 0);
82             }
83             
84             lang_names = calloc(language_count(), sizeof(char*));
85
86             gui_state(jd, _(language_get_name(0)),   GUI_SML, 1,    (l==0));
87             for(i=1; i<=language_count(); i++)
88             {
89                 language_set(i);
90                 ln1 = _(language_get_name(i));
91                 language_set(l);
92                 ln2 = _(language_get_name(i));
93                 if (strcmp(ln1, ln2) == 0)
94                     ln = ln1;
95                 else
96                 {
97                     ln = malloc(sizeof(char)*(strlen(ln1)+strlen(ln2)+4));
98                     lang_names[i-1] = ln;
99                     strcpy(ln, ln1);
100                     strcat(ln, " (");
101                     strcat(ln, ln2);
102                     strcat(ln, ")");
103                 }
104
105                 gui_state(jd, ln,   GUI_SML, i+1,    (l==i));
106             }
107         }
108         gui_layout(id, 0, 0);
109     }
110
111     audio_music_fade_to(0.5f, "bgm/inter.ogg");
112
113     return id;
114 }
115
116 static void lang_leave(int id)
117 {
118     int i;
119     for(i=0;i<language_count();i++)
120             if (lang_names[i] != NULL)
121                     free(lang_names[i]);
122     free(lang_names);
123     back_free();
124     gui_delete(id);
125 }
126
127 static void lang_paint(int id, float st)
128 {
129     config_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
130     {
131         back_draw(0);
132     }
133     config_pop_matrix();
134     gui_paint(id);
135 }
136
137 static void lang_timer(int id, float dt)
138 {
139     gui_timer(id, dt);
140     audio_timer(dt);
141 }
142
143 static void lang_point(int id, int x, int y, int dx, int dy)
144 {
145     gui_pulse(gui_point(id, x, y), 1.2f);
146 }
147
148 static void lang_stick(int id, int a, int v)
149 {
150     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
151         gui_pulse(gui_stick(id, v, 0), 1.2f);
152     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
153         gui_pulse(gui_stick(id, 0, v), 1.2f);
154 }
155
156 static int lang_click(int b, int d)
157 {
158     if (b < 0 && d == 1)
159         return lang_action(gui_token(gui_click()));
160     return 1;
161 }
162
163 static int lang_keybd(int c, int d)
164 {
165     return (d && c == SDLK_ESCAPE) ? goto_state(&st_conf) : 1;
166 }
167
168 static int lang_buttn(int b, int d)
169 {
170     if (d)
171     {
172         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
173             return lang_action(gui_token(gui_click()));
174         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
175             return goto_state(&st_conf);
176         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
177             return goto_state(&st_conf);
178     }
179     return 1;
180 }
181
182 /*---------------------------------------------------------------------------*/
183
184
185 struct state st_lang = {
186     lang_enter,
187     lang_leave,
188     lang_paint,
189     lang_timer,
190     lang_point,
191     lang_stick,
192     lang_click,
193     lang_keybd,
194     lang_buttn,
195     1, 0
196 };
197