mirror of
https://github.com/sinbad/StevesUEHelpers.git
synced 2025-02-23 09:35:25 +00:00
Code moved into separate repo
This commit is contained in:
commit
6986921439
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
Binaries/*
|
||||||
|
Intermediate/*
|
||||||
|
Plugins/*/Intermediate/*
|
||||||
|
|
||||||
|
# Cache files for the editor to use
|
||||||
|
DerivedDataCache/*
|
||||||
|
|
8
License.txt
Normal file
8
License.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
Copyright © 2020 Steve Streeting
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
183
ReadMe.md
Normal file
183
ReadMe.md
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
# Steve's UE Helper Plugin Library
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
This is a helper plugin library for [Unreal Engine 4](https://www.unrealengine.com)
|
||||||
|
which makes a bunch of things better:
|
||||||
|
|
||||||
|
* UI Improvements
|
||||||
|
* FocusableButton: button which uses Hover style to highlight when it has focus (keyboard / gamepad)
|
||||||
|
* FocusablePanel: which ensure a widget is focussed when gamepad is activated so navigation is reliable
|
||||||
|
* Also remembers last focussed widget so it can be restored when used in context stacks
|
||||||
|
* FocusableUserWidget: A hack to allow UserWidgets to delegate their focus requests to a child item (required to make focus work reliably with compound widgets)
|
||||||
|
* InputImage: Image which will change itself to an image representing a button / key based on an input action
|
||||||
|
* (It changes dynamically when the input method changes e.g. when player moves a gamepad stick)
|
||||||
|
* Includes use of a UiTheme data asset which maps FKeys to Sprite images of buttons
|
||||||
|
* MenuStack/MenuBase: a context stack of widgets so you can easily create menu sequences, implement "back" navigation
|
||||||
|
* OptionWidgetBase: a widget which implements the "choose an item" concept but adapts between mouse and keyboard/gamepad
|
||||||
|
style navigation depending on what the currently used input method is
|
||||||
|
* Input Improvements
|
||||||
|
* Tracks last used input method (keyboard / mouse / gamepad) per player
|
||||||
|
* Events raised whenever a player uses a different input method (in game, and in UI)
|
||||||
|
(Actually reliable and not dependent on vagaries of input mappings / differences between UI and game input)
|
||||||
|
|
||||||
|
## Installing this plugin
|
||||||
|
|
||||||
|
### Cloning
|
||||||
|
|
||||||
|
The best way is to clone this repository as a submodule; that way you can contribute
|
||||||
|
pull requests if you want.
|
||||||
|
|
||||||
|
```
|
||||||
|
> cd YourProject\Plugins
|
||||||
|
> git submodule add https://github.com/sinbad/StevesUEHelpers
|
||||||
|
> git add ../.gitmodules
|
||||||
|
> git commit
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively you can download the ZIP of this repo and place it in
|
||||||
|
`YourProject/Plugins/StevesUEHelpers`.
|
||||||
|
|
||||||
|
### Referencing in C++
|
||||||
|
|
||||||
|
Edit YourProject.Build.cs and do something similar to this:
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public class YourProject : ModuleRules
|
||||||
|
{
|
||||||
|
private string PluginsPath
|
||||||
|
{
|
||||||
|
get { return Path.GetFullPath( Path.Combine( ModuleDirectory, "../../Plugins/" ) ); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public YourProject(ReadOnlyTargetRules Target) : base(Target)
|
||||||
|
{
|
||||||
|
// Your existing rules
|
||||||
|
// ...
|
||||||
|
|
||||||
|
|
||||||
|
AddStevesUEHelpers();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void AddStevesUEHelpers() {
|
||||||
|
// Linker
|
||||||
|
PrivateDependencyModuleNames.AddRange(new string[] { "StevesUEHelpers" });
|
||||||
|
// Headers
|
||||||
|
PublicIncludePaths.Add(Path.Combine( PluginsPath, "StevesUEHelpers", "Source", "StevesUEHelpers", "Public"));
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
You can use most of the features without doing anything else, but certain features
|
||||||
|
require some additional setup, see below.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Widgets
|
||||||
|
|
||||||
|
Many features are contained in new widget types, which can be created directly in
|
||||||
|
UMG Design mode.
|
||||||
|
|
||||||
|
TODO: specific tutorials on new widget types.
|
||||||
|
|
||||||
|
### Game instance subsystem
|
||||||
|
|
||||||
|
In order to track stateful things like the current input mode for each player,
|
||||||
|
there is a custom `GameInstanceSubsystem` called `StevesGameSubsystem`, which
|
||||||
|
you can tap into anywhere in Blueprints by searchign for it:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Once you have access to this you can do things like tie events into the input mode
|
||||||
|
changing:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
To access this in C++, just do this:
|
||||||
|
|
||||||
|
```c++
|
||||||
|
#include "StevesUEHelpers.h"
|
||||||
|
|
||||||
|
...
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (GS)
|
||||||
|
{
|
||||||
|
// get current mode (first player assumed)
|
||||||
|
EInputMode CurrentInputMode = GS->GetLastInputModeUsed();
|
||||||
|
// Subscribe to input mode changes (remember this must be a UFUNCTION)
|
||||||
|
GS->OnInputModeChanged.AddUniqueDynamic(this, &AMyActor::OnInputModeChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
# Additional Configuration
|
||||||
|
|
||||||
|
## UiTheme
|
||||||
|
|
||||||
|
Some features of this plugin such as InputImage need a `UUiTheme` asset, which is just a Data Asset
|
||||||
|
based on the `UUiTheme` class which references other resources like button images. There is one in the Examples project as reference.
|
||||||
|
|
||||||
|
### Create a UiTheme:
|
||||||
|
|
||||||
|
|
||||||
|
1. Click Add New > Miscellaneous > Data Asset
|
||||||
|
1. Select "UiTheme" as the class
|
||||||
|
1. Save the new asset with your chosen name
|
||||||
|
|
||||||
|
### Create a Primary Asset Label
|
||||||
|
|
||||||
|
UiThemes are a new kind of primary asset, loaded at runtime. To ensure this
|
||||||
|
asset is included when packaging, create a Primary Asset Label in the same folder:
|
||||||
|
|
||||||
|
1. Click Add New > Miscellaneous > Data Asset
|
||||||
|
1. Select "Primary Asset Label" as the class
|
||||||
|
1. Name it however you like
|
||||||
|
1. Double-click to edit
|
||||||
|
1. Under "Explicit Assets", add an entry and pick the UiTheme you created above
|
||||||
|
1. Save the changes
|
||||||
|
|
||||||
|
This just ensures that the packaging system knows to include your UiTheme, since
|
||||||
|
it won't be directly referenced by any other primary asset.
|
||||||
|
|
||||||
|
### Create button sprite data
|
||||||
|
|
||||||
|
The UiTheme wants to reference DataTables which contain links between the input
|
||||||
|
keys and button sprites. So the first job is to create the button sprites.
|
||||||
|
|
||||||
|
The Example project includes some button sprites already; contained in a packed
|
||||||
|
sprite sheet. I created these using TexturePacker and imported into UE which created
|
||||||
|
the sprites, but you can create them however you like. However, we do require sprites
|
||||||
|
rather than plain textures.
|
||||||
|
|
||||||
|
This means you must have the Paper2D plugin enabled in your project.
|
||||||
|
|
||||||
|
### Linking input keys to button sprites
|
||||||
|
Once you have a set of button sprites, you need to create DataTables which map
|
||||||
|
input FKeys (which can be keys, or mouse buttons, or gamepad buttons / sticks)
|
||||||
|
to these sprites, so that for example InputImage can be told to display the action
|
||||||
|
"Fire", and then display either say the left mouse button or a gamepad trigger
|
||||||
|
depending on what's being used.
|
||||||
|
|
||||||
|
Personally I did this using a CSV file for ease of use, for example:
|
||||||
|
|
||||||
|
```csv
|
||||||
|
Name,Key,Sprite
|
||||||
|
Gamepad_LeftX,Gamepad_LeftX,"PaperSprite'/Game/Textures/UI/Sprites/Frames/XboxOne_Left_Stick'"
|
||||||
|
Gamepad_FaceButton_Bottom,Gamepad_FaceButton_Bottom,"PaperSprite'/Game/Textures/UI/Sprites/Frames/XboxOne_A'"
|
||||||
|
```
|
||||||
|
|
||||||
|
You should import this as a DataTable based on the KeySprite type. A separate
|
||||||
|
one is needed for keyboard / mouse and gamepad. Once you've created them, or
|
||||||
|
copied the ones from the examples, you should update the UiTheme asset you
|
||||||
|
created to point at these data tables.
|
||||||
|
|
||||||
|
# License
|
||||||
|
|
||||||
|
The MIT License (MIT)
|
||||||
|
Copyright © 2020 Steve Streeting
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
BIN
Resources/Icon.afphoto
Normal file
BIN
Resources/Icon.afphoto
Normal file
Binary file not shown.
BIN
Resources/Icon128.png
Normal file
BIN
Resources/Icon128.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.2 KiB |
BIN
Resources/bpexample.png
Normal file
BIN
Resources/bpexample.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 118 KiB |
BIN
Resources/gameinstance.png
Normal file
BIN
Resources/gameinstance.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 34 KiB |
184
Source/StevesUEHelpers/Private/StevesGameSubsystem.cpp
Normal file
184
Source/StevesUEHelpers/Private/StevesGameSubsystem.cpp
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
#include "StevesGameSubsystem.h"
|
||||||
|
#include "StevesGameViewportClientBase.h"
|
||||||
|
#include "StevesUEHelpers.h"
|
||||||
|
#include "Engine/AssetManager.h"
|
||||||
|
#include "Engine/GameInstance.h"
|
||||||
|
#include "Framework/Application/SlateApplication.h"
|
||||||
|
#include "GameFramework/PlayerController.h"
|
||||||
|
|
||||||
|
//PRAGMA_DISABLE_OPTIMIZATION
|
||||||
|
|
||||||
|
void UStevesGameSubsystem::Initialize(FSubsystemCollectionBase& Collection)
|
||||||
|
{
|
||||||
|
Super::Initialize(Collection);
|
||||||
|
CreateInputDetector();
|
||||||
|
InitTheme();
|
||||||
|
|
||||||
|
auto GI = GetGameInstance();
|
||||||
|
auto VC = Cast<UStevesGameViewportClientBase>(GI->GetGameViewportClient());
|
||||||
|
if (!VC)
|
||||||
|
UE_LOG(LogStevesUEHelpers, Warning, TEXT("Your GameViewportClient needs to be set to a subclass of UStevesGameViewportClientBase if you want full functionality!"))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UStevesGameSubsystem::Deinitialize()
|
||||||
|
{
|
||||||
|
Super::Deinitialize();
|
||||||
|
DestroyInputDetector();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UStevesGameSubsystem::CreateInputDetector()
|
||||||
|
{
|
||||||
|
if (!InputDetector.IsValid())
|
||||||
|
{
|
||||||
|
InputDetector = MakeShareable(new FInputModeDetector());
|
||||||
|
FSlateApplication::Get().RegisterInputPreProcessor(InputDetector);
|
||||||
|
|
||||||
|
InputDetector->OnInputModeChanged.BindUObject(this, &UStevesGameSubsystem::OnInputDetectorModeChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UStevesGameSubsystem::DestroyInputDetector()
|
||||||
|
{
|
||||||
|
if (InputDetector.IsValid())
|
||||||
|
{
|
||||||
|
FSlateApplication::Get().UnregisterInputPreProcessor(InputDetector);
|
||||||
|
InputDetector.Reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UStevesGameSubsystem::InitTheme()
|
||||||
|
{
|
||||||
|
DefaultUiTheme = LoadObject<UUiTheme>(nullptr, *DefaultUiThemePath, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UStevesGameSubsystem::OnInputDetectorModeChanged(int PlayerIndex, EInputMode NewMode)
|
||||||
|
{
|
||||||
|
auto GI = GetGameInstance();
|
||||||
|
auto VC = GI->GetGameViewportClient();
|
||||||
|
auto SVC = Cast<UStevesGameViewportClientBase>(VC);
|
||||||
|
if (VC)
|
||||||
|
{
|
||||||
|
if (NewMode == EInputMode::Gamepad)
|
||||||
|
{
|
||||||
|
// First move mouse pointer out of the way because it still generates mouse hits (unless we make source changes to Slate, ugh)
|
||||||
|
auto PC = GI->GetFirstLocalPlayerController();
|
||||||
|
FVector2D Sz;
|
||||||
|
VC->GetViewportSize(Sz);
|
||||||
|
// -1 because if you move cursor outside window when captured, Slate blows up when you press Return, ughghh
|
||||||
|
PC->SetMouseLocation(Sz.X-1,Sz.Y-1);
|
||||||
|
|
||||||
|
// Now hide it
|
||||||
|
// I've seen people use PC->bShowMouseCursor but this messes with capturing when you switch back & forth
|
||||||
|
// especially when pausing in the editor
|
||||||
|
|
||||||
|
// instead, I'm using a separate flag to suppress it, see UiFixGameViewportClient for usage
|
||||||
|
if (SVC)
|
||||||
|
SVC->SetSuppressMouseCursor(true);
|
||||||
|
}
|
||||||
|
else if (NewMode == EInputMode::Mouse)
|
||||||
|
{
|
||||||
|
if (SVC)
|
||||||
|
SVC->SetSuppressMouseCursor(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
OnInputModeChanged.Broadcast(PlayerIndex, NewMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
UStevesGameSubsystem::FInputModeDetector::FInputModeDetector()
|
||||||
|
{
|
||||||
|
// 4 local players should be plenty usually (will expand if necessary)
|
||||||
|
LastInputModeByPlayer.Init(EInputMode::Mouse, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UStevesGameSubsystem::FInputModeDetector::HandleKeyDownEvent(FSlateApplication& SlateApp, const FKeyEvent& InKeyEvent)
|
||||||
|
{
|
||||||
|
// Key down also registers for gamepad buttons
|
||||||
|
ProcessKeyOrButton(InKeyEvent.GetUserIndex(), InKeyEvent.GetKey());
|
||||||
|
|
||||||
|
// Don't consume
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UStevesGameSubsystem::FInputModeDetector::HandleAnalogInputEvent(FSlateApplication& SlateApp,
|
||||||
|
const FAnalogInputEvent& InAnalogInputEvent)
|
||||||
|
{
|
||||||
|
if (InAnalogInputEvent.GetAnalogValue() > GamepadAxisThreshold)
|
||||||
|
SetMode(InAnalogInputEvent.GetUserIndex(), EInputMode::Gamepad);
|
||||||
|
// Don't consume
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UStevesGameSubsystem::FInputModeDetector::HandleMouseMoveEvent(FSlateApplication& SlateApp, const FPointerEvent& MouseEvent)
|
||||||
|
{
|
||||||
|
FVector2D Dist = MouseEvent.GetScreenSpacePosition() - MouseEvent.GetLastScreenSpacePosition();
|
||||||
|
if (FMath::Abs(Dist.X) > MouseMoveThreshold || FMath::Abs(Dist.Y) > MouseMoveThreshold)
|
||||||
|
{
|
||||||
|
SetMode(MouseEvent.GetUserIndex(), EInputMode::Mouse);
|
||||||
|
}
|
||||||
|
// Don't consume
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UStevesGameSubsystem::FInputModeDetector::HandleMouseButtonDownEvent(FSlateApplication& SlateApp, const FPointerEvent& MouseEvent)
|
||||||
|
{
|
||||||
|
// We don't care which button
|
||||||
|
SetMode(MouseEvent.GetUserIndex(), EInputMode::Mouse);
|
||||||
|
// Don't consume
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UStevesGameSubsystem::FInputModeDetector::HandleMouseWheelOrGestureEvent(FSlateApplication& SlateApp, const FPointerEvent& InWheelEvent,
|
||||||
|
const FPointerEvent* InGestureEvent)
|
||||||
|
{
|
||||||
|
SetMode(InWheelEvent.GetUserIndex(), EInputMode::Mouse);
|
||||||
|
// Don't consume
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
EInputMode UStevesGameSubsystem::FInputModeDetector::GetLastInputMode(int PlayerIndex)
|
||||||
|
{
|
||||||
|
if (PlayerIndex >= 0 && PlayerIndex < LastInputModeByPlayer.Num())
|
||||||
|
return LastInputModeByPlayer[PlayerIndex];
|
||||||
|
|
||||||
|
// Assume default if never told
|
||||||
|
return DefaultInputMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UStevesGameSubsystem::FInputModeDetector::ProcessKeyOrButton(int PlayerIndex, FKey Key)
|
||||||
|
{
|
||||||
|
if (Key.IsGamepadKey())
|
||||||
|
{
|
||||||
|
SetMode(PlayerIndex, EInputMode::Gamepad);
|
||||||
|
}
|
||||||
|
else if (Key.IsMouseButton())
|
||||||
|
{
|
||||||
|
// Assuming mice don't have analog buttons!
|
||||||
|
SetMode(PlayerIndex, EInputMode::Mouse);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// We assume anything that's not mouse and not gamepad is a keyboard
|
||||||
|
// Assuming keyboards don't have analog buttons!
|
||||||
|
SetMode(PlayerIndex, EInputMode::Keyboard);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UStevesGameSubsystem::FInputModeDetector::SetMode(int PlayerIndex, EInputMode NewMode)
|
||||||
|
{
|
||||||
|
if (NewMode != EInputMode::Unknown && NewMode != GetLastInputMode(PlayerIndex))
|
||||||
|
{
|
||||||
|
if (PlayerIndex >= LastInputModeByPlayer.Num())
|
||||||
|
LastInputModeByPlayer.SetNum(PlayerIndex + 1);
|
||||||
|
|
||||||
|
LastInputModeByPlayer[PlayerIndex] = NewMode;
|
||||||
|
OnInputModeChanged.ExecuteIfBound(PlayerIndex, NewMode);
|
||||||
|
UE_LOG(LogTemp, Warning, TEXT("Input mode for player %d changed: %s"), PlayerIndex, *UEnum::GetValueAsString(NewMode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//PRAGMA_ENABLE_OPTIMIZATION
|
@ -0,0 +1,29 @@
|
|||||||
|
#include "StevesGameViewportClientBase.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include "Engine/Console.h"
|
||||||
|
#include "Engine/GameInstance.h"
|
||||||
|
#include "Framework/Application/SlateApplication.h"
|
||||||
|
|
||||||
|
void UStevesGameViewportClientBase::Init(FWorldContext& WorldContext, UGameInstance* OwningGameInstance,
|
||||||
|
bool bCreateNewAudioDevice)
|
||||||
|
{
|
||||||
|
Super::Init(WorldContext, OwningGameInstance, bCreateNewAudioDevice);
|
||||||
|
|
||||||
|
bSuppressMouseCursor = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
EMouseCursor::Type UStevesGameViewportClientBase::GetCursor(FViewport* InViewport, int32 X, int32 Y)
|
||||||
|
{
|
||||||
|
if (FSlateApplication::Get().IsActive() && bSuppressMouseCursor)
|
||||||
|
return EMouseCursor::None;
|
||||||
|
|
||||||
|
return Super::GetCursor(InViewport, X, Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UStevesGameViewportClientBase::SetSuppressMouseCursor(bool bSuppress)
|
||||||
|
{
|
||||||
|
bSuppressMouseCursor = bSuppress;
|
||||||
|
FSlateApplication::Get().OnCursorSet(); // necessary to make slate wake up
|
||||||
|
|
||||||
|
}
|
22
Source/StevesUEHelpers/Private/StevesUEHelpers.cpp
Normal file
22
Source/StevesUEHelpers/Private/StevesUEHelpers.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include "StevesUEHelpers.h"
|
||||||
|
|
||||||
|
#define LOCTEXT_NAMESPACE "FStevesUEHelpers"
|
||||||
|
|
||||||
|
DEFINE_LOG_CATEGORY(LogStevesUEHelpers)
|
||||||
|
|
||||||
|
void FStevesUEHelpers::StartupModule()
|
||||||
|
{
|
||||||
|
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
|
||||||
|
UE_LOG(LogStevesUEHelpers, Log, TEXT("Steve's UE Helpers Module Started"))
|
||||||
|
}
|
||||||
|
|
||||||
|
void FStevesUEHelpers::ShutdownModule()
|
||||||
|
{
|
||||||
|
// This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
|
||||||
|
// we call this function before unloading the module.
|
||||||
|
UE_LOG(LogStevesUEHelpers, Log, TEXT("Steve's UE Helpers Module Stopped"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef LOCTEXT_NAMESPACE
|
||||||
|
|
||||||
|
IMPLEMENT_MODULE(FStevesUEHelpers, StevesUEHelpers)
|
82
Source/StevesUEHelpers/Private/StevesUI/FocusableButton.cpp
Normal file
82
Source/StevesUEHelpers/Private/StevesUI/FocusableButton.cpp
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
#include "StevesUI/FocusableButton.h"
|
||||||
|
#include "StevesUI/SFocusableButton.h"
|
||||||
|
#include "Components/ButtonSlot.h"
|
||||||
|
|
||||||
|
UFocusableButton::UFocusableButton(const FObjectInitializer& ObjectInitializer)
|
||||||
|
: Super(ObjectInitializer)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
TSharedRef<SWidget> UFocusableButton::RebuildWidget()
|
||||||
|
{
|
||||||
|
MyButton = SNew(SFocusableButton)
|
||||||
|
// This part is standard button behaviour
|
||||||
|
.OnClicked(BIND_UOBJECT_DELEGATE(FOnClicked, SlateHandleClicked))
|
||||||
|
.OnPressed(BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandlePressed))
|
||||||
|
.OnReleased(BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandleReleased))
|
||||||
|
.OnHovered_UObject( this, &ThisClass::SlateHandleHovered )
|
||||||
|
.OnUnhovered_UObject( this, &ThisClass::SlateHandleUnhovered )
|
||||||
|
.ButtonStyle(&WidgetStyle)
|
||||||
|
.ClickMethod(ClickMethod)
|
||||||
|
.TouchMethod(TouchMethod)
|
||||||
|
.PressMethod(PressMethod)
|
||||||
|
.IsFocusable(IsFocusable)
|
||||||
|
// Our new events
|
||||||
|
.OnFocusReceived(BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandleFocusReceived))
|
||||||
|
.OnFocusLost(BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandleFocusLost))
|
||||||
|
;
|
||||||
|
|
||||||
|
if ( GetChildrenCount() > 0 )
|
||||||
|
{
|
||||||
|
Cast<UButtonSlot>(GetContentSlot())->BuildSlot(MyButton.ToSharedRef());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy Widget style but make normal same as hovered
|
||||||
|
FocussedStyle = WidgetStyle;
|
||||||
|
FocussedStyle.Normal = FocussedStyle.Hovered;
|
||||||
|
|
||||||
|
return MyButton.ToSharedRef();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UFocusableButton::SlateHandleFocusReceived()
|
||||||
|
{
|
||||||
|
ApplyFocusStyle();
|
||||||
|
OnFocusReceived.Broadcast();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UFocusableButton::SlateHandleFocusLost()
|
||||||
|
{
|
||||||
|
UndoFocusStyle();
|
||||||
|
OnFocusLost.Broadcast();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UFocusableButton::ApplyFocusStyle()
|
||||||
|
{
|
||||||
|
if (MyButton.IsValid() && bUseHoverStyleWhenFocussed)
|
||||||
|
{
|
||||||
|
MyButton->SetButtonStyle(&FocussedStyle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UFocusableButton::UndoFocusStyle()
|
||||||
|
{
|
||||||
|
if (MyButton.IsValid())
|
||||||
|
{
|
||||||
|
MyButton->SetButtonStyle(&WidgetStyle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UFocusableButton::SlateHandleHovered()
|
||||||
|
{
|
||||||
|
if (bTakeFocusOnHover)
|
||||||
|
{
|
||||||
|
SetFocus();
|
||||||
|
}
|
||||||
|
OnHovered.Broadcast();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UFocusableButton::SlateHandleUnhovered()
|
||||||
|
{
|
||||||
|
OnUnhovered.Broadcast();
|
||||||
|
}
|
68
Source/StevesUEHelpers/Private/StevesUI/FocusablePanel.cpp
Normal file
68
Source/StevesUEHelpers/Private/StevesUI/FocusablePanel.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include "StevesUI/FocusablePanel.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include "StevesUI.h"
|
||||||
|
#include "Blueprint/WidgetTree.h"
|
||||||
|
#include "Framework/Application/SlateApplication.h"
|
||||||
|
|
||||||
|
void UFocusablePanel::NativeConstruct()
|
||||||
|
{
|
||||||
|
Super::NativeConstruct();
|
||||||
|
|
||||||
|
// Find focus widget
|
||||||
|
if (!InitialFocusWidgetName.IsNone())
|
||||||
|
{
|
||||||
|
InitialFocusWidget = WidgetTree->FindWidget(InitialFocusWidgetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UFocusablePanel::NativeDestruct()
|
||||||
|
{
|
||||||
|
Super::NativeDestruct();
|
||||||
|
|
||||||
|
InitialFocusWidget.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UFocusablePanel::SetFocusToInitialWidget() const
|
||||||
|
{
|
||||||
|
if (InitialFocusWidget.IsValid())
|
||||||
|
{
|
||||||
|
SetWidgetFocusProperly(InitialFocusWidget.Get());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool UFocusablePanel::RestorePreviousFocus() const
|
||||||
|
{
|
||||||
|
if (PreviousFocusWidget.IsValid())
|
||||||
|
{
|
||||||
|
SetWidgetFocusProperly(PreviousFocusWidget.Get());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UFocusablePanel::SavePreviousFocus()
|
||||||
|
{
|
||||||
|
const auto SW = FSlateApplication::Get().GetUserFocusedWidget(0);
|
||||||
|
if (SW)
|
||||||
|
{
|
||||||
|
PreviousFocusWidget = FindWidgetFromSlate(SW.Get(), this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PreviousFocusWidget.Reset();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UFocusablePanel::SetFocusProperly_Implementation()
|
||||||
|
{
|
||||||
|
if (!RestorePreviousFocus())
|
||||||
|
SetFocusToInitialWidget();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
|||||||
|
#include "StevesUI/FocusableUserWidget.h"
|
||||||
|
|
||||||
|
void UFocusableUserWidget::SetFocusProperly_Implementation()
|
||||||
|
{
|
||||||
|
// Default is to call normal
|
||||||
|
SetFocus();
|
||||||
|
}
|
165
Source/StevesUEHelpers/Private/StevesUI/InputImage.cpp
Normal file
165
Source/StevesUEHelpers/Private/StevesUI/InputImage.cpp
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
#include "StevesUI/InputImage.h"
|
||||||
|
#include "StevesUI.h"
|
||||||
|
#include "StevesUI/KeySprite.h"
|
||||||
|
#include "StevesGameSubsystem.h"
|
||||||
|
#include "StevesUEHelpers.h"
|
||||||
|
#include "Blueprint/WidgetTree.h"
|
||||||
|
#include "Engine/AssetManager.h"
|
||||||
|
#include "GameFramework/InputSettings.h"
|
||||||
|
|
||||||
|
TSharedRef<SWidget> UInputImage::RebuildWidget()
|
||||||
|
{
|
||||||
|
auto Ret = Super::RebuildWidget();
|
||||||
|
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (GS)
|
||||||
|
{
|
||||||
|
GS->OnInputModeChanged.AddUniqueDynamic(this, &UInputImage::OnInputModeChanged);
|
||||||
|
CurrentInputMode = GS->GetLastInputModeUsed();
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::OnInputModeChanged(int PlayerIndex, EInputMode InputMode)
|
||||||
|
{
|
||||||
|
CurrentInputMode = InputMode;
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::SetCustomTheme(UUiTheme* Theme)
|
||||||
|
{
|
||||||
|
CustomTheme = Theme;
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::BeginDestroy()
|
||||||
|
{
|
||||||
|
Super::BeginDestroy();
|
||||||
|
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (GS)
|
||||||
|
{
|
||||||
|
GS->OnInputModeChanged.RemoveAll(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::SetFromAction(FName Name)
|
||||||
|
{
|
||||||
|
BindingType = EInputBindingType::Action;
|
||||||
|
ActionOrAxisName = Name;
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::SetFromAxis(FName Name)
|
||||||
|
{
|
||||||
|
BindingType = EInputBindingType::Axis;
|
||||||
|
ActionOrAxisName = Name;
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::SetFromKey(FKey K)
|
||||||
|
{
|
||||||
|
BindingType = EInputBindingType::Key;
|
||||||
|
Key = K;
|
||||||
|
UpdateImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::UpdateImage()
|
||||||
|
{
|
||||||
|
switch(BindingType)
|
||||||
|
{
|
||||||
|
case EInputBindingType::Action:
|
||||||
|
UpdateImageFromAction(ActionOrAxisName);
|
||||||
|
break;
|
||||||
|
case EInputBindingType::Axis:
|
||||||
|
UpdateImageFromAxis(ActionOrAxisName);
|
||||||
|
break;
|
||||||
|
case EInputBindingType::Key:
|
||||||
|
UpdateImageFromKey(Key);
|
||||||
|
break;
|
||||||
|
default: ;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
TArray<FInputActionKeyMapping> TempActionMap;
|
||||||
|
TArray<FInputAxisKeyMapping> TempAxisMap;
|
||||||
|
|
||||||
|
void UInputImage::UpdateImageFromAction(const FName& Name)
|
||||||
|
{
|
||||||
|
// Look up the key for this action
|
||||||
|
UInputSettings* Settings = UInputSettings::GetInputSettings();
|
||||||
|
TempActionMap.Empty();
|
||||||
|
Settings->GetActionMappingByName(Name, TempActionMap);
|
||||||
|
const bool WantGamepad = CurrentInputMode == EInputMode::Gamepad;
|
||||||
|
for (auto && ActionMap : TempActionMap)
|
||||||
|
{
|
||||||
|
if (ActionMap.Key.IsGamepadKey() == WantGamepad)
|
||||||
|
{
|
||||||
|
UpdateImageFromKey(ActionMap.Key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if we fell through, didn't find a mapping which matched our gamepad preference
|
||||||
|
if (TempActionMap.Num())
|
||||||
|
{
|
||||||
|
UpdateImageFromKey(TempActionMap[0].Key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::UpdateImageFromAxis(const FName& Name)
|
||||||
|
{
|
||||||
|
// Look up the key for this axis
|
||||||
|
UInputSettings* Settings = UInputSettings::GetInputSettings();
|
||||||
|
TempAxisMap.Empty();
|
||||||
|
Settings->GetAxisMappingByName(Name, TempAxisMap);
|
||||||
|
const bool WantGamepad = CurrentInputMode == EInputMode::Gamepad;
|
||||||
|
for (auto && AxisMap : TempAxisMap)
|
||||||
|
{
|
||||||
|
if (AxisMap.Key.IsGamepadKey() == WantGamepad)
|
||||||
|
{
|
||||||
|
UpdateImageFromKey(AxisMap.Key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if we fell through, didn't find a mapping which matched our gamepad preference
|
||||||
|
if (TempAxisMap.Num())
|
||||||
|
{
|
||||||
|
UpdateImageFromKey(TempAxisMap[0].Key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::UpdateImageFromKey(const FKey& InKey)
|
||||||
|
{
|
||||||
|
auto T = GetTheme();
|
||||||
|
if (T)
|
||||||
|
{
|
||||||
|
if (InKey.IsGamepadKey())
|
||||||
|
UpdateImageFromTable(InKey, T->XboxControllerImages);
|
||||||
|
else
|
||||||
|
UpdateImageFromTable(InKey, T->KeyboardMouseImages);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UInputImage::UpdateImageFromTable(const FKey& InKey, const TSoftObjectPtr<UDataTable>& Asset)
|
||||||
|
{
|
||||||
|
// Sync load for simplicity for now
|
||||||
|
const auto Table = Asset.LoadSynchronous();
|
||||||
|
// Rows are named the same as the key name
|
||||||
|
const auto SpriteRow = Table->FindRow<FKeySprite>(InKey.GetFName(), "Find Key Image");
|
||||||
|
if (SpriteRow)
|
||||||
|
SetBrushFromAtlasInterface(SpriteRow->Sprite);
|
||||||
|
}
|
||||||
|
|
||||||
|
UUiTheme* UInputImage::GetTheme()
|
||||||
|
{
|
||||||
|
if (IsValid(CustomTheme))
|
||||||
|
return CustomTheme;
|
||||||
|
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (GS)
|
||||||
|
return GS->GetDefaultUiTheme();
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
99
Source/StevesUEHelpers/Private/StevesUI/MenuBase.cpp
Normal file
99
Source/StevesUEHelpers/Private/StevesUI/MenuBase.cpp
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
#include "StevesUI/MenuBase.h"
|
||||||
|
|
||||||
|
#include "StevesUI.h"
|
||||||
|
#include "StevesGameSubsystem.h"
|
||||||
|
#include "StevesUEHelpers.h"
|
||||||
|
#include "StevesUI/MenuStack.h"
|
||||||
|
#include "Components/ContentWidget.h"
|
||||||
|
|
||||||
|
void UMenuBase::Close(bool bWasCancel)
|
||||||
|
{
|
||||||
|
// Deliberately raise before parent so stack is always last in event sequence
|
||||||
|
OnClosed.Broadcast(this, bWasCancel);
|
||||||
|
if (ParentStack.IsValid())
|
||||||
|
{
|
||||||
|
ParentStack->PopMenuIfTop(this, bWasCancel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuBase::AddedToStack(UMenuStack* Parent)
|
||||||
|
{
|
||||||
|
ParentStack = MakeWeakObjectPtr(Parent);
|
||||||
|
if (bEmbedInParentContainer)
|
||||||
|
EmbedInParent();
|
||||||
|
else
|
||||||
|
AddToViewport();
|
||||||
|
SetVisibility(ESlateVisibility::Visible);
|
||||||
|
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (bRequestFocus &&
|
||||||
|
GS && (GS->GetLastInputModeUsed() != EInputMode::Gamepad || GS->GetLastInputModeUsed() != EInputMode::Keyboard))
|
||||||
|
{
|
||||||
|
SetFocusProperly();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UMenuBase::InputModeChanged(EInputMode OldMode, EInputMode NewMode)
|
||||||
|
{
|
||||||
|
if (OldMode == EInputMode::Mouse &&
|
||||||
|
(NewMode == EInputMode::Gamepad || NewMode == EInputMode::Keyboard))
|
||||||
|
{
|
||||||
|
if (bRequestFocus)
|
||||||
|
SetFocusProperly();
|
||||||
|
}
|
||||||
|
else if ((OldMode == EInputMode::Gamepad || OldMode == EInputMode::Keyboard) &&
|
||||||
|
NewMode == EInputMode::Mouse)
|
||||||
|
{
|
||||||
|
SavePreviousFocus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuBase::RemovedFromStack(UMenuStack* Parent)
|
||||||
|
{
|
||||||
|
// This works whether embedded or not
|
||||||
|
RemoveFromParent();
|
||||||
|
PreviousFocusWidget.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuBase::SupercededInStack()
|
||||||
|
{
|
||||||
|
SavePreviousFocus();
|
||||||
|
|
||||||
|
if (bEmbedInParentContainer)
|
||||||
|
RemoveFromParent();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (bHideWhenSuperceded)
|
||||||
|
SetVisibility(ESlateVisibility::Collapsed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuBase::RegainedFocusInStack()
|
||||||
|
{
|
||||||
|
if (bEmbedInParentContainer)
|
||||||
|
EmbedInParent();
|
||||||
|
else
|
||||||
|
AddToViewport();
|
||||||
|
SetVisibility(ESlateVisibility::Visible);
|
||||||
|
|
||||||
|
if (bRequestFocus)
|
||||||
|
{
|
||||||
|
SetFocusProperly();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuBase::EmbedInParent()
|
||||||
|
{
|
||||||
|
if (ParentStack.IsValid() &&
|
||||||
|
ParentStack->MenuContainer != nullptr)
|
||||||
|
{
|
||||||
|
ParentStack->MenuContainer->SetContent(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("Cannot embed %s in parent, missing container"), *this->GetName())
|
||||||
|
|
||||||
|
}
|
152
Source/StevesUEHelpers/Private/StevesUI/MenuStack.cpp
Normal file
152
Source/StevesUEHelpers/Private/StevesUI/MenuStack.cpp
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
#include "StevesUI/MenuStack.h"
|
||||||
|
|
||||||
|
#include "StevesUI.h"
|
||||||
|
#include "StevesGameSubsystem.h"
|
||||||
|
#include "StevesUEHelpers.h"
|
||||||
|
#include "StevesUI/MenuBase.h"
|
||||||
|
#include "Framework/Application/SlateApplication.h"
|
||||||
|
|
||||||
|
|
||||||
|
void UMenuStack::NativeConstruct()
|
||||||
|
{
|
||||||
|
Super::NativeConstruct();
|
||||||
|
|
||||||
|
if (!InputPreprocessor.IsValid())
|
||||||
|
{
|
||||||
|
InputPreprocessor = MakeShareable(new FUiInputPreprocessor());
|
||||||
|
InputPreprocessor->OnUiKeyDown.BindUObject(this, &UMenuStack::HandleKeyDownEvent);
|
||||||
|
}
|
||||||
|
FSlateApplication::Get().RegisterInputPreProcessor(InputPreprocessor);
|
||||||
|
|
||||||
|
// We could technically do input change detection in our own input processor, but since this already does it nicely...
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (GS)
|
||||||
|
{
|
||||||
|
GS->OnInputModeChanged.AddDynamic(this, &UMenuStack::InputModeChanged);
|
||||||
|
LastInputMode = GS->GetLastInputModeUsed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuStack::NativeDestruct()
|
||||||
|
{
|
||||||
|
Super::NativeDestruct();
|
||||||
|
FSlateApplication::Get().UnregisterInputPreProcessor(InputPreprocessor);
|
||||||
|
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (GS)
|
||||||
|
GS->OnInputModeChanged.RemoveDynamic(this, &UMenuStack::InputModeChanged);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UMenuStack::HandleKeyDownEvent(const FKeyEvent& InKeyEvent)
|
||||||
|
{
|
||||||
|
// Hardcoding the Back / Exit menu navigation inputs because input mappings can't be trusted in UMG
|
||||||
|
// This is probably OK though, no-one redefines menu controls, right?
|
||||||
|
FKey Key = InKeyEvent.GetKey();
|
||||||
|
if (Key == EKeys::Escape || Key == EKeys::Gamepad_FaceButton_Right)
|
||||||
|
{
|
||||||
|
// This is "Back"
|
||||||
|
PopMenu(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (Key == EKeys::Gamepad_Special_Right)
|
||||||
|
{
|
||||||
|
// Shortcut to exit all menus
|
||||||
|
CloseAll(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UMenuStack::InputModeChanged(int PlayerIndex, EInputMode NewMode)
|
||||||
|
{
|
||||||
|
if (Menus.Num())
|
||||||
|
{
|
||||||
|
Menus.Last()->InputModeChanged(LastInputMode, NewMode);
|
||||||
|
}
|
||||||
|
LastInputMode = NewMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
UMenuBase* UMenuStack::PushMenuByClass(TSubclassOf<UMenuBase> MenuClass)
|
||||||
|
{
|
||||||
|
const FName Name = MakeUniqueObjectName(this->GetOuter(), MenuClass, FName("Menu"));
|
||||||
|
TSubclassOf<UUserWidget> BaseClass = MenuClass;
|
||||||
|
const auto NewMenu = Cast<UMenuBase>(CreateWidgetInstance(*this, BaseClass, Name));
|
||||||
|
PushMenuByObject(NewMenu);
|
||||||
|
|
||||||
|
return NewMenu;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuStack::PushMenuByObject(UMenuBase* NewMenu)
|
||||||
|
{
|
||||||
|
if (Menus.Num() > 0)
|
||||||
|
{
|
||||||
|
auto Top = Menus.Last();
|
||||||
|
Top->SupercededInStack();
|
||||||
|
// We keep this allocated, to restore later on back
|
||||||
|
}
|
||||||
|
Menus.Add(NewMenu);
|
||||||
|
NewMenu->AddedToStack(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuStack::PopMenu(bool bWasCancel)
|
||||||
|
{
|
||||||
|
if (Menus.Num() > 0)
|
||||||
|
{
|
||||||
|
auto Top = Menus.Last();
|
||||||
|
Top->RemovedFromStack(this);
|
||||||
|
Menus.Pop();
|
||||||
|
// No explicit destroy in UMG, let GC do it
|
||||||
|
// we could try to re-use but probably not worth it vs complexity of reset for now
|
||||||
|
|
||||||
|
if (Menus.Num() == 0)
|
||||||
|
{
|
||||||
|
LastMenuClosed(bWasCancel);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto NewTop = Menus.Last();
|
||||||
|
NewTop->RegainedFocusInStack();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuStack::PopMenuIfTop(UMenuBase* UiMenuBase, bool bWasCancel)
|
||||||
|
{
|
||||||
|
if (Menus.Last() == UiMenuBase)
|
||||||
|
{
|
||||||
|
PopMenu(bWasCancel);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("Tried to pop menu %s but it wasn't the top level"), *UiMenuBase->GetName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuStack::LastMenuClosed(bool bWasCancel)
|
||||||
|
{
|
||||||
|
RemoveFromParent();
|
||||||
|
OnClosed.Broadcast(this, bWasCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuStack::CloseAll(bool bWasCancel)
|
||||||
|
{
|
||||||
|
// We don't go through normal pop sequence, this is a shot circuit
|
||||||
|
for (int i = Menus.Num() - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
Menus[i]->RemovedFromStack(this);
|
||||||
|
}
|
||||||
|
Menus.Empty();
|
||||||
|
LastMenuClosed(bWasCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMenuStack::SetFocusProperly_Implementation()
|
||||||
|
{
|
||||||
|
// Delegate to top menu
|
||||||
|
if (Menus.Num() > 0)
|
||||||
|
Menus.Last()->SetFocusProperly();
|
||||||
|
}
|
252
Source/StevesUEHelpers/Private/StevesUI/OptionWidgetBase.cpp
Normal file
252
Source/StevesUEHelpers/Private/StevesUI/OptionWidgetBase.cpp
Normal file
@ -0,0 +1,252 @@
|
|||||||
|
#include "StevesUI/OptionWidgetBase.h"
|
||||||
|
|
||||||
|
#include "StevesUI.h"
|
||||||
|
#include "StevesGameSubsystem.h"
|
||||||
|
#include "StevesUEHelpers.h"
|
||||||
|
#include "Components/Button.h"
|
||||||
|
#include "Components/Image.h"
|
||||||
|
#include "Components/TextBlock.h"
|
||||||
|
|
||||||
|
void UOptionWidgetBase::NativeConstruct()
|
||||||
|
{
|
||||||
|
Super::NativeConstruct();
|
||||||
|
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (GS)
|
||||||
|
{
|
||||||
|
GS->OnInputModeChanged.AddDynamic(this, &UOptionWidgetBase::InputModeChanged);
|
||||||
|
UpdateFromInputMode(GS->GetLastInputModeUsed());
|
||||||
|
ClearOptions();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("StevesGameSubsystem is missing!"));
|
||||||
|
|
||||||
|
// Check we can bind everything we need, bind click
|
||||||
|
if (!MouseVersion)
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("%s should have a MouseVersion instance."), *this->GetClass()->GetName());
|
||||||
|
if (!GamepadVersion)
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("%s should have a GamepadVersion instance."), *this->GetClass()->GetName());
|
||||||
|
if (MouseUpButton)
|
||||||
|
MouseUpButton->OnClicked.AddDynamic(this, &UOptionWidgetBase::MouseUpClicked);
|
||||||
|
else
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("%s should have a MouseUpButton instance."), *this->GetClass()->GetName());
|
||||||
|
if (MouseDownButton)
|
||||||
|
MouseDownButton->OnClicked.AddDynamic(this, &UOptionWidgetBase::MouseDownClicked);
|
||||||
|
else
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("%s should have a MouseDownButton instance."), *this->GetClass()->GetName());
|
||||||
|
if (!MouseUpImage)
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("%s should have a MouseUpImage instance."), *this->GetClass()->GetName());
|
||||||
|
if (!MouseDownImage)
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("%s should have a MouseDownImage instance."), *this->GetClass()->GetName());
|
||||||
|
if (!GamepadUpImage)
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("%s should have a GamepadUpImage instance."), *this->GetClass()->GetName());
|
||||||
|
if (!GamepadDownImage)
|
||||||
|
UE_LOG(LogCustomUI, Error, TEXT("%s should have a GamepadDownImage instance."), *this->GetClass()->GetName());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UOptionWidgetBase::NativeDestruct()
|
||||||
|
{
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (GS)
|
||||||
|
{
|
||||||
|
GS->OnInputModeChanged.RemoveAll(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MouseUpButton)
|
||||||
|
MouseUpButton->OnClicked.RemoveAll(this);
|
||||||
|
if (MouseDownButton)
|
||||||
|
MouseDownButton->OnClicked.RemoveAll(this);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::ChangeOption(int Delta)
|
||||||
|
{
|
||||||
|
SetSelectedIndex(FMath::Clamp(SelectedIndex + Delta, 0, Options.Num() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UOptionWidgetBase::MouseUpClicked()
|
||||||
|
{
|
||||||
|
ChangeOption(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::MouseDownClicked()
|
||||||
|
{
|
||||||
|
ChangeOption(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::ClearOptions()
|
||||||
|
{
|
||||||
|
Options.Empty();
|
||||||
|
SetSelectedIndex(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::UpdateFromInputMode(EInputMode Mode)
|
||||||
|
{
|
||||||
|
switch (Mode)
|
||||||
|
{
|
||||||
|
case EInputMode::Mouse:
|
||||||
|
SetMouseMode();
|
||||||
|
break;
|
||||||
|
case EInputMode::Keyboard:
|
||||||
|
case EInputMode::Gamepad:
|
||||||
|
SetButtonMode();
|
||||||
|
break;
|
||||||
|
case EInputMode::Unknown:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::InputModeChanged(int PlayerIndex, EInputMode NewMode)
|
||||||
|
{
|
||||||
|
UpdateFromInputMode(NewMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::SynchronizeProperties()
|
||||||
|
{
|
||||||
|
Super::SynchronizeProperties();
|
||||||
|
|
||||||
|
SyncButtonProperties(MouseUpButton);
|
||||||
|
SyncButtonProperties(MouseDownButton);
|
||||||
|
SyncButtonProperties(GamepadVersion);
|
||||||
|
|
||||||
|
SyncTextProperties(MouseText);
|
||||||
|
SyncTextProperties(GamepadText);
|
||||||
|
|
||||||
|
SyncButtonImageProperties(MouseUpImage);
|
||||||
|
SyncButtonImageProperties(MouseDownImage);
|
||||||
|
SyncButtonImageProperties(GamepadUpImage);
|
||||||
|
SyncButtonImageProperties(GamepadDownImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::SyncButtonProperties(UButton* Button) const
|
||||||
|
{
|
||||||
|
if (Button)
|
||||||
|
{
|
||||||
|
Button->SetStyle(ButtonStyle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::SyncButtonImageProperties(UImage* Img) const
|
||||||
|
{
|
||||||
|
if (Img)
|
||||||
|
{
|
||||||
|
Img->SetBrush(ButtonImage);
|
||||||
|
Img->SetColorAndOpacity(ButtonImageColour);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::SyncTextProperties(UTextBlock* Txt) const
|
||||||
|
{
|
||||||
|
if (Txt)
|
||||||
|
{
|
||||||
|
Txt->SetFont(Font);
|
||||||
|
Txt->SetColorAndOpacity(TextColour);
|
||||||
|
Txt->SetStrikeBrush(TextStrikeBrush);
|
||||||
|
Txt->SetShadowOffset(TextShadowOffset);
|
||||||
|
Txt->SetShadowColorAndOpacity(TextShadowColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UOptionWidgetBase::SetMouseMode()
|
||||||
|
{
|
||||||
|
if (!MouseVersion)
|
||||||
|
return;
|
||||||
|
|
||||||
|
bool bHadFocus = false;
|
||||||
|
if (GamepadVersion)
|
||||||
|
{
|
||||||
|
bHadFocus = GamepadVersion->HasKeyboardFocus();
|
||||||
|
GamepadVersion->SetVisibility(ESlateVisibility::Hidden);
|
||||||
|
}
|
||||||
|
|
||||||
|
MouseVersion->SetVisibility(ESlateVisibility::Visible);
|
||||||
|
if (bHadFocus)
|
||||||
|
SetFocusProperly();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::SetButtonMode()
|
||||||
|
{
|
||||||
|
if (!GamepadVersion)
|
||||||
|
return;
|
||||||
|
const bool bHadFocus = (MouseUpButton && MouseUpButton->HasKeyboardFocus()) || (MouseDownButton && MouseDownButton->HasKeyboardFocus());
|
||||||
|
|
||||||
|
if (MouseVersion)
|
||||||
|
MouseVersion->SetVisibility(ESlateVisibility::Hidden);
|
||||||
|
|
||||||
|
GamepadVersion->SetVisibility(ESlateVisibility::Visible);
|
||||||
|
if (bHadFocus)
|
||||||
|
SetFocusProperly();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::SetFocusProperly_Implementation()
|
||||||
|
{
|
||||||
|
if (GamepadVersion && GamepadVersion->IsVisible())
|
||||||
|
GamepadVersion->SetFocus();
|
||||||
|
else if (MouseUpButton && MouseDownButton)
|
||||||
|
MouseUpButton->GetIsEnabled() ? MouseUpButton->SetFocus() : MouseDownButton->SetFocus();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::SetSelectedIndex(int NewIndex)
|
||||||
|
{
|
||||||
|
const bool bRaiseEvent = SelectedIndex != NewIndex;
|
||||||
|
|
||||||
|
SelectedIndex = NewIndex;
|
||||||
|
|
||||||
|
const FText NewText = GetSelectedOption();
|
||||||
|
if (MouseText)
|
||||||
|
MouseText->SetText(NewText);
|
||||||
|
if (GamepadText)
|
||||||
|
GamepadText->SetText(NewText);
|
||||||
|
|
||||||
|
const bool CanDecrease = SelectedIndex > 0;
|
||||||
|
const bool CanIncrease = SelectedIndex < Options.Num() - 1;
|
||||||
|
if (MouseDownButton)
|
||||||
|
MouseDownButton->SetIsEnabled(CanDecrease);
|
||||||
|
if (MouseUpButton)
|
||||||
|
MouseUpButton->SetIsEnabled(CanIncrease);
|
||||||
|
if (GamepadDownImage)
|
||||||
|
GamepadDownImage->SetVisibility(CanDecrease ? ESlateVisibility::Visible : ESlateVisibility::Hidden);
|
||||||
|
if (GamepadUpImage)
|
||||||
|
GamepadUpImage->SetVisibility(CanIncrease ? ESlateVisibility::Visible : ESlateVisibility::Hidden);
|
||||||
|
|
||||||
|
if (bRaiseEvent)
|
||||||
|
OnSelectedOptionChanged.Broadcast(this, SelectedIndex);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int UOptionWidgetBase::AddOption(FText Option)
|
||||||
|
{
|
||||||
|
return Options.Add(Option);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UOptionWidgetBase::SetOptions(const TArray<FText>& InOptions, int NewSelectedIndex)
|
||||||
|
{
|
||||||
|
Options = InOptions;
|
||||||
|
SetSelectedIndex(NewSelectedIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
FText UOptionWidgetBase::GetSelectedOption() const
|
||||||
|
{
|
||||||
|
if (Options.IsValidIndex(SelectedIndex))
|
||||||
|
return Options[SelectedIndex];
|
||||||
|
|
||||||
|
return FText();
|
||||||
|
}
|
||||||
|
|
||||||
|
EInputMode UOptionWidgetBase::GetCurrentInputMode() const
|
||||||
|
{
|
||||||
|
auto GS = GetStevesGameSubsystem(GetWorld());
|
||||||
|
if (GS)
|
||||||
|
return GS->GetLastInputModeUsed();
|
||||||
|
|
||||||
|
return EInputMode::Unknown;
|
||||||
|
}
|
||||||
|
|
75
Source/StevesUEHelpers/Private/StevesUI/SFocusableButton.cpp
Normal file
75
Source/StevesUEHelpers/Private/StevesUI/SFocusableButton.cpp
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
#include "StevesUI/SFocusableButton.h"
|
||||||
|
|
||||||
|
void SFocusableButton::Construct(const FArguments& InArgs)
|
||||||
|
{
|
||||||
|
// Call SButton construct and pass through
|
||||||
|
SButton::Construct(SButton::FArguments()
|
||||||
|
.ButtonStyle(InArgs._ButtonStyle)
|
||||||
|
.ClickMethod(InArgs._ClickMethod)
|
||||||
|
.ContentPadding(InArgs._ContentPadding)
|
||||||
|
.ContentScale(InArgs._ContentScale)
|
||||||
|
.FlowDirectionPreference(InArgs._FlowDirectionPreference)
|
||||||
|
.ForegroundColor(InArgs._ForegroundColor)
|
||||||
|
.HAlign(InArgs._HAlign)
|
||||||
|
.VAlign(InArgs._VAlign)
|
||||||
|
.IsFocusable(InArgs._IsFocusable)
|
||||||
|
.OnClicked(InArgs._OnClicked)
|
||||||
|
.OnHovered(InArgs._OnHovered)
|
||||||
|
.OnPressed(InArgs._OnPressed)
|
||||||
|
.OnReleased(InArgs._OnReleased)
|
||||||
|
.OnUnhovered(InArgs._OnUnhovered)
|
||||||
|
.PressMethod(InArgs._PressMethod)
|
||||||
|
.TouchMethod(InArgs._TouchMethod)
|
||||||
|
.DesiredSizeScale(InArgs._DesiredSizeScale)
|
||||||
|
.HoveredSoundOverride(InArgs._HoveredSoundOverride)
|
||||||
|
.PressedSoundOverride(InArgs._PressedSoundOverride)
|
||||||
|
.ButtonColorAndOpacity(InArgs._ButtonColorAndOpacity)
|
||||||
|
.TextFlowDirection(InArgs._TextFlowDirection)
|
||||||
|
.TextShapingMethod(InArgs._TextShapingMethod)
|
||||||
|
.Clipping(InArgs._Clipping)
|
||||||
|
.Cursor(InArgs._Cursor)
|
||||||
|
.Tag(InArgs._Tag)
|
||||||
|
.Visibility(InArgs._Visibility)
|
||||||
|
.AccessibleParams(InArgs._AccessibleParams)
|
||||||
|
.AccessibleText(InArgs._AccessibleText)
|
||||||
|
.ForceVolatile(InArgs._ForceVolatile)
|
||||||
|
.IsEnabled(InArgs._IsEnabled)
|
||||||
|
.RenderOpacity(InArgs._RenderOpacity)
|
||||||
|
.RenderTransform(InArgs._RenderTransform)
|
||||||
|
.RenderTransformPivot(InArgs._RenderTransformPivot)
|
||||||
|
.Text(InArgs._Text)
|
||||||
|
.TextStyle(InArgs._TextStyle)
|
||||||
|
.ToolTip(InArgs._ToolTip)
|
||||||
|
.ToolTipText(InArgs._ToolTipText)
|
||||||
|
);
|
||||||
|
|
||||||
|
OnFocusReceivedDelegate = InArgs._OnFocusReceived;
|
||||||
|
OnFocusLostDelegate = InArgs._OnFocusLost;
|
||||||
|
}
|
||||||
|
|
||||||
|
FReply SFocusableButton::OnFocusReceived(const FGeometry& MyGeometry, const FFocusEvent& InFocusEvent)
|
||||||
|
{
|
||||||
|
auto Ret = SButton::OnFocusReceived(MyGeometry, InFocusEvent);
|
||||||
|
|
||||||
|
OnFocusReceivedDelegate.ExecuteIfBound();
|
||||||
|
|
||||||
|
return Ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SFocusableButton::OnFocusLost(const FFocusEvent& InFocusEvent)
|
||||||
|
{
|
||||||
|
SButton::OnFocusLost(InFocusEvent);
|
||||||
|
|
||||||
|
OnFocusLostDelegate.ExecuteIfBound();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SFocusableButton::SetOnFocusReceived(FSimpleDelegate InOnFocusReceived)
|
||||||
|
{
|
||||||
|
OnFocusReceivedDelegate = InOnFocusReceived;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SFocusableButton::SetOnFocusLost(FSimpleDelegate InOnFocusLost)
|
||||||
|
{
|
||||||
|
OnFocusLostDelegate = InOnFocusLost;
|
||||||
|
}
|
||||||
|
|
116
Source/StevesUEHelpers/Private/StevesUI/SFocusableButton.h
Normal file
116
Source/StevesUEHelpers/Private/StevesUI/SFocusableButton.h
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "Widgets/Input/SButton.h"
|
||||||
|
|
||||||
|
class SFocusableButton : public SButton
|
||||||
|
{
|
||||||
|
// Seems in slate we have to duplicate all the args from superclass
|
||||||
|
SLATE_BEGIN_ARGS(SFocusableButton)
|
||||||
|
: _Content()
|
||||||
|
, _ButtonStyle( &FCoreStyle::Get().GetWidgetStyle< FButtonStyle >( "Button" ) )
|
||||||
|
, _TextStyle( &FCoreStyle::Get().GetWidgetStyle< FTextBlockStyle >("NormalText") )
|
||||||
|
, _HAlign( HAlign_Fill )
|
||||||
|
, _VAlign( VAlign_Fill )
|
||||||
|
, _ContentPadding(FMargin(4.0, 2.0))
|
||||||
|
, _Text()
|
||||||
|
, _ClickMethod( EButtonClickMethod::DownAndUp )
|
||||||
|
, _TouchMethod( EButtonTouchMethod::DownAndUp )
|
||||||
|
, _PressMethod( EButtonPressMethod::DownAndUp )
|
||||||
|
, _DesiredSizeScale( FVector2D(1,1) )
|
||||||
|
, _ContentScale( FVector2D(1,1) )
|
||||||
|
, _ButtonColorAndOpacity(FLinearColor::White)
|
||||||
|
, _ForegroundColor( FCoreStyle::Get().GetSlateColor( "InvertedForeground" ) )
|
||||||
|
, _IsFocusable( true )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Slot for this button's content (optional) */
|
||||||
|
SLATE_DEFAULT_SLOT( FArguments, Content )
|
||||||
|
|
||||||
|
/** The visual style of the button */
|
||||||
|
SLATE_STYLE_ARGUMENT( FButtonStyle, ButtonStyle )
|
||||||
|
|
||||||
|
/** The text style of the button */
|
||||||
|
SLATE_STYLE_ARGUMENT( FTextBlockStyle, TextStyle )
|
||||||
|
|
||||||
|
/** Horizontal alignment */
|
||||||
|
SLATE_ARGUMENT( EHorizontalAlignment, HAlign )
|
||||||
|
|
||||||
|
/** Vertical alignment */
|
||||||
|
SLATE_ARGUMENT( EVerticalAlignment, VAlign )
|
||||||
|
|
||||||
|
/** Spacing between button's border and the content. */
|
||||||
|
SLATE_ATTRIBUTE( FMargin, ContentPadding )
|
||||||
|
|
||||||
|
/** The text to display in this button, if no custom content is specified */
|
||||||
|
SLATE_ATTRIBUTE( FText, Text )
|
||||||
|
|
||||||
|
/** Called when the button is clicked */
|
||||||
|
SLATE_EVENT( FOnClicked, OnClicked )
|
||||||
|
|
||||||
|
/** Called when the button is pressed */
|
||||||
|
SLATE_EVENT( FSimpleDelegate, OnPressed )
|
||||||
|
|
||||||
|
/** Called when the button is released */
|
||||||
|
SLATE_EVENT( FSimpleDelegate, OnReleased )
|
||||||
|
|
||||||
|
SLATE_EVENT( FSimpleDelegate, OnHovered )
|
||||||
|
|
||||||
|
SLATE_EVENT( FSimpleDelegate, OnUnhovered )
|
||||||
|
|
||||||
|
/** Sets the rules to use for determining whether the button was clicked. This is an advanced setting and generally should be left as the default. */
|
||||||
|
SLATE_ARGUMENT( EButtonClickMethod::Type, ClickMethod )
|
||||||
|
|
||||||
|
/** How should the button be clicked with touch events? */
|
||||||
|
SLATE_ARGUMENT( EButtonTouchMethod::Type, TouchMethod )
|
||||||
|
|
||||||
|
/** How should the button be clicked with keyboard/controller button events? */
|
||||||
|
SLATE_ARGUMENT( EButtonPressMethod::Type, PressMethod )
|
||||||
|
|
||||||
|
SLATE_ATTRIBUTE( FVector2D, DesiredSizeScale )
|
||||||
|
|
||||||
|
SLATE_ATTRIBUTE( FVector2D, ContentScale )
|
||||||
|
|
||||||
|
SLATE_ATTRIBUTE( FSlateColor, ButtonColorAndOpacity )
|
||||||
|
|
||||||
|
SLATE_ATTRIBUTE( FSlateColor, ForegroundColor )
|
||||||
|
|
||||||
|
/** Sometimes a button should only be mouse-clickable and never keyboard focusable. */
|
||||||
|
SLATE_ARGUMENT( bool, IsFocusable )
|
||||||
|
|
||||||
|
/** The sound to play when the button is pressed */
|
||||||
|
SLATE_ARGUMENT( TOptional<FSlateSound>, PressedSoundOverride )
|
||||||
|
|
||||||
|
/** The sound to play when the button is hovered */
|
||||||
|
SLATE_ARGUMENT( TOptional<FSlateSound>, HoveredSoundOverride )
|
||||||
|
|
||||||
|
/** Which text shaping method should we use? (unset to use the default returned by GetDefaultTextShapingMethod) */
|
||||||
|
SLATE_ARGUMENT( TOptional<ETextShapingMethod>, TextShapingMethod )
|
||||||
|
|
||||||
|
/** Which text flow direction should we use? (unset to use the default returned by GetDefaultTextFlowDirection) */
|
||||||
|
SLATE_ARGUMENT( TOptional<ETextFlowDirection>, TextFlowDirection )
|
||||||
|
|
||||||
|
// This is the bit we're adding
|
||||||
|
SLATE_EVENT( FSimpleDelegate, OnFocusReceived )
|
||||||
|
SLATE_EVENT( FSimpleDelegate, OnFocusLost )
|
||||||
|
|
||||||
|
SLATE_END_ARGS()
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
void Construct( const FArguments& InArgs );
|
||||||
|
|
||||||
|
virtual FReply OnFocusReceived(const FGeometry& MyGeometry, const FFocusEvent& InFocusEvent) override;
|
||||||
|
virtual void OnFocusLost(const FFocusEvent& InFocusEvent) override;
|
||||||
|
|
||||||
|
void SetOnFocusReceived(FSimpleDelegate InOnFocusReceived);
|
||||||
|
void SetOnFocusLost(FSimpleDelegate InOnFocusLost);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
FSimpleDelegate OnFocusReceivedDelegate;
|
||||||
|
FSimpleDelegate OnFocusLostDelegate;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
51
Source/StevesUEHelpers/Private/StevesUI/StevesUI.cpp
Normal file
51
Source/StevesUEHelpers/Private/StevesUI/StevesUI.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include "StevesUI.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include "StevesUI/FocusableUserWidget.h"
|
||||||
|
#include "Blueprint/WidgetTree.h"
|
||||||
|
#include "Components/PanelWidget.h"
|
||||||
|
#include "Components/Widget.h"
|
||||||
|
|
||||||
|
DEFINE_LOG_CATEGORY(LogCustomUI);
|
||||||
|
|
||||||
|
UWidget* FindWidgetFromSlate(SWidget* SW, UWidget* Parent)
|
||||||
|
{
|
||||||
|
if (!Parent)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
if (Parent->GetCachedWidget().Get() == SW)
|
||||||
|
return Parent;
|
||||||
|
|
||||||
|
auto PW = Cast<UPanelWidget>(Parent);
|
||||||
|
if (PW)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < PW->GetChildrenCount(); ++i)
|
||||||
|
{
|
||||||
|
const auto Found = FindWidgetFromSlate(SW, PW->GetChildAt(i));
|
||||||
|
if (Found)
|
||||||
|
return Found;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// User widgets aren't panels but can have their own tree
|
||||||
|
auto UW = Cast<UUserWidget>(Parent);
|
||||||
|
if (UW)
|
||||||
|
{
|
||||||
|
return FindWidgetFromSlate(SW, UW->WidgetTree->RootWidget);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetWidgetFocusProperly(UWidget* Widget)
|
||||||
|
{
|
||||||
|
auto FW = Cast<UFocusableUserWidget>(Widget);
|
||||||
|
if (FW)
|
||||||
|
FW->SetFocusProperly();
|
||||||
|
else
|
||||||
|
Widget->SetFocus();
|
||||||
|
|
||||||
|
}
|
26
Source/StevesUEHelpers/Private/StevesUI/StevesUI.h
Normal file
26
Source/StevesUEHelpers/Private/StevesUI/StevesUI.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
|
||||||
|
class UWidget;
|
||||||
|
class SWidget;
|
||||||
|
|
||||||
|
DECLARE_LOG_CATEGORY_EXTERN(LogCustomUI, Warning, Warning)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Tries to locate a UMG widget which is using the speficied Slate widget as its native implementation
|
||||||
|
* @param SW Slate widget
|
||||||
|
* @param Parent Parent widget under which the UMG widget should be found
|
||||||
|
* @return The UMG widget if found, otherwise nullptr
|
||||||
|
*/
|
||||||
|
UWidget* FindWidgetFromSlate(SWidget* SW, UWidget* Parent);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set the focus to a given widget "properly", which means that if this is a widget derived
|
||||||
|
* from UFocusableWidget, it calls SetFocusProperly on it which allows a customised implementation.
|
||||||
|
* This is done because SetFocus() is not virtual and cannot be changed, and bIsFocusable seems to get
|
||||||
|
* reset by Slate code even if I try to set it and then override Native methods.
|
||||||
|
* @param Widget A UWidget
|
||||||
|
*/
|
||||||
|
void SetWidgetFocusProperly(UWidget* Widget);
|
123
Source/StevesUEHelpers/Public/StevesGameSubsystem.h
Normal file
123
Source/StevesUEHelpers/Public/StevesGameSubsystem.h
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "Subsystems/GameInstanceSubsystem.h"
|
||||||
|
#include "InputCoreTypes.h"
|
||||||
|
#include "Framework/Application/IInputProcessor.h"
|
||||||
|
#include "StevesHelperCommon.h"
|
||||||
|
#include "StevesUI/UiTheme.h"
|
||||||
|
|
||||||
|
#include "StevesGameSubsystem.generated.h"
|
||||||
|
|
||||||
|
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnInputModeChanged, int, PlayerIndex, EInputMode, InputMode);
|
||||||
|
|
||||||
|
UCLASS(Config=Game)
|
||||||
|
class STEVESUEHELPERS_API UStevesGameSubsystem : public UGameInstanceSubsystem
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// The default UiTheme path, the theme to use if controls don't specifically link to one.
|
||||||
|
/// Customise this in DefaultGame.ini
|
||||||
|
/// [/Script/StevesUEHelpers.StevesGameSubsystem]
|
||||||
|
/// DefaultUiThemePath="/Game/Some/Other/UiTheme.UiTheme"
|
||||||
|
/// Regardless, remember to register this file as a Primary Asset in Project Settings,
|
||||||
|
/// as described in https://docs.unrealengine.com/en-US/Engine/Basics/AssetsAndPackages/AssetManagement/index.html
|
||||||
|
/// so that it's included when packaging
|
||||||
|
UPROPERTY(Config)
|
||||||
|
FString DefaultUiThemePath;
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
|
||||||
|
virtual void Deinitialize() override;
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
DECLARE_DELEGATE_TwoParams(FInternalInputModeChanged, int /* PlayerIndex */, EInputMode)
|
||||||
|
/**
|
||||||
|
* We seem to need a separate non-UObject for an IInputProcessor, my attempt to combine the 2 fails.
|
||||||
|
* So this class just acts as a safe relay between Slate and UInputHelper
|
||||||
|
*
|
||||||
|
* This class should be registered as an input processor in order to capture all input events & detect
|
||||||
|
* what kind of devices are being used. We can't use PlayerController to do this reliably because in UMG
|
||||||
|
* mode, all the mouse move events are consumed by Slate and you never see them, so it's not possible to
|
||||||
|
* detect when the user moved a mouse.
|
||||||
|
*
|
||||||
|
* This class should be instantiated and used from some UObject of your choice, e.g. your GameInstance class,
|
||||||
|
* something like this:
|
||||||
|
*
|
||||||
|
* InputDetector = MakeShareable(new FInputModeDetector());
|
||||||
|
* FSlateApplication::Get().RegisterInputPreProcessor(InputDetector);
|
||||||
|
* InputDetector->OnInputModeChanged.BindUObject(this, &UMyGameInstance::OnInputDetectorModeChanged);
|
||||||
|
*
|
||||||
|
* Note how the OnInputModeChanged on this object is a simple delegate, not a dynamic multicast etc, because
|
||||||
|
* this is not a UObject. You should relay the input mode event changed through the owner if you want to distribute
|
||||||
|
* the information further.
|
||||||
|
*/
|
||||||
|
class FInputModeDetector : public IInputProcessor, public TSharedFromThis<FInputModeDetector>
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
TArray<EInputMode> LastInputModeByPlayer;
|
||||||
|
|
||||||
|
const EInputMode DefaultInputMode = EInputMode::Mouse;
|
||||||
|
const float MouseMoveThreshold = 1;
|
||||||
|
const float GamepadAxisThreshold = 0.2;
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Single delegate caller, owner should propagate if they want (this isn't a UObject)
|
||||||
|
FInternalInputModeChanged OnInputModeChanged;
|
||||||
|
|
||||||
|
|
||||||
|
FInputModeDetector();
|
||||||
|
|
||||||
|
virtual bool HandleKeyDownEvent(FSlateApplication& SlateApp, const FKeyEvent& InKeyEvent) override;
|
||||||
|
virtual bool
|
||||||
|
HandleAnalogInputEvent(FSlateApplication& SlateApp, const FAnalogInputEvent& InAnalogInputEvent) override;
|
||||||
|
virtual bool HandleMouseMoveEvent(FSlateApplication& SlateApp, const FPointerEvent& MouseEvent) override;
|
||||||
|
virtual bool HandleMouseButtonDownEvent(FSlateApplication& SlateApp, const FPointerEvent& MouseEvent) override;
|
||||||
|
virtual bool HandleMouseWheelOrGestureEvent(FSlateApplication& SlateApp, const FPointerEvent& InWheelEvent,
|
||||||
|
const FPointerEvent* InGestureEvent) override;
|
||||||
|
|
||||||
|
EInputMode GetLastInputMode(int PlayerIndex = 0);
|
||||||
|
|
||||||
|
// Needed but unused
|
||||||
|
virtual void Tick(const float DeltaTime, FSlateApplication& SlateApp, TSharedRef<ICursor> Cursor) override {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void ProcessKeyOrButton(int PlayerIndex, FKey Key);
|
||||||
|
void SetMode(int PlayerIndex, EInputMode NewMode);
|
||||||
|
};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
TSharedPtr<FInputModeDetector> InputDetector;
|
||||||
|
|
||||||
|
UPROPERTY(BlueprintReadWrite)
|
||||||
|
UUiTheme* DefaultUiTheme;
|
||||||
|
|
||||||
|
void CreateInputDetector();
|
||||||
|
void DestroyInputDetector();
|
||||||
|
void InitTheme();
|
||||||
|
|
||||||
|
// Called by detector
|
||||||
|
void OnInputDetectorModeChanged(int PlayerIndex, EInputMode NewMode);
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Event raised when input mode changed between gamepad and keyboard / mouse
|
||||||
|
UPROPERTY(BlueprintAssignable)
|
||||||
|
FOnInputModeChanged OnInputModeChanged;
|
||||||
|
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
EInputMode GetLastInputModeUsed(int PlayerIndex = 0) const { return InputDetector->GetLastInputMode(PlayerIndex); }
|
||||||
|
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
bool LastInputWasGamePad(int PlayerIndex = 0) const { return GetLastInputModeUsed(PlayerIndex) == EInputMode::Gamepad; }
|
||||||
|
|
||||||
|
/// Gets the default UI theme object (defaults to our own)
|
||||||
|
/// You can override this if you want
|
||||||
|
UUiTheme* GetDefaultUiTheme() { return DefaultUiTheme; };
|
||||||
|
|
||||||
|
/// Changes the default theme to a different one
|
||||||
|
void SetDefaultUiTheme(UUiTheme* NewTheme) { DefaultUiTheme = NewTheme; }
|
||||||
|
};
|
24
Source/StevesUEHelpers/Public/StevesGameViewportClientBase.h
Normal file
24
Source/StevesUEHelpers/Public/StevesGameViewportClientBase.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "Engine/GameViewportClient.h"
|
||||||
|
|
||||||
|
#include "StevesGameViewportClientBase.generated.h"
|
||||||
|
|
||||||
|
UCLASS()
|
||||||
|
class STEVESUEHELPERS_API UStevesGameViewportClientBase : public UGameViewportClient
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool bSuppressMouseCursor;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
|
||||||
|
virtual void Init(FWorldContext& WorldContext, UGameInstance* OwningGameInstance,
|
||||||
|
bool bCreateNewAudioDevice) override;
|
||||||
|
virtual EMouseCursor::Type GetCursor(FViewport* Viewport, int32 X, int32 Y) override;
|
||||||
|
|
||||||
|
virtual void SetSuppressMouseCursor(bool bSuppress);
|
||||||
|
};
|
12
Source/StevesUEHelpers/Public/StevesHelperCommon.h
Normal file
12
Source/StevesUEHelpers/Public/StevesHelperCommon.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "UObject/ObjectMacros.h"
|
||||||
|
|
||||||
|
UENUM(BlueprintType)
|
||||||
|
enum class EInputMode : uint8
|
||||||
|
{
|
||||||
|
Mouse,
|
||||||
|
Keyboard,
|
||||||
|
Gamepad,
|
||||||
|
Unknown
|
||||||
|
};
|
||||||
|
|
29
Source/StevesUEHelpers/Public/StevesUEHelpers.h
Normal file
29
Source/StevesUEHelpers/Public/StevesUEHelpers.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include "StevesGameSubsystem.h"
|
||||||
|
#include "Modules/ModuleManager.h"
|
||||||
|
#include "Engine/World.h"
|
||||||
|
|
||||||
|
DECLARE_LOG_CATEGORY_EXTERN(LogStevesUEHelpers, Verbose, Verbose);
|
||||||
|
|
||||||
|
class FStevesUEHelpers : public IModuleInterface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/** IModuleInterface implementation */
|
||||||
|
virtual void StartupModule() override;
|
||||||
|
virtual void ShutdownModule() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline UStevesGameSubsystem* GetStevesGameSubsystem(UWorld* WorldContext)
|
||||||
|
{
|
||||||
|
if (IsValid(WorldContext) && IsValid(WorldContext->GetGameInstance()))
|
||||||
|
{
|
||||||
|
return WorldContext->GetGameInstance()->GetSubsystem<UStevesGameSubsystem>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
54
Source/StevesUEHelpers/Public/StevesUI/FocusableButton.h
Normal file
54
Source/StevesUEHelpers/Public/StevesUI/FocusableButton.h
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "Components/Button.h"
|
||||||
|
#include "UObject/ObjectMacros.h"
|
||||||
|
#include "Styling/SlateTypes.h"
|
||||||
|
#include "Widgets/SWidget.h"
|
||||||
|
|
||||||
|
#include "FocusableButton.generated.h"
|
||||||
|
|
||||||
|
class SFocusableButton;
|
||||||
|
|
||||||
|
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnButtonFocusReceivedEvent);
|
||||||
|
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnButtonFocusLostEvent);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a simple subclass of UButton to provide some missing features
|
||||||
|
*
|
||||||
|
* * Focus events
|
||||||
|
* * Focus style based on hover style
|
||||||
|
* * Assign focus to self on hover to prevent double-highlighting
|
||||||
|
*/
|
||||||
|
UCLASS()
|
||||||
|
class STEVESUEHELPERS_API UFocusableButton : public UButton
|
||||||
|
{
|
||||||
|
GENERATED_UCLASS_BODY()
|
||||||
|
|
||||||
|
public:
|
||||||
|
UPROPERTY(BlueprintReadWrite, EditAnywhere)
|
||||||
|
bool bUseHoverStyleWhenFocussed = true;
|
||||||
|
|
||||||
|
UPROPERTY(BlueprintAssignable, Category="Button|Event")
|
||||||
|
FOnButtonFocusReceivedEvent OnFocusReceived;
|
||||||
|
|
||||||
|
UPROPERTY(BlueprintAssignable, Category="Button|Event")
|
||||||
|
FOnButtonFocusLostEvent OnFocusLost;
|
||||||
|
|
||||||
|
UPROPERTY(BlueprintReadWrite, EditAnywhere)
|
||||||
|
bool bTakeFocusOnHover = true;
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
FButtonStyle FocussedStyle;
|
||||||
|
|
||||||
|
void SlateHandleFocusReceived();
|
||||||
|
void SlateHandleFocusLost();
|
||||||
|
|
||||||
|
void ApplyFocusStyle();
|
||||||
|
void UndoFocusStyle();
|
||||||
|
void SlateHandleHovered();
|
||||||
|
void SlateHandleUnhovered();
|
||||||
|
|
||||||
|
virtual TSharedRef<SWidget> RebuildWidget() override;
|
||||||
|
};
|
62
Source/StevesUEHelpers/Public/StevesUI/FocusablePanel.h
Normal file
62
Source/StevesUEHelpers/Public/StevesUI/FocusablePanel.h
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "FocusableUserWidget.h"
|
||||||
|
|
||||||
|
#include "FocusablePanel.generated.h"
|
||||||
|
|
||||||
|
/// Base class for a UI Panel which has the concept of having focus, delegated to one of its children.
|
||||||
|
/// When told, it can initialise focus to a default widget. It can also remember which of its children
|
||||||
|
/// are currently focussed and restore that later.
|
||||||
|
/// Calling SetFocusProperly does the default behaviour of preferring previous but falling back on the default.
|
||||||
|
UCLASS()
|
||||||
|
class STEVESUEHELPERS_API UFocusablePanel : public UFocusableUserWidget
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// The name of the widget which will be initially focussed by default
|
||||||
|
/// This is a name because we can't link directly at edit time
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite)
|
||||||
|
FName InitialFocusWidgetName;
|
||||||
|
|
||||||
|
// I'd love to make the above a drop-down but it's a lot of faff
|
||||||
|
// See Engine\Source\Editor\UMGEditor\Private\Customizations\WidgetNavigationCustomization.cpp
|
||||||
|
// Specifically OnGenerateWidgetList
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set the current focus to the initial focus widget
|
||||||
|
* @return Whether the focus was successfully set
|
||||||
|
*/
|
||||||
|
bool SetFocusToInitialWidget() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Try to restore focus to the previously focussed child
|
||||||
|
* @return Whether the focus was successfully set
|
||||||
|
*/
|
||||||
|
bool RestorePreviousFocus() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Try to save the currently focussed child as something that can be restored later.
|
||||||
|
* @return Whether focus was saved
|
||||||
|
*/
|
||||||
|
bool SavePreviousFocus();
|
||||||
|
|
||||||
|
|
||||||
|
/// When SetFocusProperly is called, either restores previous selection or gives it to the initial selection
|
||||||
|
virtual void SetFocusProperly_Implementation() override;
|
||||||
|
protected:
|
||||||
|
|
||||||
|
/// The widget that should get the focus on init if in keyboard / gamepad mode
|
||||||
|
/// Looked up at runtime from the FName
|
||||||
|
TWeakObjectPtr<UWidget> InitialFocusWidget;
|
||||||
|
|
||||||
|
/// Previously focussed child which can be restored
|
||||||
|
TWeakObjectPtr<UWidget> PreviousFocusWidget;
|
||||||
|
|
||||||
|
virtual void NativeConstruct() override;
|
||||||
|
virtual void NativeDestruct() override;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
20
Source/StevesUEHelpers/Public/StevesUI/FocusableUserWidget.h
Normal file
20
Source/StevesUEHelpers/Public/StevesUI/FocusableUserWidget.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "Blueprint/UserWidget.h"
|
||||||
|
|
||||||
|
#include "FocusableUserWidget.generated.h"
|
||||||
|
|
||||||
|
// Hacky intermediate type for UUserWidget so that we can have focusable child widgets via SetFocusProperly
|
||||||
|
UCLASS()
|
||||||
|
class STEVESUEHELPERS_API UFocusableUserWidget : public UUserWidget
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// UWidget::SetFocus is not virtual FFS. This does the same as SetFocus by default but can be overridden,
|
||||||
|
/// e.g. to delegate focus to specific children
|
||||||
|
UFUNCTION(BlueprintNativeEvent)
|
||||||
|
void SetFocusProperly();
|
||||||
|
|
||||||
|
};
|
93
Source/StevesUEHelpers/Public/StevesUI/InputImage.h
Normal file
93
Source/StevesUEHelpers/Public/StevesUI/InputImage.h
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
|
||||||
|
#include "UiTheme.h"
|
||||||
|
#include "Components/Image.h"
|
||||||
|
#include "StevesHelperCommon.h"
|
||||||
|
#include "InputImage.generated.h"
|
||||||
|
|
||||||
|
UENUM(BlueprintType)
|
||||||
|
enum class EInputBindingType : uint8
|
||||||
|
{
|
||||||
|
/// A button action, will be looked up based on input mappings
|
||||||
|
Action = 0,
|
||||||
|
/// An axis action, will be looked up based on input mappings
|
||||||
|
Axis = 1,
|
||||||
|
/// A manually specified FKey (which can be key, button, axis)
|
||||||
|
Key = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// A special widget containing an image which populates itself based on an input action / axis and can dynamically
|
||||||
|
/// change based on the active input method.
|
||||||
|
UCLASS()
|
||||||
|
class STEVESUEHELPERS_API UInputImage : public UImage
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// What type of an input binding this image should look up
|
||||||
|
UPROPERTY(EditAnywhere)
|
||||||
|
EInputBindingType BindingType;
|
||||||
|
|
||||||
|
/// If BindingType is Action/Axis, the name of it
|
||||||
|
UPROPERTY(EditAnywhere)
|
||||||
|
FName ActionOrAxisName;
|
||||||
|
|
||||||
|
/// If BindingType is Key, the key
|
||||||
|
UPROPERTY(EditAnywhere)
|
||||||
|
FKey Key;
|
||||||
|
|
||||||
|
/// Custom theme to use for this input image set; if not supplied will use UStevesGameSubsystem::DefaultUiTheme
|
||||||
|
UPROPERTY(EditAnywhere)
|
||||||
|
UUiTheme* CustomTheme;
|
||||||
|
|
||||||
|
EInputMode CurrentInputMode;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Tell this image to display the bound action for the current input method
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual void SetFromAction(FName Name);
|
||||||
|
|
||||||
|
/// Tell this image to display the bound axis for the current input method
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual void SetFromAxis(FName Name);
|
||||||
|
|
||||||
|
/// Tell this image to display a specific key image
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual void SetFromKey(FKey K);
|
||||||
|
|
||||||
|
/// Get the binding type that we'll use to populate the image
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual EInputBindingType GetBindingType() const { return BindingType; }
|
||||||
|
|
||||||
|
/// If BindingType is Action/Axis, get the name of the action or axis to look up the image for
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual FName GetActionOrAxisName() const { return ActionOrAxisName; };
|
||||||
|
|
||||||
|
/// If BindingType is Key, get the key
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual FKey GetKey() const { return Key; }
|
||||||
|
|
||||||
|
/// Get the custom theme, if any
|
||||||
|
virtual UUiTheme* GetCustomTheme() const { return CustomTheme; }
|
||||||
|
/// Change the custom theme for this image
|
||||||
|
virtual void SetCustomTheme(UUiTheme* Theme);
|
||||||
|
|
||||||
|
virtual void BeginDestroy() override;
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual TSharedRef<SWidget> RebuildWidget() override;
|
||||||
|
void UpdateImageFromAction(const FName& Name);
|
||||||
|
void UpdateImageFromAxis(const FName& Name);
|
||||||
|
void UpdateImageFromTable(const FKey& Key, const TSoftObjectPtr<UDataTable>& Asset);
|
||||||
|
void UpdateImageFromKey(const FKey& Key);
|
||||||
|
virtual void UpdateImage();
|
||||||
|
virtual UUiTheme* GetTheme();
|
||||||
|
|
||||||
|
UFUNCTION()
|
||||||
|
void OnInputModeChanged(int PlayerIndex, EInputMode InputMode);
|
||||||
|
|
||||||
|
};
|
33
Source/StevesUEHelpers/Public/StevesUI/KeySprite.h
Normal file
33
Source/StevesUEHelpers/Public/StevesUI/KeySprite.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
|
||||||
|
#include "InputCoreTypes.h"
|
||||||
|
#include "Engine/DataTable.h"
|
||||||
|
#include "PaperSprite.h"
|
||||||
|
#include "KeySprite.generated.h"
|
||||||
|
|
||||||
|
/// Struct for the rows of a DataTable which will hold the mapping from an FKey to a Paper Sprite which represents it
|
||||||
|
/// Used for on-screen prompts
|
||||||
|
USTRUCT(BlueprintType)
|
||||||
|
struct STEVESUEHELPERS_API FKeySprite : public FTableRowBase
|
||||||
|
{
|
||||||
|
GENERATED_USTRUCT_BODY()
|
||||||
|
|
||||||
|
// Import a DataTable using this struct by creating a CSV file like this:
|
||||||
|
//
|
||||||
|
// Name,Key,Sprite
|
||||||
|
// 1,Enter,"PaperSprite'/Game/Textures/UI/Frames/Keyboard_Black_Enter'"
|
||||||
|
// 2,SpaceBar,"PaperSprite'/Game/Textures/UI/Frames/Keyboard_Black_Space'"
|
||||||
|
//
|
||||||
|
// Key is just the latter part of EKeys::Name
|
||||||
|
// Sprite is the path to the Paper2D sprite (most likely from a shared sprite sheet)
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly)
|
||||||
|
FKey Key;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly)
|
||||||
|
UPaperSprite* Sprite;
|
||||||
|
};
|
59
Source/StevesUEHelpers/Public/StevesUI/MenuBase.h
Normal file
59
Source/StevesUEHelpers/Public/StevesUI/MenuBase.h
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
|
||||||
|
#include "MenuStack.h"
|
||||||
|
#include "FocusablePanel.h"
|
||||||
|
#include "Blueprint/UserWidget.h"
|
||||||
|
|
||||||
|
#include "MenuBase.generated.h"
|
||||||
|
|
||||||
|
|
||||||
|
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnMenuClosed, UMenuBase*, Menu, bool, bWasCancelled);
|
||||||
|
/// This class is one element of the UUiMenuStack and represents one level in the chain of
|
||||||
|
/// an assumed modal stack. It is responsible for implementing how it gets added
|
||||||
|
/// to the viewport or parent container, and removed again, and what it does when receiving
|
||||||
|
/// focus.
|
||||||
|
UCLASS(Abstract, BlueprintType)
|
||||||
|
class STEVESUEHELPERS_API UMenuBase : public UFocusablePanel
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
public:
|
||||||
|
UPROPERTY(BlueprintAssignable)
|
||||||
|
FOnMenuClosed OnClosed;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
UPROPERTY(BlueprintReadOnly)
|
||||||
|
TWeakObjectPtr<UMenuStack> ParentStack;
|
||||||
|
|
||||||
|
/// Whether this menu should request focus when it is displayed
|
||||||
|
/// The widget which is focussed will either be the InitialFocusWidget on newly displayed, or
|
||||||
|
/// the previously selected widget if regaining focus
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite)
|
||||||
|
bool bRequestFocus = true;
|
||||||
|
|
||||||
|
/// Set this property to true if you want this menu to embed itself in the parent UUiMenuStack's MenuContainer
|
||||||
|
/// If false, this Menu will be added to the viewport independently and can float over other menus in the stack
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite)
|
||||||
|
bool bEmbedInParentContainer = true;
|
||||||
|
|
||||||
|
/// Whether to hide this menu when it's superceded by another in the stack. This property is only relevant
|
||||||
|
/// when bEmbedInParentContainer = false, since only one menu can be embedded at once.
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite)
|
||||||
|
bool bHideWhenSuperceded = true;
|
||||||
|
|
||||||
|
void EmbedInParent();
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// Close this menu level
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
void Close(bool bWasCancel);
|
||||||
|
|
||||||
|
TWeakObjectPtr<UMenuStack> GetParentStack() const { return ParentStack; };
|
||||||
|
|
||||||
|
void AddedToStack(UMenuStack* Parent);
|
||||||
|
void RemovedFromStack(UMenuStack* Parent);
|
||||||
|
void SupercededInStack();
|
||||||
|
void RegainedFocusInStack();
|
||||||
|
void InputModeChanged(EInputMode OldMode, EInputMode NewMode);
|
||||||
|
};
|
100
Source/StevesUEHelpers/Public/StevesUI/MenuStack.h
Normal file
100
Source/StevesUEHelpers/Public/StevesUI/MenuStack.h
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
|
||||||
|
#include "FocusableUserWidget.h"
|
||||||
|
#include "Framework/Application/IInputProcessor.h"
|
||||||
|
#include "StevesHelperCommon.h"
|
||||||
|
#include "UObject/ObjectMacros.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include "MenuStack.generated.h"
|
||||||
|
|
||||||
|
class UContentWidget;
|
||||||
|
class UMenuBase;
|
||||||
|
|
||||||
|
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnMenuStackClosed, class UMenuStack*, Stack, bool, bWasCancel);
|
||||||
|
|
||||||
|
/// Represents a modal stack of menus which take focus and have a concept of "Back"
|
||||||
|
/// Each level is a MenuBase
|
||||||
|
/// You can style this widget to be the general surrounds in which all MenuBase levels live inside
|
||||||
|
/// Create a Blueprint subclass of this and make sure you include a UContentWidget with the name
|
||||||
|
/// "MenuContainer" somewhere in the tree, which is where the menu contents will be placed.
|
||||||
|
UCLASS(Abstract, BlueprintType)
|
||||||
|
class STEVESUEHELPERS_API UMenuStack : public UFocusableUserWidget
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
// Nested class which we'll use to poke into input events before anything else eats them
|
||||||
|
// Without this it seems impossible to pick up e.g. Gamepad "B" button with UMG up
|
||||||
|
// It means we hardcode the controls for menus but that's OK in practice
|
||||||
|
class FUiInputPreprocessor : public IInputProcessor, public TSharedFromThis<FUiInputPreprocessor>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DECLARE_DELEGATE_RetVal_OneParam(bool, FOnUiKeyDown, const FKeyEvent&);
|
||||||
|
FOnUiKeyDown OnUiKeyDown;
|
||||||
|
|
||||||
|
virtual bool HandleKeyDownEvent(FSlateApplication& SlateApp, const FKeyEvent& InKeyEvent) override
|
||||||
|
{
|
||||||
|
return OnUiKeyDown.Execute(InKeyEvent);
|
||||||
|
}
|
||||||
|
// Required by IInputProcessor but we don't need
|
||||||
|
virtual void Tick(const float DeltaTime, FSlateApplication& SlateApp, TSharedRef<ICursor> Crs) override{}
|
||||||
|
};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
EInputMode LastInputMode;
|
||||||
|
|
||||||
|
TArray<UMenuBase*> Menus;
|
||||||
|
|
||||||
|
TSharedPtr<FUiInputPreprocessor> InputPreprocessor;
|
||||||
|
|
||||||
|
void LastMenuClosed(bool bWasCancel);
|
||||||
|
|
||||||
|
virtual void NativeConstruct() override;
|
||||||
|
virtual void NativeDestruct() override;
|
||||||
|
UFUNCTION()
|
||||||
|
bool HandleKeyDownEvent(const FKeyEvent& InKeyEvent);
|
||||||
|
UFUNCTION()
|
||||||
|
void InputModeChanged(int PlayerIndex, EInputMode NewMode);
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// This property will bind to a blueprint variable of the same name to contain the actual menu content
|
||||||
|
/// If not set, or the UiMenuBase is set to not use this container, levels are added independently to viewport
|
||||||
|
/// Use a NamedSlot for this most of the time, it gives you the most layout flexibility.
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UContentWidget* MenuContainer;
|
||||||
|
|
||||||
|
/// Event raised when the stack is closed for any reason. If bWasCancel, the menu stack was closed because the
|
||||||
|
/// last item was cancelled.
|
||||||
|
UPROPERTY(BlueprintAssignable)
|
||||||
|
FOnMenuStackClosed OnClosed;
|
||||||
|
|
||||||
|
/// Push a new menu level by class. This will instantiate the new menu, display it, and inform the previous menu that it's
|
||||||
|
/// been superceded. Use the returned instance if you want to cache it
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
UMenuBase* PushMenuByClass(TSubclassOf<UMenuBase> MenuClass);
|
||||||
|
|
||||||
|
/// Push a new menu level by instance on to the stack. This will display the new menu and inform the previous menu that it's
|
||||||
|
/// been superceded, which will most likely mean it will be hidden (but will retain its state)
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
void PushMenuByObject(UMenuBase* NewMenu);
|
||||||
|
|
||||||
|
/// Pop the top level of the menu stack. This *destroys* the top level menu, meaning it will lose all of its state.
|
||||||
|
/// You won't need to call this manually most of the time, because calling Close() on the MenuBase will do it.
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
void PopMenu(bool bWasCancel);
|
||||||
|
|
||||||
|
/// Get the number of active levels in the menu
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
int Count() const { return Menus.Num(); }
|
||||||
|
|
||||||
|
/// Close the entire stack at once. This does not give any of the menus chance to do anything before close, so if you
|
||||||
|
/// want them to do that, use PopMenu() until Count() == 0 instead
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
void CloseAll(bool bWasCancel);
|
||||||
|
|
||||||
|
|
||||||
|
virtual void SetFocusProperly_Implementation() override;
|
||||||
|
void PopMenuIfTop(UMenuBase* UiMenuBase, bool bWasCancel);
|
||||||
|
};
|
150
Source/StevesUEHelpers/Public/StevesUI/OptionWidgetBase.h
Normal file
150
Source/StevesUEHelpers/Public/StevesUI/OptionWidgetBase.h
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "FocusableUserWidget.h"
|
||||||
|
#include "Styling/SlateTypes.h"
|
||||||
|
#include "StevesHelperCommon.h"
|
||||||
|
#include "OptionWidgetBase.generated.h"
|
||||||
|
|
||||||
|
class UTextBlock;
|
||||||
|
class UImage;
|
||||||
|
class UButton;
|
||||||
|
|
||||||
|
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnSelectedOptionChanged, class UOptionWidgetBase*, Widget, int, NewIndex);
|
||||||
|
|
||||||
|
UCLASS(Abstract, BlueprintType)
|
||||||
|
class STEVESUEHELPERS_API UOptionWidgetBase : public UFocusableUserWidget
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
// -- Properties we replicate to child widgets
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Appearance")
|
||||||
|
FButtonStyle ButtonStyle;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Appearance")
|
||||||
|
FSlateBrush ButtonImage;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Appearance")
|
||||||
|
FLinearColor ButtonImageColour = FLinearColor::White;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Appearance")
|
||||||
|
FSlateFontInfo Font;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Appearance")
|
||||||
|
FLinearColor TextColour = FLinearColor::White;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Appearance")
|
||||||
|
FSlateBrush TextStrikeBrush;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Appearance")
|
||||||
|
FVector2D TextShadowOffset;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Appearance")
|
||||||
|
FLinearColor TextShadowColor;
|
||||||
|
|
||||||
|
// -- Properties automatically bound to Blueprint widget
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UWidget* MouseVersion;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UButton* MouseUpButton;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UButton* MouseDownButton;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UImage* MouseUpImage;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UImage* MouseDownImage;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UTextBlock* MouseText;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UButton* GamepadVersion;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UImage* GamepadUpImage;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UImage* GamepadDownImage;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (BindWidget))
|
||||||
|
UTextBlock* GamepadText;
|
||||||
|
|
||||||
|
/// Event raised when the selected option changes
|
||||||
|
FOnSelectedOptionChanged OnSelectedOptionChanged;
|
||||||
|
|
||||||
|
virtual void SynchronizeProperties() override;
|
||||||
|
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
/// Remove all options
|
||||||
|
virtual void ClearOptions();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Adds a new option
|
||||||
|
* @param Option The text for the new option
|
||||||
|
* @return The index for the new option
|
||||||
|
*/
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual int AddOption(FText Option);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets all of the options available for this control
|
||||||
|
* @param Options All options to be available
|
||||||
|
* @param NewSelectedIndex Which of the options to select by default
|
||||||
|
*/
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual void SetOptions(const TArray<FText>& Options, int NewSelectedIndex = 0);
|
||||||
|
|
||||||
|
UFUNCTION(BlueprintPure)
|
||||||
|
virtual int GetSelectedIndex() const { return SelectedIndex; }
|
||||||
|
|
||||||
|
UFUNCTION(BlueprintPure)
|
||||||
|
virtual FText GetSelectedOption() const;
|
||||||
|
/**
|
||||||
|
* @brief Change the selected index option
|
||||||
|
* @param NewIndex The new index to set, can be -1 for no selection
|
||||||
|
*/
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual void SetSelectedIndex(int NewIndex);
|
||||||
|
|
||||||
|
virtual void SetFocusProperly_Implementation() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
TArray<FText> Options;
|
||||||
|
int SelectedIndex;
|
||||||
|
|
||||||
|
virtual void SyncButtonProperties(UButton* Button) const;
|
||||||
|
virtual void SyncButtonImageProperties(UImage* Img) const;
|
||||||
|
virtual void SyncTextProperties(UTextBlock* Txt) const;
|
||||||
|
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual void SetMouseMode();
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual void SetButtonMode();
|
||||||
|
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual void UpdateFromInputMode(EInputMode Mode);
|
||||||
|
virtual void NativeConstruct() override;
|
||||||
|
virtual void NativeDestruct() override;
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual void ChangeOption(int Delta);
|
||||||
|
UFUNCTION(BlueprintCallable)
|
||||||
|
virtual EInputMode GetCurrentInputMode() const;
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
UFUNCTION()
|
||||||
|
void InputModeChanged(int PlayerIndex, EInputMode NewMode);
|
||||||
|
UFUNCTION()
|
||||||
|
void MouseUpClicked();
|
||||||
|
UFUNCTION()
|
||||||
|
void MouseDownClicked();
|
||||||
|
};
|
25
Source/StevesUEHelpers/Public/StevesUI/UiTheme.h
Normal file
25
Source/StevesUEHelpers/Public/StevesUI/UiTheme.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "Engine/DataAsset.h"
|
||||||
|
#include "Engine/DataTable.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include "UiTheme.generated.h"
|
||||||
|
|
||||||
|
/// Custom asset to conveniently hold theme information for the UI
|
||||||
|
/// Currently only lightly used to provide simple access to button images, but I intend to use
|
||||||
|
/// this more extensively later
|
||||||
|
UCLASS(Blueprintable)
|
||||||
|
class STEVESUEHELPERS_API UUiTheme : public UPrimaryDataAsset
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
UPROPERTY(EditDefaultsOnly)
|
||||||
|
TSoftObjectPtr<UDataTable> KeyboardMouseImages;
|
||||||
|
UPROPERTY(EditDefaultsOnly)
|
||||||
|
TSoftObjectPtr<UDataTable> XboxControllerImages;
|
||||||
|
|
||||||
|
};
|
49
Source/StevesUEHelpers/StevesUEHelpers.Build.cs
Normal file
49
Source/StevesUEHelpers/StevesUEHelpers.Build.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
using UnrealBuildTool;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
public class StevesUEHelpers : ModuleRules
|
||||||
|
{
|
||||||
|
public StevesUEHelpers(ReadOnlyTargetRules Target) : base(Target)
|
||||||
|
{
|
||||||
|
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
|
||||||
|
|
||||||
|
PublicIncludePaths.AddRange(
|
||||||
|
new string[] {
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
PrivateIncludePaths.AddRange(
|
||||||
|
new string[] {
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
PublicDependencyModuleNames.AddRange(
|
||||||
|
new string[]
|
||||||
|
{
|
||||||
|
"Core",
|
||||||
|
"CoreUObject",
|
||||||
|
"Engine",
|
||||||
|
"InputCore",
|
||||||
|
"Slate",
|
||||||
|
"SlateCore",
|
||||||
|
"UMG",
|
||||||
|
"Paper2D"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
PrivateDependencyModuleNames.AddRange(
|
||||||
|
new string[]
|
||||||
|
{
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
DynamicallyLoadedModuleNames.AddRange(
|
||||||
|
new string[]
|
||||||
|
{
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
31
StevesUEHelpers.uplugin
Normal file
31
StevesUEHelpers.uplugin
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"FileVersion" : 3,
|
||||||
|
"Version" : 1,
|
||||||
|
"VersionName" : "1.0",
|
||||||
|
"FriendlyName" : "Steve's UE4 Helpers",
|
||||||
|
"Description" : "A set of common helper classes for UE4 projects by Steve Streeing",
|
||||||
|
"Category" : "Code Utilities",
|
||||||
|
"CreatedBy" : "Steve Streeting",
|
||||||
|
"CreatedByURL" : "https://www.stevestreeting.com",
|
||||||
|
"DocsURL" : "",
|
||||||
|
"MarketplaceURL" : "",
|
||||||
|
"SupportURL" : "",
|
||||||
|
"EnabledByDefault" : true,
|
||||||
|
"CanContainContent" : false,
|
||||||
|
"IsBetaVersion" : true,
|
||||||
|
"Installed" : false,
|
||||||
|
"Modules" :
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"Name" : "StevesUEHelpers",
|
||||||
|
"Type" : "Runtime",
|
||||||
|
"LoadingPhase" : "Default"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Plugins": [
|
||||||
|
{
|
||||||
|
"Name": "Paper2D",
|
||||||
|
"Enabled": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user