Skip to content

Instantly share code, notes, and snippets.

@nobolu-ootsuka-unrealengine
Last active June 9, 2019 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobolu-ootsuka-unrealengine/276988ddd81d2b524885bc869d8380a0 to your computer and use it in GitHub Desktop.
Save nobolu-ootsuka-unrealengine/276988ddd81d2b524885bc869d8380a0 to your computer and use it in GitHub Desktop.
MyPlugin.cpp
#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"
#include "../Public/MyPlugin.h"
#include "MyPluginStyle.h"
#include "LevelEditor.h"
#include "MultiBox/MultiBoxBuilder.h"
#include "Textures/SlateIcon.h"
#include "Editor/MainFrame/Public/Interfaces/IMainFrameModule.h"
#include "SWindow.h"
#include "SlateApplication.h"
#include "SButton.h"
#include "SCheckbox.h"
#define LOCTEXT_NAMESPACE "MyPlugin"
class FMyPlugin : public IMyPlugin
{
/** IModuleInterface implementation */
virtual void StartupModule() override;
virtual void ShutdownModule() override;
void OnWindowMenuExtension(FMenuBuilder& MenuBuilder);
void OnMyToolMenu();
TSharedPtr<FExtender> Extender;
void OnMainFrameLoad(TSharedPtr<SWindow> InRootWindow, bool bIsNewProjectWindow);
TWeakPtr<SWindow> RootWindow;
TWeakPtr<SWindow> MyWindow;
FReply OnButtonClicked();
void OnToggleCheckBox(ECheckBoxState NewCheckedState);
ECheckBoxState GetCheckBoxState() const;
bool Check;
TSharedPtr<FMyPluginStyle> Style;
};
IMPLEMENT_MODULE(FMyPlugin, MyPlugin)
void FMyPlugin::StartupModule()
{
if (IsRunningCommandlet()) { return; }
Extender = MakeShareable(new FExtender);
Extender->AddMenuExtension(
"LevelEditor",
EExtensionHook::After,
NULL,
FMenuExtensionDelegate::CreateRaw(this, &FMyPlugin::OnWindowMenuExtension)
);
FLevelEditorModule& LevelEditorModule =
FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");
LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(Extender);
IMainFrameModule& MainFrameModule =
FModuleManager::LoadModuleChecked<IMainFrameModule>(TEXT("MainFrame"));
MainFrameModule.OnMainFrameCreationFinished().AddRaw(
this, &FMyPlugin::OnMainFrameLoad);
Check = true;
Style = MakeShareable(new FMyPluginStyle());
}
void FMyPlugin::ShutdownModule()
{
if (Extender.IsValid() && FModuleManager::Get().IsModuleLoaded("LevelEditor"))
{
FLevelEditorModule& LevelEditorModule =
FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");
LevelEditorModule.GetMenuExtensibilityManager()->RemoveExtender(Extender);
}
if (FModuleManager::Get().IsModuleLoaded("MainFrame"))
{
IMainFrameModule& MainFrameModule =
FModuleManager::LoadModuleChecked<IMainFrameModule>(TEXT("MainFrame"));
MainFrameModule.OnMainFrameCreationFinished().RemoveAll(this);
}
}
void FMyPlugin::OnMainFrameLoad(TSharedPtr<SWindow> InRootWindow, bool bIsNewProjectWindow)
{
if ((!bIsNewProjectWindow) && (InRootWindow.IsValid()))
{
RootWindow = InRootWindow;
}
}
void FMyPlugin::OnWindowMenuExtension(FMenuBuilder& MenuBuilder)
{
MenuBuilder.BeginSection("MyMenuHook", LOCTEXT("MyMenu", "MyMenu"));
MenuBuilder.AddMenuEntry(
LOCTEXT("MyMenuTitle", "MyMenuTitle"),
LOCTEXT("MyMenuToolTip", "hello..."),
FSlateIcon(Style->GetStyleSetName(), "MenuIcon"), //この行を変更
FUIAction(FExecuteAction::CreateRaw(this, &FMyPlugin::OnMyToolMenu)));
MenuBuilder.EndSection();
}
void FMyPlugin::OnMyToolMenu()
{
if (!MyWindow.IsValid())
{
TSharedPtr<SWindow> Window = SNew(SWindow)
.Title(LOCTEXT("MyWindow", "MyWindow"))
.ClientSize(FVector2D(300.f, 300.f));
MyWindow = TWeakPtr<SWindow>(Window);
if (RootWindow.IsValid())
{
FSlateApplication::Get().AddWindowAsNativeChild(
Window.ToSharedRef(), RootWindow.Pin().ToSharedRef());
}
Window->SetContent(
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.VAlign(VAlign_Center)
.FillHeight(1.f)
.Padding(2.f)
[
SNew(STextBlock)
.Text(LOCTEXT("Hello", "Hello"))
]
+ SVerticalBox::Slot()
.VAlign(VAlign_Center)
.FillHeight(1.f)
.Padding(2.f)
[
SNew(SButton)
//.Text(LOCTEXT("Button", "Button")) //削除
.OnClicked_Raw(this, &FMyPlugin::OnButtonClicked)
.Content() //追加
[
SNew(SImage)
.Image(Style->GetBrush("ButtonImage"))
]
]
+ SVerticalBox::Slot()
.VAlign(VAlign_Center)
.FillHeight(1.f)
.Padding(2.f)
[
SNew(SCheckBox)
.OnCheckStateChanged_Raw(this, &FMyPlugin::OnToggleCheckBox)
.IsChecked_Raw(this, &FMyPlugin::GetCheckBoxState)
.Content()
[
SNew(STextBlock)
.Text(LOCTEXT("CheckBox", "CheckBox"))
]
]
);
}
MyWindow.Pin()->BringToFront();
}
FReply FMyPlugin::OnButtonClicked()
{
UE_LOG(LogTemp, Log, TEXT("OnButton"));
if (Check)
{
UE_LOG(LogTemp, Log, TEXT("Check=On"));
}
else {
UE_LOG(LogTemp, Log, TEXT("Check=Off"));
}
return FReply::Handled();
}
void FMyPlugin::OnToggleCheckBox(ECheckBoxState NewCheckedState)
{
Check = (NewCheckedState == ECheckBoxState::Checked);
}
ECheckBoxState FMyPlugin::GetCheckBoxState() const
{
return Check ? ECheckBoxState::Checked : ECheckBoxState::Unchecked;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment