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.
22
#include "ui_textarea.h"
23
#include "ui_editbox.h"
24
#include "ui_button.h"
25
#include "ui_modal_messagebox.h"
26
#include "ui_listselect.h"
27
#include "ui_multilinetextarea.h"
29
#include "editorinteractive.h"
30
#include "editor_main_menu_load_map.h"
31
#include "widelands_map_loader.h"
32
#include "wexception.h"
34
#include "editor_game_base.h"
35
#include "editor_set_starting_pos_tool.h"
36
#include "overlay_manager.h"
38
#include "map_loader.h"
42
Main_Menu_Load_Map::Main_Menu_Load_Map
44
Create all the buttons etc...
47
Main_Menu_Load_Map::Main_Menu_Load_Map(Editor_Interactive *parent)
48
: UIWindow(parent, 0, 0, 500, 300, "Load Map")
53
UITextarea* tt=new UITextarea(this, 0, 0, "Load Map", Align_Left);
54
tt->set_pos((get_inner_w()-tt->get_w())/2, 5);
63
m_ls=new UIListselect(this, posx, posy, get_inner_w()/2-spacing, get_inner_h()-spacing-offsy-40);
64
m_ls->selected.set(this, &Main_Menu_Load_Map::selected);
65
m_ls->double_clicked.set(this, &Main_Menu_Load_Map::double_clicked);
67
// the descriptive areas
69
posx=get_inner_w()/2+spacing;
71
new UITextarea(this, posx, posy, 150, 20, "Name: ", Align_CenterLeft);
72
m_name=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
76
new UITextarea(this, posx, posy, 150, 20, "Author: ", Align_CenterLeft);
77
m_author=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
81
new UITextarea(this, posx, posy, 70, 20, "Size: ", Align_CenterLeft);
82
m_size=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
86
new UITextarea(this, posx, posy, 70, 20, "World: ", Align_CenterLeft);
87
m_world=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
91
new UITextarea(this, posx, posy, 70, 20, "Players: ", Align_CenterLeft);
92
m_nrplayers=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
97
new UITextarea(this, posx, posy, 70, 20, "Descr: ", Align_CenterLeft);
98
m_descr=new UIMultiline_Textarea(this, posx+70, posy, get_inner_w()-posx-spacing-70, get_inner_h()-posy-spacing-40, "---", Align_CenterLeft);
101
posy=get_inner_h()-30;
103
UIButton* but= new UIButton(this, get_inner_w()/2-spacing-80, posy, 80, 20, 0, 1);
104
but->clickedid.set(this, &Main_Menu_Load_Map::clicked);
105
but->set_title("OK");
107
but->set_enabled(false);
108
but= new UIButton(this, get_inner_w()/2+spacing, posy, 80, 20, 1, 0);
109
but->clickedid.set(this, &Main_Menu_Load_Map::clicked);
110
but->set_title("Cancel");
124
Main_Menu_Load_Map::~Main_Menu_Load_Map
126
Unregister from the registry pointer
129
Main_Menu_Load_Map::~Main_Menu_Load_Map()
135
called when the ok button has been clicked
138
void Main_Menu_Load_Map::clicked(int id) {
141
std::string filename=static_cast<const char*>(m_ls->get_selection());
143
if(g_fs->IsDirectory(filename.c_str())) {
145
FS_CanonicalizeName(buffer, sizeof(buffer), filename.c_str());
161
* called when a item is selected
163
void Main_Menu_Load_Map::selected(int i) {
164
const char* name=static_cast<const char*>(m_ls->get_selection());
166
m_ok_btn->set_enabled(true);
168
if(!g_fs->IsDirectory(name)) {
170
Map_Loader* m_ml = map->get_correct_loader(name);
171
m_ml->preload_map(true); // This has worked before, no problem
174
m_name->set_text(map->get_name());
175
m_author->set_text(map->get_author());
176
m_descr->set_text(map->get_description());
177
m_world->set_text(map->get_world_name());
180
sprintf(buf, "%i", map->get_nrplayers());
181
m_nrplayers->set_text(buf);
183
sprintf(buf, "%ix%i", map->get_width(), map->get_height());
184
m_size->set_text(buf);
188
m_name->set_text("");
189
m_author->set_text("");
190
m_descr->set_text("");
191
m_world->set_text("");
192
m_nrplayers->set_text("");
193
m_size->set_text("");
198
* An Item has been doubleclicked
200
void Main_Menu_Load_Map::double_clicked(int) {
207
void Main_Menu_Load_Map::fill_list(void) {
208
// Fill it with all files we find.
209
g_fs->FindFiles(m_curdir, "*", &m_mapfiles, 1);
211
// First, we add all directorys
212
// We manually add the parent directory
213
if(m_curdir!=m_basedir) {
216
strcpy(buffer, m_curdir.c_str());
217
strcat(buffer, "/..");
218
strcpy(buffer1,buffer);
219
FS_CanonicalizeName(buffer1, sizeof(buffer1), buffer);
221
m_ls->add_entry("<parent>", reinterpret_cast<void*>(const_cast<char*>(m_parentdir.c_str())), false, g_gr->get_picture(PicMod_Game, "pics/ls_dir.png", true));
224
for(filenameset_t::iterator pname = m_mapfiles.begin(); pname != m_mapfiles.end(); pname++) {
225
const char *name = pname->c_str();
226
if(!strcmp(FS_Filename(name),".")) continue;
227
if(!strcmp(FS_Filename(name),"..")) continue; // Upsy, appeared again. ignore
228
if(!strcmp(FS_Filename(name),"CVS")) continue; // HACK: we skip CVS dir (which is in normal checkout present) for aesthetic reasons
229
if(!g_fs->IsDirectory(name)) continue;
231
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));
236
for(filenameset_t::iterator pname = m_mapfiles.begin(); pname != m_mapfiles.end(); pname++) {
237
const char *name = pname->c_str();
239
Map_Loader* m_ml = map->get_correct_loader(name);
243
m_ml->preload_map(true);
245
switch(m_ml->get_type()) {
246
case Map_Loader::WLML: pic="pics/ls_wlmap.png"; break;
247
case Map_Loader::S2ML: pic="pics/ls_s2map.png"; break;
249
m_ls->add_entry(FS_Filename(name), reinterpret_cast<void*>(const_cast<char*>(name)), false, g_gr->get_picture(PicMod_Game, pic.c_str(), true));
250
} catch(wexception& ) {
251
// we simply skip illegal entries
258
if(m_ls->get_nr_entries())
265
void Main_Menu_Load_Map::load_map(std::string filename) {
266
Map* m_map=m_parent->get_editor()->get_map();
269
m_parent->get_editor()->cleanup_for_load(true, false);
271
Map_Loader* ml = m_map->get_correct_loader(filename.c_str());
274
//log("[Map_Loader] Loading map '%s'\n", realname.c_str());
275
ml->preload_map(true);
277
ml->load_map_complete(m_parent->get_editor(), true);
278
} catch(std::exception& exe) {
279
// This really shoudn't fail since maps are already preloaded (in map preview)
280
// and therefore valid, but if it does, a valid map must be displayed, therefore
281
// we create an empty one from scratch
282
m_parent->get_editor()->cleanup_for_load(true, false);
283
m_map->create_empty_map();
285
std::string s="Map Loading Error!\n\nReason given:\n";
287
UIModal_Message_Box* mbox= new UIModal_Message_Box(m_parent, "Load Map Error!!", s, UIModal_Message_Box::OK);
292
m_parent->get_editor()->postload();
293
m_parent->get_editor()->load_graphics();
295
// Now update all the visualisations
299
for(i=1; i<=m_parent->get_map()->get_nrplayers(); i++) {
300
text="pics/editor_player_";
301
text+=static_cast<char>(((i)/10) + 0x30);
302
text+=static_cast<char>(((i)%10) + 0x30);
303
text+="_starting_pos.png";
304
Coords fc=m_parent->get_map()->get_starting_pos(i);
306
if(fc.x==-1 && fc.y==-1) continue;
308
int picid=g_gr->get_picture(PicMod_Game, text.c_str(), true);
309
g_gr->get_picture_size(picid, &w, &h);
310
// only register, when theres no building there
311
BaseImmovable* imm = m_parent->get_map()->get_field(fc)->get_immovable();
312
if(imm && imm->get_type() == Map_Object::BUILDING) continue;
314
// no building, place overlay
315
m_parent->get_map()->get_overlay_manager()->register_overlay(fc,picid,8, Coords(w/2,STARTING_POS_HOTSPOT_Y));
318
/* Resources. we do not calculate default resources, therefore we do
319
* not expect to meet them here. */
321
for(y=0; y<m_map->get_height(); y++) {
322
for(x=0; x<m_map->get_width(); x++) {
323
Field *f=m_map->get_field(Coords(x,y));
324
int res=f->get_resources();
325
int amount=f->get_resources_amount();
326
std::string immname="";
328
immname = m_parent->get_editor()->get_map()->get_world()->get_resource(res)->get_editor_pic(amount);
330
int picid=g_gr->get_picture(PicMod_Game, immname.c_str(), true);
331
m_parent->get_map()->get_overlay_manager()->register_overlay(Coords(x,y),picid,4);
336
// Touch all triggers once, so that they do not get deleted even
338
for(i=0; i<m_map->get_number_of_triggers(); i++)
339
m_map->reference_trigger(m_map->get_trigger(i));
341
// Tell the interactive that the map is saved and all
342
m_parent->set_need_save(false);