From 2bb153b835826922b4ea29ff03b767ae4fbed119 Mon Sep 17 00:00:00 2001 From: Christophe Geuzaine <cgeuzaine@ulg.ac.be> Date: Sat, 13 Apr 2013 07:41:28 +0000 Subject: [PATCH] trying to fix gamepad crashes --- Common/CommandLine.cpp | 5 + Common/Context.cpp | 8 +- Common/Context.h | 9 +- Common/GamePad.cpp | 166 +++++++++++++++----------------- Common/Options.cpp | 15 +++ Common/Options.h | 1 + Fltk/FlGui.cpp | 16 ++-- Fltk/Navigator.cpp | 208 ++++++++++++++++++----------------------- Fltk/Navigator.h | 18 ++-- Fltk/gamepadWindow.cpp | 62 +++++------- Fltk/openglWindow.cpp | 74 ++++++--------- Fltk/openglWindow.h | 8 +- Fltk/optionWindow.cpp | 10 +- Fltk/optionWindow.h | 2 +- 14 files changed, 271 insertions(+), 331 deletions(-) diff --git a/Common/CommandLine.cpp b/Common/CommandLine.cpp index 56ad301f4e..669a667671 100644 --- a/Common/CommandLine.cpp +++ b/Common/CommandLine.cpp @@ -112,6 +112,7 @@ std::vector<std::pair<std::string, std::string> > GetUsage() s.push_back(mp("-camera", "Use camera mode view;")); s.push_back(mp("-stereo", "OpenGL quad-buffered stereo rendering (requires " "special graphic card)")); + s.push_back(mp("-gamepad", "Use gamepad controller if available")); #endif s.push_back(mp("Other options:", "")); s.push_back(mp("-", "Parse input files, then exit")); @@ -959,6 +960,10 @@ void GetOptions(int argc, char *argv[]) CTX::instance()->stereo = 1; i++; } + else if(!strcmp(argv[i] + 1, "gamepad")) { + opt_general_gamepad(0, GMSH_SET, 1.); + i++; + } else if(!strcmp(argv[i] + 1, "fontsize")) { i++; if(argv[i]) diff --git a/Common/Context.cpp b/Common/Context.cpp index 72523b278e..34083024b8 100644 --- a/Common/Context.cpp +++ b/Common/Context.cpp @@ -73,6 +73,7 @@ CTX::CTX() deltaFontSize = 0; recentFiles.resize(5); mesh.optimizeLloyd = 0; + gamepad = 0; // need to initialize these too, since the corresponding opt_XXX // routine uses the current value to set "mesh.changed" @@ -94,12 +95,17 @@ CTX::CTX() color.mesh.tangents = color.mesh.line = color.mesh.quadrangle = 0; for (int i = 0; i < 20; i++) color.mesh.carousel[i] = 0; - // added by Gauthier Becker (these are not initialized by default + // added by Gauthier Becker (these are not initialized by default // leading to valgrind error) Feel free to change the default values) mesh.switchElementTags=0; terminal=0; } +CTX::~CTX() +{ + if(gamepad) delete gamepad; +} + CTX *CTX::_instance = NULL; CTX *CTX::instance() diff --git a/Common/Context.h b/Common/Context.h index ee2f317935..10acb383d2 100644 --- a/Common/Context.h +++ b/Common/Context.h @@ -11,7 +11,8 @@ #include <string> #include "CGNSOptions.h" #include "meshPartitionOptions.h" -#include "GamePad.h" + +class GamePad; // The interface-independent context. @@ -70,7 +71,7 @@ class CTX { static CTX *_instance; public: CTX(); - ~CTX(){} + ~CTX(); static CTX *instance(); public: // files on the command line and various file names @@ -130,6 +131,8 @@ class CTX { double displayBorderFactor; // do or do not use the trackball for rotations int useTrackball, trackballHyperbolicSheet; + // use gamepad controller? + GamePad *gamepad; // point around which to rotate the scene double rotationCenter[3]; // rotate around the center of mass instead of rotationCenter[] @@ -258,8 +261,6 @@ class CTX { unsigned int tangents, normals; } mesh; } color; - // listen to a USB gamepad - GamePad gamepad; // is the machine big-endian? int bigEndian; // how RGBA values are packed and unpacked into/from an unsigned integer to be diff --git a/Common/GamePad.cpp b/Common/GamePad.cpp index 59953698d2..7e1ce448b5 100644 --- a/Common/GamePad.cpp +++ b/Common/GamePad.cpp @@ -60,12 +60,9 @@ #include <unistd.h> #endif -GamePad::GamePad() : active(false), frequency(.01), gamepad_fd(0) { - +GamePad::GamePad() : active(false), frequency(.01), gamepad_fd(0) +{ #if defined(WIN32) - - return; // FIXME the gamepad code crashes - for (int i = JOYSTICKID1 ; i < JOYSTICKID2 ; i++) { if(JOYERR_NOERROR == joyGetDevCaps(i, &caps, sizeof(JOYCAPS)) ) { /* @@ -92,30 +89,27 @@ GamePad::GamePad() : active(false), frequency(.01), gamepad_fd(0) { std::cout << " wVmin ............. " << caps.wVmin << std::endl; std::cout << " wVmax ............. " << caps.wVmax << std::endl; */ - if(( 1./frequency <(double)caps.wPeriodMin ) ||( 1./frequency >(double)caps.wPeriodMax )){ - frequency=.1/(double)caps.wPeriodMin; - } - gamepad_fd =i; - axes=std::min(GP_AXES,(int)caps.wNumAxes +2); - buttons=std::min(GP_BUTTONS,(int)caps.wNumButtons); - for (int i=0;i<buttons;i++) button[i]=false; - for (int i=0;i<axes;i++) axe[i]=0.; + if((1./frequency < (double)caps.wPeriodMin) || + (1./frequency > (double)caps.wPeriodMax)){ + frequency=.1/(double)caps.wPeriodMin; + } + gamepad_fd = i; + axes = std::min(GP_AXES,(int)caps.wNumAxes + 2); + buttons = std::min(GP_BUTTONS,(int)caps.wNumButtons); + for (int i = 0; i < buttons; i++) button[i] = false; + for (int i = 0; i < axes; i++) axe[i] = 0.; active = true; - - } } - for (int i=0;i<buttons;i++) button[i]=0; - for (int i=0;i<axes;i++) axe[i]=0; - joyGetPosEx( gamepad_fd,&infoex) ; + for(int i = 0; i < buttons; i++) button[i] = 0; + for(int i = 0; i < axes; i++) axe[i] = 0; + joyGetPosEx(gamepad_fd, &infoex); infoex.dwFlags = JOY_RETURNALL; - #elif defined(__APPLE__) // ?? #else // LINUX - gamepad_fd = open(GAMEPAD_DEV, O_RDONLY | O_NONBLOCK); if (gamepad_fd > 0) { ioctl(gamepad_fd, JSIOCGNAME(256), name); @@ -130,72 +124,69 @@ GamePad::GamePad() : active(false), frequency(.01), gamepad_fd(0) { } #endif - for (int i=0;i<std::min(9,(int)buttons);i++) button_map[i]=i; - for (int i=0;i<std::min(7,(int)axes);i++) axe_map[i]=i; + for (int i = 0; i < std::min(9, (int)buttons); i++) button_map[i] = i; + for (int i = 0; i < std::min(7, (int)axes); i++) axe_map[i] = i; axe_map[6]=1; // another recognized map "Thrustmaster Run'N' Drive Wireless PS3" // warning :: on Windows we dont have the human-friendly Model Name of the Gamepad - if ( strcmp(name,"Thrustmaster Run'N' Drive Wireless PS3" ) == 0){ - button_map[0]=1; - button_map[1]=0; - button_map[5]=6; - button_map[6]=5; + if(strcmp(name, "Thrustmaster Run'N' Drive Wireless PS3") == 0){ + button_map[0] = 1; + button_map[1] = 0; + button_map[5] = 6; + button_map[6] = 5; } } -GamePad::~GamePad() { +GamePad::~GamePad() +{ active = false; gamepad_fd = 0; - #if !defined(WIN32) && !defined(__APPLE__) - close(gamepad_fd); - #endif } -bool GamePad::toggle(const int _nbut) { +bool GamePad::toggle(const int _nbut) +{ bool res; - if( toggle_status[_nbut] ){ res = true; toggle_status[_nbut]=false;} - else { res=false; } + if(toggle_status[_nbut]){ res = true; toggle_status[_nbut] = false; } + else{ res = false; } return res; } -int GamePad::read_event() { - +int GamePad::read_event() +{ #if defined(WIN32) - infoex.dwFlags = JOY_RETURNALL; - joyGetPosEx( gamepad_fd,&infoex) ; - - axe[0]=(double)(( 1.*infoex.dwXpos-32767.)/32767.); - axe[1]=(double)(( 1.*infoex.dwYpos-32767.)/32767.); - axe[2]=(double)(( 1.*infoex.dwRpos-32767.)/32767.); - axe[3]=(double)(( 1.*infoex.dwZpos-32767.)/32767.); - if (infoex.dwPOV < 38000. ) { - double alpha = 3.14159/18000.* infoex.dwPOV; - axe[4]=1.001*sin(alpha); axe[5]=-1.001*cos(alpha); - } - else{ - axe[4]= axe[5]=0.; - } - for (int i=0;i<6;i++) if ( fabs( axe[i]) < .01) axe[i]=0.; - - bool event=false; - int event_num; - bool event_value; - for (int i=0;i<buttons;i++){ - int bin=pow(2,i); - if( button[i] != (bool)(infoex.dwButtons & bin) ) { - event=true; - event_num=i; - event_value=(bool)(infoex.dwButtons & bin); - } - } - if(event){ - if( button[event_num]==0 && event_value ) { toggle_status[event_num]=true; } - button[event_num]= event_value; + joyGetPosEx(gamepad_fd, &infoex) ; + axe[0] = (double)((1. * infoex.dwXpos - 32767.) / 32767.); + axe[1] = (double)((1. * infoex.dwYpos - 32767.) / 32767.); + axe[2] = (double)((1. * infoex.dwRpos - 32767.) / 32767.); + axe[3] = (double)((1. * infoex.dwZpos - 32767.) / 32767.); + if(infoex.dwPOV < 38000.) { + double alpha = 3.14159 / 18000. * infoex.dwPOV; + axe[4] = 1.001 * sin(alpha); + axe[5] = -1.001 * cos(alpha); + } + else{ + axe[4] = axe[5] = 0.; + } + for (int i = 0; i < 6; i++) if(fabs(axe[i]) < .01) axe[i] = 0.; + bool event = false; + int event_num; + bool event_value; + for (int i = 0; i < buttons; i++){ + int bin = pow(2, i); + if(button[i] != (bool)(infoex.dwButtons & bin)) { + event = true; + event_num = i; + event_value = (bool)(infoex.dwButtons & bin); } + } + if(event){ + if(button[event_num] == 0 && event_value){ toggle_status[event_num] = true; } + button[event_num] = event_value; + } /* std::cout<< "--------------------" << std::endl; std::cout<< infoex.dwXpos << std::endl; @@ -213,53 +204,46 @@ int GamePad::read_event() { // ?? #else // LINUX - - int result = read(gamepad_fd, &event, sizeof(event)) ; - if (result > 0) { - - switch (event.type) { + int result = read(gamepad_fd, &event, sizeof(event)); + if (result > 0){ + switch (event.type){ case JS_EVENT_INIT: break; - case JS_EVENT_INIT | JS_EVENT_AXIS: break; - case JS_EVENT_INIT | JS_EVENT_BUTTON: break; - case JS_EVENT_AXIS: - axe[(int)event.number]=(double)event.value/32767. ; + axe[(int)event.number] = (double)event.value / 32767.; break; - case JS_EVENT_BUTTON: - if(button[(int)event.number]==0. && (bool)event.value) { toggle_status[(int)event.number]=true; } - button[(int)event.number]=(bool)event.value ; + if(button[(int)event.number] == 0. && (bool)event.value) { + toggle_status[(int)event.number] = true; + } + button[(int)event.number] = (bool)event.value ; break; - default: break; } } - #endif - return 1; } -void GamePad::affiche() { - for (int i=0;i<6;i++) std::cout<<("_________"); - std::cout<<std::endl; std::cout<<" axis "; - for (int i=0;i<6;i++) std::cout<<" | "<<i; +void GamePad::affiche() +{ + for (int i = 0; i < 6; i++) std::cout<<("_________"); + std::cout<<std::endl; std::cout<<" axis "; + for (int i = 0; i < 6; i++) std::cout<<" | "<<i; std::cout<<std::endl; std::cout<<" "; - for (int i=0;i<6;i++) std::cout<<" | "<< axe[i] ; + for (int i = 0; i < 6; i++) std::cout<<" | "<< axe[i] ; std::cout<<std::endl; - for (int i=0;i<10;i++) std::cout<< ("_____"); + for (int i = 0; i < 10; i++) std::cout<< ("_____"); std::cout<<std::endl; std::cout<<" b."; - for (int i=0;i<10;i++) std::cout<<" | "<<i; + for (int i = 0; i < 10; i++) std::cout<<" | "<<i; std::cout<<std::endl; std::cout<<" "; - for (int i=0;i<10;i++) std::cout<<" | "<<button[i]; + for (int i = 0; i < 10; i++) std::cout<<" | "<<button[i]; std::cout<<std::endl; - for (int i=0;i<10;i++) std::cout<< ("_____"); + for (int i = 0; i < 10; i++) std::cout<< ("_____"); std::cout<<std::endl; - } diff --git a/Common/Options.cpp b/Common/Options.cpp index fa1671862f..07ddac3c7f 100644 --- a/Common/Options.cpp +++ b/Common/Options.cpp @@ -15,6 +15,7 @@ #include "Options.h" #include "Colors.h" #include "CommandLine.h" +#include "GamePad.h" #include "DefaultOptions.h" #if defined(HAVE_MESH) @@ -3106,6 +3107,20 @@ double opt_general_trackball_hyperbolic_sheet(OPT_ARGS_NUM) return CTX::instance()->trackballHyperbolicSheet; } +double opt_general_gamepad(OPT_ARGS_NUM) +{ + if(action & GMSH_SET){ + if(val){ + if(!CTX::instance()->gamepad) + CTX::instance()->gamepad = new GamePad(); + } + else{ + if(CTX::instance()->gamepad) delete CTX::instance()->gamepad; + } + } + return CTX::instance()->gamepad ? 1 : 0; +} + double opt_general_rotation_center_cg(OPT_ARGS_NUM) { if(action & GMSH_SET) diff --git a/Common/Options.h b/Common/Options.h index 834016f612..fbfb8942de 100644 --- a/Common/Options.h +++ b/Common/Options.h @@ -220,6 +220,7 @@ double opt_general_arrow_stem_length(OPT_ARGS_NUM); double opt_general_arrow_stem_radius(OPT_ARGS_NUM); double opt_general_trackball(OPT_ARGS_NUM); double opt_general_trackball_hyperbolic_sheet(OPT_ARGS_NUM); +double opt_general_gamepad(OPT_ARGS_NUM); double opt_general_rotation_center_cg(OPT_ARGS_NUM); double opt_general_zoom_factor(OPT_ARGS_NUM); double opt_general_expert_mode(OPT_ARGS_NUM); diff --git a/Fltk/FlGui.cpp b/Fltk/FlGui.cpp index c970764ff4..e311edf491 100644 --- a/Fltk/FlGui.cpp +++ b/Fltk/FlGui.cpp @@ -202,14 +202,12 @@ static void gmsh_search(Fl_Color col) #undef bl #undef el -///// handler : read event from GAMEPAD ///////// -static void gp_handler(void *data) +static void gp_handler(void *data) { - GamePad* gp_ptr = &(CTX::instance()->gamepad ); + GamePad *gp_ptr = CTX::instance()->gamepad; gp_ptr->read_event(); - Fl::add_timeout(gp_ptr->frequency, gp_handler,data); + Fl::add_timeout(gp_ptr->frequency, gp_handler, data); } -///// FlGui::FlGui(int argc, char **argv) { @@ -228,13 +226,11 @@ FlGui::FlGui(int argc, char **argv) Fl::set_boxtype(GMSH_SIMPLE_RIGHT_BOX, simple_right_box_draw, 0, 0, 1, 0); Fl::set_boxtype(GMSH_SIMPLE_TOP_BOX, simple_top_box_draw, 0, 1, 0, 1); - /// // add external reader for gamepad events - if (CTX::instance()->gamepad.active) { - CTX::instance()->camera = 1; - Fl::add_timeout(CTX::instance()->gamepad.frequency, gp_handler, (void*)0); + if(CTX::instance()->gamepad && CTX::instance()->gamepad->active){ + CTX::instance()->camera = 1; + Fl::add_timeout(CTX::instance()->gamepad->frequency, gp_handler, (void*)0); } - /// // add global shortcuts Fl::add_handler(globalShortcut); diff --git a/Fltk/Navigator.cpp b/Fltk/Navigator.cpp index 25772b48e2..f101570182 100644 --- a/Fltk/Navigator.cpp +++ b/Fltk/Navigator.cpp @@ -5,8 +5,6 @@ // // Contributed by Gilles Marckmann <gilles.marckmann@ec-nantes.fr> - - #include <string> #include <iostream> #include <stdio.h> @@ -17,43 +15,45 @@ #include "drawContext.h" #include "gl2ps.h" -Navigator::Navigator(double _freq, drawContext* _ctx): - ax0(0.),ax1(0.),ax0_cur(0.),ax1_cur(0.),speed(0.),awake(true) - ,ctx(_ctx),frequency(_freq) +Navigator::Navigator(double _freq, drawContext* _ctx): + ax0(0.), ax1(0.), ax0_cur(0.), ax1_cur(0.), speed(0.), awake(true), + ctx(_ctx), frequency(_freq) { ctx->camera.init(); - pad = &(CTX::instance()->gamepad); - along=AXE_Z; + pad = CTX::instance()->gamepad; + along = AXE_Z; mode = PEDESTRIAN; - reference_angle = 3.14*frequency/5.; - reference_speed = 1.5*frequency*(ctx->camera.Lc)/25.; + reference_angle = 3.14 * frequency / 5.; + reference_speed = 1.5 * frequency * (ctx->camera.Lc) / 25.; } -Navigator::~Navigator() {} ; +Navigator::~Navigator(){}; -void Navigator::setDrawContext( drawContext* _ctx) { +void Navigator::setDrawContext( drawContext* _ctx) +{ ctx = _ctx; ctx->camera.init(); } -void Navigator::setFrequency(double _freq) { +void Navigator::setFrequency(double _freq) +{ frequency = _freq; reference_angle = 3.14*frequency/5.; reference_speed = 1.5*frequency*(ctx->camera.Lc)/100.; } -void Navigator::setResponseFrequency(double _freq) { +void Navigator::setResponseFrequency(double _freq) +{ reference_angle = 3.14*frequency/5.*(_freq/.01); reference_speed = 1.5*frequency*(ctx->camera.Lc)/100.*(_freq/.01); } - void Navigator::drawIcons() { double l = CTX::instance()->smallAxesSize ; double dx = CTX::instance()->smallAxesPos[0]+.5*l ; - double dy = 1.25*l; - + double dy = 1.25*l; + ctx->fix2dCoordinates(&dx, &dy); glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -62,13 +62,12 @@ void Navigator::drawIcons() CTX::instance()->print.epsLineWidthFactor)); glColor4ubv((GLubyte *) & CTX::instance()->color.smallAxes); - + double scale=l/100.; glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable (GL_LINE_SMOOTH); - switch(mode){ case PEDESTRIAN : glBegin(GL_LINES); @@ -596,71 +595,61 @@ void Navigator::drawIcons() glVertex2d(dx+scale*3.55498581E+01,dy+scale* 5.91182632E+01); glEnd(); break; - default: break; + default: break; } - glDisable (GL_BLEND); glDisable (GL_LINE_SMOOTH); glPopMatrix(); - } - - - - - -void Navigator::move(){ - if(pad->toggle( pad->button_map[3]) ) - { - switch (mode) - { - case PEDESTRIAN : mode = DIVER ; break; - case DIVER : mode = PEDESTRIAN ; break; - default: break; - } +void Navigator::move() +{ + if(pad->toggle( pad->button_map[3])){ + switch (mode){ + case PEDESTRIAN : mode = DIVER ; break; + case DIVER : mode = PEDESTRIAN ; break; + default: break; } + } // reset 1:1 - if(pad->toggle( pad->button_map[8]) ) - { - ctx->camera.lookAtCg(); - reference_speed= 1.5*frequency*(ctx->camera.Lc)/100.; - } - // change the plane - if(pad->toggle( pad->button_map[0]) ) ctx->camera.lookAtCg(); - if(pad->toggle( pad->button_map[1]) ) - { - switch(along){ - case AXE_X : along=AXE_Y; ctx->camera.alongY(); break; - case AXE_Y : along=AXE_Z; ctx->camera.alongZ(); break; - case AXE_Z : along=AXE_X; ctx->camera.alongX(); break; - default: break; - } + if(pad->toggle( pad->button_map[8]) ){ + ctx->camera.lookAtCg(); + reference_speed= 1.5*frequency*(ctx->camera.Lc)/100.; + } + // change the plane + if(pad->toggle( pad->button_map[0]) ) ctx->camera.lookAtCg(); + if(pad->toggle( pad->button_map[1]) ){ + switch(along){ + case AXE_X : along=AXE_Y; ctx->camera.alongY(); break; + case AXE_Y : along=AXE_Z; ctx->camera.alongZ(); break; + case AXE_Z : along=AXE_X; ctx->camera.alongX(); break; + default: break; } - // reset vertical axe or invers it - if(pad->toggle( pad->button_map[2] ) ) { + } + // reset vertical axe or invers it + if(pad->toggle( pad->button_map[2] ) ){ switch(along){ case AXE_X : ctx->camera.upZ(); break; case AXE_Y : ctx->camera.upX(); break; case AXE_Z : ctx->camera.upY(); break; - default: break; - } + default: break; + } } // head movement is damped to avoid nausea - if(pad->button[ pad->button_map[4] ] ) { + if(pad->button[ pad->button_map[4] ] ){ ax0 = int(pad->axe[ pad->axe_map[0] ]*10)/10.; ax1 =int( pad->axe[ pad->axe_map[1] ]*10)/10.; - if(ax1 >0.) ax0=-ax0; + if(ax1 >0.) ax0=-ax0; } else { ax0 =0.; ax1 =0. ; } if( (ax0-ax0_cur)>0. ) { ax0_cur=ax0_cur + std::min((ax0-ax0_cur),0.005); } - else{ ax0_cur=ax0_cur + std::max((ax0-ax0_cur), -0.005); } + else{ ax0_cur=ax0_cur + std::max((ax0-ax0_cur), -0.005); } + + if( (ax1-ax1_cur) >0.) { ax1_cur=ax1_cur + std::min((ax1-ax1_cur),0.005); } + else{ ax1_cur=ax1_cur + std::max((ax1-ax1_cur), -0.005); } - if( (ax1-ax1_cur) >0.) { ax1_cur=ax1_cur + std::min((ax1-ax1_cur),0.005); } - else{ ax1_cur=ax1_cur + std::max((ax1-ax1_cur), -0.005); } - azimut = -(ax0_cur) *(2.*ctx->camera.aperture*0.01745329 ) ; elevation = (ax1_cur) *(2.*ctx->camera.aperture*0.01745329 ); @@ -670,75 +659,64 @@ void Navigator::move(){ case PEDESTRIAN : //------------------------------------------ // accelaration - if(!pad->button[ pad->button_map[4] ] ) - { - if(pad->axe[ pad->axe_map[1] ]!=0.) - { - acc=-pad->axe[ pad->axe_map[1] ] * ctx->camera.Lc / 500. * frequency ; - if (acc>0.) - { - speed = reference_speed +acc; - } - else - { - speed = reference_speed + 2.*acc; - } - speed = std::max(speed,(frequency * ctx->camera.Lc / 1000.)); - reference_speed = std::min(speed,(frequency*(ctx->camera.Lc))); - /* - std::cout<<"acc: "<< acc << std::endl; - std::cout<<"lc: "<< ctx->camera.Lc << std::endl; - std::cout<<"vitesse: "<< reference_speed << std::endl; - */ - } + if(!pad->button[ pad->button_map[4] ] ){ + if(pad->axe[ pad->axe_map[1] ]!=0.){ + acc=-pad->axe[ pad->axe_map[1] ] * ctx->camera.Lc / 500. * frequency ; + if (acc>0.){ + speed = reference_speed +acc; + } + else{ + speed = reference_speed + 2.*acc; + } + speed = std::max(speed,(frequency * ctx->camera.Lc / 1000.)); + reference_speed = std::min(speed,(frequency*(ctx->camera.Lc))); + /* + std::cout<<"acc: "<< acc << std::endl; + std::cout<<"lc: "<< ctx->camera.Lc << std::endl; + std::cout<<"vitesse: "<< reference_speed << std::endl; + */ } + } lift = -.25 *pad->axe[ pad->axe_map[5] ] *reference_speed ; lateral= 0.25* pad->axe[ pad->axe_map[4] ] *reference_speed ; angular_lat= -1.0 * (pad->axe[ pad->axe_map[2] ]) *reference_angle; // walk else run - if(pad->button[ pad->button_map[6]] ) - { - speed = -1.0 * pad->axe[ pad->axe_map[3] ] *reference_speed ; - } - else - { - speed= -4.0 * (pad->axe[ pad->axe_map[3] ]) *reference_speed ; - } + if(pad->button[ pad->button_map[6]] ){ + speed = -1.0 * pad->axe[ pad->axe_map[3] ] *reference_speed ; + } + else{ + speed= -4.0 * (pad->axe[ pad->axe_map[3] ]) *reference_speed ; + } ctx->camera.move_and_look(speed,lateral,lift,0.,0.,angular_lat,azimut,elevation); //------------------------------------- - - break; // end of mode PESDESTRIAN - - + break; // end of mode PESDESTRIAN case DIVER : //------------------------------------- // accelaration - // pad->affiche(); + // pad->affiche(); if(!pad->button[ pad->button_map[4] ]) { angular_fr = 1.0 * (pad->axe[ pad->axe_map[0] ]) *reference_angle; - if(pad->axe[ pad->axe_map[1] ]!=0. ) - { - acc=-pad->axe[ pad->axe_map[1] ] * ctx->camera.Lc / 1000. * frequency ; - if (acc>0.) { - speed = reference_speed +acc; - } - else { - speed = reference_speed + 2.*acc; - } - speed = std::max(speed,(frequency * ctx->camera.Lc / 1000.)); - reference_speed = std::min(speed,(frequency*(ctx->camera.Lc))); - } - + if(pad->axe[ pad->axe_map[1] ]!=0.){ + acc=-pad->axe[ pad->axe_map[1] ] * ctx->camera.Lc / 1000. * frequency ; + if (acc>0.) { + speed = reference_speed +acc; + } + else { + speed = reference_speed + 2.*acc; + } + speed = std::max(speed,(frequency * ctx->camera.Lc / 1000.)); + reference_speed = std::min(speed,(frequency*(ctx->camera.Lc))); + } if(pad->button[ pad->button_map[6] ] ) { - speed = reference_speed ; - } - else { + speed = reference_speed ; + } + else { speed = 0.; - } + } } - + lift = -.25 *pad->axe[ pad->axe_map[5] ] *reference_speed ; lateral= 0.25* pad->axe[ pad->axe_map[4] ] *reference_speed ; angular_lat= -1.0 * (pad->axe[ pad->axe_map[2] ]) *reference_angle; @@ -746,20 +724,16 @@ void Navigator::move(){ ctx->camera.move_and_look(speed,lateral,lift,angular_fr,angular_up,angular_lat,azimut,elevation); //------------------------------------- - - break; // end of mode DIVER case PLANE : break; // end of mode PLANE - + case CAR : break; // end of mode CAR - default: break; } // end of switch(mode) } - diff --git a/Fltk/Navigator.h b/Fltk/Navigator.h index 756ececa8d..7fdab5e1a0 100644 --- a/Fltk/Navigator.h +++ b/Fltk/Navigator.h @@ -4,7 +4,7 @@ // bugs and problems to the public mailing list <gmsh@geuz.org>. // // Contributed by Gilles Marckmann <gilles.marckmann@ec-nantes.fr> -// +// #ifndef _NAVIGATOR_H_ #define _NAVIGATOR_H_ @@ -13,8 +13,6 @@ #include "Camera.h" #include "GamePad.h" - - typedef enum { AXE_X, AXE_Y, AXE_Z } AXE ; typedef enum { PLANE, PEDESTRIAN, DIVER, CAR } NAV_MODE ; @@ -44,16 +42,14 @@ class Navigator { double lift; int ncount; public: - Navigator( double _freq, drawContext* _ctx); + Navigator(double _freq, drawContext* _ctx); Navigator(); ~Navigator(); - void setFrequency(double _freq) ; - void setResponseFrequency(double _freq) ; - void setDrawContext( drawContext* _ctx) ; + void setFrequency(double _freq); + void setResponseFrequency(double _freq); + void setDrawContext( drawContext* _ctx); void move(); void drawIcons(); -}; - - +}; -#endif // _NAVIGATOR_H_ +#endif diff --git a/Fltk/gamepadWindow.cpp b/Fltk/gamepadWindow.cpp index 24d0b45871..8dee7f9d61 100644 --- a/Fltk/gamepadWindow.cpp +++ b/Fltk/gamepadWindow.cpp @@ -11,56 +11,43 @@ typedef unsigned long intptr_t; #endif #include <string.h> #include <FL/Fl.H> -#include <FL/Fl_Tabs.H> -#include <FL/Fl_Scroll.H> -#include <FL/Fl_Color_Chooser.H> #include <FL/fl_ask.H> #include "GmshDefines.h" #include "GmshMessage.h" #include "FlGui.h" +#include "paletteWindow.h" #include "optionWindow.h" #include "GamePad.h" #include "gamepadWindow.h" -#include "graphicWindow.h" -#include "openglWindow.h" -#include "paletteWindow.h" -#include "extraDialogs.h" -#include "drawContext.h" -#include "Options.h" -#include "GModel.h" -#include "MElement.h" -#include "PView.h" -#include "PViewData.h" -#include "PViewOptions.h" -#include "OS.h" #include "Context.h" -#include "StringUtils.h" -///// handler (listen to gamepad or other names pipes) ///////// +// handler (listen to gamepad or other names pipes static void gamepadWindow_handler(void *data) { - if (CTX::instance()->gamepad.active) { + if(CTX::instance()->gamepad && CTX::instance()->gamepad->active){ gamepadWindow* gmpd_win = (gamepadWindow*)data; - GamePad* pad = &(CTX::instance()->gamepad); + GamePad* pad = CTX::instance()->gamepad; for (int i=0; i<std::min(13,GP_BUTTONS);i++) gmpd_win->gamepad.butt[i]->value(pad->button[i]); for (int i=0; i<std::min(9,GP_AXES);i++) gmpd_win->gamepad.axe[i]->value( pad->axe[i] ); - Fl::add_timeout(gmpd_win->frequency, gamepadWindow_handler,data); + Fl::add_timeout(gmpd_win->frequency, gamepadWindow_handler, data); gmpd_win->gamepad.mapping[16]->value(pad->axe_map[1]); } } -/// static void gamepad_update_cb(Fl_Widget *w) { gamepadWindow* gmpd_win = FlGui::instance()->options->gmpdoption; - GamePad* pad = &(CTX::instance()->gamepad); + GamePad* pad = CTX::instance()->gamepad; for (int i=0; i<std::min(8,GP_BUTTONS) ;i++) pad->button_map[i] = gmpd_win->gamepad.mapping[i]->value(); for (int i=0; i<std::min(7,GP_AXES);i++) pad->axe_map[i] = gmpd_win->gamepad.mapping[10+i]->value(); } -gamepadWindow::gamepadWindow() : -frequency(CTX::instance()->gamepad.frequency) +gamepadWindow::gamepadWindow() { + if(!CTX::instance()->gamepad) return; + + frequency = CTX::instance()->gamepad->frequency; + // FL_NORMAL_SIZE -= deltaFontSize; int width = 34 * FL_NORMAL_SIZE + WB; @@ -68,7 +55,6 @@ frequency(CTX::instance()->gamepad.frequency) int L = FL_NORMAL_SIZE; // int BW = width - 4 * WB; - win = new paletteWindow (width, height, CTX::instance()->nonModalWindows ? true : false); win->box(GMSH_WINDOW_BOX); @@ -91,10 +77,10 @@ frequency(CTX::instance()->gamepad.frequency) gamepad.butt[11] = new Fl_Check_Button(L +11*BB*.35 , L+BH , BH, WB, "11" ); gamepad.butt[12] = new Fl_Check_Button(L +12*BB*.35 , L+BH , BH, WB, "12" ); for (int i=0; i<13;i++) gamepad.butt[i]->deactivate(); - + Fl_Box *bta = new Fl_Box(FL_NO_BOX, L , L+1.7*BH , IW, BH, "Gamepad axes:"); bta->align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT); - + gamepad.axe[0] = new Fl_Check_Button(L , L+3.*BH , BH, WB, "0" ); gamepad.axe[1] = new Fl_Check_Button(L +1*BB*.4 , L+3.*BH , BH, WB, "1" ); gamepad.axe[2] = new Fl_Check_Button(L +2*BB*.4 , L+3.*BH , BH, WB, "2" ); @@ -105,7 +91,7 @@ frequency(CTX::instance()->gamepad.frequency) gamepad.axe[7] = new Fl_Check_Button(L +7*BB*.4 , L+3.*BH , BH, WB, "7" ); gamepad.axe[8] = new Fl_Check_Button(L +8*BB*.4 , L+3.*BH , BH, WB, "8" ); for (int i=0; i<9;i++) gamepad.axe[i]->deactivate(); - + Fl_Box *btt1 = new Fl_Box(FL_NO_BOX, L , L+4.*BH , IW, BH, "Preferences:"); btt1->align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT); Fl_Box *btt2 = new Fl_Box(FL_NO_BOX, L , L+5.*BH , IW, BH, "Action Axes:"); @@ -113,7 +99,7 @@ frequency(CTX::instance()->gamepad.frequency) Fl_Box *btt3 = new Fl_Box(FL_NO_BOX, L +width/2, L+5.*BH , IW, BH, "Action buttons:"); btt3->align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT); - GamePad* pad = &(CTX::instance()->gamepad); + GamePad* pad = CTX::instance()->gamepad; gamepad.mapping[0] = new Fl_Value_Input( L+width/2, L+6.*BH , IW/5,BH, "1:1"); gamepad.mapping[1] = new Fl_Value_Input( L+width/2, L+7.*BH , IW/5,BH, "permute axes"); @@ -127,9 +113,9 @@ frequency(CTX::instance()->gamepad.frequency) for (int i=0; i<9;i++) { // gamepad.mapping[i]->deactivate(); - gamepad.mapping[i]->align(FL_ALIGN_RIGHT); + gamepad.mapping[i]->align(FL_ALIGN_RIGHT); gamepad.mapping[i]->callback(gamepad_update_cb); - gamepad.mapping[i]->value( pad->button_map[i] ); + gamepad.mapping[i]->value(pad->button_map[i]); } gamepad.mapping[10] = new Fl_Value_Input( L, L+6.*BH , IW/5, BH, "head right/left (with button)"); @@ -140,21 +126,17 @@ frequency(CTX::instance()->gamepad.frequency) gamepad.mapping[15] = new Fl_Value_Input( L, L+11.*BH, IW/5, BH, "move up/down"); gamepad.mapping[16] = new Fl_Value_Input( L, L+12.*BH , IW/5, BH, "speed up/slow down"); - for (int i=0; i<7;i++) { + for (int i=0; i<7;i++) { gamepad.mapping[10+i]->align(FL_ALIGN_RIGHT); gamepad.mapping[10+i]->callback(gamepad_update_cb); - gamepad.mapping[10+i]->value( pad->axe_map[i] ); + gamepad.mapping[10+i]->value(pad->axe_map[i]); } - - //// add external reader for gamepad events - if (CTX::instance()->gamepad.active) { + + // add external reader for gamepad events + if (CTX::instance()->gamepad && CTX::instance()->gamepad->active) { Fl::add_timeout(frequency, gamepadWindow_handler, (void*)this); } - //// win->position(CTX::instance()->optPosition[0], CTX::instance()->optPosition[1]); win->end(); - } - - diff --git a/Fltk/openglWindow.cpp b/Fltk/openglWindow.cpp index 1948276140..cb2af287ca 100644 --- a/Fltk/openglWindow.cpp +++ b/Fltk/openglWindow.cpp @@ -5,6 +5,7 @@ #include <stdio.h> #include <string.h> +#include <FL/Fl_Tooltip.H> #include "openglWindow.h" #include "graphicWindow.h" #include "manipWindow.h" @@ -18,19 +19,18 @@ #include "FlGui.h" #include "drawContext.h" #include "Context.h" -#include <FL/Fl_Tooltip.H> - #include "Trackball.h" #include "GamePad.h" -///// Navigator handler (listen to gamepad or other names pipes) ///////// -static void navigator_handler(void *data) +// Navigator handler (listen to gamepad or other names pipes) +static void navigator_handler(void *data) { - openglWindow* gl_win = (openglWindow*)data; - if (CTX::instance()->gamepad.active) gl_win->move_with_gamepad(); - Fl::add_timeout(gl_win->frequency, navigator_handler,data); + if(CTX::instance()->gamepad && CTX::instance()->gamepad->active){ + openglWindow* gl_win = (openglWindow*)data; + gl_win->moveWithGamepad(); + Fl::add_timeout(CTX::instance()->gamepad->frequency, navigator_handler, data); + } } -/// static void lassoZoom(drawContext *ctx, mousePosition &click1, mousePosition &click2) { @@ -52,13 +52,8 @@ static void lassoZoom(drawContext *ctx, mousePosition &click1, mousePosition &cl } openglWindow::openglWindow(int x, int y, int w, int h) - : Fl_Gl_Window(x, y, w, h, "gl"), _lock(false) - , _drawn(false) - , _selection(ENT_NONE) , _trySelection(0) - , ntime(0) - , frequency(CTX::instance()->gamepad.frequency) - - + : Fl_Gl_Window(x, y, w, h, "gl"), _lock(false), _drawn(false), + _selection(ENT_NONE), _trySelection(0), Nautilus(0) { _ctx = new drawContext(); for(int i = 0; i < 3; i++) _point[i] = 0.; @@ -67,21 +62,17 @@ openglWindow::openglWindow(int x, int y, int w, int h) addPointMode = lassoMode = selectionMode = false; endSelection = undoSelection = invertSelection = quitSelection = 0; - - - //// add external reader for gamepad events - if (CTX::instance()->gamepad.active) { + + if(CTX::instance()->gamepad && CTX::instance()->gamepad->active){ Nautilus = new Navigator(frequency,_ctx); - Fl::add_timeout(frequency, navigator_handler, (void*)this); + Fl::add_timeout(CTX::instance()->gamepad->frequency, navigator_handler, (void*)this); } - //// - } openglWindow::~openglWindow() { delete _ctx; - delete Nautilus; + if(Nautilus) delete Nautilus; } void openglWindow::_drawScreenMessage() @@ -146,11 +137,10 @@ void openglWindow::draw() // that we don't fire draw() while we are already drawing, e.g. due to an // impromptu Fl::check(). The same lock is also used in _select to guarantee // that we don't mix GL_RENDER and GL_SELECT rendering passes. - _drawn=true; + _drawn = true; if(_lock) return; _lock = true; - Msg::Debug("openglWindow::draw()"); if(!context_valid()) _ctx->invalidateQuadricsAndDisplayLists(); @@ -246,10 +236,10 @@ void openglWindow::draw() cam->giveViewportDimension(_ctx->viewport[2],_ctx->viewport[3]); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - + glFrustum(cam->glFleft, cam->glFright, cam->glFbottom, cam->glFtop, cam->glFnear , cam->glFfar*cam->Lc ); - + glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDrawBuffer(GL_BACK); @@ -260,9 +250,9 @@ void openglWindow::draw() cam->up.x,cam->up.y,cam->up.z); _ctx->draw3d(); _ctx->draw2d(); - /// - if (CTX::instance()->gamepad.active){ Nautilus->drawIcons();} - /// + if(CTX::instance()->gamepad && CTX::instance()->gamepad->active && Nautilus){ + Nautilus->drawIcons(); + } _drawScreenMessage(); _drawBorder(); } @@ -745,21 +735,13 @@ void openglWindow::drawTooltip(const std::string &text) if(!enabled) Fl_Tooltip::disable(); } - - - -//// -void openglWindow::move_with_gamepad() { - if (CTX::instance()->gamepad.active) { - - if( !(_ctx->camera.on)) _ctx->camera.init(); - - if (_drawn && (_lastHandled == this || _lastHandled == 0 ) ) { - Nautilus->move(); - this->flush(); - } +void openglWindow::moveWithGamepad() +{ + if(CTX::instance()->gamepad && CTX::instance()->gamepad->active && Nautilus){ + if(!(_ctx->camera.on)) _ctx->camera.init(); + if(_drawn && (_lastHandled == this || _lastHandled == 0)){ + Nautilus->move(); + this->flush(); + } } - } - -///// diff --git a/Fltk/openglWindow.h b/Fltk/openglWindow.h index 5cd39efc55..22cd79b2b2 100644 --- a/Fltk/openglWindow.h +++ b/Fltk/openglWindow.h @@ -41,7 +41,6 @@ class openglWindow : public Fl_Gl_Window { int handle(int); public: time_t rawtime, prev_rawtime; - int ntime; double response_frequency; bool addPointMode, lassoMode, selectionMode; int endSelection, undoSelection, invertSelection, quitSelection; @@ -58,10 +57,9 @@ class openglWindow : public Fl_Gl_Window { void drawTooltip(const std::string &text); /// double frequency; - void move_with_gamepad() ; - Navigator* Nautilus; - /// + void moveWithGamepad(); + Navigator *Nautilus; }; #endif - + diff --git a/Fltk/optionWindow.cpp b/Fltk/optionWindow.cpp index acf0109ef4..cca140443d 100644 --- a/Fltk/optionWindow.cpp +++ b/Fltk/optionWindow.cpp @@ -168,9 +168,6 @@ void general_gmpdcf_cb(Fl_Widget *w, void *data) FlGui::instance()->options->gmpdoption->win->show(); } - - - void options_cb(Fl_Widget *w, void *data) { FlGui::instance()->options->win->show(); @@ -1820,12 +1817,15 @@ optionWindow::optionWindow(int deltaFontSize) general.value[31]->align(FL_ALIGN_RIGHT); general.value[31]->callback(general_options_ok_cb); - Fl_Button *gmpdcf = new Fl_Button + Fl_Button *gmpdcf = new Fl_Button (L + 2 * WB, 2 * WB + 6 * BH, 1.5*IW, BH, "Configure Gamepad"); gmpdcf->callback(general_gmpdcf_cb); + if(CTX::instance()->gamepad && CTX::instance()->gamepad->active) + gmpdcf->activate(); + else + gmpdcf->deactivate(); o->end(); - } //end of new menu for stereo o->end(); diff --git a/Fltk/optionWindow.h b/Fltk/optionWindow.h index 453e49f4a5..084904a9d2 100644 --- a/Fltk/optionWindow.h +++ b/Fltk/optionWindow.h @@ -103,5 +103,5 @@ void solver_options_cb(Fl_Widget *w, void *data); void post_options_cb(Fl_Widget *w, void *data); void view_options_cb(Fl_Widget *w, void *data); void general_gmpdcf_cb(Fl_Widget *w, void *data); -#endif +#endif -- GitLab