1
// $Id: object_selector.cxx,v 1.37 2003/03/30 13:12:35 grumbel Exp $
3
// Pingus - A free Lemmings clone
4
// Copyright (C) 1999 Ingo Ruhnke <grumbel@gmx.de>
6
// This program is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU General Public License
8
// as published by the Free Software Foundation; either version 2
9
// of the License, or (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
#include <ClanLib/Core/IOData/directory_scanner.h>
21
#include <ClanLib/Core/Resources/resource_manager.h>
22
#include <ClanLib/Core/System/system.h>
23
#include <ClanLib/Display/Display/display.h>
24
#include <ClanLib/Display/Font/font.h>
25
#include <ClanLib/Display/Input/inputbuffer.h>
26
#include <ClanLib/Display/Input/mouse.h>
27
#include "../console.hxx"
28
#include "../globals.hxx"
29
#include "../loading.hxx"
30
#include "../my_gettext.hxx"
31
#include "../path_manager.hxx"
32
#include "../pingus_resource.hxx"
33
#include "../string_converter.hxx"
34
#include "../system.hxx"
35
#include "object_selector.hxx"
36
#include "object_manager.hxx"
37
#include "string_reader.hxx"
38
#include "thumb_cache.hxx"
39
#include "weather_obj.hxx"
40
#include "../prefab.hxx"
42
#include "../worldobjsdata/entrance_data.hxx"
43
#include "../worldobjsdata/exit_data.hxx"
44
#include "../worldobjsdata/groundpiece_data.hxx"
45
#include "../worldobjsdata/hotspot_data.hxx"
46
#include "../worldobjsdata/liquid_data.hxx"
47
#include "../worldobjsdata/solid_color_background_data.hxx"
48
#include "../worldobjsdata/starfield_background_data.hxx"
49
#include "../worldobjsdata/surface_background_data.hxx"
50
#include "../worldobjsdata/thunderstorm_background_data.hxx"
52
#include "../editorobjs/bumper_obj.hxx"
53
#include "../editorobjs/conveyor_belt_obj.hxx"
54
#include "../editorobjs/exit_obj.hxx"
55
#include "../editorobjs/fake_exit_obj.hxx"
56
#include "../editorobjs/guillotine_obj.hxx"
57
#include "../editorobjs/groundpiece_obj.hxx"
58
#include "../editorobjs/hammer_obj.hxx"
59
#include "../editorobjs/hotspot_obj.hxx"
60
#include "../editorobjs/ice_block_obj.hxx"
61
#include "../editorobjs/info_box_obj.hxx"
62
#include "../editorobjs/laser_exit_obj.hxx"
63
#include "../editorobjs/smasher_obj.hxx"
64
#include "../editorobjs/spike_obj.hxx"
65
#include "../editorobjs/switch_door_obj.hxx"
66
#include "../editorobjs/teleporter_obj.hxx"
68
using namespace EditorObjs;
69
using namespace WorldObjsData;
73
ObjectSelector::ObjectSelector ()
75
last_object = "GroundPieces/";
76
font = PingusResource::load_font("Fonts/courier_small", "fonts");
77
//data_loaded = false;
80
ObjectSelector::~ObjectSelector ()
84
/** FIXME: Ugly interface, the arguments should not be the offset, but
85
instead the absolute position */
87
ObjectSelector::get_obj (ObjectManager* arg_obj_mgr,
90
// FIXME: Sick coordinate handling...
94
obj_mgr = arg_obj_mgr;
95
pos = Vector (CL_Mouse::get_x () - x_offset,
96
CL_Mouse::get_y () - y_offset,
103
ObjectSelector::get_trap ()
105
CL_Display::clear_display();
106
font->print_left(20, 20, _("1 - guillotine"));
107
font->print_left(20, 50, _("2 - hammer"));
108
font->print_left(20, 80, _("3 - spike"));
109
font->print_left(20,110, _("4 - laser_exit"));
110
font->print_left(20,140, _("5 - fake_exit"));
111
font->print_left(20,170, _("6 - smasher"));
112
font->print_left(20,200, _("7 - bumper"));
113
Display::flip_display();
120
obj_mgr->add(GuillotineObj::create(pos));
123
obj_mgr->add(HammerObj::create(pos));
126
obj_mgr->add(SpikeObj::create(pos));
129
obj_mgr->add(LaserExitObj::create(pos));
132
obj_mgr->add(FakeExitObj::create(pos));
135
obj_mgr->add(SmasherObj::create(pos));
138
obj_mgr->add(BumperObj::create(pos));
148
ObjectSelector::get_groundpiece (const Groundtype::GPType& gptype)
150
GroundpieceData data;
151
std::string datafile = std::string("groundpieces-") + Groundtype::type_to_string (gptype);
155
std::string str = select_surface(datafile);
159
data.desc = ResDescriptor(str, datafile, ResDescriptor::RD_RESOURCE);
160
data.gptype = gptype;
162
obj_mgr->add(new EditorObjs::GroundpieceObj(data));
167
ObjectSelector::get_hotspot (const std::string& filename)
171
std::string str = select_surface(filename);
175
data.desc = ResDescriptor(str, filename, ResDescriptor::RD_RESOURCE);
179
data.insert_EditorObjs(obj_mgr);
183
ObjectSelector::get_worldobj ()
185
CL_Display::clear_display();
186
font->print_left(20, 20, _("Select a WorldObj"));
187
font->print_left(20, 50, _("1 - teleporter"));
188
font->print_left(20, 70, _("2 - switch and door"));
189
font->print_left(20, 90, _("3 - ConveyorBelt"));
190
font->print_left(20, 110, _("4 - IceBlock"));
191
font->print_left(20, 130, _("5 - InfoBox"));
192
//font->print_left(20, 110, _("5 - Liquid"));
193
Display::flip_display();
200
obj_mgr->add(TeleporterObj::create(pos));
204
obj_mgr->add(SwitchDoorObj::create(pos));
208
obj_mgr->add(ConveyorBeltObj::create(pos));
212
obj_mgr->add(IceBlockObj::create(pos));
216
obj_mgr->add(InfoBoxObj::create(pos));
226
ObjectSelector::get_weather ()
231
CL_Display::clear_display();
232
font->print_left(20, 20, _("Select a weather"));
233
font->print_left(20, 50, _("1 - snow"));
234
font->print_left(20, 70, _("2 - rain"));
235
Display::flip_display();
242
type = "snow-generator";
246
type = "rain-generator";
252
std::cout << "Weather objects currently not implemented" << std::endl;
254
obj_mgr->add(new WeatherObj(type));
258
ObjectSelector::get_entrance ()
260
EntranceData entrance;
261
bool have_name = false;
264
CL_Display::clear_display();
265
font->print_left(20, 20, _("Select an entrance"));
266
font->print_left(20, 50, _("1 - generic"));
267
font->print_left(20, 70, _("2 - woodthing"));
268
font->print_left(20, 90, _("3 - cloud"));
269
font->print_left(20, 120, _("h - entrance surface (hotspot)"));
270
Display::flip_display();
278
entrance.type = "generic";
283
entrance.type = "woodthing";
288
entrance.type = "cloud";
294
get_hotspot("entrances");
298
if (verbose) std::cout << "Unknown keypressed" << std::endl;
303
entrance.insert_EditorObjs (obj_mgr);
307
ObjectSelector::get_exit ()
313
str = select_surface("exits");
320
data.desc = ResDescriptor(str, "exits", ResDescriptor::RD_RESOURCE);
322
data.insert_EditorObjs (obj_mgr);
326
ObjectSelector::get_liquid ()
328
std::cout << "ObjectSelector::get_liquid() not implemented" << std::endl;
332
data.old_width_handling = false;
334
data.desc = ResDescriptor("Liquid/slime", "liquids", ResDescriptor::RD_RESOURCE);
336
data.insert_EditorObjs (obj_mgr);
340
ObjectSelector::get_from_file ()
342
CL_Display::clear_display();
343
font->print_left(20, 20, _("What object type do you want?"));
344
font->print_left(20, 50, _("h - Hotspot"));
345
font->print_left(20, 70, _("g - Groundpiece (ground) [not implemented]"));
346
Display::flip_display();
354
System::Directory dir = System::opendir (System::get_statdir () + "images/");
355
std::list<std::string> strs;
357
for (System::DirectoryIter i = dir.begin (); i != dir.end (); ++i)
359
if (i->type == System::DirectoryEntry::DE_FILE)
361
//std::cout << "Directory entry: " << System::get_statdir() + "images/" + i->name << std::endl;
362
strs.push_back (System::get_statdir() + "images/" + i->name);
366
StringReader reader("Input the image name:",
367
System::get_statdir () + "images/");
368
reader.set_strings(&strs);
369
std::string file = reader.read_string();
372
// FIXME: Ugly hack, since ClanLib appends './'
373
data.desc = ResDescriptor ("../../../../../../../../../../../" + file,
374
"", ResDescriptor::RD_FILE);
375
data.insert_EditorObjs(obj_mgr);
381
console << "ObjectSelector: Inserting groundpieces is not implemented" << std::endl;
391
ObjectSelector::select_obj_type ()
393
CL_Display::clear_display();
394
font->print_left(20, 20, _("Which object do you want?"));
395
font->print_left(20, 70, _("g - Groundpiece (ground)"));
396
font->print_left(20, 90, _("s - Groundpiece (solid)"));
397
font->print_left(20,110, _("b - Groundpiece (bridge)"));
398
font->print_left(20,130, _("n - Groundpiece (transparent)"));
399
font->print_left(20,150, _("r - Groundpiece (remove)"));
400
font->print_left(20,170, _("h - Hotspot"));
401
font->print_left(20,190, _("e - Entrance"));
402
font->print_left(20,210, _("x - Exit"));
403
font->print_left(20,230, _("l - Liquid"));
404
font->print_left(20,250, _("w - Weather"));
405
font->print_left(20,270, _("t - Traps"));
406
font->print_left(20,290, _("o - WorldObject"));
407
font->print_left(20,310, _("z - Background"));
408
font->print_left(20,330, _("p - Prefab (ObjectGroup)"));
409
font->print_left(20,350, _("f - something from file (~/.pingus/images/)"));
410
Display::flip_display();
421
get_groundpiece(Groundtype::GP_BRIDGE);
425
get_groundpiece(Groundtype::GP_REMOVE);
429
get_groundpiece(Groundtype::GP_SOLID);
433
get_groundpiece(Groundtype::GP_GROUND);
437
get_groundpiece(Groundtype::GP_TRANSPARENT);
441
get_hotspot("hotspots");
453
std::cout << "ObjectSelector: Liquid not implemented" << std::endl;
485
ObjectSelector::get_prefab ()
487
CL_DirectoryScanner dir;
488
std::vector<std::string> dir_lst;
489
if (dir.scan(path_manager.complete ("prefabs/"), "*.xml"))
491
while (dir.next ()) {
492
std::string filename = dir.get_name ();
493
dir_lst.push_back (filename.substr(0, filename.length()-4));
497
CL_Display::clear_display();
498
font->print_left(20, 20, _("Which prefab do you want?"));
500
for (std::vector<std::string>::size_type i = 0; i < dir_lst.size (); ++i)
502
font->print_left(20, 60 + i * 30, to_string(i + 1) + " - " + dir_lst[i]);
505
Display::flip_display();
545
if ((item-1) < static_cast<int>(dir_lst.size()))
547
std::cout << "ObjectSelector: Inserting prefab: " << dir_lst[item-1] << std::endl;
548
Prefab* prefab = Prefab::create(dir_lst[item-1]);
549
prefab->get_data()->insert_EditorObjs(obj_mgr);
554
std::cout << "ObjectSelector: invalid prefab selected" << std::endl;
559
ObjectSelector::get_background ()
561
CL_Display::clear_display();
562
font->print_left(20, 20, _("Which object do you want?"));
563
font->print_left(20, 50, _("1 - Surface Background"));
564
font->print_left(20, 70, _("2 - Solid Color Background"));
565
font->print_left(20, 90, _("3 - Starfield Background"));
566
font->print_left(20,110, _("4 - Thunderstorm Background"));
567
Display::flip_display();
575
SurfaceBackgroundData data;
576
data.desc.datafile = "textures";
577
data.desc.res_name = select_surface("textures");
579
if (!data.desc.res_name.empty())
581
data.insert_EditorObjs(obj_mgr);
587
SolidColorBackgroundData().insert_EditorObjs(obj_mgr);
590
StarfieldBackgroundData().insert_EditorObjs(obj_mgr);
593
ThunderstormBackgroundData ().insert_EditorObjs(obj_mgr);
600
ObjectSelector::select_surface (std::vector<surface_obj>& sur_list)
602
std::cout << "ObjectSelector: Selecting surface out of: " << sur_list.size () << std::endl;
603
SurfaceSelector sur_selector(&sur_list);
605
return sur_selector.select();
609
ObjectSelector::select_surface (const std::string & resource_file)
612
bool datafile_loaded;
613
CL_ResourceManager* res = PingusResource::get(resource_file);
614
GroundpieceData data;
616
datafile_loaded = data_loaded[resource_file];
620
std::list<std::string>* liste = res->get_resources_of_type("surface");
622
std::vector<surface_obj> sur_list;
625
for(std::list<std::string>::iterator i = liste->begin(); i != liste->end(); ++i)
627
// FIXME: This should be moved to ThumbCache
630
sur_obj.datafile = resource_file;
631
sur_obj.thumbnail = ThumbCache::load (*i, resource_file);
633
sur_list.push_back(sur_obj);
635
if (!datafile_loaded && (j % 25) == 0)
637
loading_screen.draw_progress(i->c_str(), (float)j / liste->size());
640
// Showing the mousecursor again, since loading_screen hides it
641
Display::show_cursor();
642
data_loaded[resource_file] = true;
643
//data_loaded = false;
644
return select_surface(sur_list);
648
ObjectSelector::read_key ()
657
CL_System::keep_alive();
659
if (keys.peek_key().state != CL_Key::NoKey)
661
key = keys.get_key();
662
//std::cout << "key: " << key.ascii << std::endl;
669
ObjectSelector::read_string (const std::string & description, const std::string & def_str)
671
StringReader reader(description, def_str);
672
reader.set_strings(PingusResource::get("global")->get_resources_of_type("surface"));
673
return reader.read_string();
676
} // namespace EditorNS