X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2F3d%2Flayout.cpp;h=2927e446741df4e1ad2ae194949831f449cc2afc;hb=e3e3a940c75dcad126d5cf08f0802efdab914568;hp=05c398d302a33b3fb22bcc968d82d90f21409abe;hpb=6e5d36dbc3f1e4a221d424fa7d57b07998df67a8;p=r2c2.git diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index 05c398d..2927e44 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -1,54 +1,53 @@ -/* $Id$ - -This file is part of the MSP Märklin suite -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - -#include -#include -#include -#include -#include -#include -#include -#include #include "layout.h" +#include "signal.h" #include "track.h" #include "vehicle.h" using namespace std; using namespace Msp; -namespace Marklin { +namespace R2C2 { Layout3D::Layout3D(Layout &l): layout(l), catalogue(layout.get_catalogue()) { - 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)); + // South, 15° from zenith + sun.set_position(0, -0.259, 0.966, 0); + lighting.attach(0, sun); - const set <racks = layout.get_tracks(); - for(set::iterator i=ltracks.begin(); i!=ltracks.end(); ++i) - track_added(**i); + layout.signal_object_added.connect(sigc::mem_fun(this, &Layout3D::object_added)); + layout.signal_object_removed.connect(sigc::mem_fun(this, &Layout3D::object_removed)); + + const set &lobjs = layout.get_all(); + for(set::iterator i=lobjs.begin(); i!=lobjs.end(); ++i) + object_added(**i); } Layout3D::~Layout3D() { + while(!signals.empty()) + delete signals.begin()->second; while(!tracks.empty()) delete tracks.begin()->second; while(!vehicles.empty()) delete vehicles.begin()->second; } -void Layout3D::add_track(Track3D &t) +void Layout3D::get_bounds(Vector &minp, Vector &maxp) const { - if(tracks.count(&t.get_track())) - throw KeyError("Duplicate track"); + Geometry::BoundingBox bbox; + + for(TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + bbox = bbox|i->second->get_track().get_type().get_shape()->get_axis_aligned_bounding_box(); + + minp = bbox.get_minimum_point(); + maxp = bbox.get_maximum_point(); +} - tracks[&t.get_track()] = &t; +void Layout3D::add_track(Track3D &t) +{ + insert_unique(tracks, &t.get_track(), &t); } void Layout3D::remove_track(Track3D &t) @@ -58,55 +57,12 @@ void Layout3D::remove_track(Track3D &t) Track3D &Layout3D::get_track(Track &t) const { - TrackMap::const_iterator i = tracks.find(&t); - if(i==tracks.end()) - throw KeyError("Unknown track"); - - return *i->second; -} - -Track3D *Layout3D::pick_track(float x, float y, float size) const -{ - vector select_buf; - GL::select_buffer(select_buf); - GL::render_mode(GL::SELECT); - - { - GL::PushMatrix push_mat; - GL::load_identity(); - - GL::ClipPlane(1, 0, x-size, 0).apply_to(0); - GL::ClipPlane(-1, 0, -x-size, 0).apply_to(1); - GL::ClipPlane(0, 1, y-size, 0).apply_to(2); - GL::ClipPlane(0, -1, -y-size, 0).apply_to(3); - } - - scene.render(0); - - GL::ClipPlane::disable(0); - GL::ClipPlane::disable(1); - GL::ClipPlane::disable(2); - GL::ClipPlane::disable(3); - - GL::render_mode(GL::RENDER); - Track3D *track = 0; - unsigned track_depth = numeric_limits::max(); - for(vector::iterator i=select_buf.begin(); i!=select_buf.end(); ++i) - if(i->min_depthnames.empty()) - { - track = reinterpret_cast(i->names.back()); - track_depth = i->min_depth; - } - - return track; + return *get_item(tracks, &t); } void Layout3D::add_vehicle(Vehicle3D &v) { - if(vehicles.count(&v.get_vehicle())) - throw KeyError("Duplicate vehicle"); - - vehicles[&v.get_vehicle()] = &v; + insert_unique(vehicles, &v.get_vehicle(), &v); } void Layout3D::remove_vehicle(Vehicle3D &v) @@ -116,28 +72,54 @@ void Layout3D::remove_vehicle(Vehicle3D &v) Vehicle3D &Layout3D::get_vehicle(Vehicle &v) const { - VehicleMap::const_iterator i = vehicles.find(&v); - if(i==vehicles.end()) - throw KeyError("Unknown vehicle"); + return *get_item(vehicles, &v); +} - return *i->second; +void Layout3D::add_signal(Signal3D &s) +{ + insert_unique(signals, &s.get_signal(), &s); } -void Layout3D::track_added(Track &t) +void Layout3D::remove_signal(Signal3D &s) { - new Track3D(*this, t); + signals.erase(&s.get_signal()); } -void Layout3D::track_removed(Track &t) +Signal3D &Layout3D::get_signal(Signal &s) const { - TrackMap::iterator i = tracks.find(&t); - if(i!=tracks.end()) - delete i->second; + return *get_item(signals, &s); } -void Layout3D::vehicle_added(Vehicle &v) +void Layout3D::object_added(Object &o) { - new Vehicle3D(*this, v); + if(Track *t = dynamic_cast(&o)) + new Track3D(*this, *t); + else if(Signal *s = dynamic_cast(&o)) + new Signal3D(*this, *s); + else if(Vehicle *v = dynamic_cast(&o)) + new Vehicle3D(*this, *v); +} + +void Layout3D::object_removed(Object &o) +{ + if(Track *t = dynamic_cast(&o)) + { + TrackMap::iterator i = tracks.find(t); + if(i!=tracks.end()) + delete i->second; + } + else if(Signal *s = dynamic_cast(&o)) + { + SignalMap::iterator i = signals.find(s); + if(i!=signals.end()) + delete i->second; + } + else if(Vehicle *v = dynamic_cast(&o)) + { + VehicleMap::iterator i = vehicles.find(v); + if(i!=vehicles.end()) + delete i->second; + } } -} // namespace Marklin +} // namespace R2C2