Skip to content

Commit e31f91b

Browse files
author
Roberto De Ioris
committed
added file/directory/font native dialogues
1 parent 9a88281 commit e31f91b

3 files changed

Lines changed: 143 additions & 0 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyEngine.cpp‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "Kismet/KismetSystemLibrary.h"
44
#include "Kismet/KismetMathLibrary.h"
55

6+
#include "Developer/DesktopPlatform/Public/IDesktopPlatform.h"
7+
#include "Developer/DesktopPlatform/Public/DesktopPlatformModule.h"
8+
69

710

811
PyObject *py_unreal_engine_log(PyObject * self, PyObject * args) {
@@ -855,3 +858,134 @@ PyObject *py_unreal_engine_get_transient_package(PyObject *self, PyObject * args
855858
Py_INCREF(ret);
856859
return (PyObject *)ret;
857860
}
861+
862+
PyObject *py_unreal_engine_open_file_dialog(PyObject *self, PyObject * args) {
863+
char *title;
864+
char *default_path = (char *)"";
865+
char *default_file = (char *)"";
866+
char *file_types = (char *)"";
867+
PyObject *py_multiple = nullptr;
868+
869+
if (!PyArg_ParseTuple(args, "s|sssO:open_file_dialog", &title, &default_path, &default_file, &file_types, &py_multiple))
870+
return nullptr;
871+
872+
IDesktopPlatform *DesktopPlatform = FDesktopPlatformModule::Get();
873+
if (!DesktopPlatform)
874+
return PyErr_Format(PyExc_Exception, "unable to get reference to DesktopPlatform module");
875+
876+
TSharedPtr<SWindow> ParentWindow = FGlobalTabmanager::Get()->GetRootWindow();
877+
if (!ParentWindow.IsValid())
878+
return PyErr_Format(PyExc_Exception, "unable to get the Root Window");
879+
880+
if (!ParentWindow->GetNativeWindow().IsValid())
881+
return PyErr_Format(PyExc_Exception, "unable to get Native Root Window");
882+
883+
TArray<FString> files;
884+
885+
if (!DesktopPlatform->OpenFileDialog(ParentWindow->GetNativeWindow()->GetOSWindowHandle(), FString(UTF8_TO_TCHAR(title)),
886+
FString(UTF8_TO_TCHAR(default_path)),
887+
FString(UTF8_TO_TCHAR(default_file)),
888+
FString(UTF8_TO_TCHAR(file_types)),
889+
(py_multiple && PyObject_IsTrue(py_multiple)) ? EFileDialogFlags::Multiple : EFileDialogFlags::None,
890+
files)) {
891+
Py_RETURN_NONE;
892+
}
893+
894+
PyObject *py_list = PyList_New(0);
895+
for (FString file : files) {
896+
PyList_Append(py_list, PyUnicode_FromString(TCHAR_TO_UTF8(*file)));
897+
}
898+
return py_list;
899+
}
900+
901+
PyObject *py_unreal_engine_open_directory_dialog(PyObject *self, PyObject * args) {
902+
char *title;
903+
char *default_path = (char *)"";
904+
905+
if (!PyArg_ParseTuple(args, "s|s:open_directory_dialog", &title, &default_path))
906+
return nullptr;
907+
908+
IDesktopPlatform *DesktopPlatform = FDesktopPlatformModule::Get();
909+
if (!DesktopPlatform)
910+
return PyErr_Format(PyExc_Exception, "unable to get reference to DesktopPlatform module");
911+
912+
TSharedPtr<SWindow> ParentWindow = FGlobalTabmanager::Get()->GetRootWindow();
913+
if (!ParentWindow.IsValid())
914+
return PyErr_Format(PyExc_Exception, "unable to get the Root Window");
915+
916+
if (!ParentWindow->GetNativeWindow().IsValid())
917+
return PyErr_Format(PyExc_Exception, "unable to get Native Root Window");
918+
919+
FString choosen_dir;
920+
921+
if (!DesktopPlatform->OpenDirectoryDialog(ParentWindow->GetNativeWindow()->GetOSWindowHandle(), FString(UTF8_TO_TCHAR(title)),
922+
FString(UTF8_TO_TCHAR(default_path)),
923+
choosen_dir)) {
924+
Py_RETURN_NONE;
925+
}
926+
927+
return PyUnicode_FromString(TCHAR_TO_UTF8(*choosen_dir));
928+
}
929+
930+
PyObject *py_unreal_engine_open_font_dialog(PyObject *self, PyObject * args) {
931+
932+
IDesktopPlatform *DesktopPlatform = FDesktopPlatformModule::Get();
933+
if (!DesktopPlatform)
934+
return PyErr_Format(PyExc_Exception, "unable to get reference to DesktopPlatform module");
935+
936+
TSharedPtr<SWindow> ParentWindow = FGlobalTabmanager::Get()->GetRootWindow();
937+
if (!ParentWindow.IsValid())
938+
return PyErr_Format(PyExc_Exception, "unable to get the Root Window");
939+
940+
if (!ParentWindow->GetNativeWindow().IsValid())
941+
return PyErr_Format(PyExc_Exception, "unable to get Native Root Window");
942+
943+
FString font_name;
944+
float height;
945+
EFontImportFlags flags;
946+
947+
if (!DesktopPlatform->OpenFontDialog(ParentWindow->GetNativeWindow()->GetOSWindowHandle(), font_name, height, flags)) {
948+
Py_RETURN_NONE;
949+
}
950+
951+
return Py_BuildValue((char*)"(sfi)", TCHAR_TO_UTF8(*font_name), height, flags);
952+
}
953+
954+
PyObject *py_unreal_engine_save_file_dialog(PyObject *self, PyObject * args) {
955+
char *title;
956+
char *default_path = (char *)"";
957+
char *default_file = (char *)"";
958+
char *file_types = (char *)"";
959+
PyObject *py_multiple = nullptr;
960+
961+
if (!PyArg_ParseTuple(args, "s|sssO:save_file_dialog", &title, &default_path, &default_file, &file_types, &py_multiple))
962+
return nullptr;
963+
964+
IDesktopPlatform *DesktopPlatform = FDesktopPlatformModule::Get();
965+
if (!DesktopPlatform)
966+
return PyErr_Format(PyExc_Exception, "unable to get reference to DesktopPlatform module");
967+
968+
TSharedPtr<SWindow> ParentWindow = FGlobalTabmanager::Get()->GetRootWindow();
969+
if (!ParentWindow.IsValid())
970+
return PyErr_Format(PyExc_Exception, "unable to get the Root Window");
971+
972+
if (!ParentWindow->GetNativeWindow().IsValid())
973+
return PyErr_Format(PyExc_Exception, "unable to get Native Root Window");
974+
975+
TArray<FString> files;
976+
977+
if (!DesktopPlatform->SaveFileDialog(ParentWindow->GetNativeWindow()->GetOSWindowHandle(), FString(UTF8_TO_TCHAR(title)),
978+
FString(UTF8_TO_TCHAR(default_path)),
979+
FString(UTF8_TO_TCHAR(default_file)),
980+
FString(UTF8_TO_TCHAR(file_types)),
981+
(py_multiple && PyObject_IsTrue(py_multiple)) ? EFileDialogFlags::Multiple : EFileDialogFlags::None,
982+
files)) {
983+
Py_RETURN_NONE;
984+
}
985+
986+
PyObject *py_list = PyList_New(0);
987+
for (FString file : files) {
988+
PyList_Append(py_list, PyUnicode_FromString(TCHAR_TO_UTF8(*file)));
989+
}
990+
return py_list;
991+
}

‎Source/UnrealEnginePython/Private/UEPyEngine.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ PyObject *py_unreal_engine_object_path_to_package_name(PyObject *, PyObject *);
6060
PyObject *py_unreal_engine_get_path(PyObject *, PyObject *);
6161
PyObject *py_unreal_engine_get_base_filename(PyObject *, PyObject *);
6262

63+
PyObject *py_unreal_engine_open_file_dialog(PyObject *, PyObject *);
64+
PyObject *py_unreal_engine_open_font_dialog(PyObject *, PyObject *);
65+
PyObject *py_unreal_engine_open_directory_dialog(PyObject *, PyObject *);
66+
PyObject *py_unreal_engine_save_file_dialog(PyObject *, PyObject *);
6367

6468
#if WITH_EDITOR
6569
PyObject *py_unreal_engine_editor_get_active_viewport_screenshot(PyObject *, PyObject *);

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ static PyMethodDef unreal_engine_methods[] = {
177177
{ "get_transient_package", (PyCFunction)py_unreal_engine_get_transient_package, METH_VARARGS, "" },
178178

179179

180+
{ "open_file_dialog", py_unreal_engine_open_file_dialog, METH_VARARGS, "" },
181+
{ "save_file_dialog", py_unreal_engine_save_file_dialog, METH_VARARGS, "" },
182+
{ "open_directory_dialog", py_unreal_engine_open_directory_dialog, METH_VARARGS, "" },
183+
{ "open_font_dialog", py_unreal_engine_open_font_dialog, METH_VARARGS, "" },
184+
180185
// slate
181186

182187
{ "register_nomad_tab_spawner", py_unreal_engine_register_nomad_tab_spawner, METH_VARARGS, "" },

0 commit comments

Comments
 (0)