UnrealImGui/Source/ImGui/Private/Widgets/SImGuiWidget.h
Sebastian 852a501022 Moved input state from SImGuiWidget to FImGuiContextProxy and modified implementation of FImGuiInputState reset functionality.
Moving input state to context proxy decouples it a bit from a particular input source and although not used right now, it allows multiple sources to send input in parallel.
2019-06-23 20:29:25 +01:00

148 lines
4.8 KiB
C++

// Distributed under the MIT License (MIT) (see accompanying LICENSE file)
#pragma once
#include "ImGuiModuleDebug.h"
#include "ImGuiModuleSettings.h"
#include <Widgets/SCompoundWidget.h>
// Hide ImGui Widget debug in non-developer mode.
#define IMGUI_WIDGET_DEBUG IMGUI_MODULE_DEVELOPER
class FImGuiInputState;
class FImGuiModuleManager;
class SImGuiCanvasControl;
class UImGuiInputHandler;
// Slate widget for rendering ImGui output and storing Slate inputs.
class SImGuiWidget : public SCompoundWidget
{
typedef SCompoundWidget Super;
public:
SLATE_BEGIN_ARGS(SImGuiWidget)
{}
SLATE_ARGUMENT(FImGuiModuleManager*, ModuleManager)
SLATE_ARGUMENT(UGameViewportClient*, GameViewport)
SLATE_ARGUMENT(int32, ContextIndex)
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
~SImGuiWidget();
// Get index of the context that this widget is targeting.
int32 GetContextIndex() const { return ContextIndex; }
//----------------------------------------------------------------------------------------------------
// SWidget overrides
//----------------------------------------------------------------------------------------------------
virtual void Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime) override;
virtual bool SupportsKeyboardFocus() const override { return bInputEnabled && !IsConsoleOpened(); }
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;
virtual FReply OnAnalogValueChanged(const FGeometry& MyGeometry, const FAnalogInputEvent& AnalogInputEvent) override;
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;
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;
virtual FCursorReply OnCursorQuery(const FGeometry& MyGeometry, const FPointerEvent& CursorEvent) const override;
private:
enum class EInputMode : uint8
{
None,
// Mouse pointer only without user focus
MousePointerOnly,
// Full input with user focus (mouse, keyboard and depending on navigation mode gamepad)
Full
};
void CreateInputHandler(const FStringClassReference& HandlerClassReference);
void ReleaseInputHandler();
void SetUseSoftwareCursor(bool bUse) { bUseSoftwareCursor = bUse; }
void RegisterImGuiSettingsDelegates();
void UnregisterImGuiSettingsDelegates();
FORCEINLINE void CopyModifierKeys(const FInputEvent& InputEvent);
bool IsConsoleOpened() const;
// Update visibility based on input enabled state.
void UpdateVisibility();
ULocalPlayer* GetLocalPlayer() const;
void TakeFocus();
void ReturnFocus();
void UpdateInputEnabled();
// Determine new input mode based on hints.
void UpdateInputMode(bool bHasKeyboardFocus, bool bHasMousePointer);
void UpdateCanvasControlMode(const FInputEvent& InputEvent);
void OnPostImGuiUpdate();
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;
void SetImGuiTransform(const FSlateRenderTransform& Transform) { ImGuiTransform = Transform; }
#if IMGUI_WIDGET_DEBUG
void OnDebugDraw();
#endif // IMGUI_WIDGET_DEBUG
FImGuiModuleManager* ModuleManager = nullptr;
TWeakObjectPtr<UGameViewportClient> GameViewport;
TWeakObjectPtr<UImGuiInputHandler> InputHandler;
FSlateRenderTransform ImGuiTransform;
FSlateRenderTransform ImGuiRenderTransform;
mutable TArray<FSlateVertex> VertexBuffer;
mutable TArray<SlateIndex> IndexBuffer;
int32 ContextIndex = 0;
FImGuiInputState* InputState;
EInputMode InputMode = EInputMode::None;
bool bInputEnabled = false;
// Whether or not ImGui should draw its own cursor.
bool bUseSoftwareCursor = false;
TSharedPtr<SImGuiCanvasControl> CanvasControlWidget;
TWeakPtr<SWidget> PreviousUserFocusedWidget;
};