]> git.tdb.fi Git - r2c2.git/commitdiff
Show each train's blocks in a unique color
authorMikko Rasa <tdb@tdb.fi>
Tue, 16 Mar 2010 17:26:49 +0000 (17:26 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 16 Mar 2010 17:26:49 +0000 (17:26 +0000)
source/engineer/engineer.cpp
source/engineer/engineer.h

index 2e70fb4eddef55477f1ef1b8ad2b863391fc0584..371e82e49e7c4d4fc6b69315be3e0ca1b355d4a6 100644 (file)
@@ -6,6 +6,7 @@ Distributed under the GPL
 */
 
 #include <cmath>
+#include <cstdlib>
 #include <limits>
 #include <signal.h>
 #include <msp/core/except.h>
@@ -34,6 +35,8 @@ using namespace std;
 using namespace Marklin;
 using namespace Msp;
 
+Application::RegApp<Engineer> 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<Train *>::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 = &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<Train *, GL::Color>::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<Train *, GL::Color>::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_sq<min_d_sq)
+                               min_d_sq = d_sq;
+               }
+               if(min_d_sq>best_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> Engineer::reg;
index 5fa05871973e302872b589589df060b3c29680d3..48d60a2df9ae82679c75615852ed86d6f3c9aaf3 100644 (file)
@@ -42,7 +42,7 @@ private:
        Marklin::Server *server;
        Msp::IO::EventDispatcher event_disp;
        Marklin::Overlay3D *overlay;
-       std::list<Marklin::Train *> new_trains;
+       std::map<Marklin::Train *, Msp::GL::Color> train_colors;
 
        Msp::GL::Camera camera;
        Msp::GL::Lighting lighting;