]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/designer.cpp
Fix memory leaks and other bad stuff
[r2c2.git] / source / designer / designer.cpp
index a28811f74fb254f3fa10446b8be5e7213a01ecf4..99655683c1a3bc772d3fe6737d336d2a6d2e7802 100644 (file)
@@ -1,13 +1,15 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
 #include <signal.h>
 #include <cmath>
 #include <GL/gl.h>
+#include <msp/gl/blend.h>
+#include <msp/gl/framebuffer.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/misc.h>
 #include <msp/gl/projection.h>
@@ -38,40 +40,32 @@ using namespace Msp;
 Application::RegApp<Designer> Designer::reg;
 
 Designer::Designer(int argc, char **argv):
-       screen_w(1280),
-       screen_h(960),
+       window(1280, 960),
        base_object(0),
        cur_route(0),
        mode(SELECT),
+       manipulator(*this, selection),
+       measure(*this),
        input(0),
-       cam_yaw(M_PI/2),
-       cam_pitch(-M_PI/4),
-       cam_pos(0, -0.5, 0.5),
-       shift(false),
-       move_x(0),
-       move_y(0),
-       zoom(0),
-       rotate(0),
-       pitch(0)
+       camera_ctl(window, camera),
+       shift(false)
 {
+       window.set_title("Railway Designer");
+       window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
+       window.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
+       window.signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
+       window.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
+       window.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
+
+       manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
+       manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
+       measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
+       measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
+
+       // Setup catalogue and layout
        DataFile::load(catalogue, "tracks.dat");
 
-       cat_layout = new Layout(catalogue);
-       cat_layout_3d = new Layout3D(*cat_layout);
-
-       const map<unsigned, TrackType *> &ctracks = catalogue.get_tracks();
-       unsigned n = 0;
-       for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
-       {
-               Track *track = new Track(*i->second);
-               track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
-               track->set_rotation(M_PI/2);
-               cat_layout->add_track(*track);
-       }
-
-       manipulator = new Manipulator(*this);
-       manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
-       manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
+       cat_layout_3d = new Layout3D(catalogue.get_layout());
 
        layout = new Layout(catalogue);
        layout_3d = new Layout3D(*layout);
@@ -80,9 +74,6 @@ Designer::Designer(int argc, char **argv):
        {
                filename = argv[1];
                DataFile::load(*layout, argv[1]);
-               const list<Track3D *> &ltracks = layout_3d->get_tracks();
-               for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
-                       update_track_color(**i);
 
                if(!layout->get_base().empty())
                {
@@ -91,42 +82,14 @@ Designer::Designer(int argc, char **argv):
                }
        }
 
-       selection = new Selection;
-       manipulator->set_selection(selection);
-
-       measure = new Measure(*this);
-       measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
-       measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
-}
-
-Designer::~Designer()
-{
-       delete manipulator;
-       delete selection;
-       delete layout;
-       delete layout_3d;
-       delete cat_layout;
-       delete cat_layout_3d;
-       delete measure;
-}
-
-int Designer::main()
-{
-       dpy = new Graphics::Display;
-       wnd = new Graphics::Window(*dpy, screen_w, screen_h);
-       glc = new Graphics::GLContext(*wnd);
-       wnd->set_title("Railway Designer");
-
-       wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
-       wnd->show();
-
-       glEnableClientState(GL_VERTEX_ARRAY);
-       glEnable(GL_DEPTH_TEST);
-       glEnable(GL_BLEND);
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glEnable(GL_CULL_FACE);
+       // Setup OpenGL
+       GL::enable(GL::DEPTH_TEST);
+       GL::enable(GL::BLEND);
+       GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
+       GL::enable(GL_CULL_FACE);
 
-       pipeline = new GL::Pipeline(screen_w, screen_h, false);
+       pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
+       pipeline->set_camera(&camera);
        pipeline->add_renderable(layout_3d->get_scene());
        if(base_object)
                pipeline->add_renderable(*base_object);
@@ -137,8 +100,12 @@ int Designer::main()
        GL::PipelinePass *pass = &pipeline->add_pass(GL::Tag());
        pass->lighting = &lighting;
 
+       camera.set_up_direction(GL::Vector3(0, 0, 1));
+       view_all();
+
+       // Setup UI
        DataFile::load(ui_res, "marklin.res");
-       root = new GLtk::Root(ui_res, *wnd);
+       root = new GLtk::Root(ui_res, window);
 
        lbl_tooltip = new GLtk::Label(ui_res);
        lbl_tooltip->set_style("tooltip");
@@ -147,26 +114,34 @@ int Designer::main()
 
        toolbar = new Toolbar(*this);
        root->add(*toolbar);
-       toolbar->set_position(0, screen_h-toolbar->get_geometry().h);
+       toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
        toolbar->set_visible(true);
 
+       overlay = new Overlay3D(window, camera, ui_res.get_default_font());
 
-       wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
-       wnd->signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
-       wnd->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
-       wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
-
-       mode = SELECT;
-
-       Application::main();
+       const list<Track3D *> &tracks = layout_3d->get_tracks();
+       for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               update_track_icon(**i);
+}
 
+Designer::~Designer()
+{
+       delete overlay;
        delete root;
+       delete pipeline;
+       delete base_object;
+       delete layout_3d;
+       delete layout;
+       delete cat_layout_3d;
+}
+
+int Designer::main()
+{
+       window.show();
 
-       delete glc;
-       delete wnd;
-       delete dpy;
+       mode = SELECT;
 
-       return exit_code;
+       return Application::main();
 }
 
 void Designer::save()
@@ -194,7 +169,7 @@ void Designer::add_selection_to_route()
 
        try
        {
-               const set<Track *> &stracks = selection->get_tracks();
+               const set<Track *> &stracks = selection.get_tracks();
                set<const Track *> tracks(stracks.begin(), stracks.end());
                cur_route->add_tracks(tracks);
        }
@@ -204,29 +179,14 @@ void Designer::add_selection_to_route()
        }
 }
 
-void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
+Point Designer::map_pointer_coords(int x, int y)
 {
-       float cos_pitch = cos(cam_pitch);
-       float sin_pitch = sin(cam_pitch);
-       float cos_yaw = cos(cam_yaw);
-       float sin_yaw = sin(cam_yaw);
-
-       float rx = sin_yaw*0.55228;
-       float ry = -cos_yaw*0.55228;
+       float xf = x*2.0/window.get_width()-1.0;
+       float yf = y*2.0/window.get_height()-1.0;
+       GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
+       const GL::Vector3 &pos = camera.get_position();
 
-       float ux = cos_yaw*-sin_pitch*0.41421;
-       float uy = sin_yaw*-sin_pitch*0.41421;
-       float uz = cos_pitch*0.41421;
-
-       float xf = static_cast<float>(x)*2/screen_w-1;
-       float yf = static_cast<float>(y)*2/screen_h-1;
-
-       float vx = cos_yaw*cos_pitch + xf*rx + yf*ux;
-       float vy = sin_yaw*cos_pitch + xf*ry + yf*uy;
-       float vz = sin_pitch + yf*uz;
-
-       gx = cam_pos.x-vx*cam_pos.z/vz;
-       gy = cam_pos.y-vy*cam_pos.z/vz;
+       return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
 }
 
 void Designer::tick()
@@ -235,51 +195,8 @@ void Designer::tick()
        float dt = (t-last_tick)/Msp::Time::sec;
        last_tick = t;
 
-       dpy->tick();
-
-       if(move_y)
-       {
-               cam_pos.x += cos(cam_yaw)*dt*move_y;
-               cam_pos.y += sin(cam_yaw)*dt*move_y;
-       }
-       if(move_x)
-       {
-               cam_pos.x += sin(cam_yaw)*dt*move_x;
-               cam_pos.y += -cos(cam_yaw)*dt*move_x;
-       }
-       if(zoom)
-       {
-               cam_pos.x += cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
-               cam_pos.y += sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
-               cam_pos.z += sin(cam_pitch)*dt*zoom;
-       }
-       if(rotate)
-       {
-               float vx = cos(cam_yaw)*cos(cam_pitch);
-               float vy = sin(cam_yaw)*cos(cam_pitch);
-               float vz = sin(cam_pitch);
-
-               float gx = cam_pos.x-vx*cam_pos.z/vz;
-               float gy = cam_pos.y-vy*cam_pos.z/vz;
-               float d = sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
-
-               cam_yaw += M_PI*dt*rotate;
-               if(cam_yaw>M_PI*2)
-                       cam_yaw -= M_PI*2;
-               else if(cam_yaw<0)
-                       cam_yaw += M_PI*2;
-
-               cam_pos.x = gx+cos(cam_yaw)*d;
-               cam_pos.y = gy+sin(cam_yaw)*d;
-       }
-       if(pitch)
-       {
-               cam_pitch += M_PI/2*dt*pitch;
-               if(cam_pitch>M_PI/12)
-                       cam_pitch = M_PI/12;
-               else if(cam_pitch<-M_PI/2)
-                       cam_pitch = -M_PI/2;
-       }
+       window.get_display().tick();
+       camera_ctl.tick(dt);
 
        if(tooltip_timeout && t>tooltip_timeout)
        {
@@ -312,7 +229,7 @@ void Designer::tick()
 
        render();
 
-       glc->swap_buffers();
+       window.swap_buffers();
 }
 
 void Designer::key_press(unsigned code, unsigned mod, wchar_t)
@@ -329,24 +246,24 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t)
                mode = CATALOGUE;
        else if(key==Msp::Input::KEY_G)
        {
-               manipulator->start_move();
+               manipulator.start_move();
                mode = MANIPULATE;
        }
        else if(key==Msp::Input::KEY_R)
        {
-               manipulator->start_rotate();
+               manipulator.start_rotate();
                mode = MANIPULATE;
        }
        else if(key==Msp::Input::KEY_D)
        {
-               manipulator->duplicate();
-               manipulator->start_move();
+               manipulator.duplicate();
+               manipulator.start_move();
                mode = MANIPULATE;
        }
        else if(key==Msp::Input::KEY_W)
                save();
        else if(key==Msp::Input::KEY_PLUS)
-               selection->select_more();
+               selection.select_more();
        else if(key==Msp::Input::KEY_L && (mod&1))
        {
                const set<Track *> &tracks = layout->get_tracks();
@@ -356,39 +273,40 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t)
                IO::print("Total length: %.1fm\n", len);
        }
        else if(key==Msp::Input::KEY_L)
-               selection->select_linked();
+               selection.select_linked();
        else if(key==Msp::Input::KEY_M)
        {
-               measure->start();
+               measure.start();
                mode = MEASURE;
        }
        else if(key==Msp::Input::KEY_Z)
        {
-               manipulator->start_elevate();
+               manipulator.start_elevate();
                mode = MANIPULATE;
        }
        else if(key==Msp::Input::KEY_ESC)
        {
                if(mode==MANIPULATE)
-                       manipulator->cancel();
+                       manipulator.cancel();
                else if(mode==CATALOGUE)
                        mode = SELECT;
                else
-                       selection->clear();
+                       selection.clear();
        }
        else if(key==Msp::Input::KEY_X)
        {
-               set<Track *> tracks = selection->get_tracks();
-               selection->clear();
+               set<Track *> tracks = selection.get_tracks();
+               selection.clear();
                for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
                {
+                       overlay->clear(layout_3d->get_track(**i));
                        layout->remove_track(**i);
                        delete *i;
                }
        }
        else if(key==Msp::Input::KEY_F && (mod&1))
        {
-               const set<Track *> &tracks = selection->get_tracks();
+               const set<Track *> &tracks = selection.get_tracks();
                const set<Track *> &ltracks = layout->get_tracks();
                for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                {
@@ -398,19 +316,19 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t)
                                if(*j!=*i)
                                        (*i)->snap_to(**j, true);
 
-                       update_track_color(layout_3d->get_track(**i));
+                       update_track_icon(layout_3d->get_track(**i));
                }
        }
        else if(key==Msp::Input::KEY_F)
-               manipulator->flatten();
+               manipulator.flatten();
        else if(key==Msp::Input::KEY_E && (mod&1))
-               manipulator->even_slope(true);
+               manipulator.even_slope(true);
        else if(key==Msp::Input::KEY_E)
-               manipulator->even_slope();
+               manipulator.even_slope();
        else if(key==Msp::Input::KEY_T)
        {
-               Track *track = selection->get_track();
-               if(selection->size()==1 && track->get_type().get_n_paths()>1)
+               Track *track = selection.get_track();
+               if(selection.size()==1 && track->get_type().get_n_paths()>1)
                {
                        input = new ::Input(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
                        input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
@@ -420,7 +338,7 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t)
        }
        else if(key==Msp::Input::KEY_S)
        {
-               const set<Track *> &tracks = selection->get_tracks();
+               const set<Track *> &tracks = selection.get_tracks();
                bool ok = false;
                int id = -1;
                for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
@@ -445,26 +363,6 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t)
        }
        else if(key==Msp::Input::KEY_A)
                add_selection_to_route();
-       else if(key==Msp::Input::KEY_RIGHT)
-               rotate = -1;
-       else if(key==Msp::Input::KEY_LEFT)
-               rotate = 1;
-       else if(key==Msp::Input::KEY_UP)
-               move_y = 1;
-       else if(key==Msp::Input::KEY_DOWN)
-               move_y = -1;
-       else if(key==Msp::Input::KEY_INSERT)
-               zoom = -1;
-       else if(key==Msp::Input::KEY_PGUP)
-               zoom = 1;
-       else if(key==Msp::Input::KEY_HOME)
-               pitch = 1;
-       else if(key==Msp::Input::KEY_END)
-               pitch = -1;
-       else if(key==Msp::Input::KEY_DELETE)
-               move_x = -1;
-       else if(key==Msp::Input::KEY_PGDN)
-               move_x = 1;
 }
 
 void Designer::key_release(unsigned code, unsigned)
@@ -476,24 +374,13 @@ void Designer::key_release(unsigned code, unsigned)
 
        if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
                shift = false;
-       else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
-               rotate = 0;
-       else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
-               move_y = 0;
-       else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
-               zoom = 0;
-       else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
-               pitch = 0;
-       else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
-               move_x = 0;
 }
 
 void Designer::button_press(int x, int y, unsigned btn, unsigned)
 {
-       y = screen_h-y-1;
+       y = window.get_height()-y-1;
 
-       float gx, gy;
-       map_pointer_coords(x, y, gx, gy);
+       Point ground = map_pointer_coords(x, y);
 
        if(mode==CATALOGUE)
        {
@@ -503,11 +390,11 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned)
                        if(ctrack)
                        {
                                Track *track = ctrack->get_track().copy();
-                               track->set_position(Point(gx, gy, 0));
+                               track->set_position(ground);
                                layout->add_track(*track);
 
-                               selection->clear();
-                               selection->add_track(track);
+                               selection.clear();
+                               selection.add_track(track);
 
                                mode = SELECT;
                        }
@@ -523,20 +410,20 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned)
                        if(track)
                        {
                                if(!shift)
-                                       selection->clear();
-                               selection->toggle_track(&track->get_track());
+                                       selection.clear();
+                               selection.toggle_track(&track->get_track());
                        }
                }
        }
        else if(mode==MANIPULATE)
-               manipulator->button_press(x, y, gx, gy, btn);
+               manipulator.button_press(x, y, ground.x, ground.y, btn);
        else if(mode==MEASURE)
-               measure->button_press(x, y, gx, gy, btn);
+               measure.button_press(x, y, ground.x, ground.y, btn);
 }
 
 void Designer::pointer_motion(int x, int y)
 {
-       y = screen_h-y-1;
+       y = window.get_height()-y-1;
 
        pointer_x = x;
        pointer_y = y;
@@ -548,49 +435,46 @@ void Designer::pointer_motion(int x, int y)
 
        if(mode!=INPUT)
        {
-               float gx, gy;
-               map_pointer_coords(x, y, gx, gy);
-               manipulator->pointer_motion(x, y, gx, gy);
-               measure->pointer_motion(x, y, gx, gy);
+               Point ground = map_pointer_coords(x, y);
+               manipulator.pointer_motion(x, y, ground.x, ground.y);
+               measure.pointer_motion(x, y, ground.x, ground.y);
        }
 }
 
-void Designer::project_3d()
-{
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
-       glMatrixMode(GL_MODELVIEW);
-}
-
 void Designer::apply_camera()
 {
-       glLoadIdentity();
        if(mode==CATALOGUE)
-               glTranslatef(0, 0, -1);
-       else
        {
-               glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
-               glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
-               glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
+               GL::matrix_mode(GL::PROJECTION);
+               GL::load_identity();
+               GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
+               GL::matrix_mode(GL::MODELVIEW);
+               GL::load_identity();
+               GL::translate(0, 0, -1);
        }
+       else
+               camera.apply();
 }
 
 void Designer::render()
 {
-       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+       GL::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
        GL::enable(GL::DEPTH_TEST);
        GL::Texture::unbind();
 
-       project_3d();
-       apply_camera();
        if(mode==CATALOGUE)
+       {
+               apply_camera();
                cat_layout_3d->get_scene().render();
+       }
        else
        {
                pipeline->render_all();
                layout_3d->get_endpoint_scene().render();
                GL::enable(GL_CULL_FACE);
+               GL::disable(GL::DEPTH_TEST);
+               overlay->render(0);
+               GL::enable(GL::DEPTH_TEST);
                /*if(cur_route)
                {
                        glColor4f(0.5, 0.8, 1.0, 1.0);
@@ -609,14 +493,14 @@ void Designer::render()
                        }
                }*/
 
-               manipulator->render();
+               manipulator.render();
                if(mode==MEASURE)
-                       measure->render();
+                       measure.render();
        }
 
        GL::matrix_mode(GL::PROJECTION);
        GL::load_identity();
-       GL::ortho_bottomleft(screen_w, screen_h);
+       GL::ortho_bottomleft(window.get_width(), window.get_height());
        GL::matrix_mode(GL::MODELVIEW);
        GL::load_identity();
 
@@ -631,31 +515,32 @@ Track3D *Designer::pick_track(int x, int y)
        if(mode==CATALOGUE)
                l = cat_layout_3d;
 
-       float xx = (static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
-       float yy = (static_cast<float>(y)/screen_h-0.5)*0.82843;
-       float size = 4.0/screen_h*0.82843;
+       float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
+       float yy = (float(y)/window.get_height()-0.5)*0.82843;
+       float size = 4.0/window.get_height()*0.82843;
 
-       project_3d();
        apply_camera();
 
        return l->pick_track(xx, yy, size);
 }
 
-void Designer::update_track_color(Track3D &track)
+void Designer::update_track_icon(Track3D &track)
 {
-       if(track.get_track().get_sensor_id())
+       overlay->clear(track);
+
+       if(track.get_track().get_flex())
+               overlay->add_graphic(track, "flex");
+
+       if(unsigned sid = track.get_track().get_sensor_id())
        {
-               if(track.get_track().get_flex())
-                       track.set_color(GL::Color(1, 0.6, 1));
-               else
-                       track.set_color(GL::Color(0.7, 0.7, 1));
+               overlay->add_graphic(track, "sensor");
+               overlay->set_label(track, lexical_cast(sid));
+       }
+       else if(unsigned tid = track.get_track().get_turnout_id())
+       {
+               overlay->add_graphic(track, "turnout");
+               overlay->set_label(track, lexical_cast(tid));
        }
-       else if(track.get_track().get_turnout_id())
-               track.set_color(GL::Color(0.8, 1, 0.8));
-       else if(track.get_track().get_flex())
-               track.set_color(GL::Color(1, 0.8, 0.8));
-       else
-               track.set_color(GL::Color(1, 1, 1));
 }
 
 void Designer::manipulation_status(const string &status)
@@ -670,10 +555,10 @@ void Designer::manipulation_done(bool)
 
 void Designer::measure_changed()
 {
-       float pard = measure->get_parallel_distance()*1000;
-       float perpd = measure->get_perpendicular_distance()*1000;
+       float pard = measure.get_parallel_distance()*1000;
+       float perpd = measure.get_perpendicular_distance()*1000;
        float d = sqrt(pard*pard+perpd*perpd);
-       float adiff = measure->get_angle_difference()*180/M_PI;
+       float adiff = measure.get_angle_difference()*180/M_PI;
        string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
        set_tooltip(pointer_x, pointer_y, info);
 }
@@ -688,8 +573,8 @@ void Designer::set_tooltip(int x, int y, const std::string &text)
        int fontsize = ui_res.get_default_font().get_default_size();
        int h = fontsize+6;
        int w = static_cast<int>(ui_res.get_default_font().get_string_width(text)*fontsize)+6;
-       x = max(min(static_cast<int>(screen_w)-w, x), 0);
-       y = max(min(static_cast<int>(screen_h)-h, y), 0);
+       x = max(min(static_cast<int>(window.get_width())-w, x), 0);
+       y = max(min(static_cast<int>(window.get_height())-h, y), 0);
        lbl_tooltip->set_text(text);
        lbl_tooltip->set_geometry(GLtk::Geometry(x, y, w, h));
        lbl_tooltip->set_visible(true);
@@ -709,24 +594,24 @@ void Designer::save_accept()
 
 void Designer::turnout_id_accept()
 {
-       Track *track = selection->get_track();
+       Track *track = selection.get_track();
        unsigned id = lexical_cast<unsigned>(input->get_text());
        track->set_turnout_id(id);
 
-       update_track_color(layout_3d->get_track(*track));
+       update_track_icon(layout_3d->get_track(*track));
 
        input_dismiss();
 }
 
 void Designer::sensor_id_accept()
 {
-       const set<Track *> &tracks = selection->get_tracks();
+       const set<Track *> &tracks = selection.get_tracks();
        unsigned id = lexical_cast<unsigned>(input->get_text());
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
        {
                (*i)->set_sensor_id(id);
 
-               update_track_color(layout_3d->get_track(**i));
+               update_track_icon(layout_3d->get_track(**i));
        }
 
        input_dismiss();
@@ -738,3 +623,28 @@ void Designer::input_dismiss()
        input = 0;
        mode = SELECT;
 }
+
+void Designer::view_all()
+{
+       Point minp;
+       Point maxp;
+
+       const list<Track3D *> &tracks = layout_3d->get_tracks();
+       for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       {
+               Point tmin;
+               Point tmax;
+               (*i)->get_bounds(0, tmin, tmax);
+               minp.x = min(minp.x, tmin.x);
+               minp.y = min(minp.y, tmin.y);
+               maxp.x = max(maxp.x, tmax.x);
+               maxp.y = max(maxp.y, tmax.y);
+       }
+
+       float t = tan(camera.get_field_of_view()/2)*2;
+       float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
+       float cam_dist = size/t+size*0.25;
+       Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2);
+       camera.set_position(GL::Vector3(center.x, center.y-cam_dist*0.5, cam_dist*0.866));
+       camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
+}