Add lime.ui.FileDialog
This commit is contained in:
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -43,3 +43,6 @@
|
||||
[submodule "project/lib/lzma"]
|
||||
path = project/lib/lzma
|
||||
url = https://github.com/native-toolkit/lzma
|
||||
[submodule "project/lib/nfd"]
|
||||
path = project/lib/nfd
|
||||
url = https://github.com/native-toolkit/nfd
|
||||
|
||||
80
lime/ui/FileDialog.hx
Normal file
80
lime/ui/FileDialog.hx
Normal file
@@ -0,0 +1,80 @@
|
||||
package lime.ui;
|
||||
|
||||
|
||||
import lime.system.System;
|
||||
import lime.utils.Resource;
|
||||
|
||||
#if sys
|
||||
import sys.io.File;
|
||||
#end
|
||||
|
||||
|
||||
class FileDialog {
|
||||
|
||||
|
||||
public static function openFile (filter = null, defaultPath:String = null):String {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_file_dialog_open_file (filter, defaultPath);
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function openFiles (filter = null, defaultPath:String = null):Array<String> {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_file_dialog_open_files (filter, defaultPath);
|
||||
#else
|
||||
return [];
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function saveFile (data:Resource, filter = null, defaultPath:String = null):String {
|
||||
|
||||
var path = null;
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
|
||||
path = lime_file_dialog_save_file (filter, defaultPath);
|
||||
|
||||
if (path != null) {
|
||||
|
||||
try {
|
||||
|
||||
File.saveBytes (path, data);
|
||||
|
||||
} catch (e:Dynamic) {
|
||||
|
||||
path = null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
return path;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Native Methods
|
||||
|
||||
|
||||
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
private static var lime_file_dialog_open_file = System.load ("lime", "lime_file_dialog_open_file", 2);
|
||||
private static var lime_file_dialog_open_files = System.load ("lime", "lime_file_dialog_open_files", 2);
|
||||
private static var lime_file_dialog_save_file = System.load ("lime", "lime_file_dialog_save_file", 2);
|
||||
#end
|
||||
|
||||
|
||||
}
|
||||
31
lime/utils/Resource.hx
Normal file
31
lime/utils/Resource.hx
Normal file
@@ -0,0 +1,31 @@
|
||||
package lime.utils;
|
||||
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
|
||||
abstract Resource(Bytes) from Bytes to Bytes {
|
||||
|
||||
|
||||
public function new (size:Int = 0) {
|
||||
|
||||
this = Bytes.alloc (size);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@:from private static inline function __fromString (value:String):Resource {
|
||||
|
||||
return Bytes.ofString (value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@:to private static inline function __toString (value:Resource):String {
|
||||
|
||||
return (value:Bytes).toString ();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
<set name="LIME_HARFBUZZ" value="1" />
|
||||
<set name="LIME_LZMA" value="1" />
|
||||
<!-- <set name="LIME_NEKO" value="1" if="linux" /> -->
|
||||
<set name="LIME_NFD" value="1" if="windows || mac || linux" />
|
||||
<set name="LIME_OGG" value="1" />
|
||||
<set name="LIME_OPENAL" value="1" />
|
||||
<set name="LIME_OPENGL" value="1" />
|
||||
@@ -112,6 +113,15 @@
|
||||
|
||||
</section>
|
||||
|
||||
<section if="LIME_NFD">
|
||||
|
||||
<compilerflag value="-I${NATIVE_TOOLKIT_PATH}/nfd/src/include/" />
|
||||
<compilerflag value="-DLIME_NFD" />
|
||||
|
||||
<file name="src/ui/FileDialog.cpp" />
|
||||
|
||||
</section>
|
||||
|
||||
<section if="LIME_OGG">
|
||||
|
||||
<compilerflag value="-I${NATIVE_TOOLKIT_PATH}/ogg/include/" />
|
||||
@@ -221,6 +231,7 @@
|
||||
<include name="lib/jpeg/files.xml" />
|
||||
<include name="lib/lzma/files.xml" />
|
||||
<include name="lib/neko/files.xml" />
|
||||
<include name="lib/nfd/files.xml" />
|
||||
<include name="lib/ogg/files.xml" />
|
||||
<include name="lib/openal/files.xml" />
|
||||
<include name="lib/openal-android/files.xml" if="android" />
|
||||
@@ -245,6 +256,7 @@
|
||||
<files id="native-toolkit-harfbuzz" if="LIME_HARFBUZZ" />
|
||||
<files id="native-toolkit-lzma" if="LIME_LZMA" />
|
||||
<files id="native-toolkit-neko" if="LIME_NEKO" />
|
||||
<files id="native-toolkit-nfd" if="LIME_NFD" />
|
||||
<files id="native-toolkit-ogg" if="LIME_OGG" />
|
||||
<files id="native-toolkit-openal" if="LIME_OPENAL" unless="mac || iphone || android || emscripten" />
|
||||
<files id="native-toolkit-openal-android" if="LIME_OPENAL android" />
|
||||
|
||||
25
project/include/ui/FileDialog.h
Normal file
25
project/include/ui/FileDialog.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef LIME_UI_FILE_DIALOG_H
|
||||
#define LIME_UI_FILE_DIALOG_H
|
||||
|
||||
|
||||
#include <utils/QuickVec.h>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
class FileDialog {
|
||||
|
||||
public:
|
||||
|
||||
static const char* OpenFile (const char* filter = NULL, const char* defaultPath = NULL);
|
||||
static void OpenFiles (QuickVec<const char*>* files, const char* filter = NULL, const char* defaultPath = NULL);
|
||||
static const char* SaveFile (const char* filter = NULL, const char* defaultPath = NULL);
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
1
project/lib/nfd
Submodule
1
project/lib/nfd
Submodule
Submodule project/lib/nfd added at d329ac0237
@@ -25,6 +25,7 @@
|
||||
#include <system/System.h>
|
||||
#include <text/Font.h>
|
||||
#include <text/TextLayout.h>
|
||||
#include <ui/FileDialog.h>
|
||||
#include <ui/Gamepad.h>
|
||||
#include <ui/GamepadEvent.h>
|
||||
#include <ui/KeyEvent.h>
|
||||
@@ -193,6 +194,51 @@ namespace lime {
|
||||
}
|
||||
|
||||
|
||||
value lime_file_dialog_open_file (value filter, value defaultPath) {
|
||||
|
||||
#ifdef LIME_NFD
|
||||
const char* path = FileDialog::OpenFile (val_string (filter), val_string (defaultPath));
|
||||
if (path) return alloc_string (path);
|
||||
#endif
|
||||
|
||||
return alloc_null ();
|
||||
|
||||
}
|
||||
|
||||
|
||||
value lime_file_dialog_open_files (value filter, value defaultPath) {
|
||||
|
||||
#ifdef LIME_NFD
|
||||
QuickVec<const char*> files;
|
||||
|
||||
FileDialog::OpenFiles (&files, val_string (filter), val_string (defaultPath));
|
||||
value result = alloc_array (files.size ());
|
||||
|
||||
for (int i = 0; i < files.size (); i++) {
|
||||
|
||||
val_array_set_i (result, i, alloc_string (files[i]));
|
||||
|
||||
}
|
||||
#else
|
||||
value result = alloc_array (0);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
value lime_file_dialog_save_file (value filter, value defaultPath) {
|
||||
|
||||
#ifdef LIME_NFD
|
||||
return alloc_string (FileDialog::SaveFile (val_string (filter), val_string (defaultPath)));
|
||||
#else
|
||||
return alloc_null ();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
void lime_font_destroy (value handle) {
|
||||
|
||||
#ifdef LIME_FREETYPE
|
||||
@@ -1238,6 +1284,9 @@ namespace lime {
|
||||
DEFINE_PRIM (lime_bytes_read_file, 1);
|
||||
DEFINE_PRIM (lime_clipboard_get_text, 0);
|
||||
DEFINE_PRIM (lime_clipboard_set_text, 1);
|
||||
DEFINE_PRIM (lime_file_dialog_open_file, 2);
|
||||
DEFINE_PRIM (lime_file_dialog_open_files, 2);
|
||||
DEFINE_PRIM (lime_file_dialog_save_file, 2);
|
||||
DEFINE_PRIM (lime_font_get_ascender, 1);
|
||||
DEFINE_PRIM (lime_font_get_descender, 1);
|
||||
DEFINE_PRIM (lime_font_get_family_name, 1);
|
||||
|
||||
100
project/src/ui/FileDialog.cpp
Normal file
100
project/src/ui/FileDialog.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <ui/FileDialog.h>
|
||||
#include <nfd.h>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
const char* FileDialog::OpenFile (const char* filter, const char* defaultPath) {
|
||||
|
||||
nfdchar_t *savePath = NULL;
|
||||
nfdresult_t result = NFD_OpenDialog (filter, defaultPath, &savePath);
|
||||
|
||||
switch (result) {
|
||||
|
||||
case NFD_OKAY:
|
||||
|
||||
return savePath;
|
||||
break;
|
||||
|
||||
case NFD_CANCEL:
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
printf ("Error: %s\n", NFD_GetError ());
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return savePath;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void FileDialog::OpenFiles (QuickVec<const char*>* files, const char* filter, const char* defaultPath) {
|
||||
|
||||
nfdpathset_t pathSet;
|
||||
nfdresult_t result = NFD_OpenDialogMultiple (filter, defaultPath, &pathSet);
|
||||
|
||||
switch (result) {
|
||||
|
||||
case NFD_OKAY:
|
||||
{
|
||||
printf("okay\n");
|
||||
printf("size: %d\n", NFD_PathSet_GetCount (&pathSet));
|
||||
for (int i = 0; i < NFD_PathSet_GetCount (&pathSet); i++) {
|
||||
|
||||
printf ("%s\n", NFD_PathSet_GetPath (&pathSet, i));
|
||||
files->push_back (NFD_PathSet_GetPath (&pathSet, i));
|
||||
|
||||
}
|
||||
|
||||
//NFD_PathSet_Free (&pathSet);
|
||||
break;
|
||||
}
|
||||
|
||||
case NFD_CANCEL:
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
printf ("Error: %s\n", NFD_GetError ());
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
const char* FileDialog::SaveFile (const char* filter, const char* defaultPath) {
|
||||
|
||||
nfdchar_t *savePath = NULL;
|
||||
nfdresult_t result = NFD_SaveDialog (filter, defaultPath, &savePath);
|
||||
|
||||
switch (result) {
|
||||
|
||||
case NFD_OKAY:
|
||||
|
||||
return savePath;
|
||||
break;
|
||||
|
||||
case NFD_CANCEL:
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
printf ("Error: %s\n", NFD_GetError ());
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return savePath;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user