X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdesigner%2Fdesigner.cpp;h=83164b5da169ba05aaf98876754208deec54b5b6;hb=283542ae424b94786652fbf0c67b883b63a8e7a0;hp=d0bc940b4cea510e57083dd00e204c0d38f7aea6;hpb=06c100aacb559fbbe7380e15981c4772092c269b;p=r2c2.git diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index d0bc940..83164b5 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -1,665 +1,648 @@ +/* $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 "libmarklin/route.h" +#include "libmarklin/tracktype.h" +#include "3d/path.h" #include "designer.h" #include "input.h" #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), + window(1280, 960), + base_object(0), + cur_route(0), mode(SELECT), - cam_yaw(M_PI/2), - cam_pitch(-M_PI/4), - cam_pos(0, -0.5, 0.5), - shift(false), - move_x(0), - move_y(0), - zoom(0), - rotate(0), - pitch(0) -{ - catalogue.load("tracks.dat"); - - cat_layout=new Layout(catalogue); - cat_layout_3d=new Layout3D(*cat_layout); - - const Catalogue::TrackMap &ctracks=catalogue.get_tracks(); - unsigned n=0; - for(Catalogue::TrackMap::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n) - { - Track *track=i->second->copy(); - 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(*this, selection), + measure(*this), + track_wrap(*this, selection) +{ + window.set_title("Railway Designer"); + window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0)); + + selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed)); + manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status)); + manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done)); + measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed)); + measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done)); + + // Setup catalogue and layout + DataFile::load(catalogue, "tracks.dat"); + + cat_layout_3d = new Layout3D(catalogue.get_layout()); - 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); + layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added)); + layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed)); if(argc>1) { - layout->load(argv[1]); - const Track3DSeq <racks=layout_3d->get_tracks(); - for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) + filename = argv[1]; + DataFile::load(*layout, argv[1]); + + if(!layout->get_base().empty()) { - if((*i)->get_track().get_sensor_id()) - (*i)->set_color(Color(1, 1, 0.5)); - else if((*i)->get_track().get_turnout_id()) - (*i)->set_color(Color(0.5, 1, 1)); - else if((*i)->get_track().get_flex()) - (*i)->set_color(Color(1, 0.5, 1)); + base_object = new GL::Object; + DataFile::load(*base_object, layout->get_base()); } } - selection=new Selection; - manipulator->set_selection(selection); + // Setup OpenGL + GL::enable(GL_CULL_FACE); + + pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false); + pipeline->set_camera(&camera); + pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0); + if(base_object) + pipeline->add_renderable(*base_object); + pipeline->add_renderable_for_pass(track_wrap, "unlit"); + pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit"); + pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit"); + + light.set_position(0, -0.259, 0.966, 0); + lighting.attach(0, light); + + GL::PipelinePass *pass = &pipeline->add_pass(0); + pass->lighting = &lighting; + pass->depth_test = &GL::DepthTest::lequal(); + + pass = &pipeline->add_pass("unlit"); + pass->depth_test = &GL::DepthTest::lequal(); + pass->blend = &GL::Blend::alpha(); + + pass = &pipeline->add_pass("blended"); + pass->lighting = &lighting; + pass->depth_test = &GL::DepthTest::lequal(); + pass->blend = &GL::Blend::alpha(); + + camera.set_up_direction(GL::Vector3(0, 0, 1)); + camera.set_look_direction(GL::Vector3(0, 0.5, -0.866)); + + // Setup UI + DataFile::load(ui_res, "marklin.res"); + root = new GLtk::Root(ui_res, window); + root->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press)); + root->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press)); + root->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion)); + root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip)); + + toolbar = new Toolbar(*this); + root->add(*toolbar); + toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h); + toolbar->set_focusable(false); + + GLtk::Panel *statusbar = new GLtk::Panel(ui_res); + root->add(*statusbar); + statusbar->set_size(window.get_width(), 20); + statusbar->set_focusable(false); + + lbl_status = new GLtk::Label(ui_res); + statusbar->add(*lbl_status); + lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16)); + + overlay = new Overlay3D(window, camera, ui_res.get_default_font()); + + camera_ctl = new CameraController(*this, *root, camera); + camera_ctl->view_all(); + + const Layout3D::TrackMap &tracks = layout_3d->get_tracks(); + for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + update_track_icon(*i->second); +} - 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)); +Designer::~Designer() +{ + delete camera_ctl; + delete overlay; + delete root; + delete pipeline; + delete base_object; + delete layout_3d; + delete layout; + delete cat_layout_3d; } int Designer::main() { - setenv("__GL_SYNC_TO_VBLANK", "1", 0); - SDL_Init(SDL_INIT_VIDEO); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); - SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL); - SDL_EnableUNICODE(1); - - glEnableClientState(GL_VERTEX_ARRAY); - glEnable(GL_DEPTH_TEST); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_CULL_FACE); - - font=new GL::Font(); - Parser::load(*font, "dejavu-20.font"); - - mode=SELECT; + window.show(); - Application::main(); + mode = SELECT; - delete font; - delete input; - - SDL_Quit(); - - return exit_code; + return Application::main(); } -void Designer::map_pointer_coords(int x, int y, float &gx, float &gy) +void Designer::save() { - 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 ux=cos_yaw*-sin_pitch*0.41421; - float uy=sin_yaw*-sin_pitch*0.41421; - float uz=cos_pitch*0.41421; + InputDialog *input = new InputDialog(*this, "Save layout", filename); + input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save)); +} - float xf=(float)x*2/screen_w-1; - float yf=1-(float)y*2/screen_h; +void Designer::quit() +{ + exit(0); +} - 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; +void Designer::new_track() +{ + mode = CATALOGUE; +} - gx=cam_pos.x-vx*cam_pos.z/vz; - gy=cam_pos.y-vy*cam_pos.z/vz; +void Designer::set_turnout_id() +{ + Track *track = selection.get_track(); + if(selection.size()==1 && track->get_type().is_turnout()) + { + InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id())); + input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept)); + } } -void Designer::tick() +void Designer::set_sensor_id() { - SDL_Event event; - while(SDL_PollEvent(&event)) + const set &tracks = selection.get_tracks(); + bool ok = false; + int id = -1; + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) { - float gx,gy; - switch(event.type) + if(!(*i)->get_type().is_turnout()) + ok = true; + if(static_cast((*i)->get_sensor_id())!=id) { - case SDL_MOUSEBUTTONDOWN: - map_pointer_coords(event.button.x, event.button.y, gx, gy); - button_press(event.button.x, screen_h-1-event.button.y, gx, gy, event.button.button); - break; - case SDL_MOUSEMOTION: - map_pointer_coords(event.motion.x, event.motion.y, gx, gy); - pointer_motion(event.motion.x, screen_h-1-event.motion.y, gx, gy); - break; - case SDL_KEYDOWN: - key_press(event.key.keysym.sym, event.key.keysym.mod, event.key.keysym.unicode); - break; - case SDL_KEYUP: - key_release(event.key.keysym.sym, event.key.keysym.mod); - break; - case SDL_QUIT: - exit(0); - break; + if(id==-1) + id = (*i)->get_sensor_id(); + else + id = -2; } } - const Time::TimeStamp t=Time::now(); - float dt=(t-last_tick)/Time::sec; - last_tick=t; - - if(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; - } - if(zoom) + if(ok) { - 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; + InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string())); + input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept)); } - if(rotate) +} + +void Designer::rename_route() +{ + if(!cur_route) + return; + + InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name()); + input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept)); +} + +void Designer::edit_route(Route *r) +{ + cur_route = r; + show_route(r); +} + +void Designer::add_selection_to_route() +{ + if(!cur_route) + return; + + try { - 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; - - cam_yaw+=M_PI*dt*rotate; - if(cam_yaw>M_PI*2) - cam_yaw-=M_PI*2; - else if(cam_yaw<0) - cam_yaw+=M_PI*2; - - cam_pos.x=gx+cos(cam_yaw)*d; - cam_pos.y=gy+sin(cam_yaw)*d; + cur_route->add_tracks(selection.get_tracks()); } - if(pitch) + catch(const Exception &e) { - cam_pitch+=M_PI/2*dt*pitch; - if(cam_pitch>M_PI/12) - cam_pitch=M_PI/12; - else if(cam_pitch<-M_PI/2) - cam_pitch=-M_PI/2; + lbl_status->set_text(e.what()); } - if(tooltip_timeout && t>tooltip_timeout) - { - Track3D *t3d=0; + show_route(cur_route); +} - if(mode==CATALOGUE) - t3d=pick_track(pointer_x, pointer_y); - else - t3d=pick_track(pointer_x, pointer_y); +Point Designer::map_pointer_coords(int x, int y) +{ + float xf = x*2.0/window.get_width()-1.0; + float yf = y*2.0/window.get_height()-1.0; + GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0)); + const GL::Vector3 &pos = camera.get_position(); - if(t3d) - { - const Track &track=t3d->get_track(); - ostringstream ss; - ss.precision(2); - ss<(ss.str()); - - move_tooltip(pointer_x, pointer_y); - } - else - tooltip=L""; + return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z); +} - tooltip_timeout=Time::TimeStamp(); - } +void Designer::tick() +{ + const Msp::Time::TimeStamp t = Msp::Time::now(); + float dt = (t-last_tick)/Msp::Time::sec; + last_tick = t; - render(); + window.get_display().tick(); + root->tick(); + camera_ctl->tick(dt); - SDL_GL_SwapBuffers(); -} + for(list::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i) + layout_3d->get_track(**i).get_path().set_mask(0); + new_tracks.clear(); -Designer::~Designer() -{ - delete manipulator; - delete selection; - delete layout; - delete layout_3d; - delete cat_layout; - delete cat_layout_3d; - delete measure; -} + render(); -/*** private ***/ + window.swap_buffers(); +} -void Designer::key_press(unsigned key, unsigned mod, wchar_t ch) +void Designer::key_press(unsigned key, unsigned mod, wchar_t) { - if(mode==INPUT) - { - input->key_press(key, mod, ch); - return; - } - - if(key==SDLK_RSHIFT || key==SDLK_LSHIFT) - shift=true; + key = Input::key_from_sys(key); + mod = Input::mod_from_sys(mod); - if(key==SDLK_n) - mode=CATALOGUE; - else if(key==SDLK_g) + if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT)) { - manipulator->start_move(); - mode=MANIPULATE; + manipulator.start_extend(); + mode = MANIPULATE; } - else if(key==SDLK_r) + else if(key==Msp::Input::KEY_N) + new_track(); + else if(key==Msp::Input::KEY_G) { - manipulator->start_rotate(); - mode=MANIPULATE; + manipulator.start_move(); + mode = MANIPULATE; } - else if(key==SDLK_d) + else if(key==Msp::Input::KEY_R) { - manipulator->duplicate(); - manipulator->start_move(); - mode=MANIPULATE; + manipulator.start_rotate(); + mode = MANIPULATE; } - else if(key==SDLK_w) + else if(key==Msp::Input::KEY_D) { - 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; + manipulator.duplicate(); + manipulator.start_move(); + mode = MANIPULATE; } - else if(key==SDLK_PLUS) - selection->select_more(); - else if(key==SDLK_l && (mod&KMOD_SHIFT)) + else if(key==Msp::Input::KEY_W) + save(); + else if(key==Msp::Input::KEY_PLUS) + selection.select_more(); + else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT)) + selection.select_blocks(); + else if(key==Msp::Input::KEY_L) + selection.select_linked(); + else if(key==Msp::Input::KEY_M) { - const TrackSeq &tracks=layout->get_tracks(); - float len=0; - for(TrackSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - len+=(*i)->get_total_length(); - cout<<"Total length: "<select_linked(); - else if(key==SDLK_m) + else if(key==Msp::Input::KEY_Z) { - measure->start(); - mode=MEASURE; + manipulator.start_elevate(); + mode = MANIPULATE; } - else if(key==SDLK_z) - { - manipulator->start_elevate(); - mode=MANIPULATE; - } - else if(key==SDLK_ESCAPE) + else if(key==Msp::Input::KEY_ESC) { if(mode==MANIPULATE) - manipulator->cancel(); + manipulator.cancel(); else if(mode==CATALOGUE) - mode=SELECT; + mode = SELECT; else - selection->clear(); + selection.clear(); } - else if(key==SDLK_x) + else if(key==Msp::Input::KEY_X) { - Selection::TrackSet tracks=selection->get_tracks(); - selection->clear(); - for(Selection::TrackSet::iterator i=tracks.begin(); i!=tracks.end(); ++i) + set tracks = selection.get_tracks(); + selection.clear(); + for(set::iterator i=tracks.begin(); i!=tracks.end(); ++i) { - layout->remove_track(*i); + overlay->clear(layout_3d->get_track(**i)); + layout->remove_track(**i); delete *i; } } - else if(key==SDLK_f && (mod&KMOD_SHIFT)) + else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT)) { - const Selection::TrackSet &tracks=selection->get_tracks(); - const TrackSeq <racks=layout->get_tracks(); - for(Selection::TrackSet::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + 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()); (*i)->break_links(); - for(TrackSeq::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j) + for(set::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j) if(*j!=*i) (*i)->snap_to(**j, true); - Track3D *t3d=layout_3d->get_track(*i); - if((*i)->get_flex()) - t3d->set_color(Color(1, 0.5, 1)); - else - t3d->set_color(Color(1, 1, 1)); - } - } - else if(key==SDLK_f) - manipulator->flatten(); - else if(key==SDLK_e && (mod&KMOD_SHIFT)) - manipulator->even_slope(true); - else if(key==SDLK_e) - manipulator->even_slope(); - else if(key==SDLK_t) - { - Track *track=selection->get_track(); - if(selection->size()==1 && track->get_n_routes()>1) - { - ostringstream ss; - ss<get_turnout_id(); - input=new Input(*this, "Turnout ID", ss.str()); - 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; - } - } - else if(key==SDLK_s) - { - Track *track=selection->get_track(); - if(selection->size()==1 && track->get_n_routes()==1) - { - ostringstream ss; - ss<get_sensor_id(); - input=new Input(*this, "Sensor ID", ss.str()); - 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; + update_track_icon(layout_3d->get_track(**i)); } } - else if(key==SDLK_RIGHT) - rotate=-1; - else if(key==SDLK_LEFT) - rotate=1; - else if(key==SDLK_UP) - move_y=1; - else if(key==SDLK_DOWN) - move_y=-1; - else if(key==SDLK_INSERT) - zoom=-1; - else if(key==SDLK_PAGEUP) - zoom=1; - else if(key==SDLK_HOME) - pitch=1; - else if(key==SDLK_END) - pitch=-1; - else if(key==SDLK_DELETE) - move_x=-1; - else if(key==SDLK_PAGEDOWN) - move_x=1; -} - -void Designer::key_release(unsigned key, unsigned) -{ - if(mode==INPUT) - return; - - if(key==SDLK_RSHIFT || key==SDLK_LSHIFT) - shift=false; - else if(key==SDLK_RIGHT || key==SDLK_LEFT) - rotate=0; - else if(key==SDLK_UP || key==SDLK_DOWN) - move_y=0; - else if(key==SDLK_INSERT || key==SDLK_PAGEUP) - zoom=0; - else if(key==SDLK_HOME || key==SDLK_END) - pitch=0; - else if(key==SDLK_DELETE || key==SDLK_PAGEDOWN) - move_x=0; + else if(key==Msp::Input::KEY_F) + manipulator.flatten(); + else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT)) + manipulator.even_slope(true); + else if(key==Msp::Input::KEY_E) + manipulator.even_slope(); + else if(key==Msp::Input::KEY_T) + set_turnout_id(); + else if(key==Msp::Input::KEY_S) + set_sensor_id(); + else if(key==Msp::Input::KEY_A) + add_selection_to_route(); + else if(key==Msp::Input::KEY_C) + manipulator.connect(); } -void Designer::button_press(int x, int y, float gx, float gy, unsigned btn) +void Designer::button_press(int x, int y, unsigned btn, unsigned mod) { + y = window.get_height()-y-1; + mod = Input::mod_from_sys(mod); + + Point ground = map_pointer_coords(x, y); + if(mode==CATALOGUE) { if(btn==1) { - Track3D *ctrack=pick_track(x, y); + Track3D *ctrack = pick_track(x, y); if(ctrack) { - Track *track=ctrack->get_track().copy(); - track->set_position(Point(gx, gy, 0)); - layout->add_track(track); + Track *track = new Track(*layout, ctrack->get_track().get_type()); + track->set_position(ground); - selection->clear(); - selection->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) - selection->clear(); - selection->toggle_track(&track->get_track()); + if(!(mod&Input::MOD_SHIFT)) + selection.clear(); + selection.toggle_track(&track->get_track()); } } } else if(mode==MANIPULATE) - manipulator->button_press(x, y, gx, gy, btn); + manipulator.button_press(x, y, ground.x, ground.y, btn); else if(mode==MEASURE) - measure->button_press(x, y, gx, gy, btn); + measure.button_press(x, y, ground.x, ground.y, btn); } -void Designer::pointer_motion(int x, int y, float gx, float gy) +void Designer::pointer_motion(int x, int y) { - if(mode==SELECT || mode==CATALOGUE) - { - pointer_x=x; - pointer_y=y; - tooltip_timeout=Time::now()+100*Time::msec; - } + y = window.get_height()-y-1; - if(mode!=INPUT) + if(!root->get_child_at(x, y)) { - manipulator->pointer_motion(x, y, gx, gy); - measure->pointer_motion(x, y, gx, gy); + Point ground = map_pointer_coords(x, y); + manipulator.pointer_motion(x, y, ground.x, ground.y); + measure.pointer_motion(x, y, ground.x, ground.y); } - - if(mode==MEASURE || mode==MANIPULATE) - move_tooltip(x, y); -} - -void Designer::project_3d() -{ - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10); - glMatrixMode(GL_MODELVIEW); } void Designer::apply_camera() { - glLoadIdentity(); if(mode==CATALOGUE) - glTranslatef(0, 0, -1); - else { - glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0); - glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1); - glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z); + GL::matrix_mode(GL::PROJECTION); + GL::load_identity(); + GL::frustum_centered(0.11046, 0.082843, 0.1, 10); + GL::matrix_mode(GL::MODELVIEW); + GL::load_identity(); + GL::translate(0, 0, -1); } + else + camera.apply(); } void Designer::render() { - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - glEnable(GL_DEPTH_TEST); + GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT); - project_3d(); - apply_camera(); if(mode==CATALOGUE) - cat_layout_3d->render(); + { + apply_camera(); + cat_layout_3d->get_scene().render(); + } else { - layout_3d->render(true); - manipulator->render(); + pipeline->render_all(); + GL::enable(GL_CULL_FACE); + { + GL::Bind bind_blend(GL::Blend::alpha()); + overlay->render(0); + } + + GL::Bind bind_depth(GL::DepthTest::lequal()); if(mode==MEASURE) - measure->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); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - glDisable(GL_DEPTH_TEST); - - 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::matrix_mode(GL::PROJECTION); + GL::load_identity(); + GL::ortho_bottomleft(window.get_width(), window.get_height()); + GL::matrix_mode(GL::MODELVIEW); + GL::load_identity(); + + GL::disable(GL::DEPTH_TEST); - if(mode==INPUT) - input->render(); + GL::Bind bind_blend(GL::Blend::alpha()); + root->render(); + GL::Texture::unbind(); +} + +void Designer::track_added(Track &trk) +{ + new_tracks.push_back(&trk); +} + +void Designer::track_removed(Track &trk) +{ + list::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk); + if(i!=new_tracks.end()) + new_tracks.erase(i); } 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=((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 = ((float(x)-window.get_width()/2)/window.get_height())*0.82843; + float yy = (float(y)/window.get_height()-0.5)*0.82843; + float size = 4.0/window.get_height()*0.82843; - project_3d(); apply_camera(); return l->pick_track(xx, yy, size); } +void Designer::update_track_icon(Track3D &track) +{ + overlay->clear(track); + + if(track.get_track().get_flex()) + overlay->add_graphic(track, "flex"); + + if(unsigned sid = track.get_track().get_sensor_id()) + { + overlay->add_graphic(track, "sensor"); + overlay->set_label(track, lexical_cast(sid)); + } + else if(unsigned tid = track.get_track().get_turnout_id()) + { + if(tid<0x800) + { + overlay->add_graphic(track, "turnout"); + overlay->set_label(track, lexical_cast(tid)); + } + } +} + +void Designer::selection_changed() +{ + const set &tracks = selection.get_tracks(); + if(tracks.empty()) + lbl_status->set_text(string()); + else + { + float len = 0; + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + len += (*i)->get_type().get_total_length(); + lbl_status->set_text(format("%.2fm of track selected\n", len)); + } +} + void Designer::manipulation_status(const string &status) { - tooltip=decode(status); + lbl_status->set_text(status); } void Designer::manipulation_done(bool) { - mode=SELECT; + mode = SELECT; + selection_changed(); } 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 "<(ss.str()); + 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; + string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff); + lbl_status->set_text(info); } void Designer::measure_done() { - mode=SELECT; + mode = SELECT; + selection_changed(); } -void Designer::move_tooltip(int x, int y) +void Designer::turnout_id_accept(const string &text) { - 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); + Track *track = selection.get_track(); + unsigned id = (text.empty() ? 0 : lexical_cast(text)); + track->set_turnout_id(id); + + update_track_icon(layout_3d->get_track(*track)); } -void Designer::save_accept() +void Designer::sensor_id_accept(const string &text) { - layout->save(input->get_text()); + const set &tracks = selection.get_tracks(); + unsigned id = (text.empty() ? 0 : lexical_cast(text)); + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + { + (*i)->set_sensor_id(id); - input_dismiss(); + update_track_icon(layout_3d->get_track(**i)); + } } -void Designer::turnout_id_accept() +void Designer::route_name_accept(const string &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(Color(0.5, 1, 1)); - else - t3d->set_color(Color(1, 1, 1)); - - input_dismiss(); + if(cur_route) + cur_route->set_name(text); } -void Designer::sensor_id_accept() +string Designer::tooltip(int x, int y) { - Track *track=selection->get_track(); - unsigned id=lexical_cast(input->get_text()); - track->set_sensor_id(id); - - Track3D *t3d=layout_3d->get_track(track); - if(id) - t3d->set_color(Color(1, 1, 0.5)); - else - t3d->set_color(Color(1, 1, 1)); + if(Track3D *t3d = pick_track(x, y)) + { + const Track &track = t3d->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()) + info += format(" (turnout %d)", track.get_turnout_id()); + else if(track.get_sensor_id()) + info += format(" (sensor %d)", track.get_sensor_id()); + + return info; + } - input_dismiss(); + return string(); } -void Designer::input_dismiss() +void Designer::show_route(const Route *route) { - delete input; - input=0; - mode=SELECT; + const set <racks = layout->get_tracks(); + for(set::iterator i=ltracks.begin(); i!=ltracks.end(); ++i) + { + Track3D &t3d = layout_3d->get_track(**i); + if(route && route->has_track(**i)) + { + t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0)); + if((*i)->get_type().is_turnout()) + { + unsigned tid = (*i)->get_turnout_id(); + int path = (tid ? route->get_turnout(tid) : -1); + if(path>=0) + t3d.get_path().set_path(path); + else + t3d.get_path().set_mask((*i)->get_type().get_paths()); + } + else + t3d.get_path().set_path(0); + } + else + t3d.get_path().set_mask(0); + } } - -Application::RegApp Designer::reg;