2017-03-26 20:32:57 +00:00
|
|
|
// Distributed under the MIT License (MIT) (see accompanying LICENSE file)
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-04-22 15:38:04 +00:00
|
|
|
#include "ImGuiInputState.h"
|
|
|
|
|
2017-03-26 20:32:57 +00:00
|
|
|
#include <Widgets/SLeafWidget.h>
|
|
|
|
|
2017-04-22 15:38:04 +00:00
|
|
|
|
2017-03-26 20:32:57 +00:00
|
|
|
class FImGuiModuleManager;
|
2018-07-10 16:40:57 +00:00
|
|
|
class UImGuiInputHandler;
|
2017-03-26 20:32:57 +00:00
|
|
|
|
2017-04-22 15:38:04 +00:00
|
|
|
// Slate widget for rendering ImGui output and storing Slate inputs.
|
2017-03-26 20:32:57 +00:00
|
|
|
class SImGuiWidget : public SLeafWidget
|
|
|
|
{
|
2017-08-19 20:19:38 +00:00
|
|
|
typedef SLeafWidget Super;
|
|
|
|
|
2017-03-26 20:32:57 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
SLATE_BEGIN_ARGS(SImGuiWidget)
|
|
|
|
{}
|
|
|
|
SLATE_ARGUMENT(FImGuiModuleManager*, ModuleManager)
|
2017-09-27 20:16:54 +00:00
|
|
|
SLATE_ARGUMENT(UGameViewportClient*, GameViewport)
|
2017-08-28 19:29:07 +00:00
|
|
|
SLATE_ARGUMENT(int32, ContextIndex)
|
2017-03-26 20:32:57 +00:00
|
|
|
SLATE_END_ARGS()
|
|
|
|
|
|
|
|
void Construct(const FArguments& InArgs);
|
|
|
|
|
2017-04-22 15:38:04 +00:00
|
|
|
~SImGuiWidget();
|
|
|
|
|
2017-08-28 19:29:07 +00:00
|
|
|
// Get index of the context that this widget is targeting.
|
|
|
|
int32 GetContextIndex() const { return ContextIndex; }
|
|
|
|
|
|
|
|
// Get input state associated with this widget.
|
2017-04-22 15:38:04 +00:00
|
|
|
const FImGuiInputState& GetInputState() const { return InputState; }
|
|
|
|
|
2017-09-10 20:05:37 +00:00
|
|
|
// Get the game viewport to which this widget is attached.
|
|
|
|
const TWeakObjectPtr<UGameViewportClient>& GetGameViewport() const { return GameViewport; }
|
|
|
|
|
2018-04-21 21:43:15 +00:00
|
|
|
// Detach widget from viewport assigned during construction (effectively allowing to dispose this widget).
|
2017-09-27 20:16:54 +00:00
|
|
|
void Detach();
|
2017-09-10 20:05:37 +00:00
|
|
|
|
2017-04-22 15:38:04 +00:00
|
|
|
//----------------------------------------------------------------------------------------------------
|
|
|
|
// SWidget overrides
|
|
|
|
//----------------------------------------------------------------------------------------------------
|
|
|
|
|
2017-09-02 17:42:41 +00:00
|
|
|
virtual void Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime) override;
|
|
|
|
|
2018-07-10 16:40:57 +00:00
|
|
|
virtual bool SupportsKeyboardFocus() const override { return bInputEnabled && !IsConsoleOpened(); }
|
2017-04-22 15:38:04 +00:00
|
|
|
|
|
|
|
virtual FReply OnKeyChar(const FGeometry& MyGeometry, const FCharacterEvent& CharacterEvent) override;
|
|
|
|
|
|
|
|
virtual FReply OnKeyDown(const FGeometry& MyGeometry, const FKeyEvent& KeyEvent) override;
|
|
|
|
|
|
|
|
virtual FReply OnKeyUp(const FGeometry& MyGeometry, const FKeyEvent& KeyEvent) override;
|
|
|
|
|
2018-05-17 22:25:47 +00:00
|
|
|
virtual FReply OnAnalogValueChanged(const FGeometry& MyGeometry, const FAnalogInputEvent& AnalogInputEvent) override;
|
|
|
|
|
2017-04-22 15:38:04 +00:00
|
|
|
virtual FReply OnMouseButtonDown(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent) override;
|
|
|
|
|
|
|
|
virtual FReply OnMouseButtonDoubleClick(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent) override;
|
|
|
|
|
|
|
|
virtual FReply OnMouseButtonUp(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent) override;
|
|
|
|
|
|
|
|
virtual FReply OnMouseWheel(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent) override;
|
|
|
|
|
|
|
|
virtual FReply OnMouseMove(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent) override;
|
|
|
|
|
2017-08-28 19:29:07 +00:00
|
|
|
virtual FReply OnFocusReceived(const FGeometry& MyGeometry, const FFocusEvent& FocusEvent) override;
|
|
|
|
|
|
|
|
virtual void OnFocusLost(const FFocusEvent& FocusEvent) override;
|
|
|
|
|
|
|
|
virtual void OnMouseEnter(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent) override;
|
|
|
|
|
|
|
|
virtual void OnMouseLeave(const FPointerEvent& MouseEvent) override;
|
|
|
|
|
2018-07-10 16:40:57 +00:00
|
|
|
virtual FCursorReply OnCursorQuery(const FGeometry& MyGeometry, const FPointerEvent& CursorEvent) const override;
|
|
|
|
|
2017-03-26 20:32:57 +00:00
|
|
|
private:
|
|
|
|
|
2017-09-02 17:42:41 +00:00
|
|
|
enum class EInputMode : uint8
|
2017-08-28 19:29:07 +00:00
|
|
|
{
|
|
|
|
None,
|
2017-09-21 21:09:03 +00:00
|
|
|
// Mouse pointer only without user focus
|
|
|
|
MousePointerOnly,
|
2018-05-17 22:25:47 +00:00
|
|
|
// Full input with user focus (mouse, keyboard and depending on navigation mode gamepad)
|
|
|
|
Full
|
2017-08-28 19:29:07 +00:00
|
|
|
};
|
|
|
|
|
2018-07-10 16:40:57 +00:00
|
|
|
void CreateInputHandler();
|
|
|
|
void ReleaseInputHandler();
|
|
|
|
void RecreateInputHandler();
|
|
|
|
|
2018-10-28 21:15:02 +00:00
|
|
|
void UpdateSoftwareCursorMode();
|
|
|
|
|
|
|
|
void RegisterImGuiSettingsDelegates();
|
|
|
|
void UnregisterImGuiSettingsDelegates();
|
2018-07-10 16:40:57 +00:00
|
|
|
|
2018-04-21 21:43:15 +00:00
|
|
|
// If needed, add to event reply a mouse lock or unlock request.
|
|
|
|
FORCEINLINE FReply WithMouseLockRequests(FReply&& Reply);
|
|
|
|
|
2017-08-19 20:19:38 +00:00
|
|
|
FORCEINLINE void CopyModifierKeys(const FInputEvent& InputEvent);
|
2017-08-28 19:29:07 +00:00
|
|
|
FORCEINLINE void CopyModifierKeys(const FPointerEvent& MouseEvent);
|
|
|
|
|
2017-09-16 20:52:14 +00:00
|
|
|
bool IsConsoleOpened() const;
|
|
|
|
|
2018-04-21 21:43:15 +00:00
|
|
|
void SetMouseCursorOverride(EMouseCursor::Type InMouseCursorOverride);
|
|
|
|
|
2017-09-02 17:42:41 +00:00
|
|
|
// Update visibility based on input enabled state.
|
|
|
|
void SetVisibilityFromInputEnabled();
|
|
|
|
|
|
|
|
// Update input enabled state from console variable.
|
|
|
|
void UpdateInputEnabled();
|
|
|
|
|
2017-09-21 21:09:03 +00:00
|
|
|
// Determine new input mode based on hints.
|
|
|
|
void UpdateInputMode(bool bHasKeyboardFocus, bool bHasMousePointer);
|
|
|
|
|
|
|
|
void UpdateMouseStatus();
|
|
|
|
|
|
|
|
FORCEINLINE bool HasMouseEventNotification() const { return bReceivedMouseEvent; }
|
|
|
|
FORCEINLINE void NotifyMouseEvent() { bReceivedMouseEvent = true; }
|
|
|
|
FORCEINLINE void ClearMouseEventNotification() { bReceivedMouseEvent = false; }
|
2017-08-19 20:19:38 +00:00
|
|
|
|
2017-04-22 15:38:04 +00:00
|
|
|
void OnPostImGuiUpdate();
|
|
|
|
|
2018-04-21 21:43:15 +00:00
|
|
|
// Update canvas map mode based on input state.
|
|
|
|
void UpdateCanvasMapMode(const FInputEvent& InputEvent);
|
|
|
|
void SetCanvasMapMode(bool bEnabled);
|
|
|
|
|
|
|
|
void AddCanvasScale(float Delta);
|
|
|
|
void UdateCanvasScale(float DeltaSeconds);
|
|
|
|
|
|
|
|
void UpdateCanvasDraggingConditions(const FPointerEvent& MouseEvent);
|
|
|
|
void UpdateCanvasDragging(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent);
|
|
|
|
|
|
|
|
// Canvas scale in which the whole canvas is visible in the viewport. We don't scale below that value.
|
|
|
|
float GetMinCanvasScale() const;
|
|
|
|
|
|
|
|
// Normalized canvas scale mapping range [MinCanvasScale..1] to [0..1].
|
|
|
|
float GetNormalizedCanvasScale(float Scale) const;
|
|
|
|
|
|
|
|
// Position of the canvas origin, given the current canvas scale and offset. Uses NormalizedCanvasScale to smoothly
|
|
|
|
// transition between showing visible canvas area at scale 1 and the whole canvas at min canvas scale.
|
|
|
|
FVector2D GetCanvasPosition(float Scale, const FVector2D& Offset) const;
|
|
|
|
|
|
|
|
bool InFrameGrabbingRange(const FVector2D& Position, float Scale, const FVector2D& Offset) const;
|
|
|
|
|
|
|
|
FVector2D GetViewportSize() const;
|
|
|
|
|
2017-03-26 20:32:57 +00:00
|
|
|
virtual int32 OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyClippingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& WidgetStyle, bool bParentEnabled) const override;
|
|
|
|
|
|
|
|
virtual FVector2D ComputeDesiredSize(float) const override;
|
|
|
|
|
2017-09-09 10:40:44 +00:00
|
|
|
void OnDebugDraw();
|
|
|
|
|
2017-03-26 20:32:57 +00:00
|
|
|
FImGuiModuleManager* ModuleManager = nullptr;
|
2017-09-10 20:05:37 +00:00
|
|
|
TWeakObjectPtr<UGameViewportClient> GameViewport;
|
2018-07-10 16:40:57 +00:00
|
|
|
TWeakObjectPtr<UImGuiInputHandler> InputHandler;
|
2017-03-26 20:32:57 +00:00
|
|
|
|
|
|
|
mutable TArray<FSlateVertex> VertexBuffer;
|
|
|
|
mutable TArray<SlateIndex> IndexBuffer;
|
2017-04-22 15:38:04 +00:00
|
|
|
|
2017-08-28 19:29:07 +00:00
|
|
|
int32 ContextIndex = 0;
|
|
|
|
|
2018-04-21 21:43:15 +00:00
|
|
|
FImGuiInputState InputState;
|
|
|
|
|
2017-08-28 19:29:07 +00:00
|
|
|
EInputMode InputMode = EInputMode::None;
|
2017-09-02 17:42:41 +00:00
|
|
|
bool bInputEnabled = false;
|
2017-09-21 21:09:03 +00:00
|
|
|
bool bReceivedMouseEvent = false;
|
2018-04-21 21:43:15 +00:00
|
|
|
bool bMouseLock = false;
|
2017-08-28 19:29:07 +00:00
|
|
|
|
2018-10-28 21:15:02 +00:00
|
|
|
// Whether or not ImGui should draw its own cursor.
|
|
|
|
bool bUseSoftwareCursor = false;
|
|
|
|
|
2018-04-21 21:43:15 +00:00
|
|
|
// Canvas map mode allows to zoom in/out and navigate between different parts of ImGui canvas.
|
|
|
|
bool bCanvasMapMode = false;
|
|
|
|
|
|
|
|
// If enabled (only if not fully zoomed out), allows to drag ImGui canvas. Dragging canvas modifies canvas offset.
|
|
|
|
bool bCanvasDragging = false;
|
|
|
|
|
|
|
|
// If enabled (only if zoomed out), allows to drag a frame that represents a visible area of the ImGui canvas.
|
|
|
|
// Mouse deltas are converted to canvas offset by linear formula derived from GetCanvasPosition function.
|
|
|
|
bool bFrameDragging = false;
|
|
|
|
|
|
|
|
// True, if mouse and input are in state that allows to start frame dragging. Used for highlighting.
|
|
|
|
bool bFrameDraggingReady = false;
|
|
|
|
|
|
|
|
bool bFrameDraggingSkipMouseMove = false;
|
|
|
|
|
|
|
|
EMouseCursor::Type MouseCursorOverride = EMouseCursor::None;
|
|
|
|
|
|
|
|
float TargetCanvasScale = 1.f;
|
|
|
|
|
|
|
|
float CanvasScale = 1.f;
|
|
|
|
FVector2D CanvasOffset = FVector2D::ZeroVector;
|
|
|
|
|
|
|
|
float ImGuiFrameCanvasScale = 1.f;
|
|
|
|
FVector2D ImGuiFrameCanvasOffset = FVector2D::ZeroVector;
|
2017-09-10 20:05:37 +00:00
|
|
|
|
|
|
|
TWeakPtr<SWidget> PreviousUserFocusedWidget;
|
2017-03-26 20:32:57 +00:00
|
|
|
};
|