add fileOpenDialog and enable loading of urdf from GUI

(will add .bullet file support soon)
Uses native Windows (getFileOpenFileName) and Mac OSX NSOpenPanel,
on Linux using pipe popen to zenity)
This commit is contained in:
Erwin Coumans
2014-08-31 11:53:44 -07:00
parent 8595928949
commit f199a4a972
14 changed files with 250 additions and 50 deletions

View File

@@ -81,7 +81,7 @@ public:
virtual void setWindowTitle(const char* title);
int fileOpenDialog(char* filename, int maxNameLength);
};

View File

@@ -989,8 +989,48 @@ void MacOpenGLWindow::setRequestExit()
m_internalData->m_exitRequested = true;
}
#include <string.h>
int MacOpenGLWindow::fileOpenDialog(char* filename, int maxNameLength)
{
//save/restore the OpenGL context, NSOpenPanel can mess it up
//http://stackoverflow.com/questions/13987148/nsopenpanel-breaks-my-sdl-opengl-app
NSOpenGLContext *foo = [NSOpenGLContext currentContext];
// get the url of a .txt file
NSOpenPanel * zOpenPanel = [NSOpenPanel openPanel];
NSArray * zAryOfExtensions = [NSArray arrayWithObject:@"urdf"];
[zOpenPanel setAllowedFileTypes:zAryOfExtensions];
NSInteger zIntResult = [zOpenPanel runModal];
[foo makeCurrentContext];
if (zIntResult == NSFileHandlingPanelCancelButton) {
NSLog(@"readUsingOpenPanel cancelled");
return 0;
}
NSURL *zUrl = [zOpenPanel URL];
if (zUrl)
{
//without the file://
NSString *myString = [zUrl absoluteString];
int slen = [myString length];
if (slen < maxNameLength)
{
const char *cfilename=[myString UTF8String];
//expect file:// at start of URL
const char* p = strstr(cfilename, "file://");
if (p==cfilename)
{
int actualLen = strlen(cfilename)-7;
memcpy(filename, cfilename+7,actualLen);
filename[actualLen]=0;
return actualLen;
}
}
}
return 0;
}

View File

@@ -137,6 +137,40 @@ void Win32OpenGLWindow::endRendering()
}
int Win32OpenGLWindow::fileOpenDialog(char* fileName, int maxFileNameLength)
{
//wchar_t wideChars[1024];
OPENFILENAME ofn ;
ZeroMemory( &ofn , sizeof( ofn));
ofn.lStructSize = sizeof ( ofn );
ofn.hwndOwner = NULL ;
#ifdef UNICODE
WCHAR bla[1024];
ofn.lpstrFile = bla;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = 1023;
ofn.lpstrFilter = L"URDF\0*.urdf\0";
#else
ofn.lpstrFile = fileName;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = 1023;
//ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.lpstrFilter = "URDF\0*.urdf\0";
#endif
ofn.nFilterIndex =1;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = 0 ;
ofn.lpstrInitialDir=NULL ;
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;
GetOpenFileName( &ofn );
return strlen(fileName);
//return 0;
}

View File

@@ -51,6 +51,8 @@ public:
virtual void endRendering();
virtual float getRetinaScale() const {return 1.f;}
virtual int fileOpenDialog(char* fileName, int maxFileNameLength);
};

View File

@@ -959,4 +959,29 @@ b3KeyboardCallback X11OpenGLWindow::getKeyboardCallback()
{
return m_data->m_keyboardCallback;
}
#include <stdio.h>
int X11OpenGLWindow::fileOpenDialog(char* filename, int maxNameLength)
{
int len = 0;
FILE * output = popen("zenity --file-selection --file-filter=\"*.urdf\" --file-filter=\"*.*\"","r");
if (output)
{
while( fgets(filename, maxNameLength-1, output) != NULL )
{
len=strlen(filename);
if (len>0)
{
filename[len-1]=0;
printf("file open (length=%d) = %s\n", len,filename);
}
}
pclose(output);
} else
{
printf("Error: fileOpenDialog no popen output, perhaps install zenity?\n");
}
XRaiseWindow(m_data->m_dpy, m_data->m_win);
return len;
}

View File

@@ -59,6 +59,7 @@ public:
virtual void setWindowTitle(const char* title);
int fileOpenDialog(char* filename, int maxNameLength);
};

View File

@@ -110,6 +110,8 @@ class b3gWindowInterface
virtual float getRetinaScale() const =0;
virtual int fileOpenDialog(char* fileName, int maxFileNameLength) = 0;
};
#endif //B3G_WINDOW_INTERFACE_H