Skip to content

Commit a953810

Browse files
author
Roberto De Ioris
committed
added directories support to the python editor
1 parent 3b95f49 commit a953810

File tree

8 files changed

+71
-24
lines changed

8 files changed

+71
-24
lines changed

Source/PythonEditor/Private/PythonEditorStyle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void FPythonEditorStyle::Initialize()
4747
StyleSet->Set("PythonEditor.TabIcon", new IMAGE_BRUSH("UI/PythonEditor_16x", Icon16x16));
4848

4949
StyleSet->Set("PythonEditor.New", new IMAGE_BRUSH("UI/GenericFile", Icon40x40));
50+
StyleSet->Set("PythonEditor.NewDirectory", new IMAGE_BRUSH("UI/GenericFile", Icon40x40));
5051
StyleSet->Set("PythonEditor.New.Small", new IMAGE_BRUSH("UI/GenericFile", Icon16x16));
5152
StyleSet->Set("PythonEditor.Delete", new IMAGE_BRUSH("UI/DeleteFile", Icon40x40));
5253
StyleSet->Set("PythonEditor.Delete.Small", new IMAGE_BRUSH("UI/DeleteFile", Icon16x16));

Source/PythonEditor/Private/PythonProjectEditor.cpp

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ void FPythonProjectEditor::BindCommands()
221221
FExecuteAction::CreateSP(this, &FPythonProjectEditor::New_Internal),
222222
FCanExecuteAction::CreateSP(this, &FPythonProjectEditor::CanNew)
223223
);
224+
ToolkitCommands->MapAction(FPythonProjectEditorCommands::Get().NewDirectory,
225+
FExecuteAction::CreateSP(this, &FPythonProjectEditor::NewDirectory_Internal),
226+
FCanExecuteAction::CreateSP(this, &FPythonProjectEditor::CanNew)
227+
);
224228
ToolkitCommands->MapAction(FPythonProjectEditorCommands::Get().Delete,
225229
FExecuteAction::CreateSP(this, &FPythonProjectEditor::Delete_Internal),
226230
FCanExecuteAction::CreateSP(this, &FPythonProjectEditor::CanDelete)
@@ -299,24 +303,41 @@ TSharedRef<SWidget> FPythonProjectEditor::CreatePythonEditorWidget(TSharedRef<FT
299303
return SNew(SPythonEditor, Item);
300304
}
301305

302-
FString FPythonProjectEditor::GetNoneRepeatName()
306+
FString FPythonProjectEditor::GetSafeName(bool IsDirectory)
303307
{
304308
UPythonProject* PythonProject = PythonProjectBeingEdited.Get();
305309

310+
TArray<UPythonProjectItem*> selItems = MyPythonProjectEditor->GetSelectedItems();
311+
FString basePath = PythonProject->Path;
312+
if (selItems.Num() > 0) {
313+
if (selItems[0]->Type == EPythonProjectItemType::Folder) {
314+
basePath = selItems[0]->Path;
315+
}
316+
}
317+
306318
int suf = 2;
307319
FString scriptName;
308320
while (true)
309321
{
310322

311323
FString SufStr = FString::Printf(TEXT("%d"), suf);
312-
scriptName = PythonProject->Path / TEXT("PythonScript") + SufStr + TEXT(".py");
313-
if (!FPaths::FileExists(scriptName)) {
314-
return scriptName;
324+
if (!IsDirectory) {
325+
scriptName = basePath / TEXT("PythonScript") + SufStr + TEXT(".py");
326+
if (!FPaths::FileExists(scriptName)) {
327+
return scriptName;
328+
}
329+
}
330+
else {
331+
scriptName = basePath / TEXT("NewDirectory") + SufStr;
332+
if (!FPaths::DirectoryExists(scriptName)) {
333+
return scriptName;
334+
}
315335
}
316336

317337
suf++;
318338
}
319339
}
340+
320341
void FPythonProjectEditor::New_Internal()
321342
{
322343
New();
@@ -326,7 +347,7 @@ bool FPythonProjectEditor::New()
326347
{
327348
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
328349

329-
FString scriptName = GetNoneRepeatName();
350+
FString scriptName = GetSafeName(false);
330351
IFileHandle* fileHandle = PlatformFile.OpenWrite(*scriptName);
331352
if (fileHandle == NULL) {
332353
return false;
@@ -335,7 +356,26 @@ bool FPythonProjectEditor::New()
335356
UPythonProject* PythonProject = PythonProjectBeingEdited.Get();
336357
PythonProject->RescanChildren();
337358
UPythonProjectItem* item = MyPythonProjectEditor->SelectByPath(scriptName);
338-
OpenFileForEditing(item);
359+
if (item)
360+
OpenFileForEditing(item);
361+
return true;
362+
}
363+
364+
void FPythonProjectEditor::NewDirectory_Internal()
365+
{
366+
NewDirectory();
367+
}
368+
369+
bool FPythonProjectEditor::NewDirectory()
370+
{
371+
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
372+
373+
FString dirName = GetSafeName(true);
374+
if (!PlatformFile.CreateDirectory(*dirName)) {
375+
return false;
376+
}
377+
UPythonProject* PythonProject = PythonProjectBeingEdited.Get();
378+
PythonProject->RescanChildren();
339379
return true;
340380
}
341381

Source/PythonEditor/Private/PythonProjectEditor.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class FPythonProjectEditor : public FWorkflowCentricApplication, public FGCObjec
5151
TSharedPtr<class FPythonProjectEditorToolbar> GetToolbarBuilder() { return ToolbarBuilder; }
5252
bool New();
5353

54+
bool NewDirectory();
55+
5456
bool Delete();
5557

5658
bool Save();
@@ -61,13 +63,15 @@ class FPythonProjectEditor : public FWorkflowCentricApplication, public FGCObjec
6163

6264
bool ExecuteInSandbox();
6365

64-
FString GetNoneRepeatName();
66+
FString GetSafeName(bool IsDirectory);
6567

6668
private:
6769
void BindCommands();
6870

6971
void New_Internal();
7072

73+
void NewDirectory_Internal();
74+
7175
void Delete_Internal();
7276

7377
void Save_Internal();

Source/PythonEditor/Private/PythonProjectEditorCommands.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ FPythonProjectEditorCommands::FPythonProjectEditorCommands()
1515
void FPythonProjectEditorCommands::RegisterCommands()
1616
{
1717
UI_COMMAND(New, "New", "New Python Script.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control, EKeys::N));
18+
UI_COMMAND(NewDirectory, "New Directory", "New Directory.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control | EModifierKey::Shift, EKeys::N));
1819
UI_COMMAND(Delete, "Delete", "Delete Python Script.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control, EKeys::D));
1920

2021
UI_COMMAND(Save, "Save", "Save the currently active document.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control, EKeys::S));

Source/PythonEditor/Private/PythonProjectEditorCommands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class FPythonProjectEditorCommands : public TCommands<FPythonProjectEditorComman
1010
FPythonProjectEditorCommands();
1111

1212
TSharedPtr<FUICommandInfo> New;
13+
TSharedPtr<FUICommandInfo> NewDirectory;
1314
TSharedPtr<FUICommandInfo> Delete;
1415
TSharedPtr<FUICommandInfo> Save;
1516
TSharedPtr<FUICommandInfo> SaveAll;

Source/PythonEditor/Private/PythonProjectEditorToolbar.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void FPythonProjectEditorToolbar::FillEditorToolbar(FToolBarBuilder& ToolbarBuil
2626
ToolbarBuilder.BeginSection(TEXT("FileManagement"));
2727
{
2828
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().New);
29+
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().NewDirectory);
2930
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().Delete);
3031
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().Save);
3132
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().SaveAll);

Source/PythonEditor/Private/SPythonProjectEditor.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ TArray<UPythonProjectItem*> SPythonProjectEditor::GetSelectedItems()
107107

108108
void SPythonProjectEditor::FolderNameChanged(UPythonProjectItem* Item)
109109
{
110-
111-
FPythonProjectEditor::Get()->CloseFileForEditing(Item);
112-
FPythonProjectEditor::Get()->OpenFileForEditing(Item);
110+
if (Item->Type == EPythonProjectItemType::File) {
111+
FPythonProjectEditor::Get()->CloseFileForEditing(Item);
112+
FPythonProjectEditor::Get()->OpenFileForEditing(Item);
113+
}
113114
}
114115
bool SPythonProjectEditor::IsTreeItemSelected(UPythonProjectItem* Item) const
115116
{

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,17 @@ void FUnrealEnginePythonModule::StartupModule()
104104
local_dict = main_dict;// PyDict_New();
105105

106106
// Redirecting stdout
107-
char const* code = "import sys\r\nclass StdoutCatcher :\r\n\tdef __init__(self) :\r\n\t\tself.data = ''\r\n\tdef flush(self) :\r\n\t\tself.data = ''\r\n\tdef write(self, stuff) :\r\n\t\tif(stuff.strip()!=''):\r\n\t\t\tself.data = self.data+'python: '+ stuff\r\ncatcher = StdoutCatcher()\r\nsys.stdout = catcher";
107+
char const* code = "import sys\n"
108+
"import unreal_engine as ue\n"
109+
"class UnrealEngineOutput:\n"
110+
" def __init__(self, logger):\n"
111+
" self.logger = logger\n"
112+
" def write(self, buf):\n"
113+
" self.logger(buf)\n"
114+
" def flush(self):\n"
115+
" return\n"
116+
"sys.stdout = UnrealEngineOutput(ue.log)\n"
117+
"sys.stderr = UnrealEngineOutput(ue.log_error)\n";
108118
PyRun_SimpleString(code);
109119

110120
if (PyImport_ImportModule("ue_site")) {
@@ -144,18 +154,6 @@ void FUnrealEnginePythonModule::RunString(char *str) {
144154
unreal_engine_py_log_error();
145155
return;
146156
}
147-
else {
148-
//Get stdout output information
149-
PyObject* catcher = PyObject_GetAttrString((PyObject*)main_module, "catcher");
150-
PyObject* output = PyObject_GetAttrString(catcher, "data");
151-
char * buffer = PyUnicode_AsUTF8(output);
152-
UE_LOG(LogPython, Log, TEXT("%s"), ANSI_TO_TCHAR(buffer));
153-
154-
PyRun_SimpleString("sys.stdout.flush()");
155-
Py_DECREF(catcher);
156-
Py_DECREF(output);
157-
158-
}
159157
Py_DECREF(eval_ret);
160158
}
161159

@@ -236,7 +234,7 @@ void FUnrealEnginePythonModule::RunFile(char *filename) {
236234
}
237235
#endif
238236

239-
}
237+
}
240238

241239
// run a python script in a new sub interpreter (useful for unit tests)
242240
void FUnrealEnginePythonModule::RunFileSandboxed(char *filename) {

0 commit comments

Comments
 (0)