2
* Copyright (C) 2002-4 by the Widelands Development Team
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
#include "game_loader.h"
22
#include "game_main_menu_save_game.h"
23
#include "game_preload_data_packet.h"
24
#include "game_saver.h"
25
#include "interactive_player.h"
26
#include "ui_button.h"
27
#include "ui_editbox.h"
28
#include "ui_listselect.h"
29
#include "ui_modal_messagebox.h"
30
#include "ui_textarea.h"
34
Game_Main_Menu_Save_Game::Game_Main_Menu_Save_Game
36
Create all the buttons etc...
39
Game_Main_Menu_Save_Game::Game_Main_Menu_Save_Game(Interactive_Player* parent, UIUniqueWindowRegistry* registry)
40
: UIUniqueWindow(parent,registry,400,270,"Save Game") {
44
UITextarea* tt=new UITextarea(this, 0, 0, "Save Game", Align_Left);
45
tt->set_pos((get_inner_w()-tt->get_w())/2, 5);
54
m_ls=new UIListselect(this, posx, posy, get_inner_w()/2-spacing, get_inner_h()-spacing-offsy-60);
55
m_ls->selected.set(this, &Game_Main_Menu_Save_Game::selected);
56
m_ls->double_clicked.set(this, &Game_Main_Menu_Save_Game::double_clicked);
58
m_editbox=new UIEdit_Box(this, posx, posy+get_inner_h()-spacing-offsy-60+3, get_inner_w()/2-spacing, 20, 1, 0);
59
m_editbox->changed.set(this, &Game_Main_Menu_Save_Game::edit_box_changed);
61
// the descriptive areas
63
posx=get_inner_w()/2+spacing;
65
new UITextarea(this, posx, posy, 150, 20, "Map Name: ", Align_CenterLeft);
66
m_name=new UITextarea(this, posx+90, posy, 200, 20, "---", Align_CenterLeft);
70
new UITextarea(this, posx, posy, 150, 20, "Game Time: ", Align_CenterLeft);
71
m_gametime=new UITextarea(this, posx+90, posy, 200, 20, "---", Align_CenterLeft);
76
posy=get_inner_h()-30;
77
UIButton* but= new UIButton(this, get_inner_w()/2-spacing-80, posy, 80, 20, 0, 1);
78
but->clickedid.set(this, &Game_Main_Menu_Save_Game::clicked);
80
but->set_enabled(false);
82
but= new UIButton(this, get_inner_w()/2+spacing, posy, 80, 20, 1, 0);
83
but->clickedid.set(this, &Game_Main_Menu_Save_Game::clicked);
84
but->set_title("Cancel");
97
Game_Main_Menu_Save_Game::~Game_Main_Menu_Save_Game
99
Unregister from the registry pointer
102
Game_Main_Menu_Save_Game::~Game_Main_Menu_Save_Game()
108
called when the ok button has been clicked
111
void Game_Main_Menu_Save_Game::clicked(int id) {
114
std::string filename=m_editbox->get_text();
117
// Maybe a dir is selected
118
filename=static_cast<const char*>(m_ls->get_selection());
121
if(g_fs->IsDirectory(filename.c_str())) {
123
FS_CanonicalizeName(buffer, sizeof(buffer), filename.c_str());
129
if(save_game(filename))
139
* called when a item is selected
141
void Game_Main_Menu_Save_Game::selected(int i) {
142
const char* name=static_cast<const char*>(m_ls->get_selection());
144
if(!g_fs->IsDirectory(name)) {
145
Game_Loader gl(name, m_parent->get_game());
146
Game_Preload_Data_Packet gpdp;
147
gl.preload_game(&gpdp); // This has worked before, no problem
149
char* fname = strdup(FS_Filename(name));
150
FS_StripExtension(fname);
151
m_editbox->set_text(fname);
153
m_ok_btn->set_enabled(true);
155
m_name->set_text(gpdp.get_mapname());
158
uint gametime = gpdp.get_gametime();
160
int hours = gametime / 3600000;
161
gametime -= hours * 3600000;
162
int minutes = gametime / 60000;
164
sprintf(buf, "%02i:%02i", hours, minutes);
165
m_gametime->set_text(buf);
167
m_name->set_text("");
168
m_gametime->set_text("");
173
* An Item has been doubleclicked
175
void Game_Main_Menu_Save_Game::double_clicked(int) {
182
void Game_Main_Menu_Save_Game::fill_list(void) {
183
// Fill it with all files we find.
184
g_fs->FindFiles(m_curdir, "*", &m_gamefiles, 1);
186
for(filenameset_t::iterator pname = m_gamefiles.begin(); pname != m_gamefiles.end(); pname++) {
187
const char *name = pname->c_str();
188
if(!strcmp(FS_Filename(name),".")) continue;
189
if(!strcmp(FS_Filename(name),"..")) continue; // Upsy, appeared again. ignore
190
if(!g_fs->IsDirectory(name)) continue;
192
m_ls->add_entry(FS_Filename(name), reinterpret_cast<void*>(const_cast<char*>(name)), false, g_gr->get_picture(PicMod_Game, "pics/ls_dir.png", true));
195
Game_Preload_Data_Packet gpdp;
197
for(filenameset_t::iterator pname = m_gamefiles.begin(); pname != m_gamefiles.end(); pname++) {
198
const char *name = pname->c_str();
200
Game_Loader* gl = new Game_Loader(name,m_parent->get_game());
203
gl->preload_game(&gpdp);
204
char* fname = strdup(FS_Filename(name));
205
FS_StripExtension(fname);
206
m_ls->add_entry(fname, reinterpret_cast<void*>(const_cast<char*>(name)));
208
} catch(wexception& ) {
209
// we simply skip illegal entries
214
if(m_ls->get_nr_entries())
219
* The editbox was changed. Enable ok button
221
void Game_Main_Menu_Save_Game::edit_box_changed(void) {
222
m_ok_btn->set_enabled(true);
228
* returns true if dialog should close, false if it
231
bool Game_Main_Menu_Save_Game::save_game(std::string filename) {
233
// Make sure that the base directory exists
234
g_fs->EnsureDirectoryExists(m_basedir);
236
// ok, first check if the extension matches (ignoring case)
237
bool assign_extension=true;
238
if(filename.size() >= strlen(WLGF_SUFFIX)) {
239
char buffer[10]; // enough for the extension
240
filename.copy(buffer, sizeof(WLGF_SUFFIX), filename.size()-strlen(WLGF_SUFFIX));
241
if(!strncasecmp(buffer, WLGF_SUFFIX, strlen(WLGF_SUFFIX)))
242
assign_extension=false;
245
filename+=WLGF_SUFFIX;
247
// Now append directory name
248
std::string complete_filename=m_curdir;
249
complete_filename+="/";
250
complete_filename+=filename;
252
// Check if file exists, if it does, show a warning
253
if(g_fs->FileExists(complete_filename)) {
254
std::string s="A File with the name ";
255
s+=FS_Filename(filename.c_str());
256
s+=" exists already. Overwrite?";
257
UIModal_Message_Box* mbox= new UIModal_Message_Box(m_parent, "Save Game Error!!", s, UIModal_Message_Box::YESNO);
258
bool retval=mbox->run();
264
Game_Saver* gs=new Game_Saver(complete_filename.c_str(), m_parent->get_game());
267
} catch(std::exception& exe) {
268
std::string s="Game Saving Error!\nSaved Game-File may be corrupt!\n\nReason given:\n";
270
UIModal_Message_Box* mbox= new UIModal_Message_Box(m_parent, "Save Game Error!!", s, UIModal_Message_Box::OK);