-
Notifications
You must be signed in to change notification settings - Fork 706
Expand file tree
/
Copy pathUnLuaEditorToolbar.cpp
More file actions
379 lines (321 loc) · 13.9 KB
/
UnLuaEditorToolbar.cpp
File metadata and controls
379 lines (321 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "Misc/EngineVersionComparison.h"
#include "UnLuaPrivate.h"
#include "UnLuaEditorCore.h"
#include "UnLuaEditorToolbar.h"
#include "UnLuaEditorCommands.h"
#include "UnLuaInterface.h"
#include "Animation/AnimInstance.h"
#include "Blueprint/UserWidget.h"
#include "HAL/PlatformApplicationMisc.h"
#include "Interfaces/IPluginManager.h"
#include "BlueprintEditor.h"
#include "LuaModuleLocator.h"
#include "SBlueprintEditorToolbar.h"
#include "Framework/Docking/SDockingTabWell.h"
#include "Framework/Notifications/NotificationManager.h"
#include "Kismet2/BlueprintEditorUtils.h"
#include "Layout/Children.h"
#include "Widgets/Notifications/SNotificationList.h"
#include "ToolMenus.h"
#include "UnLuaSettings.h"
#include "UnLuaIntelliSense.h"
#include "Animation/AnimNotifies/AnimNotifyState.h"
#define LOCTEXT_NAMESPACE "FUnLuaEditorModule"
FUnLuaEditorToolbar::FUnLuaEditorToolbar()
: CommandList(new FUICommandList),
ContextObject(nullptr)
{
}
void FUnLuaEditorToolbar::Initialize()
{
BindCommands();
}
void FUnLuaEditorToolbar::BindCommands()
{
const auto& Commands = FUnLuaEditorCommands::Get();
CommandList->MapAction(Commands.CreateLuaTemplate, FExecuteAction::CreateRaw(this, &FUnLuaEditorToolbar::CreateLuaTemplate_Executed));
CommandList->MapAction(Commands.CopyAsRelativePath, FExecuteAction::CreateRaw(this, &FUnLuaEditorToolbar::CopyAsRelativePath_Executed));
CommandList->MapAction(Commands.BindToLua, FExecuteAction::CreateRaw(this, &FUnLuaEditorToolbar::BindToLua_Executed));
CommandList->MapAction(Commands.UnbindFromLua, FExecuteAction::CreateRaw(this, &FUnLuaEditorToolbar::UnbindFromLua_Executed));
CommandList->MapAction(Commands.RevealInExplorer, FExecuteAction::CreateRaw(this, &FUnLuaEditorToolbar::RevealInExplorer_Executed));
}
void FUnLuaEditorToolbar::BuildToolbar(FToolBarBuilder& ToolbarBuilder, UObject* InContextObject)
{
if (!InContextObject)
return;
ToolbarBuilder.BeginSection(NAME_None);
const auto Blueprint = Cast<UBlueprint>(InContextObject);
ToolbarBuilder.AddComboButton(
FUIAction(),
FOnGetContent::CreateLambda([&, Blueprint, InContextObject]()
{
ContextObject = InContextObject;
const auto BindingStatus = GetBindingStatus(Blueprint);
const FUnLuaEditorCommands& Commands = FUnLuaEditorCommands::Get();
FMenuBuilder MenuBuilder(true, CommandList);
if (BindingStatus == NotBound)
{
MenuBuilder.AddMenuEntry(Commands.BindToLua, NAME_None, LOCTEXT("Bind", "Bind"));
}
else
{
MenuBuilder.AddMenuEntry(Commands.CopyAsRelativePath, NAME_None, LOCTEXT("CopyAsRelativePath", "Copy as Relative Path"));
MenuBuilder.AddMenuEntry(Commands.RevealInExplorer, NAME_None, LOCTEXT("RevealInExplorer", "Reveal in Explorer"));
MenuBuilder.AddMenuEntry(Commands.CreateLuaTemplate, NAME_None, LOCTEXT("CreateLuaTemplate", "Create Lua Template"));
MenuBuilder.AddMenuEntry(Commands.UnbindFromLua, NAME_None, LOCTEXT("Unbind", "Unbind"));
}
return MenuBuilder.MakeWidget();
}),
LOCTEXT("UnLua_Label", "UnLua"),
LOCTEXT("UnLua_ToolTip", "UnLua"),
TAttribute<FSlateIcon>::Create([Blueprint]
{
const auto BindingStatus = GetBindingStatus(Blueprint);
FString InStyleName;
switch (BindingStatus)
{
case Unknown:
InStyleName = "UnLuaEditor.Status_Unknown";
break;
case NotBound:
InStyleName = "UnLuaEditor.Status_NotBound";
break;
case Bound:
InStyleName = "UnLuaEditor.Status_Bound";
break;
case BoundButInvalid:
InStyleName = "UnLuaEditor.Status_BoundButInvalid";
break;
default:
check(false);
}
return FSlateIcon("UnLuaEditorStyle", *InStyleName);
})
);
ToolbarBuilder.EndSection();
BuildNodeMenu();
}
void FUnLuaEditorToolbar::BuildNodeMenu()
{
FToolMenuOwnerScoped OwnerScoped(this);
UToolMenu* BPMenu = UToolMenus::Get()->ExtendMenu("GraphEditor.GraphNodeContextMenu.K2Node_FunctionResult");
BPMenu->AddDynamicSection("UnLua", FNewToolMenuDelegate::CreateLambda([this](UToolMenu* ToolMenu)
{
UGraphNodeContextMenuContext* GraphNodeCtx = ToolMenu->FindContext<UGraphNodeContextMenuContext>();
if (GraphNodeCtx && GraphNodeCtx->Graph)
{
if (GraphNodeCtx->Graph->GetName() == "GetModuleName")
{
FToolMenuSection& UnLuaSection = ToolMenu->AddSection("UnLua", FText::FromString("UnLua"));
UnLuaSection.AddEntry(FToolMenuEntry::InitMenuEntryWithCommandList(FUnLuaEditorCommands::Get().RevealInExplorer, CommandList, LOCTEXT("RevealInExplorer", "Reveal in Explorer")));
}
}
}), FToolMenuInsert(NAME_None, EToolMenuInsertType::First));
}
TSharedRef<FExtender> FUnLuaEditorToolbar::GetExtender(UObject* InContextObject)
{
TSharedRef<FExtender> ToolbarExtender(new FExtender());
const auto ExtensionDelegate = FToolBarExtensionDelegate::CreateLambda([this, InContextObject](FToolBarBuilder& ToolbarBuilder)
{
BuildToolbar(ToolbarBuilder, InContextObject);
});
ToolbarExtender->AddToolBarExtension("Debugging", EExtensionHook::After, CommandList, ExtensionDelegate);
return ToolbarExtender;
}
void FUnLuaEditorToolbar::BindToLua_Executed() const
{
const auto Blueprint = Cast<UBlueprint>(ContextObject);
if (!IsValid(Blueprint))
return;
const auto TargetClass = Blueprint->GeneratedClass;
if (!IsValid(TargetClass))
return;
if (TargetClass->ImplementsInterface(UUnLuaInterface::StaticClass()))
return;
#if UE_VERSION_OLDER_THAN(5, 1, 0)
const auto Ok = FBlueprintEditorUtils::ImplementNewInterface(Blueprint, FName("UnLuaInterface"));
#else
const auto Ok = FBlueprintEditorUtils::ImplementNewInterface(Blueprint, FTopLevelAssetPath(UUnLuaInterface::StaticClass()));
#endif
if (!Ok)
return;
FString LuaModuleName;
const auto ModifierKeys = FSlateApplication::Get().GetModifierKeys();
const auto bIsAltDown = ModifierKeys.IsLeftAltDown() || ModifierKeys.IsRightAltDown();
if (bIsAltDown)
{
const auto Package = Blueprint->GetTypedOuter(UPackage::StaticClass());
LuaModuleName = Package->GetName().RightChop(6).Replace(TEXT("/"), TEXT("."));
}
else
{
const auto Settings = GetDefault<UUnLuaSettings>();
if (Settings && Settings->ModuleLocatorClass)
{
const auto ModuleLocator = Cast<ULuaModuleLocator>(Settings->ModuleLocatorClass->GetDefaultObject());
LuaModuleName = ModuleLocator->Locate(TargetClass);
}
}
if (!LuaModuleName.IsEmpty())
{
const auto InterfaceDesc = *Blueprint->ImplementedInterfaces.FindByPredicate([](const FBPInterfaceDescription& Desc)
{
return Desc.Interface == UUnLuaInterface::StaticClass();
});
InterfaceDesc.Graphs[0]->Nodes[1]->Pins[1]->DefaultValue = LuaModuleName;
}
#if !UE_VERSION_OLDER_THAN(4, 26, 0)
const auto BlueprintEditors = FModuleManager::LoadModuleChecked<FBlueprintEditorModule>("Kismet").GetBlueprintEditors();
for (auto BlueprintEditor : BlueprintEditors)
{
const auto MyBlueprintEditor = static_cast<FBlueprintEditor*>(&BlueprintEditors[0].Get());
if (!MyBlueprintEditor || MyBlueprintEditor->GetBlueprintObj() != Blueprint)
continue;
MyBlueprintEditor->Compile();
const auto Func = Blueprint->GeneratedClass->FindFunctionByName(FName("GetModuleName"));
const auto GraphToOpen = FBlueprintEditorUtils::FindScopeGraph(Blueprint, Func);
MyBlueprintEditor->OpenGraphAndBringToFront(GraphToOpen);
}
#endif
}
void FUnLuaEditorToolbar::UnbindFromLua_Executed() const
{
const auto Blueprint = Cast<UBlueprint>(ContextObject);
if (!IsValid(Blueprint))
return;
const auto TargetClass = Blueprint->GeneratedClass;
if (!IsValid(TargetClass))
return;
if (!TargetClass->ImplementsInterface(UUnLuaInterface::StaticClass()))
return;
#if UE_VERSION_OLDER_THAN(5, 1, 0)
FBlueprintEditorUtils::RemoveInterface(Blueprint, FName("UnLuaInterface"));
#else
FBlueprintEditorUtils::RemoveInterface(Blueprint, FTopLevelAssetPath(UUnLuaInterface::StaticClass()));
#endif
#if !UE_VERSION_OLDER_THAN(4, 26, 0)
const auto BlueprintEditors = FModuleManager::LoadModuleChecked<FBlueprintEditorModule>("Kismet").GetBlueprintEditors();
for (auto BlueprintEditor : BlueprintEditors)
{
const auto MyBlueprintEditor = static_cast<FBlueprintEditor*>(&BlueprintEditors[0].Get());
if (!MyBlueprintEditor || MyBlueprintEditor->GetBlueprintObj() != Blueprint)
continue;
MyBlueprintEditor->Compile();
MyBlueprintEditor->RefreshEditors();
}
#endif
const auto ActiveTab = FGlobalTabmanager::Get()->GetActiveTab();
if (!ActiveTab)
return;
const auto DockingTabWell = ActiveTab->GetParent();
if (!DockingTabWell)
return;
const auto DockTabs = DockingTabWell->GetChildren(); // DockingTabWell->GetTabs();
for (auto i = 0; i < DockTabs->Num(); i++)
{
const auto DockTab = StaticCastSharedRef<SDockTab>(DockTabs->GetChildAt(i));
const auto Label = DockTab->GetTabLabel();
if (Label.ToString().Equals("$$ Get Module Name $$"))
{
DockTab->RequestCloseTab();
}
}
}
void FUnLuaEditorToolbar::CreateLuaTemplate_Executed()
{
const auto Blueprint = Cast<UBlueprint>(ContextObject);
if (!IsValid(Blueprint))
return;
UClass* Class = Blueprint->GeneratedClass;
const auto Func = Class->FindFunctionByName(FName("GetModuleName"));
if (!IsValid(Func))
return;
FString ModuleName;
Class->GetDefaultObject()->ProcessEvent(Func, &ModuleName);
if (ModuleName.IsEmpty())
{
FNotificationInfo Info(LOCTEXT("ModuleNameRequired", "Please specify a module name first"));
Info.ExpireDuration = 5;
FSlateNotificationManager::Get().AddNotification(Info);
return;
}
TArray<FString> ModuleNameParts;
ModuleName.ParseIntoArray(ModuleNameParts, TEXT("."));
const auto TemplateName = ModuleNameParts.Last();
const auto RelativePath = ModuleName.Replace(TEXT("."), TEXT("/"));
const auto FileName = FString::Printf(TEXT("%s%s.lua"), *GLuaSrcFullPath, *RelativePath);
if (FPaths::FileExists(FileName))
{
UE_LOG(LogUnLua, Warning, TEXT("%s"), *FText::Format(LOCTEXT("FileAlreadyExists", "Lua file ({0}) is already existed!"), FText::FromString(TemplateName)).ToString());
return;
}
static FString BaseDir = IPluginManager::Get().FindPlugin(TEXT("UnLua"))->GetBaseDir();
for (auto TemplateClass = Class; TemplateClass; TemplateClass = TemplateClass->GetSuperClass())
{
auto TemplateClassName = TemplateClass->GetName().EndsWith("_C") ? TemplateClass->GetName().LeftChop(2) : TemplateClass->GetName();
auto RelativeFilePath = "Config/LuaTemplates" / TemplateClassName + ".lua";
auto FullFilePath = FPaths::ProjectDir() / RelativeFilePath;
if (!FPaths::FileExists(FullFilePath))
FullFilePath = BaseDir / RelativeFilePath;
if (!FPaths::FileExists(FullFilePath))
continue;
FString Content;
FFileHelper::LoadFileToString(Content, *FullFilePath);
Content = Content.Replace(TEXT("TemplateName"), *TemplateName)
.Replace(TEXT("ClassName"), *UnLua::IntelliSense::GetTypeName(Class));
FFileHelper::SaveStringToFile(Content, *FileName, FFileHelper::EEncodingOptions::ForceUTF8WithoutBOM);
break;
}
}
void FUnLuaEditorToolbar::RevealInExplorer_Executed()
{
const auto Blueprint = Cast<UBlueprint>(ContextObject);
if (!IsValid(Blueprint))
return;
const auto TargetClass = Blueprint->GeneratedClass;
if (!IsValid(TargetClass))
return;
if (!TargetClass->ImplementsInterface(UUnLuaInterface::StaticClass()))
return;
const auto Func = TargetClass->FindFunctionByName(FName("GetModuleName"));
if (!IsValid(Func))
return;
FString ModuleName;
const auto DefaultObject = TargetClass->GetDefaultObject();
DefaultObject->UObject::ProcessEvent(Func, &ModuleName);
const auto RelativePath = ModuleName.Replace(TEXT("."), TEXT("/"));
const auto FileName = FString::Printf(TEXT("%s%s.lua"), *GLuaSrcFullPath, *RelativePath);
if (IFileManager::Get().FileExists(*FileName))
{
FPlatformProcess::ExploreFolder(*FileName);
}
else
{
FNotificationInfo NotificationInfo(FText::FromString("UnLua Notification"));
NotificationInfo.Text = LOCTEXT("FileNotExist", "The file does not exist.");
NotificationInfo.bFireAndForget = true;
NotificationInfo.ExpireDuration = 100.0f;
NotificationInfo.bUseThrobber = true;
FSlateNotificationManager::Get().AddNotification(NotificationInfo);
}
}
void FUnLuaEditorToolbar::CopyAsRelativePath_Executed() const
{
const auto Blueprint = Cast<UBlueprint>(ContextObject);
if (!IsValid(Blueprint))
return;
const auto TargetClass = Blueprint->GeneratedClass;
if (!IsValid(TargetClass))
return;
if (!TargetClass->ImplementsInterface(UUnLuaInterface::StaticClass()))
return;
const auto Func = TargetClass->FindFunctionByName(FName("GetModuleName"));
if (!IsValid(Func))
return;
FString ModuleName;
const auto DefaultObject = TargetClass->GetDefaultObject();
DefaultObject->UObject::ProcessEvent(Func, &ModuleName);
const auto RelativePath = ModuleName.Replace(TEXT("."), TEXT("/")) + TEXT(".lua");
FPlatformApplicationMisc::ClipboardCopy(*RelativePath);
}
#undef LOCTEXT_NAMESPACE