]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/designer.cpp
Use maps instead of lists in Layout3D
[r2c2.git] / source / designer / designer.cpp
index b0971c25271be9d65390ff08ca5841614e7c84b6..cb327c560efeb51236169b369675d42b16c26798 100644 (file)
@@ -27,6 +27,7 @@ Distributed under the GPL
 #include <msp/time/utils.h>
 #include "libmarklin/route.h"
 #include "libmarklin/tracktype.h"
+#include "3d/path.h"
 #include "designer.h"
 #include "input.h"
 #include "manipulator.h"
@@ -47,11 +48,13 @@ Designer::Designer(int argc, char **argv):
        mode(SELECT),
        manipulator(*this, selection),
        measure(*this),
-       camera_ctl(window, camera)
+       camera_ctl(window, camera),
+       track_wrap(*this, selection)
 {
        window.set_title("Railway Designer");
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
 
+       selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
        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));
@@ -65,6 +68,8 @@ Designer::Designer(int argc, char **argv):
        layout = new Layout(catalogue);
        layout_3d = new Layout3D(*layout);
 
+       layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
+
        if(argc>1)
        {
                filename = argv[1];
@@ -78,22 +83,32 @@ Designer::Designer(int argc, char **argv):
        }
 
        // 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(window.get_width(), window.get_height(), false);
        pipeline->set_camera(&camera);
-       pipeline->add_renderable(layout_3d->get_scene());
+       pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
        if(base_object)
                pipeline->add_renderable(*base_object);
+       pipeline->add_renderable_for_pass(track_wrap, "unlit");
+       pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
+       pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
 
        light.set_position(0, -0.259, 0.966, 0);
        lighting.attach(0, light);
 
-       GL::PipelinePass *pass = &pipeline->add_pass(GL::Tag());
+       GL::PipelinePass *pass = &pipeline->add_pass(0);
+       pass->lighting = &lighting;
+       pass->depth_test = &GL::DepthTest::lequal();
+
+       pass = &pipeline->add_pass("unlit");
+       pass->depth_test = &GL::DepthTest::lequal();
+       pass->blend = &GL::Blend::alpha();
+
+       pass = &pipeline->add_pass("blended");
        pass->lighting = &lighting;
+       pass->depth_test = &GL::DepthTest::lequal();
+       pass->blend = &GL::Blend::alpha();
 
        camera.set_up_direction(GL::Vector3(0, 0, 1));
        view_all();
@@ -122,9 +137,9 @@ Designer::Designer(int argc, char **argv):
 
        overlay = new Overlay3D(window, camera, ui_res.get_default_font());
 
-       const list<Track3D *> &tracks = layout_3d->get_tracks();
-       for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               update_track_icon(**i);
+       const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
+       for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               update_track_icon(*i->second);
 }
 
 Designer::~Designer()
@@ -198,9 +213,19 @@ void Designer::set_sensor_id()
        }
 }
 
-void Designer::edit_route(Route &r)
+void Designer::rename_route()
 {
-       cur_route = &r;
+       if(!cur_route)
+               return;
+
+       InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
+       input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
+}
+
+void Designer::edit_route(Route *r)
+{
+       cur_route = r;
+       show_route(r);
 }
 
 void Designer::add_selection_to_route()
@@ -210,14 +235,14 @@ void Designer::add_selection_to_route()
 
        try
        {
-               const set<Track *> &stracks = selection.get_tracks();
-               set<const Track *> tracks(stracks.begin(), stracks.end());
-               cur_route->add_tracks(tracks);
+               cur_route->add_tracks(selection.get_tracks());
        }
        catch(const Exception &e)
        {
-               IO::print("%s\n", e.what());
+               lbl_status->set_text(e.what());
        }
+
+       show_route(cur_route);
 }
 
 Point Designer::map_pointer_coords(int x, int y)
@@ -240,6 +265,10 @@ void Designer::tick()
        root->tick();
        camera_ctl.tick(dt);
 
+       for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
+               layout_3d->get_track(**i).get_path().set_mask(0);
+       new_tracks.clear();
+
        render();
 
        window.swap_buffers();
@@ -273,13 +302,7 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t)
        else if(key==Msp::Input::KEY_PLUS)
                selection.select_more();
        else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
-       {
-               const set<Track *> &tracks = layout->get_tracks();
-               float len = 0;
-               for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-                       len += (*i)->get_type().get_total_length();
-               IO::print("Total length: %.1fm\n", len);
-       }
+               selection.select_blocks();
        else if(key==Msp::Input::KEY_L)
                selection.select_linked();
        else if(key==Msp::Input::KEY_M)
@@ -415,9 +438,7 @@ void Designer::apply_camera()
 
 void Designer::render()
 {
-       GL::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
-       GL::enable(GL::DEPTH_TEST);
-       GL::Texture::unbind();
+       GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
 
        if(mode==CATALOGUE)
        {
@@ -427,30 +448,13 @@ void Designer::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);
-                       const set<const Track *> &rtracks = cur_route->get_tracks();
-                       const map<unsigned, int> &turnouts = cur_route->get_turnouts();
-                       for(set<const Track *>::const_iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
-                       {
-                               unsigned path = 0;
-                               if(unsigned tid=(*i)->get_turnout_id())
-                               {
-                                       map<unsigned, int>::const_iterator j = turnouts.find(tid);
-                                       if(j!=turnouts.end())
-                                               path = j->second;
-                               }
-                               layout_3d->get_track(**i).render_path(path);
-                       }
-               }*/
+                       GL::Bind bind_blend(GL::Blend::alpha());
+                       overlay->render(0);
+               }
 
-               manipulator.render();
+               GL::Bind bind_depth(GL::DepthTest::lequal());
                if(mode==MEASURE)
                        measure.render();
        }
@@ -463,7 +467,14 @@ void Designer::render()
 
        GL::disable(GL::DEPTH_TEST);
 
+       GL::Bind bind_blend(GL::Blend::alpha());
        root->render();
+       GL::Texture::unbind();
+}
+
+void Designer::track_added(Track &trk)
+{
+       new_tracks.push_back(&trk);
 }
 
 Track3D *Designer::pick_track(int x, int y)
@@ -495,8 +506,25 @@ void Designer::update_track_icon(Track3D &track)
        }
        else if(unsigned tid = track.get_track().get_turnout_id())
        {
-               overlay->add_graphic(track, "turnout");
-               overlay->set_label(track, lexical_cast(tid));
+               if(tid<0x800)
+               {
+                       overlay->add_graphic(track, "turnout");
+                       overlay->set_label(track, lexical_cast(tid));
+               }
+       }
+}
+
+void Designer::selection_changed()
+{
+       const set<Track *> &tracks = selection.get_tracks();
+       if(tracks.empty())
+               lbl_status->set_text(string());
+       else
+       {
+               float len = 0;
+               for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+                       len += (*i)->get_type().get_total_length();
+               lbl_status->set_text(format("%.2fm of track selected\n", len));
        }
 }
 
@@ -507,8 +535,8 @@ void Designer::manipulation_status(const string &status)
 
 void Designer::manipulation_done(bool)
 {
-       lbl_status->set_text(string());
        mode = SELECT;
+       selection_changed();
 }
 
 void Designer::measure_changed()
@@ -523,8 +551,8 @@ void Designer::measure_changed()
 
 void Designer::measure_done()
 {
-       lbl_status->set_text(string());
        mode = SELECT;
+       selection_changed();
 }
 
 void Designer::turnout_id_accept(const string &text)
@@ -548,17 +576,23 @@ void Designer::sensor_id_accept(const string &text)
        }
 }
 
+void Designer::route_name_accept(const string &text)
+{
+       if(cur_route)
+               cur_route->set_name(text);
+}
+
 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)
+       const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
+       for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
        {
                Point tmin;
                Point tmax;
-               (*i)->get_bounds(0, tmin, tmax);
+               i->second->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);
@@ -592,3 +626,29 @@ string Designer::tooltip(int x, int y)
 
        return string();
 }
+
+void Designer::show_route(const Route *route)
+{
+       const set<Track *> &ltracks = layout->get_tracks();
+       for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
+       {
+               Track3D &t3d = layout_3d->get_track(**i);
+               if(route && route->has_track(**i))
+               {
+                       t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
+                       if((*i)->get_type().is_turnout())
+                       {
+                               unsigned tid = (*i)->get_turnout_id();
+                               int path = (tid ? route->get_turnout(tid) : -1);
+                               if(path>=0)
+                                       t3d.get_path().set_path(path);
+                               else
+                                       t3d.get_path().set_mask((*i)->get_type().get_paths());
+                       }
+                       else
+                               t3d.get_path().set_path(0);
+               }
+               else
+                       t3d.get_path().set_mask(0);
+       }
+}