1 /*
2 * confirmbox.c - confirm box
3 * Copyright (C) 2008-2010 Alexandre Martins <alemartf(at)gmail(dot)com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <math.h>
21 #include "confirmbox.h"
22 #include "../entities/actor.h"
23 #include "../entities/font.h"
24 #include "../core/video.h"
25 #include "../core/sprite.h"
26 #include "../core/input.h"
27 #include "../core/audio.h"
28 #include "../core/timer.h"
29 #include "../core/scene.h"
30 #include "../core/soundfactory.h"
31
32
33 /* private data */
34 #define MAX_OPTIONS 5
35 #define NO_OPTION -1
36 static image_t *box, *background;
37 static v2d_t boxpos;
38 static font_t *textfnt;
39 static font_t *optionfnt[MAX_OPTIONS][2];
40 static actor_t *icon;
41 static input_t *input;
42 static char text[1024], option[MAX_OPTIONS][128];
43 static int option_count;
44 static int current_option = NO_OPTION;
45 static int fxfade_in, fxfade_out;
46
47
48 /* public functions */
49
50 /*
51 * confirmbox_init()
52 * Initializes this scene. Please remember to
53 * call confirmbox_alert() before starting this
54 * scene!
55 */
confirmbox_init()56 void confirmbox_init()
57 {
58 int i;
59
60 background = image_create(video_get_backbuffer()->w, video_get_backbuffer()->h);
61 image_blit(video_get_backbuffer(), background, 0, 0, 0, 0, video_get_backbuffer()->w, video_get_backbuffer()->h);
62
63 box = sprite_get_image(sprite_get_animation("SD_CONFIRMBOX", 0), 0);
64 boxpos = v2d_new( (VIDEO_SCREEN_W-box->w)/2 , VIDEO_SCREEN_H );
65
66 input = input_create_user();
67 icon = actor_create();
68 actor_change_animation(icon, sprite_get_animation("SD_TITLEFOOT", 0));
69
70 textfnt = font_create(8);
71 font_set_text(textfnt, text);
72 font_set_width(textfnt, 164);
73
74 for(i=0; i<option_count; i++) {
75 optionfnt[i][0] = font_create(8);
76 optionfnt[i][1] = font_create(8);
77 font_set_text(optionfnt[i][0], option[i]);
78 font_set_text(optionfnt[i][1], "<color=ffff00>%s</color>", option[i]);
79 }
80
81 current_option = 0;
82 fxfade_in = TRUE;
83 fxfade_out = FALSE;
84 }
85
86
87 /*
88 * confirmbox_release()
89 * Releases the scene
90 */
confirmbox_release()91 void confirmbox_release()
92 {
93 int i;
94
95 for(i=0; i<option_count; i++) {
96 font_destroy(optionfnt[i][0]);
97 font_destroy(optionfnt[i][1]);
98 }
99
100 actor_destroy(icon);
101 input_destroy(input);
102 font_destroy(textfnt);
103 image_destroy(background);
104 }
105
106
107 /*
108 * confirmbox_update()
109 * Updates the scene
110 */
confirmbox_update()111 void confirmbox_update()
112 {
113 int i;
114 float dt = timer_get_delta(), speed = 5*VIDEO_SCREEN_H;
115
116 /* fade-in */
117 if(fxfade_in) {
118 if( boxpos.y <= (VIDEO_SCREEN_H-box->h)/2 )
119 fxfade_in = FALSE;
120 else
121 boxpos.y -= speed*dt;
122 }
123
124 /* fade-out */
125 if(fxfade_out) {
126 if( boxpos.y >= VIDEO_SCREEN_H ) {
127 fxfade_out = FALSE;
128 scenestack_pop();
129 return;
130 }
131 else
132 boxpos.y += speed*dt;
133 }
134
135 /* positioning stuff */
136 icon->position = v2d_new(boxpos.x + current_option*box->w/option_count + 10, boxpos.y + box->h*0.75 - 1);
137 textfnt->position = v2d_new(boxpos.x + 10 , boxpos.y + 10);
138 for(i=0; i<option_count; i++) {
139 optionfnt[i][0]->position = v2d_new(boxpos.x + i*box->w/option_count + 25, boxpos.y + box->h*0.75);
140 optionfnt[i][1]->position = optionfnt[i][0]->position;
141 }
142
143 /* input */
144 if(!fxfade_in && !fxfade_out) {
145 if(input_button_pressed(input, IB_LEFT)) {
146 /* left */
147 sound_play( soundfactory_get("choose") );
148 current_option = ( ((current_option-1)%option_count) + option_count )%option_count;
149 }
150 else if(input_button_pressed(input, IB_RIGHT)) {
151 /* right */
152 sound_play( soundfactory_get("choose") );
153 current_option = (current_option+1)%option_count;
154 }
155 else if(input_button_pressed(input, IB_FIRE1) || input_button_pressed(input, IB_FIRE3)) {
156 /* confirm */
157 sound_play( soundfactory_get("select") );
158 fxfade_out = TRUE;
159 }
160 }
161 }
162
163
164
165 /*
166 * confirmbox_render()
167 * Renders the scene
168 */
confirmbox_render()169 void confirmbox_render()
170 {
171 int i, k;
172 v2d_t cam = v2d_new(VIDEO_SCREEN_W/2, VIDEO_SCREEN_H/2);
173
174 image_blit(background, video_get_backbuffer(), 0, 0, 0, 0, background->w, background->h);
175 image_draw(box, video_get_backbuffer(), boxpos.x, boxpos.y, IF_NONE);
176 font_render(textfnt, cam);
177
178 for(i=0; i<option_count; i++) {
179 k = (i==current_option) ? 1 : 0;
180 font_render(optionfnt[i][k], cam);
181 }
182
183 actor_render(icon, cam);
184 }
185
186
187
188
189 /*
190 * confirmbox_alert()
191 * Configures this scene (call me before initializing this scene!)
192 * PS: option2 may be NULL
193 */
confirmbox_alert(char * ptext,char * option1,char * option2)194 void confirmbox_alert(char *ptext, char *option1, char *option2)
195 {
196 current_option = -1;
197 strcpy(text, ptext);
198 strcpy(option[0], option1);
199
200 if(option2) {
201 strcpy(option[1], option2);
202 option_count = 2;
203 }
204 else
205 option_count = 1;
206 }
207
208
209 /*
210 * confirmbox_selected_option()
211 * Returns the selected option (1, 2, ..., n), or
212 * 0 if nothing has been selected.
213 * This must be called AFTER this scene
214 * gets released
215 */
confirmbox_selected_option()216 int confirmbox_selected_option()
217 {
218 if(current_option != NO_OPTION) {
219 int ret = current_option + 1;
220 current_option = NO_OPTION;
221 return ret;
222 }
223 else
224 return 0; /* nothing */
225 }
226
227