Misc input related changes to facilitate upcoming merges.

This commit is contained in:
ocornut 2022-09-22 18:42:00 +02:00
parent 85f327d8d3
commit f359dca0dc
3 changed files with 37 additions and 38 deletions

View File

@ -4046,6 +4046,18 @@ static void UpdateAliasKey(ImGuiKey key, bool v, float analog_value)
key_data->AnalogValue = analog_value;
}
// [Internal] Do not use directly (should read io.KeyMods instead)
static ImGuiModFlags GetMergedModFlags()
{
ImGuiContext& g = *GImGui;
ImGuiModFlags key_mods = ImGuiModFlags_None;
if (g.IO.KeyCtrl) { key_mods |= ImGuiModFlags_Ctrl; }
if (g.IO.KeyShift) { key_mods |= ImGuiModFlags_Shift; }
if (g.IO.KeyAlt) { key_mods |= ImGuiModFlags_Alt; }
if (g.IO.KeySuper) { key_mods |= ImGuiModFlags_Super; }
return key_mods;
}
static void ImGui::UpdateKeyboardInputs()
{
ImGuiContext& g = *GImGui;
@ -4086,10 +4098,10 @@ static void ImGui::UpdateKeyboardInputs()
}
if (io.BackendUsingLegacyKeyArrays == 1)
{
io.KeysData[ImGuiKey_ModCtrl].Down = io.KeyCtrl;
io.KeysData[ImGuiKey_ModShift].Down = io.KeyShift;
io.KeysData[ImGuiKey_ModAlt].Down = io.KeyAlt;
io.KeysData[ImGuiKey_ModSuper].Down = io.KeySuper;
GetKeyData(ImGuiKey_ModCtrl)->Down = io.KeyCtrl;
GetKeyData(ImGuiKey_ModShift)->Down = io.KeyShift;
GetKeyData(ImGuiKey_ModAlt)->Down = io.KeyAlt;
GetKeyData(ImGuiKey_ModSuper)->Down = io.KeySuper;
}
}
@ -4382,18 +4394,6 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags()
io.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false;
}
// [Internal] Do not use directly (can read io.KeyMods instead)
ImGuiModFlags ImGui::GetMergedModFlags()
{
ImGuiContext& g = *GImGui;
ImGuiModFlags key_mods = ImGuiModFlags_None;
if (g.IO.KeyCtrl) { key_mods |= ImGuiModFlags_Ctrl; }
if (g.IO.KeyShift) { key_mods |= ImGuiModFlags_Shift; }
if (g.IO.KeyAlt) { key_mods |= ImGuiModFlags_Alt; }
if (g.IO.KeySuper) { key_mods |= ImGuiModFlags_Super; }
return key_mods;
}
void ImGui::NewFrame()
{
IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?");
@ -7778,8 +7778,8 @@ static const char* const GKeyNames[] =
"GamepadL1", "GamepadR1", "GamepadL2", "GamepadR2", "GamepadL3", "GamepadR3",
"GamepadLStickLeft", "GamepadLStickRight", "GamepadLStickUp", "GamepadLStickDown",
"GamepadRStickLeft", "GamepadRStickRight", "GamepadRStickUp", "GamepadRStickDown",
"ModCtrl", "ModShift", "ModAlt", "ModSuper",
"MouseLeft", "MouseRight", "MouseMiddle", "MouseX1", "MouseX2", "MouseWheelX", "MouseWheelY",
"ModCtrl", "ModShift", "ModAlt", "ModSuper",
};
IM_STATIC_ASSERT(ImGuiKey_NamedKey_COUNT == IM_ARRAYSIZE(GKeyNames));
@ -8146,33 +8146,33 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs)
{
ImGuiKey key = e->Key.Key;
IM_ASSERT(key != ImGuiKey_None);
const int keydata_index = (key - ImGuiKey_KeysData_OFFSET);
ImGuiKeyData* keydata = &io.KeysData[keydata_index];
e->IgnoredAsSame = (keydata->Down == e->Key.Down && keydata->AnalogValue == e->Key.AnalogValue);
const int key_data_index = (key - ImGuiKey_KeysData_OFFSET);
ImGuiKeyData* key_data = &io.KeysData[key_data_index];
e->IgnoredAsSame = (key_data->Down == e->Key.Down && key_data->AnalogValue == e->Key.AnalogValue);
if (!e->IgnoredAsSame)
{
// Trickling Rule: Stop processing queued events if we got multiple action on the same button
if (trickle_fast_inputs && keydata->Down != e->Key.Down && (key_changed_mask.TestBit(keydata_index) || text_inputted || mouse_button_changed != 0))
if (trickle_fast_inputs && key_data->Down != e->Key.Down && (key_changed_mask.TestBit(key_data_index) || text_inputted || mouse_button_changed != 0))
break;
keydata->Down = e->Key.Down;
keydata->AnalogValue = e->Key.AnalogValue;
key_data->Down = e->Key.Down;
key_data->AnalogValue = e->Key.AnalogValue;
key_changed = true;
key_changed_mask.SetBit(keydata_index);
key_changed_mask.SetBit(key_data_index);
if (key == ImGuiKey_ModCtrl || key == ImGuiKey_ModShift || key == ImGuiKey_ModAlt || key == ImGuiKey_ModSuper)
{
if (key == ImGuiKey_ModCtrl) { io.KeyCtrl = keydata->Down; }
if (key == ImGuiKey_ModShift) { io.KeyShift = keydata->Down; }
if (key == ImGuiKey_ModAlt) { io.KeyAlt = keydata->Down; }
if (key == ImGuiKey_ModSuper) { io.KeySuper = keydata->Down; }
if (key == ImGuiKey_ModCtrl) { io.KeyCtrl = key_data->Down; }
if (key == ImGuiKey_ModShift) { io.KeyShift = key_data->Down; }
if (key == ImGuiKey_ModAlt) { io.KeyAlt = key_data->Down; }
if (key == ImGuiKey_ModSuper) { io.KeySuper = key_data->Down; }
io.KeyMods = GetMergedModFlags();
}
// Allow legacy code using io.KeysDown[GetKeyIndex()] with new backends
#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO
io.KeysDown[key] = keydata->Down;
io.KeysDown[key] = key_data->Down;
if (io.KeyMap[key] != -1)
io.KeysDown[io.KeyMap[key]] = keydata->Down;
io.KeysDown[io.KeyMap[key]] = key_data->Down;
#endif
}
}

10
imgui.h
View File

@ -1437,6 +1437,10 @@ enum ImGuiKey : int
ImGuiKey_GamepadRStickUp, // [Analog]
ImGuiKey_GamepadRStickDown, // [Analog]
// Mouse Buttons (auto-submitted from AddMouseButtonEvent() calls)
// - This is mirroring the data also written to io.MouseDown[], io.MouseWheel, in a format allowing them to be accessed via standard key API.
ImGuiKey_MouseLeft, ImGuiKey_MouseRight, ImGuiKey_MouseMiddle, ImGuiKey_MouseX1, ImGuiKey_MouseX2, ImGuiKey_MouseWheelX, ImGuiKey_MouseWheelY,
// Keyboard Modifiers (explicitly submitted by backend via AddKeyEvent() calls)
// - This is mirroring the data also written to io.KeyCtrl, io.KeyShift, io.KeyAlt, io.KeySuper, in a format allowing
// them to be accessed via standard key API, allowing calls such as IsKeyPressed(), IsKeyReleased(), querying duration etc.
@ -1447,10 +1451,6 @@ enum ImGuiKey : int
// backends tend to interfere and break that equivalence. The safer decision is to relay that ambiguity down to the end-user...
ImGuiKey_ModCtrl, ImGuiKey_ModShift, ImGuiKey_ModAlt, ImGuiKey_ModSuper,
// Mouse Buttons (auto-submitted from AddMouseButtonEvent() calls)
// - This is mirroring the data also written to io.MouseDown[], io.MouseWheel, in a format allowing them to be accessed via standard key API.
ImGuiKey_MouseLeft, ImGuiKey_MouseRight, ImGuiKey_MouseMiddle, ImGuiKey_MouseX1, ImGuiKey_MouseX2, ImGuiKey_MouseWheelX, ImGuiKey_MouseWheelY,
// End of list
ImGuiKey_COUNT, // No valid ImGuiKey is ever greater than this value
@ -1890,7 +1890,7 @@ struct ImGuiStyle
//-----------------------------------------------------------------------------
// [Internal] Storage used by IsKeyDown(), IsKeyPressed() etc functions.
// If prior to 1.87 you used io.KeysDownDuration[] (which was marked as internal), you should use GetKeyData(key)->DownDuration and not io.KeysData[key]->DownDuration.
// If prior to 1.87 you used io.KeysDownDuration[] (which was marked as internal), you should use GetKeyData(key)->DownDuration and *NOT* io.KeysData[key]->DownDuration.
struct ImGuiKeyData
{
bool Down; // True for if key is down

View File

@ -1173,7 +1173,7 @@ struct ImGuiPtrOrIndex
typedef ImBitArray<ImGuiKey_NamedKey_COUNT, -ImGuiKey_NamedKey_BEGIN> ImBitArrayForNamedKeys;
// Extend ImGuiKey_
// [Internal] Key ranges
#define ImGuiKey_LegacyNativeKey_BEGIN 0
#define ImGuiKey_LegacyNativeKey_END 512
#define ImGuiKey_Keyboard_BEGIN (ImGuiKey_NamedKey_BEGIN)
@ -1181,7 +1181,7 @@ typedef ImBitArray<ImGuiKey_NamedKey_COUNT, -ImGuiKey_NamedKey_BEGIN> ImBitAr
#define ImGuiKey_Gamepad_BEGIN (ImGuiKey_GamepadStart)
#define ImGuiKey_Gamepad_END (ImGuiKey_GamepadRStickDown + 1)
#define ImGuiKey_Aliases_BEGIN (ImGuiKey_MouseLeft)
#define ImGuiKey_Aliases_END (ImGuiKey_COUNT)
#define ImGuiKey_Aliases_END (ImGuiKey_MouseWheelY + 1)
// [Internal] Named shortcuts for Navigation
#define ImGuiKey_NavKeyboardTweakSlow ImGuiKey_ModCtrl
@ -2720,7 +2720,6 @@ namespace ImGui
inline void SetActiveIdUsingKey(ImGuiKey key) { ImGuiContext& g = *GImGui; g.ActiveIdUsingKeyInputMask.SetBit(key); }
inline ImGuiKey MouseButtonToKey(ImGuiMouseButton button) { IM_ASSERT(button >= 0 && button < ImGuiMouseButton_COUNT); return (ImGuiKey)(ImGuiKey_MouseLeft + button); }
IMGUI_API bool IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold = -1.0f);
IMGUI_API ImGuiModFlags GetMergedModFlags();
IMGUI_API ImVec2 GetKeyVector2d(ImGuiKey key_left, ImGuiKey key_right, ImGuiKey key_up, ImGuiKey key_down);
IMGUI_API float GetNavTweakPressedAmount(ImGuiAxis axis);
IMGUI_API int CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate);