Java Code Examples for org.lwjgl.system.MemoryStack#mallocPointer()

The following examples show how to use org.lwjgl.system.MemoryStack#mallocPointer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LWJGUIDialog.java    From LWJGUI with MIT License 6 votes vote down vote up
/**
 * Opens a file open dialog.
 * 
 * @param title window title
 * @param defaultPath default file path
 * @param filterDescription description of the accepted file extension(s)
 * @param acceptedFileExtension the first accepted file extension (example: "txt", use * for all)
 * @param additionalAcceptedFileExtensions any additional accepted file extensions
 * 
 * @return the selected file
 */
public static File showOpenFileDialog(String title, File defaultPath, String filterDescription, String acceptedFileExtension, String... additionalAcceptedFileExtensions){

	MemoryStack stack = MemoryStack.stackPush();

	PointerBuffer filters = stack.mallocPointer(1 + additionalAcceptedFileExtensions.length);

       filters.put(stack.UTF8("*." + acceptedFileExtension));
       for(int i = 0; i < additionalAcceptedFileExtensions.length; i++){
		filters.put(stack.UTF8("*." + additionalAcceptedFileExtensions[i]));
       }

       filters.flip();

       defaultPath = defaultPath.getAbsoluteFile();
       String defaultString = defaultPath.getAbsolutePath();
       if(defaultPath.isDirectory() && !defaultString.endsWith(File.separator)){
       	defaultString += File.separator;
       }
       
       String result = TinyFileDialogs.tinyfd_openFileDialog(title, defaultString, filters, filterDescription, false);

	stack.pop();

	return result != null ? new File(result) : null; 
}
 
Example 2
Source File: LWJGUIDialog.java    From LWJGUI with MIT License 3 votes vote down vote up
/**
 * Opens a file save dialog.
 * 
 * @param title window title
 * @param defaultPath default file path
 * @param filterDescription description of the accepted file extension(s)
 * @param fileExtension the file extension (example: "txt")
 * @param forceExtension the user can select any file regardless of extension. If this is set to true, then the given extension will be automatically added if the extension is wrong.
 * 
 * @return the selected file
 */
public static File showSaveFileDialog(String title, File defaultPath, String filterDescription, String fileExtension, boolean forceExtension){

	MemoryStack stack = MemoryStack.stackPush();

	PointerBuffer filters = stack.mallocPointer(1);

       filters.put(stack.UTF8("*." + fileExtension)).flip();
       
       defaultPath = defaultPath.getAbsoluteFile();
       String defaultString = defaultPath.getAbsolutePath();
       if(defaultPath.isDirectory() && !defaultString.endsWith(File.separator)){
       	defaultString += File.separator;
       }

       //System.out.println(defaultString + " : exists: " + new File(defaultString).exists());
       
       String result = TinyFileDialogs.tinyfd_saveFileDialog(title, defaultString, filters, filterDescription);

       stack.pop();

       if(result == null){
       	return null;
       }
       
       if(forceExtension && !result.endsWith("." + fileExtension)){
       	result += "." + fileExtension;
       }

       return new File(result);
}