better hs tables
[neverball] / ball / st_done.c
1 /*   
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <string.h>
16
17 #include "gui.h"
18 #include "set.h"
19 #include "game.h"
20 #include "util.h"
21 #include "demo.h"
22 #include "level.h"
23 #include "audio.h"
24 #include "config.h"
25
26 #include "st_done.h"
27 #include "st_title.h"
28
29 /*---------------------------------------------------------------------------*/
30
31 #define DONE_OK 2
32
33 static int high;
34 static int time_i;
35 static int coin_i;
36
37 static int done_action(int i)
38 {
39     char player[MAXNAM];
40     size_t l;
41
42     audio_play(AUD_MENU, 1.0f);
43
44     config_get_s(CONFIG_PLAYER, player, MAXNAM);
45     l = strlen(player);
46
47     switch (i)
48     {
49     case DONE_OK:
50         level_free();
51         return goto_state(&st_title);
52
53     case GUI_CL:
54         gui_keyboard_lock();
55         break;
56
57     case GUI_BS:
58         if (l > 0)
59         {
60             player[l - 1] = 0;
61
62             config_set_s(CONFIG_PLAYER, player);
63             level_name(0, player, time_i, coin_i);
64             set_most_coins(0, coin_i);
65             set_best_times(0, time_i);
66         }
67         break;
68
69     default:
70         if (l < MAXNAM - 1)
71         {
72             player[l + 0] = gui_keyboard_char((char) i);
73             player[l + 1] = 0;
74
75             config_set_s(CONFIG_PLAYER, player);
76             level_name(0, player, time_i, coin_i);
77             set_most_coins(0, coin_i);
78             set_best_times(0, time_i);
79         }
80     }
81     return 1;
82 }
83
84 static int done_enter(void)
85 {
86     const char *s1 = _("New Set Record");
87     const char *s2 = _("Set Complete");
88
89     int id, jd;
90
91     time_i = 3;
92     coin_i = 3;
93     high   = level_done(&time_i, &coin_i);
94
95     if ((id = gui_vstack(0)))
96     {
97         int gid;
98
99         if (high)
100             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
101         else
102             gid = gui_label(id, s2, GUI_MED, GUI_ALL, gui_blu, gui_grn);
103
104         gui_space(id);
105
106         if ((jd = gui_harray(id)))
107         {
108             gui_most_coins(jd, 3, 1);
109             gui_best_times(jd, 3, 1);
110         }
111
112         gui_space(id);
113         gui_start(id, _("Main Menu"), GUI_SML, DONE_OK, 0);
114
115         if (high) gui_keyboard(id);
116
117         gui_layout(id, 0, 0);
118         gui_pulse(gid, 1.2f);
119     }
120
121     set_most_coins(0, coin_i);
122     set_best_times(0, time_i);
123
124     return id;
125 }
126
127 static void done_leave(int id)
128 {
129     gui_delete(id);
130 }
131
132 static void done_paint(int id, float st)
133 {
134     game_draw(0, st);
135     gui_paint(id);
136 }
137
138 static void done_timer(int id, float dt)
139 {
140     gui_timer(id, dt);
141     audio_timer(dt);
142 }
143
144 static void done_point(int id, int x, int y, int dx, int dy)
145 {
146     gui_pulse(gui_point(id, x, y), 1.2f);
147 }
148
149 static void done_stick(int id, int a, int v)
150 {
151     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
152         gui_pulse(gui_stick(id, v, 0), 1.2f);
153     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
154         gui_pulse(gui_stick(id, 0, v), 1.2f);
155 }
156
157 static int done_click(int b, int d)
158 {
159     if (b <= 0 && d == 1)
160         return done_action(gui_token(gui_click()));
161     return 1;
162 }
163
164 static int done_keybd(int c, int d)
165 {
166     if (d && c == SDLK_ESCAPE)
167         goto_state(&st_title);
168     return 1;
169 }
170
171 static int done_buttn(int b, int d)
172 {
173     if (d)
174     {
175         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
176             return done_click(0, 1);
177         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
178             return goto_state(&st_title);
179     }
180     return 1;
181 }
182
183 /*---------------------------------------------------------------------------*/
184
185 struct state st_done = {
186     done_enter,
187     done_leave,
188     done_paint,
189     done_timer,
190     done_point,
191     done_stick,
192     done_click,
193     done_keybd,
194     done_buttn,
195     1, 0
196 };
197