From c87c4200e105a21c1d1f9faec20c4c08ddbb53ee Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 16 Mar 2010 17:26:49 +0000 Subject: [PATCH] Show each train's blocks in a unique color --- source/engineer/engineer.cpp | 72 ++++++++++++++++++++++++++---------- source/engineer/engineer.h | 2 +- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 2e70fb4..371e82e 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -6,6 +6,7 @@ Distributed under the GPL */ #include +#include #include #include #include @@ -34,6 +35,8 @@ using namespace std; using namespace Marklin; 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), @@ -60,6 +63,8 @@ Engineer::Engineer(int argc, char **argv): main_panel->set_position(0, window.get_height()-main_panel->get_geometry().h); main_panel->set_visible(true); + overlay = new Overlay3D(window, camera, ui_res.get_default_font()); + // Setup railroad control DataFile::load(catalogue, "tracks.dat"); DataFile::load(catalogue, "locos.dat"); @@ -81,8 +86,6 @@ Engineer::Engineer(int argc, char **argv): // Setup 3D view DataFile::load(arrow_mesh, "arrow.mesh"); - overlay = new Overlay3D(window, camera, ui_res.get_default_font()); - pipeline.set_camera(&camera); pipeline.add_renderable(layout_3d.get_scene()); @@ -141,10 +144,6 @@ void Engineer::tick() layout.tick(); event_disp.tick(Time::zero); - for(list::iterator i=new_trains.begin(); i!=new_trains.end(); ++i) - overlay->set_label(layout_3d.get_train(**i), (*i)->get_name()); - new_trains.clear(); - GL::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT); pipeline.render_all(); @@ -253,7 +252,8 @@ void Engineer::pointer_motion(int x, int y) reset_block_color(*placing_block); placing_block = █ placing_entry = 0; - set_block_color(*placing_block, GL::Color(0.5, 1, 0.7)); + if(!placing_block->get_train()) + set_block_color(*placing_block, GL::Color(0)); } } } @@ -319,19 +319,26 @@ void Engineer::set_block_color(const Block &block, const GL::Color &color) void Engineer::reset_block_color(const Block &block) { + bool active = false; if(unsigned sid=block.get_sensor_id()) - { - if(layout.get_driver().get_sensor(sid)) - { - set_block_color(block, GL::Color(1, 0.5, 0.3)); - return; - } - } + active = layout.get_driver().get_sensor(sid); if(block.get_train()) - set_block_color(block, GL::Color(1, 1, 0.3)); + { + 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, 1, 1)); + set_block_color(block, GL::Color(1)); } void Engineer::sensor_event(unsigned addr, bool) @@ -370,7 +377,36 @@ void Engineer::train_added(Train &train) train_panels.push_back(tpanel); tpanel->set_visible(true); - new_trains.push_back(&train); + Train3D &t3d = layout_3d.get_train(train); + overlay->set_label(t3d, train.get_name()); + train.signal_name_changed.connect(sigc::bind<0>(sigc::mem_fun(overlay, &Overlay3D::set_label), sigc::ref(t3d))); + + 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; + 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) + { + float dr = color.r-j->second.r; + float dg = color.g-j->second.g; + float db = color.b-j->second.b; + float d_sq = dr*dr+dg*dg+db*db; + if(d_sqbest_d_sq) + { + best_color = color; + best_d_sq = min_d_sq; + } + } + train_colors[&train] = best_color; } void Engineer::sighandler(int sig) @@ -388,5 +424,3 @@ void Engineer::sighandler(int sig) else if(sig==SIGTERM || sig==SIGINT) exit(0); } - -Application::RegApp Engineer::reg; diff --git a/source/engineer/engineer.h b/source/engineer/engineer.h index 5fa0587..48d60a2 100644 --- a/source/engineer/engineer.h +++ b/source/engineer/engineer.h @@ -42,7 +42,7 @@ private: Marklin::Server *server; Msp::IO::EventDispatcher event_disp; Marklin::Overlay3D *overlay; - std::list new_trains; + std::map train_colors; Msp::GL::Camera camera; Msp::GL::Lighting lighting; -- 2.45.2