]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/designer.cpp
Add train status display
[r2c2.git] / source / designer / designer.cpp
index eb5a0efca455ecd40379ddd34e5c0ca3af2e6c68..3ca630f2431cfccbc12b475b91e357fd0b9edc65 100644 (file)
@@ -5,12 +5,14 @@
 #include <SDL.h>
 #include <msp/gl/rendermode.h>
 #include <msp/gl/select.h>
+#include <msp/gl/texture2d.h>
 #include <msp/strings/codec.h>
 #include <msp/strings/lexicalcast.h>
 #include <msp/strings/utf8.h>
 #include <msp/strings/utils.h>
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
+#include "libmarklin/tracktype.h"
 #include "designer.h"
 #include "input.h"
 #include "manipulator.h"
@@ -36,21 +38,19 @@ Designer::Designer(int argc, char **argv):
        rotate(0),
        pitch(0)
 {
-       cout<<"blah?\n";
        catalogue.load("tracks.dat");
-       cout<<catalogue.get_tracks().size()<<'\n';
 
        cat_layout=new Layout(catalogue);
        cat_layout_3d=new Layout3D(*cat_layout);
 
-       const Catalogue::TrackMap &ctracks=catalogue.get_tracks();
+       const map<unsigned, TrackType *> &ctracks=catalogue.get_tracks();
        unsigned n=0;
-       for(Catalogue::TrackMap::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
+       for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
        {
-               Track *track=i->second->copy();
+               Track *track=new Track(*i->second);
                track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
                track->set_rotation(M_PI/2);
-               cat_layout->add_track(track);
+               cat_layout->add_track(*track);
        }
 
        manipulator=new Manipulator(*this);
@@ -63,15 +63,15 @@ Designer::Designer(int argc, char **argv):
        if(argc>1)
        {
                layout->load(argv[1]);
-               const Track3DSeq &ltracks=layout_3d->get_tracks();
-               for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
+               const list<Track3D *> &ltracks=layout_3d->get_tracks();
+               for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
                {
                        if((*i)->get_track().get_sensor_id())
-                               (*i)->set_color(Color(1, 1, 0.5));
+                               (*i)->set_color(GL::Color(1, 1, 0.5));
                        else if((*i)->get_track().get_turnout_id())
-                               (*i)->set_color(Color(0.5, 1, 1));
+                               (*i)->set_color(GL::Color(0.5, 1, 1));
                        else if((*i)->get_track().get_flex())
-                               (*i)->set_color(Color(1, 0.5, 1));
+                               (*i)->set_color(GL::Color(1, 0.5, 1));
                }
        }
 
@@ -83,6 +83,17 @@ Designer::Designer(int argc, char **argv):
        measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
 }
 
+Designer::~Designer()
+{
+       delete manipulator;
+       delete selection;
+       delete layout;
+       delete layout_3d;
+       delete cat_layout;
+       delete cat_layout_3d;
+       delete measure;
+}
+
 int Designer::main()
 {
        setenv("__GL_SYNC_TO_VBLANK", "1", 0);
@@ -98,14 +109,19 @@ int Designer::main()
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glEnable(GL_CULL_FACE);
 
+       GL::Texture2D *font_tex=new GL::Texture2D;
+       font_tex->set_min_filter(GL::LINEAR);
+       font_tex->load_image("dejavu-20.png");
        font=new GL::Font();
-       Parser::load(*font, "dejavu.font");
+       font->set_texture(*font_tex);
+       DataFile::load(*font, "dejavu-20.font");
 
        mode=SELECT;
 
        Application::main();
 
        delete font;
+       delete font_tex;
        delete input;
 
        SDL_Quit();
@@ -127,8 +143,8 @@ void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
        float uy=sin_yaw*-sin_pitch*0.41421;
        float uz=cos_pitch*0.41421;
 
-       float xf=(float)x*2/screen_w-1;
-       float yf=1-(float)y*2/screen_h;
+       float xf=static_cast<float>(x)*2/screen_w-1;
+       float yf=1-static_cast<float>(y)*2/screen_h;
 
        float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
        float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
@@ -143,7 +159,7 @@ void Designer::tick()
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
-               float gx,gy;
+               float gx, gy;
                switch(event.type)
                {
                case SDL_MOUSEBUTTONDOWN:
@@ -226,21 +242,22 @@ void Designer::tick()
                if(t3d)
                {
                        const Track &track=t3d->get_track();
+                       const TrackType &ttype=track.get_type();
                        ostringstream ss;
                        ss.precision(2);
-                       ss<<track.get_article_number()<<' '<<track.get_description();
+                       ss<<ttype.get_article_number()<<' '<<ttype.get_description();
                        if(mode!=CATALOGUE)
-                               ss<<" (slope "<<track.get_slope()/track.get_length()*100<<"%)";
+                               ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
                        if(track.get_turnout_id())
                                ss<<" (turnout "<<track.get_turnout_id()<<')';
                        else if(track.get_sensor_id())
                                ss<<" (sensor "<<track.get_sensor_id()<<')';
-                       tooltip=decode<Utf8>(ss.str());
+                       tooltip=ss.str();
 
                        move_tooltip(pointer_x, pointer_y);
                }
                else
-                       tooltip=L"";
+                       tooltip="";
 
                tooltip_timeout=Time::TimeStamp();
        }
@@ -250,17 +267,6 @@ void Designer::tick()
        SDL_GL_SwapBuffers();
 }
 
-Designer::~Designer()
-{
-       delete manipulator;
-       delete selection;
-       delete layout;
-       delete layout_3d;
-       delete cat_layout;
-       delete cat_layout_3d;
-       delete measure;
-}
-
 /*** private ***/
 
 void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
@@ -303,10 +309,10 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
                selection->select_more();
        else if(key==SDLK_l && (mod&KMOD_SHIFT))
        {
-               const TrackSeq &tracks=layout->get_tracks();
+               const set<Track *> &tracks=layout->get_tracks();
                float len=0;
-               for(TrackSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-                       len+=(*i)->get_total_length();
+               for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+                       len+=(*i)->get_type().get_total_length();
                cout<<"Total length: "<<len<<"m\n";
        }
        else if(key==SDLK_l)
@@ -332,31 +338,31 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
        }
        else if(key==SDLK_x)
        {
-               Selection::TrackSet tracks=selection->get_tracks();
+               set<Track *> tracks=selection->get_tracks();
                selection->clear();
-               for(Selection::TrackSet::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
                {
-                       layout->remove_track(*i);
+                       layout->remove_track(**i);
                        delete *i;
                }
        }
        else if(key==SDLK_f && (mod&KMOD_SHIFT))
        {
-               const Selection::TrackSet &tracks=selection->get_tracks();
-               const TrackSeq &ltracks=layout->get_tracks();
-               for(Selection::TrackSet::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               const set<Track *> &tracks=selection->get_tracks();
+               const set<Track *> &ltracks=layout->get_tracks();
+               for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                {
                        (*i)->set_flex(!(*i)->get_flex());
                        (*i)->break_links();
-                       for(TrackSeq::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
+                       for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
                                if(*j!=*i)
                                        (*i)->snap_to(**j, true);
 
-                       Track3D *t3d=layout_3d->get_track(*i);
+                       Track3D &t3d=layout_3d->get_track(**i);
                        if((*i)->get_flex())
-                               t3d->set_color(Color(1, 0.5, 1));
+                               t3d.set_color(GL::Color(1, 0.5, 1));
                        else
-                               t3d->set_color(Color(1, 1, 1));
+                               t3d.set_color(GL::Color(1, 1, 1));
                }
        }
        else if(key==SDLK_f)
@@ -368,7 +374,7 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
        else if(key==SDLK_t)
        {
                Track *track=selection->get_track();
-               if(selection->size()==1 && track->get_n_routes()>1)
+               if(selection->size()==1 && track->get_type().get_n_routes()>1)
                {
                        ostringstream ss;
                        ss<<track->get_turnout_id();
@@ -381,7 +387,7 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
        else if(key==SDLK_s)
        {
                Track *track=selection->get_track();
-               if(selection->size()==1 && track->get_n_routes()==1)
+               if(selection->size()==1 && track->get_type().get_n_routes()==1)
                {
                        ostringstream ss;
                        ss<<track->get_sensor_id();
@@ -443,7 +449,7 @@ void Designer::button_press(int x, int y, float gx, float gy, unsigned btn)
                        {
                                Track *track=ctrack->get_track().copy();
                                track->set_position(Point(gx, gy, 0));
-                               layout->add_track(track);
+                               layout->add_track(*track);
 
                                selection->clear();
                                selection->add_track(track);
@@ -530,18 +536,6 @@ void Designer::render()
                        measure->render();
        }
 
-       /*glBegin(GL_LINES);
-       glColor4f(1, 0, 0, 1);
-       glVertex3f(0, 0, 0);
-       glVertex3f(0.5, 0, 0);
-       glColor4f(0, 1, 0, 1);
-       glVertex3f(0, 0, 0);
-       glVertex3f(0, 0.5, 0);
-       glColor4f(0, 0, 1, 1);
-       glVertex3f(0, 0, 0);
-       glVertex3f(0, 0, 0.5);
-       glEnd();*/
-
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, screen_w, 0, screen_h, 0, 1);
@@ -576,9 +570,9 @@ Track3D *Designer::pick_track(int x, int y)
        if(mode==CATALOGUE)
                l=cat_layout_3d;
 
-       float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843;
-       float yy=((float)y/screen_h-0.5)*0.82843;
-       float size=(float)4/screen_h*0.82843;
+       float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
+       float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
+       float size=4.0/screen_h*0.82843;
 
        project_3d();
        apply_camera();
@@ -588,7 +582,7 @@ Track3D *Designer::pick_track(int x, int y)
 
 void Designer::manipulation_status(const string &status)
 {
-       tooltip=decode<Utf8>(status);
+       tooltip=status;
 }
 
 void Designer::manipulation_done(bool)
@@ -605,7 +599,7 @@ void Designer::measure_changed()
        ostringstream ss;
        ss.precision(3);
        ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
-       tooltip=decode<Utf8>(ss.str());
+       tooltip=ss.str();
 }
 
 void Designer::measure_done()
@@ -615,9 +609,9 @@ void Designer::measure_done()
 
 void Designer::move_tooltip(int x, int y)
 {
-       int w=(int)(font->get_string_width(tooltip)*20);
-       tooltip_x=max(min((int)screen_w-w, x), 0);
-       tooltip_y=max(min((int)screen_h-20, y), 0);
+       int w=static_cast<int>(font->get_string_width(tooltip)*20);
+       tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
+       tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
 }
 
 void Designer::save_accept()
@@ -633,11 +627,11 @@ void Designer::turnout_id_accept()
        unsigned id=lexical_cast<unsigned>(input->get_text());
        track->set_turnout_id(id);
 
-       Track3D *t3d=layout_3d->get_track(track);
+       Track3D &t3d=layout_3d->get_track(*track);
        if(id)
-               t3d->set_color(Color(0.5, 1, 1));
+               t3d.set_color(GL::Color(0.5, 1, 1));
        else
-               t3d->set_color(Color(1, 1, 1));
+               t3d.set_color(GL::Color(1, 1, 1));
 
        input_dismiss();
 }
@@ -648,11 +642,11 @@ void Designer::sensor_id_accept()
        unsigned id=lexical_cast<unsigned>(input->get_text());
        track->set_sensor_id(id);
 
-       Track3D *t3d=layout_3d->get_track(track);
+       Track3D &t3d=layout_3d->get_track(*track);
        if(id)
-               t3d->set_color(Color(1, 1, 0.5));
+               t3d.set_color(GL::Color(1, 1, 0.5));
        else
-               t3d->set_color(Color(1, 1, 1));
+               t3d.set_color(GL::Color(1, 1, 1));
 
        input_dismiss();
 }