X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdesigner%2Fdesigner.cpp;h=8ab0465c2347a89d3bf21352001199f2939084ba;hb=1ff06c5bc46a677fa389ef86c6b26664368f1653;hp=c9c045af2e253d9091d89cefa964b17618b12a58;hpb=90790c9a28793d31b9ea38eea2f55652a0e9297b;p=r2c2.git diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index c9c045a..8ab0465 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -1,13 +1,14 @@ /* $Id$ -This file is part of the MSP Märklin suite +This file is part of R²C² Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ #include +#include #include -#include +#include #include #include #include @@ -25,34 +26,39 @@ Distributed under the GPL #include #include #include -#include "libmarklin/route.h" -#include "libmarklin/tracktype.h" +#include "libr2c2/route.h" +#include "libr2c2/tracktype.h" #include "3d/path.h" #include "designer.h" #include "input.h" #include "manipulator.h" #include "measure.h" #include "selection.h" +#include "svgexporter.h" #include "toolbar.h" using namespace std; -using namespace Marklin; +using namespace R2C2; using namespace Msp; Application::RegApp Designer::reg; Designer::Designer(int argc, char **argv): window(1280, 960), + ui_res("r2c2.res"), + root(ui_res, window), base_object(0), cur_route(0), mode(SELECT), - manipulator(*this, selection), + manipulator(*this, root, selection), measure(*this), - camera_ctl(window, camera) + camera_ctl(*this, root, 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)); @@ -66,6 +72,9 @@ 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)); + layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed)); + if(argc>1) { filename = argv[1]; @@ -79,59 +88,68 @@ 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(); + camera.set_look_direction(GL::Vector3(0, 0.5, -0.866)); // Setup UI - DataFile::load(ui_res, "marklin.res"); - root = new GLtk::Root(ui_res, window); - root->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press)); - root->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press)); - root->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion)); - root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip)); + root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press)); + root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press)); + root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion)); + root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip)); toolbar = new Toolbar(*this); - root->add(*toolbar); + root.add(*toolbar); toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h); toolbar->set_focusable(false); - GLtk::Panel *statusbar = new GLtk::Panel(ui_res); - root->add(*statusbar); + GLtk::Panel *statusbar = new GLtk::Panel; + root.add(*statusbar); statusbar->set_size(window.get_width(), 20); statusbar->set_focusable(false); - lbl_status = new GLtk::Label(ui_res); + lbl_status = new GLtk::Label; statusbar->add(*lbl_status); lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16)); overlay = new Overlay3D(window, camera, ui_res.get_default_font()); - const list &tracks = layout_3d->get_tracks(); - for(list::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - update_track_icon(**i); + camera_ctl.view_all(); + + 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() { delete overlay; - delete root; delete pipeline; delete base_object; delete layout_3d; @@ -199,9 +217,24 @@ void Designer::set_sensor_id() } } -void Designer::edit_route(Route &r) +void Designer::rename_route() +{ + 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::svg_export() { - cur_route = &r; + InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg"); + input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept)); +} + +void Designer::edit_route(Route *r) +{ + cur_route = r; show_route(r); } @@ -212,19 +245,17 @@ void Designer::add_selection_to_route() try { - const set &stracks = selection.get_tracks(); - set 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); + show_route(cur_route); } -Point Designer::map_pointer_coords(int x, int y) +Point Designer::map_pointer_to_ground(int x, int y) { float xf = x*2.0/window.get_width()-1.0; float yf = y*2.0/window.get_height()-1.0; @@ -240,10 +271,17 @@ void Designer::tick() float dt = (t-last_tick)/Msp::Time::sec; last_tick = t; - window.get_display().tick(); - root->tick(); + if(mode==MANIPULATE_DONE) + mode = SELECT; + + window.tick(); + root.tick(); camera_ctl.tick(dt); + for(list::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(); @@ -254,7 +292,12 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t) key = Input::key_from_sys(key); mod = Input::mod_from_sys(mod); - if(key==Msp::Input::KEY_N) + if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT)) + { + manipulator.start_extend(); + mode = MANIPULATE; + } + else if(key==Msp::Input::KEY_N) new_track(); else if(key==Msp::Input::KEY_G) { @@ -277,13 +320,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 &tracks = layout->get_tracks(); - float len = 0; - for(set::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) @@ -343,6 +380,10 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t) set_sensor_id(); else if(key==Msp::Input::KEY_A) add_selection_to_route(); + else if(key==Msp::Input::KEY_C) + manipulator.connect(); + else if(key==Msp::Input::KEY_V) + svg_export(); } void Designer::button_press(int x, int y, unsigned btn, unsigned mod) @@ -350,7 +391,7 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned mod) y = window.get_height()-y-1; mod = Input::mod_from_sys(mod); - Point ground = map_pointer_coords(x, y); + Point ground = map_pointer_to_ground(x, y); if(mode==CATALOGUE) { @@ -384,8 +425,6 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned mod) } } } - else if(mode==MANIPULATE) - manipulator.button_press(x, y, ground.x, ground.y, btn); else if(mode==MEASURE) measure.button_press(x, y, ground.x, ground.y, btn); } @@ -394,10 +433,9 @@ void Designer::pointer_motion(int x, int y) { y = window.get_height()-y-1; - if(!root->get_child_at(x, y)) + if(!root.get_child_at(x, y)) { - Point ground = map_pointer_coords(x, y); - manipulator.pointer_motion(x, y, ground.x, ground.y); + Point ground = map_pointer_to_ground(x, y); measure.pointer_motion(x, y, ground.x, ground.y); } } @@ -419,9 +457,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) { @@ -431,15 +467,13 @@ void Designer::render() else { pipeline->render_all(); - layout_3d->get_endpoint_scene().render(); - if(cur_route) - layout_3d->get_path_scene().render(); GL::enable(GL_CULL_FACE); - GL::disable(GL::DEPTH_TEST); - overlay->render(0); - GL::enable(GL::DEPTH_TEST); + { + GL::Bind bind_blend(GL::Blend::alpha()); + overlay->render(0); + } - manipulator.render(); + GL::Bind bind_depth(GL::DepthTest::lequal()); if(mode==MEASURE) measure.render(); } @@ -452,7 +486,21 @@ void Designer::render() GL::disable(GL::DEPTH_TEST); - root->render(); + GL::Bind bind_blend(GL::Blend::alpha()); + root.render(); + GL::Texture::unbind(); +} + +void Designer::track_added(Track &trk) +{ + new_tracks.push_back(&trk); +} + +void Designer::track_removed(Track &trk) +{ + list::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk); + if(i!=new_tracks.end()) + new_tracks.erase(i); } Track3D *Designer::pick_track(int x, int y) @@ -484,8 +532,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 &tracks = selection.get_tracks(); + if(tracks.empty()) + lbl_status->set_text(string()); + else + { + float len = 0; + for(set::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)); } } @@ -496,8 +561,8 @@ void Designer::manipulation_status(const string &status) void Designer::manipulation_done(bool) { - lbl_status->set_text(string()); - mode = SELECT; + mode = MANIPULATE_DONE; + selection_changed(); } void Designer::measure_changed() @@ -512,8 +577,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) @@ -537,29 +602,16 @@ void Designer::sensor_id_accept(const string &text) } } -void Designer::view_all() +void Designer::route_name_accept(const string &text) { - Point minp; - Point maxp; - - const list &tracks = layout_3d->get_tracks(); - for(list::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); - } + if(cur_route) + cur_route->set_name(text); +} - 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)); +void Designer::svg_export_accept(const string &text) +{ + SvgExporter svg_exp(*layout); + svg_exp.save(text); } string Designer::tooltip(int x, int y) @@ -582,20 +634,19 @@ string Designer::tooltip(int x, int y) return string(); } -void Designer::show_route(const Route &route) +void Designer::show_route(const Route *route) { const set <racks = layout->get_tracks(); - const set &rtracks = route.get_tracks(); for(set::iterator i=ltracks.begin(); i!=ltracks.end(); ++i) { Track3D &t3d = layout_3d->get_track(**i); - if(rtracks.count(*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); + int path = (tid ? route->get_turnout(tid) : -1); if(path>=0) t3d.get_path().set_path(path); else