~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/editor/ui_menus/editor_main_menu_load_map.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-4 by the Widelands Development Team
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
18
 */
 
19
 
 
20
#include <stdio.h>
 
21
#include <string>
 
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"
 
28
#include "editor.h"
 
29
#include "editorinteractive.h"
 
30
#include "editor_main_menu_load_map.h"
 
31
#include "widelands_map_loader.h"
 
32
#include "wexception.h"
 
33
#include "error.h"
 
34
#include "editor_game_base.h"
 
35
#include "editor_set_starting_pos_tool.h"
 
36
#include "overlay_manager.h"
 
37
#include "world.h"
 
38
#include "map_loader.h"
 
39
 
 
40
/*
 
41
===============
 
42
Main_Menu_Load_Map::Main_Menu_Load_Map
 
43
 
 
44
Create all the buttons etc...
 
45
===============
 
46
*/
 
47
Main_Menu_Load_Map::Main_Menu_Load_Map(Editor_Interactive *parent)
 
48
        : UIWindow(parent, 0, 0, 500, 300, "Load Map")
 
49
{
 
50
   m_parent=parent;
 
51
 
 
52
   // Caption
 
53
   UITextarea* tt=new UITextarea(this, 0, 0, "Load Map", Align_Left);
 
54
   tt->set_pos((get_inner_w()-tt->get_w())/2, 5);
 
55
 
 
56
   int spacing=5;
 
57
   int offsx=spacing;
 
58
   int offsy=30;
 
59
   int posx=offsx;
 
60
   int posy=offsy;
 
61
 
 
62
   // listselect
 
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);
 
66
 
 
67
   // the descriptive areas
 
68
   // Name
 
69
   posx=get_inner_w()/2+spacing;
 
70
   posy+=20;
 
71
   new UITextarea(this, posx, posy, 150, 20, "Name: ", Align_CenterLeft);
 
72
   m_name=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
 
73
   posy+=20+spacing;
 
74
 
 
75
   // Author
 
76
   new UITextarea(this, posx, posy, 150, 20, "Author: ", Align_CenterLeft);
 
77
   m_author=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
 
78
   posy+=20+spacing;
 
79
 
 
80
   // Size
 
81
   new UITextarea(this, posx, posy, 70, 20, "Size: ", Align_CenterLeft);
 
82
   m_size=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
 
83
   posy+=20+spacing;
 
84
 
 
85
   // World
 
86
   new UITextarea(this, posx, posy, 70, 20, "World: ", Align_CenterLeft);
 
87
   m_world=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
 
88
   posy+=20+spacing;
 
89
 
 
90
   // Players
 
91
   new UITextarea(this, posx, posy, 70, 20, "Players: ", Align_CenterLeft);
 
92
   m_nrplayers=new UITextarea(this, posx+70, posy, 200, 20, "---", Align_CenterLeft);
 
93
   posy+=20+spacing;
 
94
 
 
95
 
 
96
   // Description
 
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);
 
99
 
 
100
   posx=5;
 
101
   posy=get_inner_h()-30;
 
102
 
 
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");
 
106
   m_ok_btn=but;
 
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");
 
111
 
 
112
 
 
113
   m_basedir="maps";
 
114
   m_curdir="maps";
 
115
 
 
116
   fill_list();
 
117
 
 
118
   center_to_parent();
 
119
   move_to_top();
 
120
}
 
121
 
 
122
/*
 
123
===============
 
124
Main_Menu_Load_Map::~Main_Menu_Load_Map
 
125
 
 
126
Unregister from the registry pointer
 
127
===============
 
128
*/
 
129
Main_Menu_Load_Map::~Main_Menu_Load_Map()
 
130
{
 
131
}
 
132
 
 
133
/*
 
134
===========
 
135
called when the ok button has been clicked
 
136
===========
 
137
*/
 
138
void Main_Menu_Load_Map::clicked(int id) {
 
139
   if(id==1) {
 
140
      // Ok
 
141
      std::string filename=static_cast<const char*>(m_ls->get_selection());
 
142
 
 
143
      if(g_fs->IsDirectory(filename.c_str())) {
 
144
         char buffer[256];
 
145
         FS_CanonicalizeName(buffer, sizeof(buffer), filename.c_str());
 
146
         m_curdir=buffer;
 
147
         m_ls->clear();
 
148
         m_mapfiles.clear();
 
149
         fill_list();
 
150
      } else {
 
151
         load_map(filename);
 
152
         die();
 
153
      }
 
154
   } else {
 
155
      // Cancel
 
156
      die();
 
157
   }
 
158
}
 
159
 
 
160
/*
 
161
 * called when a item is selected
 
162
 */
 
163
void Main_Menu_Load_Map::selected(int i) {
 
164
   const char* name=static_cast<const char*>(m_ls->get_selection());
 
165
 
 
166
   m_ok_btn->set_enabled(true);
 
167
 
 
168
   if(!g_fs->IsDirectory(name)) {
 
169
      Map* map=new Map();
 
170
      Map_Loader* m_ml = map->get_correct_loader(name);
 
171
      m_ml->preload_map(true); // This has worked before, no problem
 
172
      delete m_ml;
 
173
 
 
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());
 
178
 
 
179
      char buf[200];
 
180
      sprintf(buf, "%i", map->get_nrplayers());
 
181
      m_nrplayers->set_text(buf);
 
182
 
 
183
      sprintf(buf, "%ix%i", map->get_width(), map->get_height());
 
184
      m_size->set_text(buf);
 
185
 
 
186
      delete map;
 
187
   } else {
 
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("");
 
194
   }
 
195
}
 
196
 
 
197
/*
 
198
 * An Item has been doubleclicked
 
199
 */
 
200
void Main_Menu_Load_Map::double_clicked(int) {
 
201
   clicked(1);
 
202
}
 
203
 
 
204
/*
 
205
 * fill the file list
 
206
 */
 
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);
 
210
  
 
211
   // First, we add all directorys
 
212
   // We manually add the parent directory
 
213
   if(m_curdir!=m_basedir) {
 
214
      char buffer[256];
 
215
      char buffer1[256];
 
216
      strcpy(buffer, m_curdir.c_str());
 
217
      strcat(buffer, "/..");
 
218
      strcpy(buffer1,buffer);
 
219
      FS_CanonicalizeName(buffer1, sizeof(buffer1), buffer);
 
220
      m_parentdir=buffer1;
 
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));
 
222
   }
 
223
 
 
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;
 
230
 
 
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));
 
232
   }
 
233
  
 
234
   Map* map=new Map();
 
235
 
 
236
   for(filenameset_t::iterator pname = m_mapfiles.begin(); pname != m_mapfiles.end(); pname++) {
 
237
      const char *name = pname->c_str();
 
238
      
 
239
      Map_Loader* m_ml = map->get_correct_loader(name);
 
240
      if(!m_ml) continue; 
 
241
 
 
242
      try {
 
243
         m_ml->preload_map(true);
 
244
         std::string pic="";
 
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;
 
248
         }
 
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
 
252
      }
 
253
      delete m_ml;
 
254
 
 
255
   }
 
256
   delete map;
 
257
   
 
258
   if(m_ls->get_nr_entries())
 
259
      m_ls->select(0);
 
260
}
 
261
 
 
262
/*
 
263
 * Load map complete
 
264
 */
 
265
void Main_Menu_Load_Map::load_map(std::string filename) {
 
266
   Map* m_map=m_parent->get_editor()->get_map();
 
267
 
 
268
   if(filename!="") {
 
269
      m_parent->get_editor()->cleanup_for_load(true, false);
 
270
 
 
271
      Map_Loader* ml = m_map->get_correct_loader(filename.c_str());
 
272
 
 
273
      try {
 
274
         //log("[Map_Loader] Loading map '%s'\n", realname.c_str());
 
275
         ml->preload_map(true);
 
276
 
 
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();
 
284
 
 
285
         std::string s="Map Loading Error!\n\nReason given:\n";
 
286
         s+=exe.what();
 
287
         UIModal_Message_Box* mbox= new UIModal_Message_Box(m_parent, "Load Map Error!!", s, UIModal_Message_Box::OK);
 
288
         mbox->run();
 
289
         delete mbox;
 
290
      }
 
291
 
 
292
      m_parent->get_editor()->postload();
 
293
      m_parent->get_editor()->load_graphics();
 
294
 
 
295
      // Now update all the visualisations
 
296
      // Player positions
 
297
      std::string text;
 
298
      int i=0;
 
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);
 
305
 
 
306
         if(fc.x==-1 && fc.y==-1) continue;
 
307
         int w, h;
 
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;
 
313
         
 
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));
 
316
      }
 
317
 
 
318
      /* Resources. we do not calculate default resources, therefore we do
 
319
       * not expect to meet them here. */
 
320
      uint x,y;
 
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="";
 
327
            if(amount)
 
328
               immname = m_parent->get_editor()->get_map()->get_world()->get_resource(res)->get_editor_pic(amount);
 
329
            if(immname!="") {
 
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);
 
332
            }
 
333
         }
 
334
      }
 
335
 
 
336
      // Touch all triggers once, so that they do not get deleted even
 
337
      // when unreferenced
 
338
      for(i=0; i<m_map->get_number_of_triggers(); i++)
 
339
         m_map->reference_trigger(m_map->get_trigger(i));
 
340
 
 
341
      // Tell the interactive that the map is saved and all
 
342
      m_parent->set_need_save(false);
 
343
 
 
344
      delete ml;
 
345
   }
 
346
}