825293b31f632f7b88c32dc8a99fdfbbdf3d78df
[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_start.h"
28 #include "st_name.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 #define DONE_OK   1
33 #define DONE_NAME 2
34
35 extern struct state st_done_bis;
36
37 static int high;
38 static int time_i;
39 static int coin_i;
40
41 static int done_action(int i)
42 {
43     audio_play(AUD_MENU, 1.0f);
44
45     switch (i)
46     {
47     case DONE_OK:
48         return goto_state(&st_start);
49
50     case DONE_NAME:
51         return goto_name(&st_done_bis, &st_done_bis);
52     }
53     return 1;
54 }
55
56 static int done_init(int * gidp)
57 {
58     const char *s1 = _("New Challenge Record");
59     const char *s2 = _("Challenge Complete");
60
61     int id, jd;
62
63     if ((id = gui_vstack(0)))
64     {
65         int gid;
66
67         if (high)
68             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
69         else
70             gid = gui_label(id, s2, GUI_MED, GUI_ALL, gui_blu, gui_grn);
71
72         if (gidp)
73             *gidp = gid;
74
75         gui_space(id);
76
77         if ((jd = gui_harray(id)))
78         {
79             gui_most_coins(jd, 3, 1);
80             gui_best_times(jd, 3, 1);
81         }
82
83         gui_space(id);
84         
85         if ((jd = gui_harray(id)))
86         {
87             if (high)
88                gui_state(jd, _("Change Player Name"), GUI_SML, DONE_NAME, 0);
89             gui_start(jd, _("OK"), GUI_SML, DONE_OK, 0);
90         }
91
92         gui_layout(id, 0, 0);
93     }
94
95     set_most_coins(0, coin_i);
96     set_best_times(0, time_i);
97
98     return id;
99 }
100
101 static int done_enter(void)
102 {
103     int gid, r;
104     
105     time_i = 3;
106     coin_i = 3;
107     high   = level_done(&time_i, &coin_i);
108     
109     r = done_init(&gid);
110     gui_pulse(gid, 1.2f);    
111     return r;
112 }
113
114 static int done_bis_enter(void)
115 {
116     char player[MAXNAM];
117     config_get_s(CONFIG_PLAYER, player, MAXNAM);
118     level_name(0, player, time_i, coin_i);
119     return done_init(NULL);
120 }
121
122 static void done_leave(int id)
123 {
124     gui_delete(id);
125 }
126
127 static void done_paint(int id, float st)
128 {
129     game_draw(0, st);
130     gui_paint(id);
131 }
132
133 static void done_timer(int id, float dt)
134 {
135     gui_timer(id, dt);
136     audio_timer(dt);
137 }
138
139 static void done_point(int id, int x, int y, int dx, int dy)
140 {
141     gui_pulse(gui_point(id, x, y), 1.2f);
142 }
143
144 static void done_stick(int id, int a, int v)
145 {
146     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
147         gui_pulse(gui_stick(id, v, 0), 1.2f);
148     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
149         gui_pulse(gui_stick(id, 0, v), 1.2f);
150 }
151
152 static int done_click(int b, int d)
153 {
154     if (b <= 0 && d == 1)
155         return done_action(gui_token(gui_click()));
156     return 1;
157 }
158
159 static int done_buttn(int b, int d)
160 {
161     if (d)
162     {
163         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
164             return done_click(0, 1);
165         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
166             return done_action(DONE_OK);
167     }
168     return 1;
169 }
170
171 /*---------------------------------------------------------------------------*/
172
173 struct state st_done = {
174     done_enter,
175     done_leave,
176     done_paint,
177     done_timer,
178     done_point,
179     done_stick,
180     done_click,
181     NULL,
182     done_buttn,
183     1, 0
184 };
185
186 struct state st_done_bis = {
187     done_bis_enter,
188     done_leave,
189     done_paint,
190     done_timer,
191     done_point,
192     done_stick,
193     done_click,
194     NULL,
195     done_buttn,
196     1, 0
197 };
198