X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdesigner%2Fdesigner.cpp;h=e5608331023d038d9146ccb53d375555dbf2dc4f;hb=f79b3fd58999a73a4a28663b46fdd96c58167a18;hp=a21dd096497dcf36a34c67edfaeb21bc4a806974;hpb=084d6e9f87fbe81d500fe2de273cefd8d78a0d77;p=r2c2.git diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index a21dd09..e560833 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -25,7 +25,9 @@ Distributed under the GPL #include #include #include +#include "libmarklin/route.h" #include "libmarklin/tracktype.h" +#include "3d/path.h" #include "designer.h" #include "input.h" #include "manipulator.h" @@ -46,16 +48,10 @@ Designer::Designer(int argc, char **argv): mode(SELECT), manipulator(*this, selection), measure(*this), - input(0), - camera_ctl(window, camera), - shift(false) + camera_ctl(window, camera) { 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)); @@ -83,22 +79,26 @@ 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(*base_object, 0); + 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(); camera.set_up_direction(GL::Vector3(0, 0, 1)); view_all(); @@ -106,17 +106,20 @@ Designer::Designer(int argc, char **argv): // 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)); toolbar = new Toolbar(*this); root->add(*toolbar); toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h); - toolbar->set_visible(true); + toolbar->set_focusable(false); GLtk::Panel *statusbar = new GLtk::Panel(ui_res); root->add(*statusbar); statusbar->set_size(window.get_width(), 20); - statusbar->set_visible(true); + statusbar->set_focusable(false); lbl_status = new GLtk::Label(ui_res); statusbar->add(*lbl_status); @@ -127,6 +130,8 @@ Designer::Designer(int argc, char **argv): const list &tracks = layout_3d->get_tracks(); for(list::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) update_track_icon(**i); + + edit_route(0); } Designer::~Designer() @@ -151,10 +156,8 @@ int Designer::main() void Designer::save() { - input = new ::Input(*this, "Save layout", filename); - input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss)); - input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept)); - mode = INPUT; + InputDialog *input = new InputDialog(*this, "Save layout", filename); + input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save)); } void Designer::quit() @@ -162,9 +165,59 @@ void Designer::quit() exit(0); } -void Designer::edit_route(Route &r) +void Designer::new_track() +{ + mode = CATALOGUE; +} + +void Designer::set_turnout_id() +{ + Track *track = selection.get_track(); + if(selection.size()==1 && track->get_type().is_turnout()) + { + InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id())); + input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept)); + } +} + +void Designer::set_sensor_id() +{ + const set &tracks = selection.get_tracks(); + bool ok = false; + int id = -1; + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + { + if(!(*i)->get_type().is_turnout()) + ok = true; + if(static_cast((*i)->get_sensor_id())!=id) + { + if(id==-1) + id = (*i)->get_sensor_id(); + else + id = -2; + } + } + + if(ok) + { + InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string())); + input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept)); + } +} + +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::edit_route(Route *r) { - cur_route = &r; + cur_route = r; + show_route(r); } void Designer::add_selection_to_route() @@ -174,14 +227,14 @@ 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()); } + + show_route(cur_route); } Point Designer::map_pointer_coords(int x, int y) @@ -209,18 +262,13 @@ void Designer::tick() window.swap_buffers(); } -void Designer::key_press(unsigned code, unsigned mod, wchar_t) +void Designer::key_press(unsigned key, unsigned mod, wchar_t) { - unsigned key = Msp::Input::key_from_sys(code); - - if(mode==INPUT) - return; - - if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R) - shift = true; + key = Input::key_from_sys(key); + mod = Input::mod_from_sys(mod); if(key==Msp::Input::KEY_N) - mode = CATALOGUE; + new_track(); else if(key==Msp::Input::KEY_G) { manipulator.start_move(); @@ -241,7 +289,7 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t) save(); else if(key==Msp::Input::KEY_PLUS) selection.select_more(); - else if(key==Msp::Input::KEY_L && (mod&1)) + else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT)) { const set &tracks = layout->get_tracks(); float len = 0; @@ -281,7 +329,7 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t) delete *i; } } - else if(key==Msp::Input::KEY_F && (mod&1)) + else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT)) { const set &tracks = selection.get_tracks(); const set <racks = layout->get_tracks(); @@ -298,64 +346,22 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t) } else if(key==Msp::Input::KEY_F) manipulator.flatten(); - else if(key==Msp::Input::KEY_E && (mod&1)) + else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT)) manipulator.even_slope(true); else if(key==Msp::Input::KEY_E) 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) - { - input = new ::Input(*this, "Turnout ID", lexical_cast(track->get_turnout_id())); - input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss)); - input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept)); - mode = INPUT; - } - } + set_turnout_id(); else if(key==Msp::Input::KEY_S) - { - const set &tracks = selection.get_tracks(); - bool ok = false; - int id = -1; - for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - { - if((*i)->get_type().get_n_paths()==1) - ok = true; - if(static_cast((*i)->get_sensor_id())!=id) - { - if(id==-1) - id = (*i)->get_sensor_id(); - else - id = -2; - } - } - if(ok) - { - input = new ::Input(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string())); - input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss)); - input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept)); - mode = INPUT; - } - } + set_sensor_id(); else if(key==Msp::Input::KEY_A) add_selection_to_route(); } -void Designer::key_release(unsigned code, unsigned) -{ - unsigned key = Msp::Input::key_from_sys(code); - - if(mode==INPUT) - return; - - if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R) - shift = false; -} - -void Designer::button_press(int x, int y, unsigned btn, unsigned) +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); @@ -366,9 +372,8 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned) Track3D *ctrack = pick_track(x, y); if(ctrack) { - Track *track = ctrack->get_track().copy(); + Track *track = new Track(*layout, ctrack->get_track().get_type()); track->set_position(ground); - layout->add_track(*track); selection.clear(); selection.add_track(track); @@ -386,7 +391,7 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned) Track3D *track = pick_track(x, y); if(track) { - if(!shift) + if(!(mod&Input::MOD_SHIFT)) selection.clear(); selection.toggle_track(&track->get_track()); } @@ -402,7 +407,7 @@ void Designer::pointer_motion(int x, int y) { y = window.get_height()-y-1; - if(mode!=INPUT) + if(!root->get_child_at(x, y)) { Point ground = map_pointer_coords(x, y); manipulator.pointer_motion(x, y, ground.x, ground.y); @@ -427,9 +432,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) { @@ -439,29 +442,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 &rtracks = cur_route->get_tracks(); - const map &turnouts = cur_route->get_turnouts(); - for(set::const_iterator i=rtracks.begin(); i!=rtracks.end(); ++i) - { - unsigned path = 0; - if(unsigned tid=(*i)->get_turnout_id()) - { - map::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); + } + GL::Bind bind_depth(GL::DepthTest::lequal()); manipulator.render(); if(mode==MEASURE) measure.render(); @@ -475,7 +462,9 @@ void Designer::render() GL::disable(GL::DEPTH_TEST); + GL::Bind bind_blend(GL::Blend::alpha()); root->render(); + GL::Texture::unbind(); } Track3D *Designer::pick_track(int x, int y) @@ -507,8 +496,11 @@ 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)); + } } } @@ -539,43 +531,31 @@ void Designer::measure_done() mode = SELECT; } -void Designer::save_accept() -{ - layout->save(input->get_text()); - - input_dismiss(); -} - -void Designer::turnout_id_accept() +void Designer::turnout_id_accept(const string &text) { Track *track = selection.get_track(); - unsigned id = lexical_cast(input->get_text()); + unsigned id = (text.empty() ? 0 : lexical_cast(text)); track->set_turnout_id(id); update_track_icon(layout_3d->get_track(*track)); - - input_dismiss(); } -void Designer::sensor_id_accept() +void Designer::sensor_id_accept(const string &text) { const set &tracks = selection.get_tracks(); - unsigned id = lexical_cast(input->get_text()); + unsigned id = (text.empty() ? 0 : lexical_cast(text)); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) { (*i)->set_sensor_id(id); update_track_icon(layout_3d->get_track(**i)); } - - input_dismiss(); } -void Designer::input_dismiss() +void Designer::route_name_accept(const string &text) { - delete input; - input = 0; - mode = SELECT; + if(cur_route) + cur_route->set_name(text); } void Designer::view_all() @@ -622,3 +602,29 @@ string Designer::tooltip(int x, int y) return string(); } + +void Designer::show_route(const Route *route) +{ + const set <racks = layout->get_tracks(); + for(set::iterator i=ltracks.begin(); i!=ltracks.end(); ++i) + { + Track3D &t3d = layout_3d->get_track(**i); + if(route && route->get_tracks().count(*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); + } +}