mirror of
https://github.com/kevinporetti/UnrealImGui.git
synced 2025-01-19 00:40:32 +00:00
fc6fd2e498
- Added common debug config file for module-wide debug symbols and loggers. - Added IMGUI_MODULE_DEVELOPER symbol to enable or disable module developer mode, that is intended to add additional module debug options hidden from normal usage. - ImGui Widget debug is only available in developer mode because in normal usage it adds noise to ImGui console variables while not being very useful. - Merged LogImGuiInputHandler and LogImGuiInputHandlerFactory loggers as they are part of the same feature. - Renamed LogImGuiInputState to LogImGuiInput.
167 lines
4.0 KiB
C++
167 lines
4.0 KiB
C++
// Distributed under the MIT License (MIT) (see accompanying LICENSE file)
|
|
|
|
#include "ImGuiPrivatePCH.h"
|
|
|
|
#include "ImGuiInputState.h"
|
|
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <type_traits>
|
|
|
|
|
|
// If TCHAR is wider than ImWchar, enable or disable validation of input character before conversions.
|
|
#define VALIDATE_INPUT_CHARACTERS 1
|
|
|
|
#if VALIDATE_INPUT_CHARACTERS
|
|
DEFINE_LOG_CATEGORY_STATIC(LogImGuiInput, Warning, All);
|
|
#endif
|
|
|
|
namespace
|
|
{
|
|
template<typename T, std::enable_if_t<(sizeof(T) <= sizeof(ImWchar)), T>* = nullptr>
|
|
ImWchar CastInputChar(T Char)
|
|
{
|
|
return static_cast<ImWchar>(Char);
|
|
}
|
|
|
|
template<typename T, std::enable_if_t<!(sizeof(T) <= sizeof(ImWchar)), T>* = nullptr>
|
|
ImWchar CastInputChar(T Char)
|
|
{
|
|
#if VALIDATE_INPUT_CHARACTERS
|
|
// We only need a runtime validation if TCHAR is wider than ImWchar.
|
|
// Signed and unsigned integral types with the same size as ImWchar should be safely converted. As long as the
|
|
// char value is in that range we can safely use it, otherwise we should log an error to notify about possible
|
|
// truncations.
|
|
static constexpr auto MinLimit = (std::numeric_limits<std::make_signed_t<ImWchar>>::min)();
|
|
static constexpr auto MaxLimit = (std::numeric_limits<std::make_unsigned_t<ImWchar>>::max)();
|
|
UE_CLOG(!(Char >= MinLimit && Char <= MaxLimit), LogImGuiInput, Error,
|
|
TEXT("TCHAR value '%c' (%#x) is out of range %d (%#x) to %u (%#x) that can be safely converted to ImWchar. ")
|
|
TEXT("If you wish to disable this validation, please set VALIDATE_INPUT_CHARACTERS in ImGuiInputState.cpp to 0."),
|
|
Char, Char, MinLimit, MinLimit, MaxLimit, MaxLimit);
|
|
#endif
|
|
|
|
return static_cast<ImWchar>(Char);
|
|
}
|
|
}
|
|
|
|
FImGuiInputState::FImGuiInputState()
|
|
{
|
|
ResetState();
|
|
}
|
|
|
|
void FImGuiInputState::AddCharacter(TCHAR Char)
|
|
{
|
|
if (InputCharactersNum < Utilities::GetArraySize(InputCharacters))
|
|
{
|
|
InputCharacters[InputCharactersNum++] = CastInputChar(Char);
|
|
InputCharacters[InputCharactersNum] = 0;
|
|
}
|
|
}
|
|
|
|
void FImGuiInputState::SetKeyDown(uint32 KeyIndex, bool bIsDown)
|
|
{
|
|
if (KeyIndex < Utilities::GetArraySize(KeysDown))
|
|
{
|
|
if (KeysDown[KeyIndex] != bIsDown)
|
|
{
|
|
KeysDown[KeyIndex] = bIsDown;
|
|
KeysUpdateRange.AddPosition(KeyIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
void FImGuiInputState::SetMouseDown(uint32 MouseIndex, bool bIsDown)
|
|
{
|
|
if (MouseIndex < Utilities::GetArraySize(MouseButtonsDown))
|
|
{
|
|
if (MouseButtonsDown[MouseIndex] != bIsDown)
|
|
{
|
|
MouseButtonsDown[MouseIndex] = bIsDown;
|
|
MouseButtonsUpdateRange.AddPosition(MouseIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
void FImGuiInputState::Reset(bool bKeyboard, bool bMouse, bool bNavigation)
|
|
{
|
|
if (bKeyboard)
|
|
{
|
|
ClearCharacters();
|
|
ClearKeys();
|
|
}
|
|
|
|
if (bMouse)
|
|
{
|
|
ClearMouseButtons();
|
|
ClearMouseAnalogue();
|
|
}
|
|
|
|
if (bKeyboard && bMouse)
|
|
{
|
|
ClearModifierKeys();
|
|
}
|
|
|
|
if (bNavigation)
|
|
{
|
|
ClearNavigationInputs();
|
|
}
|
|
}
|
|
|
|
void FImGuiInputState::ClearUpdateState()
|
|
{
|
|
if (InputCharactersNum > 0)
|
|
{
|
|
ClearCharacters();
|
|
}
|
|
|
|
KeysUpdateRange.SetEmpty();
|
|
MouseButtonsUpdateRange.SetEmpty();
|
|
|
|
MouseWheelDelta = 0.f;
|
|
}
|
|
|
|
void FImGuiInputState::ClearCharacters()
|
|
{
|
|
using std::fill;
|
|
fill(InputCharacters, &InputCharacters[Utilities::GetArraySize(InputCharacters)], 0);
|
|
InputCharactersNum = 0;
|
|
}
|
|
|
|
void FImGuiInputState::ClearKeys()
|
|
{
|
|
using std::fill;
|
|
fill(KeysDown, &KeysDown[Utilities::GetArraySize(KeysDown)], false);
|
|
|
|
// Expand update range because keys array has been updated.
|
|
KeysUpdateRange.SetFull();
|
|
}
|
|
|
|
void FImGuiInputState::ClearMouseButtons()
|
|
{
|
|
using std::fill;
|
|
fill(MouseButtonsDown, &MouseButtonsDown[Utilities::GetArraySize(MouseButtonsDown)], false);
|
|
|
|
// Expand update range because mouse buttons array has been updated.
|
|
MouseButtonsUpdateRange.SetFull();
|
|
}
|
|
|
|
void FImGuiInputState::ClearMouseAnalogue()
|
|
{
|
|
MousePosition = FVector2D::ZeroVector;
|
|
MouseWheelDelta = 0.f;
|
|
}
|
|
|
|
void FImGuiInputState::ClearModifierKeys()
|
|
{
|
|
bIsControlDown = false;
|
|
bIsShiftDown = false;
|
|
bIsAltDown = false;
|
|
}
|
|
|
|
void FImGuiInputState::ClearNavigationInputs()
|
|
{
|
|
using std::fill;
|
|
fill(NavigationInputs, &NavigationInputs[Utilities::GetArraySize(NavigationInputs)], 0.f);
|
|
}
|
|
|