From f4ffb776aa0d4f51c058d27fb0f51449c05302c3 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 8 Nov 2010 14:29:23 +0000 Subject: [PATCH] Implement removing vehicles from a train --- source/3d/layout.cpp | 8 +++++++ source/3d/layout.h | 1 + source/engineer/trainproperties.cpp | 35 ++++++++++++++++++++++++++--- source/engineer/trainproperties.h | 2 ++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index 05c398d..05aaa42 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -29,6 +29,7 @@ Layout3D::Layout3D(Layout &l): layout.signal_track_added.connect(sigc::mem_fun(this, &Layout3D::track_added)); layout.signal_track_removed.connect(sigc::mem_fun(this, &Layout3D::track_removed)); layout.signal_vehicle_added.connect(sigc::mem_fun(this, &Layout3D::vehicle_added)); + layout.signal_vehicle_removed.connect(sigc::mem_fun(this, &Layout3D::vehicle_removed)); const set <racks = layout.get_tracks(); for(set::iterator i=ltracks.begin(); i!=ltracks.end(); ++i) @@ -140,4 +141,11 @@ void Layout3D::vehicle_added(Vehicle &v) new Vehicle3D(*this, v); } +void Layout3D::vehicle_removed(Vehicle &v) +{ + VehicleMap::iterator i = vehicles.find(&v); + if(i!=vehicles.end()) + delete i->second; +} + } // namespace Marklin diff --git a/source/3d/layout.h b/source/3d/layout.h index f2de127..54269bc 100644 --- a/source/3d/layout.h +++ b/source/3d/layout.h @@ -57,6 +57,7 @@ private: void track_added(Track &); void track_removed(Track &); void vehicle_added(Vehicle &); + void vehicle_removed(Vehicle &); }; } // namespace Marklin diff --git a/source/engineer/trainproperties.cpp b/source/engineer/trainproperties.cpp index efbf842..bb8860b 100644 --- a/source/engineer/trainproperties.cpp +++ b/source/engineer/trainproperties.cpp @@ -23,7 +23,7 @@ TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t engineer(e), train(t) { - set_size(250, 275); + set_size(250, 305); GLtk::Label *label; add(*(label = new GLtk::Label(res, "Train properties"))); @@ -61,10 +61,16 @@ TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t drp_priority->append("Express passenger"); add(*(lst_vehicles = new GLtk::List(res))); - lst_vehicles->set_geometry(GLtk::Geometry(10, geom.h-205, geom.w-20, 100)); + lst_vehicles->set_geometry(GLtk::Geometry(10, 100, geom.w-20, geom.h-205)); + + GLtk::Button *btn; + + add(*(btn = new GLtk::Button(res, "Rem"))); + btn->set_geometry(GLtk::Geometry(10, 70, 40, 25)); + btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::remove_vehicle_clicked)); add(*(drp_new_vehicle = new GLtk::Dropdown(res))); - drp_new_vehicle->set_geometry(GLtk::Geometry(10, geom.h-230, geom.w-20, 20)); + drp_new_vehicle->set_geometry(GLtk::Geometry(10, 45, geom.w-20, 20)); drp_new_vehicle->append("(new vehicle)"); drp_new_vehicle->set_selected_index(0); for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i) @@ -108,6 +114,9 @@ void TrainProperties::on_ok_clicked() train->set_name(ent_name->get_text()); train->set_priority(drp_priority->get_selected_index()-2); + // The locomotive is vehicle 0 so we need to add 1 + for(set::const_iterator i=rem_vehicles.end(); i!=rem_vehicles.begin();) + train->remove_vehicle(*--i+1); for(vector::const_iterator i=add_vehicles.begin(); i!=add_vehicles.end(); ++i) train->add_vehicle(**i); } @@ -124,6 +133,26 @@ void TrainProperties::new_vehicle_selected(unsigned n, const string &) drp_new_vehicle->set_selected_index(0); } +void TrainProperties::remove_vehicle_clicked() +{ + int selected = lst_vehicles->get_selected_index(); + if(selected<0) + return; + + lst_vehicles->remove(selected); + + unsigned n_vehicles = train->get_n_vehicles()-rem_vehicles.size(); + if(static_cast(selected)>=n_vehicles) + add_vehicles.erase(add_vehicles.begin()+(selected-n_vehicles)); + else + { + for(set::const_iterator i=rem_vehicles.begin(); i!=rem_vehicles.end(); ++i) + if(*i<=static_cast(selected)) + ++selected; + rem_vehicles.insert(selected); + } +} + const VehicleType &TrainProperties::get_vehicle_type(unsigned n, bool loco) { const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles(); diff --git a/source/engineer/trainproperties.h b/source/engineer/trainproperties.h index c75015b..b69569f 100644 --- a/source/engineer/trainproperties.h +++ b/source/engineer/trainproperties.h @@ -27,12 +27,14 @@ private: Msp::GLtk::List *lst_vehicles; Msp::GLtk::Dropdown *drp_new_vehicle; std::vector add_vehicles; + std::set rem_vehicles; public: TrainProperties(Engineer &, const Msp::GLtk::Resources &, Marklin::Train *); private: virtual void on_ok_clicked(); void new_vehicle_selected(unsigned, const std::string &); + void remove_vehicle_clicked(); const Marklin::VehicleType &get_vehicle_type(unsigned, bool); }; -- 2.45.2