X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fengineer%2Fengineer.cpp;h=f0999d91cd9ffaddd1736f0c6fd6493e36077084;hb=5116fff210cdc3f0fbdae13046cc60450aad5e8f;hp=9db63fe0c70c22f81eb68074e88067fb6ade1fd6;hpb=2152b8a18d1a716f51b80c53f11d2a9599d94e23;p=r2c2.git diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 9db63fe..f0999d9 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -1,50 +1,44 @@ -/* $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 +#include +#include +#include #include #include #include #include -#include #include -#include #include -#include +#include #include #include -#include "libmarklin/driver.h" -#include "libmarklin/tracktype.h" +#include "libr2c2/driver.h" +#include "libr2c2/trackcircuit.h" +#include "libr2c2/tracktype.h" +#include "3d/allocation.h" #include "3d/path.h" #include "3d/track.h" +#include "3d/trackcircuit.h" #include "3d/vehicle.h" #include "engineer.h" -#include "mainpanel.h" -#include "trainpanel.h" -#include "trainproperties.h" +#include "mainwindow.h" +#include "traindialog.h" #include "trainview.h" using namespace std; -using namespace Marklin; +using namespace R2C2; using namespace Msp; -Application::RegApp Engineer::reg; - Engineer::Engineer(int argc, char **argv): options(argc, argv), window(options.screen_w, options.screen_h, options.fullscreen), + keyboard(window), + mouse(window), + ui_res("r2c2.res"), layout(catalogue, (options.driver.empty() ? 0 : Driver::create(options.driver))), layout_3d(layout), server(0), @@ -53,26 +47,23 @@ Engineer::Engineer(int argc, char **argv): picking_track(0), picking_entry(0), picking_path(0), - pointer_x(0), - pointer_y(0), pointer_moved(false) { // Setup GUI window.set_title("Railroad Engineer"); window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0)); - DataFile::load(ui_res, "marklin.res"); - root = new GLtk::Root(ui_res, window); - root->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press)); - root->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion)); + root = new GLtk::Root(ui_res, &window, &keyboard, &mouse); + mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Engineer::button_press), false)); + mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Engineer::axis_motion), false)); root->set_visible(true); - main_panel = new MainPanel(*this, ui_res); - root->add(*main_panel); - main_panel->set_position(0, window.get_height()-main_panel->get_geometry().h); - main_panel->set_visible(true); + main_wnd = new MainWindow(*this); + root->add(*main_wnd); + main_wnd->autosize(); + main_wnd->set_position(0, window.get_height()-main_wnd->get_geometry().h); - overlay = new Overlay3D(window, camera, ui_res.get_default_font()); + overlay = new Overlay3D(ui_res.get_default_font()); // Setup railroad control DataFile::load(catalogue, "tracks.dat"); @@ -81,9 +72,17 @@ Engineer::Engineer(int argc, char **argv): DataFile::load(layout, options.layout_fn); layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added)); - layout.signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved)); layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status)); - layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Engineer::sensor_event)); + const set &blocks = layout.get_all(); + for(set::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) + if(TrackCircuit *tc = (*i)->get_sensor()) + new TrackCircuit3D(layout_3d, *tc); + + const set &tracks = layout.get_all(); + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + if((*i)->get_type().is_turnout()) + new Path3D(layout_3d.get(**i)); + if(FS::exists(options.state_fn)) DataFile::load(layout, options.state_fn); @@ -99,18 +98,22 @@ Engineer::Engineer(int argc, char **argv): pipeline.set_camera(&camera); pipeline.add_renderable_for_pass(layout_3d.get_scene(), 0); pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit"); + pipeline.add_renderable_for_pass(*overlay, "overlay"); light.set_position(GL::Vector4(0, -0.259, 0.966, 0)); light.set_diffuse(GL::Color(0.9)); lighting.set_ambient(GL::Color(0.4)); lighting.attach(0, light); - GL::PipelinePass *pass = &pipeline.add_pass(0); - pass->depth_test = &GL::DepthTest::lequal(); - pass->lighting = &lighting; + GL::Pipeline::Pass *pass = &pipeline.add_pass(0); + pass->set_depth_test(&GL::DepthTest::lequal()); + pass->set_lighting(&lighting); pass = &pipeline.add_pass("unlit"); - pass->depth_test = &GL::DepthTest::lequal(); + pass->set_depth_test(&GL::DepthTest::lequal()); + + pass = &pipeline.add_pass("overlay"); + pass->set_blend(&GL::Blend::alpha()); view_all(); @@ -131,7 +134,10 @@ Engineer::~Engineer() layout.get_driver().flush(); if(!options.simulate) - layout.save_trains(options.state_fn); + { + layout.save_dynamic(options.state_fn+".tmp"); + FS::rename(options.state_fn+".tmp", options.state_fn); + } delete overlay; delete root; @@ -141,20 +147,10 @@ Engineer::~Engineer() void Engineer::set_status(const string &text) { - main_panel->set_status_text(text); + main_wnd->set_status_text(text); status_timeout = Time::now()+10*Time::sec; } -void Engineer::rearrange_panels() -{ - int y = main_panel->get_geometry().y; - for(list::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) - { - y -= (*i)->get_geometry().h; - (*i)->set_position(0, y); - } -} - void Engineer::add_train_view(TrainView &tv) { train_views.push_back(&tv); @@ -183,6 +179,10 @@ void Engineer::tick() { window.get_display().tick(); + for(list::iterator i=new_trains.begin(); i!=new_trains.end(); ++i) + process_new_train(**i); + new_trains.clear(); + layout.tick(); event_disp.tick(Time::zero); @@ -191,17 +191,13 @@ void Engineer::tick() if(status_timeout && Time::now()>status_timeout) { - main_panel->set_status_text(string()); + main_wnd->set_status_text(string()); status_timeout = Time::TimeStamp(); } GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT); - pipeline.render_all(); - { - GL::Bind blend(GL::Blend::alpha()); - overlay->render(0); - } + pipeline.render(); if(pointer_moved) { @@ -209,55 +205,40 @@ void Engineer::tick() if(picking) { - Track3D *track = pick_track(pointer_x, window.get_height()-pointer_y-1); - if(track && &track->get_track()!=picking_track) + Track *track = dynamic_cast(pick_object(pointer)); + if(track && track!=picking_track) { - picking_track = &track->get_track(); + picking_track = track; if(picking_entry>=0) picking_entry = 0; delete picking_path; - picking_path = new Path3D(*track); - if(picking_entry>=0) - picking_path->set_mask(picking_track->get_type().get_endpoints()[picking_entry].paths); - else - picking_path->set_mask(picking_track->get_type().get_paths()); + picking_path = new Path3D(layout_3d.get(*track)); picking_path->set_color(GL::Color(0)); - picking_path->set_layer(1); + picking_path->set_layer(2); } } } if(picking && picking_track && picking_entry>=0) { - GL::PushMatrix push_mat; + camera.apply(); + GL::MatrixStack::Push push_mat(GL::MatrixStack::modelview()); - float rot = picking_track->get_endpoint_direction(picking_entry); - Point pos = picking_track->get_endpoint_position(picking_entry); + Snap sn = picking_track->get_snap_node(picking_entry); - GL::translate(pos.x, pos.y, pos.z+0.03); - GL::rotate(rot*180/M_PI+180, 0, 0, 1); + GL::MatrixStack::modelview() *= GL::Matrix::translation(sn.position+GL::Vector3(0, 0, 0.03)); + GL::MatrixStack::modelview() *= GL::Matrix::rotation(sn.rotation+Angle::half_turn(), 0, 0, 1); arrow_mesh.draw(); } - const GLtk::Geometry &rgeom = root->get_geometry(); - GL::matrix_mode(GL::PROJECTION); - GL::load_identity(); - GL::ortho_bottomleft(rgeom.w, rgeom.h); - GL::matrix_mode(GL::MODELVIEW); - GL::load_identity(); - - { - GL::Bind blend(GL::Blend::alpha()); - root->render(); - GL::Texture::unbind(); - } + root->render(); window.swap_buffers(); } -void Engineer::button_press(int x, int y, unsigned btn, unsigned) +void Engineer::button_press(unsigned btn) { if(picking) { @@ -271,24 +252,22 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned) else if(btn==3 && picking_entry>=0) { picking_entry = (picking_entry+1)%picking_track->get_type().get_endpoints().size(); - picking_path->set_mask(picking_track->get_type().get_endpoints()[picking_entry].paths); } } else { - Track3D *t3d = pick_track(x, window.get_height()-y-1); - if(t3d) + Object *obj = pick_object(pointer); + if(Track *track = dynamic_cast(obj)) { - Track &track = t3d->get_track(); - if(track.get_turnout_id()) + if(track->get_turnout_id()) { - Block &block = layout.get_block_by_track(track); - if(block.get_train() && !block.get_train()->free_block(block)) + Block &block = track->get_block(); + if(block.get_train() && block.get_train()->is_block_critical(block)) set_status("Turnout is busy"); else { - unsigned paths = track.get_type().get_paths(); - unsigned i = track.get_active_path()+1; + unsigned paths = track->get_type().get_paths(); + unsigned i = track->get_active_path()+1; while(!(paths&(1<>i)) @@ -296,54 +275,56 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned) else ++i; } - track.set_active_path(i); - set_status(format("Turnout %d", track.get_turnout_id())); + track->set_active_path(i); + set_status(format("Turnout %d", track->get_turnout_id())); } } - if(unsigned sid = track.get_sensor_id()) + if(unsigned sid = track->get_sensor_id()) { if(options.simulate) layout.get_driver().set_sensor(sid, !layout.get_driver().get_sensor(sid)); set_status(format("Sensor %d", sid)); } } + else if(Vehicle *veh = dynamic_cast(obj)) + { + TrainDialog *dlg = new TrainDialog(*this, *veh->get_train()); + root->add(*dlg); + dlg->autosize(); + } } } -void Engineer::pointer_motion(int x, int y) +void Engineer::axis_motion(unsigned axis, float value, float) { - pointer_x = x; - pointer_y = y; + if(axis==0) + pointer.x = value; + if(axis==1) + pointer.y = value; pointer_moved = true; } void Engineer::view_all() { - const list &tracks = layout_3d.get_tracks(); + const Layout3D::ObjectMap &objects = layout_3d.get_all(); - float view_aspect = float(window.get_width()-200)/window.get_height(); - float view_height = tan(camera.get_field_of_view()/2)*2; + float view_aspect = float(window.get_width())/window.get_height(); + float view_height = tan(camera.get_field_of_view()/2.0f)*2.0f; float best_score = 0; GL::Vector3 pos; GL::Vector3 up; - for(float angle=0; angle::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - { - Point minp, maxp; - (*i)->get_bounds(angle, minp, maxp); - min_x = min(min_x, minp.x); - max_x = max(max_x, maxp.x); - min_y = min(min_y, minp.y); - max_y = max(max_y, maxp.y); - } + Transform trans = Transform::rotation(-angle, Vector(0, 0, 1)); + BoundingBox bbox; + for(Layout3D::ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) + bbox = bbox|trans.transform(i->second->get_object().get_bounding_box()); + + const Vector &minp = bbox.get_minimum_point(); + const Vector &maxp = bbox.get_maximum_point(); - float width = max_x-min_x; - float height = max_y-min_y; + float width = maxp.x-minp.x; + float height = maxp.y-minp.y; float aspect = width/height; float score = min(aspect/view_aspect, view_aspect/aspect); @@ -354,8 +335,8 @@ void Engineer::view_all() float size = max(width/view_aspect, height); float c = cos(angle); float s = sin(angle); - float x = (min_x+max_x)/2-size*105/window.get_height(); - float y = (min_y+max_y)/2; + float x = (minp.x+maxp.x)/2; + float y = (minp.y+maxp.y)/2; float z = max(size*1.05/view_height, 0.15); pos = GL::Vector3(c*x-s*y, s*x+c*y, z); @@ -370,81 +351,37 @@ void Engineer::view_all() camera.set_depth_clip(pos.z*0.5, pos.z*1.5); } -void Engineer::set_block_color(const Block &block, const GL::Color &color) +Object *Engineer::pick_object(const Vector &p) { - const set &tracks = block.get_tracks(); - for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - layout_3d.get_track(**i).get_path().set_color(color); -} + const GL::Vector3 &start = camera.get_position(); + GL::Vector4 ray = camera.unproject(GL::Vector4(p.x, p.y, 0, 0)); -void Engineer::reset_block_color(const Block &block) -{ - bool active = false; - if(unsigned sid=block.get_sensor_id()) - active = layout.get_driver().get_sensor(sid); - - if(block.get_train()) - { - GL::Color color; - map::iterator i = train_colors.find(block.get_train()); - if(i!=train_colors.end()) - color = i->second; - - if(active) - set_block_color(block, color*0.6); - else - set_block_color(block, color*0.5+0.5); - } - else if(active) - set_block_color(block, GL::Color(0.6)); - else - set_block_color(block, GL::Color(1)); + // XXX Do this better; make this function a template? + if(Vehicle *veh = layout.pick(Ray(start, Vector(ray)))) + return veh; + return layout.pick(Ray(start, Vector(ray))); } -void Engineer::sensor_event(unsigned addr, bool) +void Engineer::process_new_train(Train &train) { - const set &blocks = layout.get_blocks(); - for(set::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) - if((*i)->get_sensor_id()==addr) - reset_block_color(**i); -} - -void Engineer::block_reserved(const Block &block, const Train *) -{ - reset_block_color(block); -} - -Track3D *Engineer::pick_track(int x, int y) -{ - float view_height = tan(camera.get_field_of_view()/2)*2; - float xx = ((float(x)-window.get_width()/2)/window.get_height())*view_height; - float yy = (float(y)/window.get_height()-0.5)*view_height; - float size = 4.0/window.get_height()*view_height; - - camera.apply(); - - return layout_3d.pick_track(xx, yy, size); + Vehicle3D &loco3d = layout_3d.get(train.get_vehicle(0)); + overlay->set_label(loco3d, train.get_name()); + train.signal_name_changed.connect(sigc::bind<0>(sigc::mem_fun(overlay, &Overlay3D::set_label), sigc::ref(loco3d))); } void Engineer::train_added(Train &train) { - TrainPanel *tpanel = new TrainPanel(*this, ui_res, train); - root->add(*tpanel); - train_panels.push_back(tpanel); - rearrange_panels(); - - Vehicle3D &loco3d = layout_3d.get_vehicle(train.get_vehicle(0)); - overlay->set_label(loco3d, train.get_name()); - train.signal_name_changed.connect(sigc::bind<0>(sigc::mem_fun(overlay, &Overlay3D::set_label), sigc::ref(loco3d))); + new_trains.push_back(&train); GL::Color best_color; float best_d_sq = 0; for(unsigned i=0; i<10; ++i) { GL::Color color; - color.r = rand()*1.0/RAND_MAX; - color.g = rand()*1.0/RAND_MAX; - color.b = rand()*1.0/RAND_MAX; + unsigned h = rand()%3; + color.r = (h==0 ? 0.0 : rand()*1.0/RAND_MAX); + color.g = (h==1 ? 0.0 : rand()*1.0/RAND_MAX); + color.b = (h==2 ? 0.0 : rand()*1.0/RAND_MAX); color = color*(1/max(max(color.r, color.g), color.b)); float min_d_sq = 3; for(map::const_iterator j=train_colors.begin(); j!=train_colors.end(); ++j) @@ -463,6 +400,9 @@ void Engineer::train_added(Train &train) } } train_colors[&train] = best_color; + + Allocation3D *alloc = new Allocation3D(layout_3d, train); + alloc->set_color(best_color); } void Engineer::sighandler(int sig)