X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdesigner%2Fdesigner.cpp;h=2913aea1e43ec00dff88536869259ed7e523a6b9;hb=ae0600f7c3df5b6ef46992b1423888dd0e9a5026;hp=9edf816afcdef1ec7a557b42a9617c586777f3a4;hpb=52cbe8d99669f843f8f75c51128e2748584dd03a;p=r2c2.git diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index 9edf816..2913aea 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -1,11 +1,18 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + #include #include #include #include -#include #include #include #include +#include #include #include #include @@ -26,6 +33,7 @@ using namespace Msp; Designer::Designer(int argc, char **argv): screen_w(1280), screen_h(960), + base_mesh(0), input(0), mode(SELECT), cam_yaw(M_PI/2), @@ -63,15 +71,21 @@ Designer::Designer(int argc, char **argv): 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) + const list <racks=layout_3d->get_tracks(); + for(list::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)); + } + + if(!layout->get_base().empty()) + { + base_mesh=new GL::Mesh; + DataFile::load(*base_mesh, layout->get_base()); } } @@ -83,14 +97,30 @@ 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); - 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); + dpy=new Graphics::Display; + wnd=new Graphics::Window(*dpy, screen_w, screen_h); + glc=new Graphics::GLContext(*wnd); + + 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); glEnable(GL_DEPTH_TEST); @@ -113,7 +143,9 @@ int Designer::main() delete font_tex; delete input; - SDL_Quit(); + delete glc; + delete wnd; + delete dpy; return exit_code; } @@ -132,8 +164,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(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; @@ -145,36 +177,12 @@ void Designer::map_pointer_coords(int x, int y, float &gx, float &gy) void Designer::tick() { - SDL_Event event; - while(SDL_PollEvent(&event)) - { - float gx,gy; - switch(event.type) - { - 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; - } - } - - const Time::TimeStamp t=Time::now(); - float dt=(t-last_tick)/Time::sec; + 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; @@ -248,66 +256,57 @@ void Designer::tick() else tooltip=""; - tooltip_timeout=Time::TimeStamp(); + tooltip_timeout=Msp::Time::TimeStamp(); } render(); - SDL_GL_SwapBuffers(); -} - -Designer::~Designer() -{ - delete manipulator; - delete selection; - delete layout; - delete layout_3d; - delete cat_layout; - delete cat_layout_3d; - delete measure; + glc->swap_buffers(); } /*** private ***/ -void Designer::key_press(unsigned key, unsigned mod, wchar_t ch) +void Designer::key_press(unsigned code, unsigned mod, wchar_t ch) { + unsigned key=Msp::Input::key_from_sys(code); + if(mode==INPUT) { input->key_press(key, mod, ch); return; } - if(key==SDLK_RSHIFT || key==SDLK_LSHIFT) + if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R) shift=true; - if(key==SDLK_n) + if(key==Msp::Input::KEY_N) mode=CATALOGUE; - else if(key==SDLK_g) + else if(key==Msp::Input::KEY_G) { manipulator->start_move(); mode=MANIPULATE; } - else if(key==SDLK_r) + else if(key==Msp::Input::KEY_R) { manipulator->start_rotate(); mode=MANIPULATE; } - else if(key==SDLK_d) + else if(key==Msp::Input::KEY_D) { manipulator->duplicate(); manipulator->start_move(); mode=MANIPULATE; } - else if(key==SDLK_w) + else if(key==Msp::Input::KEY_W) { - input=new Input(*this, "Filename"); + 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; } - else if(key==SDLK_PLUS) + else if(key==Msp::Input::KEY_PLUS) selection->select_more(); - else if(key==SDLK_l && (mod&KMOD_SHIFT)) + else if(key==Msp::Input::KEY_L && (mod&1)) { const set &tracks=layout->get_tracks(); float len=0; @@ -315,19 +314,19 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t ch) len+=(*i)->get_type().get_total_length(); cout<<"Total length: "<select_linked(); - else if(key==SDLK_m) + else if(key==Msp::Input::KEY_M) { measure->start(); mode=MEASURE; } - else if(key==SDLK_z) + else if(key==Msp::Input::KEY_Z) { manipulator->start_elevate(); mode=MANIPULATE; } - else if(key==SDLK_ESCAPE) + else if(key==Msp::Input::KEY_ESC) { if(mode==MANIPULATE) manipulator->cancel(); @@ -336,7 +335,7 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t ch) else selection->clear(); } - else if(key==SDLK_x) + else if(key==Msp::Input::KEY_X) { set tracks=selection->get_tracks(); selection->clear(); @@ -346,7 +345,7 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t ch) delete *i; } } - else if(key==SDLK_f && (mod&KMOD_SHIFT)) + else if(key==Msp::Input::KEY_F && (mod&1)) { const set &tracks=selection->get_tracks(); const set <racks=layout->get_tracks(); @@ -358,88 +357,95 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t ch) 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) + else if(key==Msp::Input::KEY_F) manipulator->flatten(); - else if(key==SDLK_e && (mod&KMOD_SHIFT)) + else if(key==Msp::Input::KEY_E && (mod&1)) manipulator->even_slope(true); - else if(key==SDLK_e) + else if(key==Msp::Input::KEY_E) manipulator->even_slope(); - else if(key==SDLK_t) + else if(key==Msp::Input::KEY_T) { Track *track=selection->get_track(); if(selection->size()==1 && track->get_type().get_n_routes()>1) { ostringstream ss; ss<get_turnout_id(); - input=new Input(*this, "Turnout ID", ss.str()); + 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) + else if(key==Msp::Input::KEY_S) { Track *track=selection->get_track(); if(selection->size()==1 && track->get_type().get_n_routes()==1) { ostringstream ss; ss<get_sensor_id(); - input=new Input(*this, "Sensor ID", ss.str()); + 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; } } - else if(key==SDLK_RIGHT) + else if(key==Msp::Input::KEY_RIGHT) rotate=-1; - else if(key==SDLK_LEFT) + else if(key==Msp::Input::KEY_LEFT) rotate=1; - else if(key==SDLK_UP) + else if(key==Msp::Input::KEY_UP) move_y=1; - else if(key==SDLK_DOWN) + else if(key==Msp::Input::KEY_DOWN) move_y=-1; - else if(key==SDLK_INSERT) + else if(key==Msp::Input::KEY_INSERT) zoom=-1; - else if(key==SDLK_PAGEUP) + else if(key==Msp::Input::KEY_PGUP) zoom=1; - else if(key==SDLK_HOME) + else if(key==Msp::Input::KEY_HOME) pitch=1; - else if(key==SDLK_END) + else if(key==Msp::Input::KEY_END) pitch=-1; - else if(key==SDLK_DELETE) + else if(key==Msp::Input::KEY_DELETE) move_x=-1; - else if(key==SDLK_PAGEDOWN) + else if(key==Msp::Input::KEY_PGDN) move_x=1; } -void Designer::key_release(unsigned key, unsigned) +void Designer::key_release(unsigned code, unsigned) { + unsigned key=Msp::Input::key_from_sys(code); + if(mode==INPUT) return; - if(key==SDLK_RSHIFT || key==SDLK_LSHIFT) + if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R) shift=false; - else if(key==SDLK_RIGHT || key==SDLK_LEFT) + else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT) rotate=0; - else if(key==SDLK_UP || key==SDLK_DOWN) + else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN) move_y=0; - else if(key==SDLK_INSERT || key==SDLK_PAGEUP) + else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP) zoom=0; - else if(key==SDLK_HOME || key==SDLK_END) + else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END) pitch=0; - else if(key==SDLK_DELETE || key==SDLK_PAGEDOWN) + else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN) move_x=0; } -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) { + y=screen_h-y-1; + + float gx, gy; + map_pointer_coords(x, y, gx, gy); + if(mode==CATALOGUE) { if(btn==1) @@ -479,13 +485,18 @@ void Designer::button_press(int x, int y, float gx, float gy, unsigned btn) measure->button_press(x, y, gx, gy, btn); } -void Designer::pointer_motion(int x, int y, float gx, float gy) +void Designer::pointer_motion(int x, int y) { + y=screen_h-y-1; + + float gx, gy; + map_pointer_coords(x, y, gx, gy); + if(mode==SELECT || mode==CATALOGUE) { pointer_x=x; pointer_y=y; - tooltip_timeout=Time::now()+100*Time::msec; + tooltip_timeout=Msp::Time::now()+100*Msp::Time::msec; } if(mode!=INPUT) @@ -530,24 +541,17 @@ void Designer::render() cat_layout_3d->render(); else { + if(base_mesh) + { + GL::Texture::unbind(); + base_mesh->draw(); + } layout_3d->render(true); manipulator->render(); if(mode==MEASURE) 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); @@ -582,9 +586,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(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(); @@ -621,9 +625,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(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); } void Designer::save_accept() @@ -639,11 +643,11 @@ void Designer::turnout_id_accept() unsigned id=lexical_cast(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(); } @@ -654,11 +658,11 @@ void Designer::sensor_id_accept() unsigned id=lexical_cast(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(); }