X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdesigner%2Fdesigner.cpp;h=4763b8fda6294f3d12ff420c1245bb1e6b975dc5;hb=f42183985c65e1e12f19e9246dee90b8e7e44a34;hp=180ed50ff1fb55ebd933302e74cfd4b9d0bdcf75;hpb=de09cba1d12af38ff2e7e4312d67624fe63bf0e4;p=r2c2.git diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index 180ed50..4763b8f 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -1,18 +1,22 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ #include #include -#include #include +#include +#include +#include #include #include +#include #include #include +#include #include #include #include @@ -25,16 +29,21 @@ Distributed under the GPL #include "manipulator.h" #include "measure.h" #include "selection.h" +#include "toolbar.h" using namespace std; using namespace Marklin; using namespace Msp; +Application::RegApp Designer::reg; + Designer::Designer(int argc, char **argv): screen_w(1280), screen_h(960), - input(0), + base_mesh(0), + cur_route(0), mode(SELECT), + input(0), cam_yaw(M_PI/2), cam_pitch(-M_PI/4), cam_pos(0, -0.5, 0.5), @@ -45,47 +54,47 @@ Designer::Designer(int argc, char **argv): rotate(0), pitch(0) { - catalogue.load("tracks.dat"); + DataFile::load(catalogue, "tracks.dat"); - cat_layout=new Layout(catalogue); - cat_layout_3d=new Layout3D(*cat_layout); + cat_layout = new Layout(catalogue); + cat_layout_3d = new Layout3D(*cat_layout); - const map &ctracks=catalogue.get_tracks(); - unsigned n=0; + const map &ctracks = catalogue.get_tracks(); + unsigned n = 0; for(map::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n) { - Track *track=new Track(*i->second); + 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); } - manipulator=new Manipulator(*this); + manipulator = new Manipulator(*this); manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status)); manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done)); - layout=new Layout(catalogue); - layout_3d=new Layout3D(*layout); + layout = new Layout(catalogue); + layout_3d = new Layout3D(*layout); if(argc>1) { - layout->load(argv[1]); - const list <racks=layout_3d->get_tracks(); + filename = argv[1]; + DataFile::load(*layout, argv[1]); + const list <racks = layout_3d->get_tracks(); for(list::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) + update_track_color(**i); + + if(!layout->get_base().empty()) { - if((*i)->get_track().get_sensor_id()) - (*i)->set_color(GL::Color(1, 1, 0.5)); - else if((*i)->get_track().get_turnout_id()) - (*i)->set_color(GL::Color(0.5, 1, 1)); - else if((*i)->get_track().get_flex()) - (*i)->set_color(GL::Color(1, 0.5, 1)); + base_mesh = new GL::Mesh; + DataFile::load(*base_mesh, layout->get_base()); } } - selection=new Selection; + selection = new Selection; manipulator->set_selection(selection); - measure=new Measure(*this); + measure = new Measure(*this); measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed)); measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done)); } @@ -103,16 +112,12 @@ Designer::~Designer() int Designer::main() { - dpy=new Graphics::Display; - wnd=new Graphics::Window(*dpy, screen_w, screen_h); - glc=new Graphics::GLContext(*wnd); + dpy = new Graphics::Display; + wnd = new Graphics::Window(*dpy, screen_w, screen_h); + glc = new Graphics::GLContext(*wnd); + wnd->set_title("Railway Designer"); wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0)); - wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press)); - wnd->signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release)); - wnd->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press)); - wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion)); - wnd->show(); glEnableClientState(GL_VERTEX_ARRAY); @@ -121,20 +126,30 @@ 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(); - font->set_texture(*font_tex); - DataFile::load(*font, "dejavu-20.font"); + DataFile::load(ui_res, "marklin.res"); + root = new GLtk::Root(ui_res, *wnd); + + lbl_tooltip = new GLtk::Label(ui_res); + lbl_tooltip->set_style("tooltip"); + root->add(*lbl_tooltip); + lbl_tooltip->set_visible(false); - mode=SELECT; + toolbar = new Toolbar(*this); + root->add(*toolbar); + toolbar->set_position(0, screen_h-toolbar->get_geometry().h); + toolbar->set_visible(true); + + + wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press)); + wnd->signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release)); + wnd->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press)); + wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion)); + + mode = SELECT; Application::main(); - delete font; - delete font_tex; - delete input; + delete root; delete glc; delete wnd; @@ -143,113 +158,145 @@ int Designer::main() return exit_code; } +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; +} + +void Designer::quit() +{ + exit(0); +} + +void Designer::edit_route(Route &r) +{ + cur_route = &r; +} + +void Designer::add_selection_to_route() +{ + if(!cur_route) + return; + + try + { + const set &stracks = selection->get_tracks(); + set tracks(stracks.begin(), stracks.end()); + cur_route->add_tracks(tracks); + } + catch(const Exception &e) + { + IO::print("%s\n", e.what()); + } +} + void Designer::map_pointer_coords(int x, int y, float &gx, float &gy) { - float cos_pitch=cos(cam_pitch); - float sin_pitch=sin(cam_pitch); - float cos_yaw=cos(cam_yaw); - float sin_yaw=sin(cam_yaw); + float cos_pitch = cos(cam_pitch); + float sin_pitch = sin(cam_pitch); + float cos_yaw = cos(cam_yaw); + float sin_yaw = sin(cam_yaw); - float rx=sin_yaw*0.55228; - float ry=-cos_yaw*0.55228; + float rx = sin_yaw*0.55228; + float ry = -cos_yaw*0.55228; - float ux=cos_yaw*-sin_pitch*0.41421; - float uy=sin_yaw*-sin_pitch*0.41421; - float uz=cos_pitch*0.41421; + float ux = cos_yaw*-sin_pitch*0.41421; + float uy = sin_yaw*-sin_pitch*0.41421; + float uz = cos_pitch*0.41421; - float xf=static_cast(x)*2/screen_w-1; - float yf=1-static_cast(y)*2/screen_h; + float xf = static_cast(x)*2/screen_w-1; + float yf = static_cast(y)*2/screen_h-1; - float vx=cos_yaw*cos_pitch + xf*rx + yf*ux; - float vy=sin_yaw*cos_pitch + xf*ry + yf*uy; - float vz=sin_pitch + yf*uz; + float vx = cos_yaw*cos_pitch + xf*rx + yf*ux; + float vy = sin_yaw*cos_pitch + xf*ry + yf*uy; + float vz = sin_pitch + yf*uz; - gx=cam_pos.x-vx*cam_pos.z/vz; - gy=cam_pos.y-vy*cam_pos.z/vz; + gx = cam_pos.x-vx*cam_pos.z/vz; + gy = cam_pos.y-vy*cam_pos.z/vz; } void Designer::tick() { - const Msp::Time::TimeStamp t=Msp::Time::now(); - float dt=(t-last_tick)/Msp::Time::sec; - last_tick=t; + const Msp::Time::TimeStamp t = Msp::Time::now(); + float dt = (t-last_tick)/Msp::Time::sec; + last_tick = t; dpy->tick(); if(move_y) { - cam_pos.x+=cos(cam_yaw)*dt*move_y; - cam_pos.y+=sin(cam_yaw)*dt*move_y; + cam_pos.x += cos(cam_yaw)*dt*move_y; + cam_pos.y += sin(cam_yaw)*dt*move_y; } if(move_x) { - cam_pos.x+=sin(cam_yaw)*dt*move_x; - cam_pos.y+=-cos(cam_yaw)*dt*move_x; + cam_pos.x += sin(cam_yaw)*dt*move_x; + cam_pos.y += -cos(cam_yaw)*dt*move_x; } if(zoom) { - cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom; - cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom; - cam_pos.z+=sin(cam_pitch)*dt*zoom; + cam_pos.x += cos(cam_yaw)*cos(cam_pitch)*dt*zoom; + cam_pos.y += sin(cam_yaw)*cos(cam_pitch)*dt*zoom; + cam_pos.z += sin(cam_pitch)*dt*zoom; } if(rotate) { - float vx=cos(cam_yaw)*cos(cam_pitch); - float vy=sin(cam_yaw)*cos(cam_pitch); - float vz=sin(cam_pitch); + float vx = cos(cam_yaw)*cos(cam_pitch); + float vy = sin(cam_yaw)*cos(cam_pitch); + float vz = sin(cam_pitch); - float gx=cam_pos.x-vx*cam_pos.z/vz; - float gy=cam_pos.y-vy*cam_pos.z/vz; - float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz; + float gx = cam_pos.x-vx*cam_pos.z/vz; + float gy = cam_pos.y-vy*cam_pos.z/vz; + float d = sqrt(vx*vx+vy*vy)*cam_pos.z/vz; - cam_yaw+=M_PI*dt*rotate; + cam_yaw += M_PI*dt*rotate; if(cam_yaw>M_PI*2) - cam_yaw-=M_PI*2; + cam_yaw -= M_PI*2; else if(cam_yaw<0) - cam_yaw+=M_PI*2; + cam_yaw += M_PI*2; - cam_pos.x=gx+cos(cam_yaw)*d; - cam_pos.y=gy+sin(cam_yaw)*d; + cam_pos.x = gx+cos(cam_yaw)*d; + cam_pos.y = gy+sin(cam_yaw)*d; } if(pitch) { - cam_pitch+=M_PI/2*dt*pitch; + cam_pitch += M_PI/2*dt*pitch; if(cam_pitch>M_PI/12) - cam_pitch=M_PI/12; + cam_pitch = M_PI/12; else if(cam_pitch<-M_PI/2) - cam_pitch=-M_PI/2; + cam_pitch = -M_PI/2; } if(tooltip_timeout && t>tooltip_timeout) { - Track3D *t3d=0; + Track3D *t3d = 0; if(mode==CATALOGUE) - t3d=pick_track(pointer_x, pointer_y); + t3d = pick_track(pointer_x, pointer_y); else - t3d=pick_track(pointer_x, pointer_y); + t3d = pick_track(pointer_x, pointer_y); if(t3d) { - const Track &track=t3d->get_track(); - const TrackType &ttype=track.get_type(); - ostringstream ss; - ss.precision(2); - ss<get_track(); + const TrackType &ttype = track.get_type(); + string info = format("%d %s", ttype.get_article_number(), ttype.get_description()); + if(mode!=CATALOGUE && abs(track.get_slope())>1e-4) + info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100)); if(track.get_turnout_id()) - ss<<" (turnout "<swap_buffers(); } -/*** private ***/ - -void Designer::key_press(unsigned code, unsigned mod, wchar_t ch) +void Designer::key_press(unsigned code, unsigned mod, wchar_t) { - unsigned key=Msp::Input::key_from_sys(code); + unsigned key = Msp::Input::key_from_sys(code); if(mode==INPUT) - { - input->key_press(key, mod, ch); return; - } if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R) - shift=true; + shift = true; if(key==Msp::Input::KEY_N) - mode=CATALOGUE; + mode = CATALOGUE; else if(key==Msp::Input::KEY_G) { manipulator->start_move(); - mode=MANIPULATE; + mode = MANIPULATE; } else if(key==Msp::Input::KEY_R) { manipulator->start_rotate(); - mode=MANIPULATE; + mode = MANIPULATE; } else if(key==Msp::Input::KEY_D) { manipulator->duplicate(); manipulator->start_move(); - mode=MANIPULATE; + mode = MANIPULATE; } else if(key==Msp::Input::KEY_W) - { - input=new ::Input(*this, "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; - } + save(); else if(key==Msp::Input::KEY_PLUS) selection->select_more(); else if(key==Msp::Input::KEY_L && (mod&1)) { - const set &tracks=layout->get_tracks(); - float len=0; + const set &tracks = layout->get_tracks(); + float len = 0; for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - len+=(*i)->get_type().get_total_length(); - cout<<"Total length: "<get_type().get_total_length(); + IO::print("Total length: %.1fm\n", len); } else if(key==Msp::Input::KEY_L) selection->select_linked(); else if(key==Msp::Input::KEY_M) { measure->start(); - mode=MEASURE; + mode = MEASURE; } else if(key==Msp::Input::KEY_Z) { manipulator->start_elevate(); - mode=MANIPULATE; + mode = MANIPULATE; } else if(key==Msp::Input::KEY_ESC) { if(mode==MANIPULATE) manipulator->cancel(); else if(mode==CATALOGUE) - mode=SELECT; + mode = SELECT; else selection->clear(); } else if(key==Msp::Input::KEY_X) { - set tracks=selection->get_tracks(); + set tracks = selection->get_tracks(); selection->clear(); for(set::iterator i=tracks.begin(); i!=tracks.end(); ++i) { @@ -340,8 +377,8 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t ch) } else if(key==Msp::Input::KEY_F && (mod&1)) { - const set &tracks=selection->get_tracks(); - const set <racks=layout->get_tracks(); + const set &tracks = selection->get_tracks(); + const set <racks = layout->get_tracks(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) { (*i)->set_flex(!(*i)->get_flex()); @@ -350,11 +387,7 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t ch) if(*j!=*i) (*i)->snap_to(**j, true); - Track3D &t3d=layout_3d->get_track(**i); - if((*i)->get_flex()) - t3d.set_color(GL::Color(1, 0.5, 1)); - else - t3d.set_color(GL::Color(1, 1, 1)); + update_track_color(layout_3d->get_track(**i)); } } else if(key==Msp::Input::KEY_F) @@ -365,76 +398,88 @@ void Designer::key_press(unsigned code, unsigned mod, wchar_t ch) manipulator->even_slope(); else if(key==Msp::Input::KEY_T) { - Track *track=selection->get_track(); - if(selection->size()==1 && track->get_type().get_n_routes()>1) + Track *track = selection->get_track(); + if(selection->size()==1 && track->get_type().get_n_paths()>1) { - ostringstream ss; - ss<get_turnout_id(); - input=new ::Input(*this, "Turnout ID", ss.str()); + 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; + mode = INPUT; } } else if(key==Msp::Input::KEY_S) { - Track *track=selection->get_track(); - if(selection->size()==1 && track->get_type().get_n_routes()==1) + 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) { - ostringstream ss; - ss<get_sensor_id(); - input=new ::Input(*this, "Sensor ID", ss.str()); + 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; + mode = INPUT; } } + else if(key==Msp::Input::KEY_A) + add_selection_to_route(); else if(key==Msp::Input::KEY_RIGHT) - rotate=-1; + rotate = -1; else if(key==Msp::Input::KEY_LEFT) - rotate=1; + rotate = 1; else if(key==Msp::Input::KEY_UP) - move_y=1; + move_y = 1; else if(key==Msp::Input::KEY_DOWN) - move_y=-1; + move_y = -1; else if(key==Msp::Input::KEY_INSERT) - zoom=-1; + zoom = -1; else if(key==Msp::Input::KEY_PGUP) - zoom=1; + zoom = 1; else if(key==Msp::Input::KEY_HOME) - pitch=1; + pitch = 1; else if(key==Msp::Input::KEY_END) - pitch=-1; + pitch = -1; else if(key==Msp::Input::KEY_DELETE) - move_x=-1; + move_x = -1; else if(key==Msp::Input::KEY_PGDN) - move_x=1; + move_x = 1; } void Designer::key_release(unsigned code, unsigned) { - unsigned key=Msp::Input::key_from_sys(code); + 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; + shift = false; else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT) - rotate=0; + rotate = 0; else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN) - move_y=0; + move_y = 0; else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP) - zoom=0; + zoom = 0; else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END) - pitch=0; + pitch = 0; else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN) - move_x=0; + move_x = 0; } void Designer::button_press(int x, int y, unsigned btn, unsigned) { - y=screen_h-y-1; + y = screen_h-y-1; float gx, gy; map_pointer_coords(x, y, gx, gy); @@ -443,27 +488,27 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned) { if(btn==1) { - Track3D *ctrack=pick_track(x, y); + Track3D *ctrack = pick_track(x, y); if(ctrack) { - Track *track=ctrack->get_track().copy(); + Track *track = ctrack->get_track().copy(); track->set_position(Point(gx, gy, 0)); layout->add_track(*track); selection->clear(); selection->add_track(track); - mode=SELECT; + mode = SELECT; } } else - mode=SELECT; + mode = SELECT; } else if(mode==SELECT) { if(btn==1) { - Track3D *track=pick_track(x, y); + Track3D *track = pick_track(x, y); if(track) { if(!shift) @@ -480,26 +525,23 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned) void Designer::pointer_motion(int x, int y) { - y=screen_h-y-1; + y = screen_h-y-1; - float gx, gy; - map_pointer_coords(x, y, gx, gy); + pointer_x = x; + pointer_y = y; if(mode==SELECT || mode==CATALOGUE) - { - pointer_x=x; - pointer_y=y; - tooltip_timeout=Msp::Time::now()+100*Msp::Time::msec; - } + tooltip_timeout = Msp::Time::now()+100*Msp::Time::msec; + + clear_tooltip(); if(mode!=INPUT) { + float gx, gy; + map_pointer_coords(x, y, gx, gy); manipulator->pointer_motion(x, y, gx, gy); measure->pointer_motion(x, y, gx, gy); } - - if(mode==MEASURE || mode==MANIPULATE) - move_tooltip(x, y); } void Designer::project_3d() @@ -526,7 +568,7 @@ void Designer::apply_camera() void Designer::render() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - glEnable(GL_DEPTH_TEST); + GL::enable(GL::DEPTH_TEST); project_3d(); apply_camera(); @@ -534,49 +576,54 @@ void Designer::render() cat_layout_3d->render(); else { + if(base_mesh) + { + GL::Texture::unbind(); + base_mesh->draw(); + } layout_3d->render(true); + 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); + } + } manipulator->render(); if(mode==MEASURE) measure->render(); } - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0, screen_w, 0, screen_h, 0, 1); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - glDisable(GL_DEPTH_TEST); + GL::matrix_mode(GL::PROJECTION); + GL::load_identity(); + GL::ortho_bottomleft(screen_w, screen_h); + GL::matrix_mode(GL::MODELVIEW); + GL::load_identity(); - if(!tooltip.empty()) - { - glTranslatef(tooltip_x, tooltip_y, 0); - glScalef(20, 20, 20); - float width=font->get_string_width(tooltip); - glColor4f(0, 0, 0, 0.5); - glBegin(GL_QUADS); - glVertex2f(0, 0); - glVertex2f(width, 0); - glVertex2f(width, 1); - glVertex2f(0, 1); - glEnd(); - glColor4f(1, 1, 1, 1); - font->draw_string(tooltip); - } + GL::disable(GL::DEPTH_TEST); - if(mode==INPUT) - input->render(); + root->render(); } Track3D *Designer::pick_track(int x, int y) { - Layout3D *l=layout_3d; + Layout3D *l = layout_3d; if(mode==CATALOGUE) - l=cat_layout_3d; + l = cat_layout_3d; - float xx=(static_cast(x-static_cast(screen_w)/2)/screen_h)*0.82843; - float yy=(static_cast(y)/screen_h-0.5)*0.82843; - float size=4.0/screen_h*0.82843; + float xx = (static_cast(x-static_cast(screen_w)/2)/screen_h)*0.82843; + float yy = (static_cast(y)/screen_h-0.5)*0.82843; + float size = 4.0/screen_h*0.82843; project_3d(); apply_camera(); @@ -584,38 +631,63 @@ Track3D *Designer::pick_track(int x, int y) return l->pick_track(xx, yy, size); } +void Designer::update_track_color(Track3D &track) +{ + if(track.get_track().get_sensor_id()) + { + if(track.get_track().get_flex()) + track.set_color(GL::Color(1, 0.6, 1)); + else + track.set_color(GL::Color(0.7, 0.7, 1)); + } + else if(track.get_track().get_turnout_id()) + track.set_color(GL::Color(0.8, 1, 0.8)); + else if(track.get_track().get_flex()) + track.set_color(GL::Color(1, 0.8, 0.8)); + else + track.set_color(GL::Color(1, 1, 1)); +} + void Designer::manipulation_status(const string &status) { - tooltip=status; + set_tooltip(pointer_x, pointer_y, status); } void Designer::manipulation_done(bool) { - mode=SELECT; + mode = SELECT; } void Designer::measure_changed() { - float pard=measure->get_parallel_distance()*1000; - float perpd=measure->get_perpendicular_distance()*1000; - float d=sqrt(pard*pard+perpd*perpd); - float adiff=measure->get_angle_difference()*180/M_PI; - ostringstream ss; - ss.precision(3); - ss<<"Par "<get_parallel_distance()*1000; + float perpd = measure->get_perpendicular_distance()*1000; + float d = sqrt(pard*pard+perpd*perpd); + float adiff = measure->get_angle_difference()*180/M_PI; + string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff); + set_tooltip(pointer_x, pointer_y, info); } void Designer::measure_done() { - mode=SELECT; + mode = SELECT; } -void Designer::move_tooltip(int x, int y) +void Designer::set_tooltip(int x, int y, const std::string &text) { - int w=static_cast(font->get_string_width(tooltip)*20); - tooltip_x=max(min(static_cast(screen_w)-w, x), 0); - tooltip_y=max(min(static_cast(screen_h)-20, y), 0); + int fontsize = ui_res.get_default_font().get_default_size(); + int h = fontsize+6; + int w = static_cast(ui_res.get_default_font().get_string_width(text)*fontsize)+6; + x = max(min(static_cast(screen_w)-w, x), 0); + y = max(min(static_cast(screen_h)-h, y), 0); + lbl_tooltip->set_text(text); + lbl_tooltip->set_geometry(GLtk::Geometry(x, y, w, h)); + lbl_tooltip->set_visible(true); +} + +void Designer::clear_tooltip() +{ + lbl_tooltip->set_visible(false); } void Designer::save_accept() @@ -627,30 +699,25 @@ void Designer::save_accept() void Designer::turnout_id_accept() { - Track *track=selection->get_track(); - unsigned id=lexical_cast(input->get_text()); + Track *track = selection->get_track(); + unsigned id = lexical_cast(input->get_text()); track->set_turnout_id(id); - Track3D &t3d=layout_3d->get_track(*track); - if(id) - t3d.set_color(GL::Color(0.5, 1, 1)); - else - t3d.set_color(GL::Color(1, 1, 1)); + update_track_color(layout_3d->get_track(*track)); input_dismiss(); } void Designer::sensor_id_accept() { - Track *track=selection->get_track(); - unsigned id=lexical_cast(input->get_text()); - track->set_sensor_id(id); + const set &tracks = selection->get_tracks(); + unsigned id = lexical_cast(input->get_text()); + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + { + (*i)->set_sensor_id(id); - Track3D &t3d=layout_3d->get_track(*track); - if(id) - t3d.set_color(GL::Color(1, 1, 0.5)); - else - t3d.set_color(GL::Color(1, 1, 1)); + update_track_color(layout_3d->get_track(**i)); + } input_dismiss(); } @@ -658,8 +725,6 @@ void Designer::sensor_id_accept() void Designer::input_dismiss() { delete input; - input=0; - mode=SELECT; + input = 0; + mode = SELECT; } - -Application::RegApp Designer::reg;