Skip to content

Commit

Permalink
Backends: OSX, Android: Replaced AddKeyModsEvent() added by 790132a i…
Browse files Browse the repository at this point in the history
…n favor of unified key enum system. (ocornut#4921, ocornut#4858)
  • Loading branch information
thedmd authored and sergeyn committed Mar 19, 2022
1 parent 9a1b5a9 commit 013647e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 75 deletions.
51 changes: 7 additions & 44 deletions backends/imgui_impl_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,23 @@

// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago)with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
// 2021-03-04: Initial version.

#include "imgui.h"
#include "imgui_impl_android.h"
#include <time.h>
#include <map>
#include <queue>
#include <android/native_window.h>
#include <android/input.h>
#include <android/keycodes.h>
#include <android/log.h>

struct KeyEvent
{
ImGuiKey Key;
bool Down;
int NativeKeycode;
int NativeScancode;

KeyEvent(): Key(ImGuiKey_None), Down(false), NativeKeycode(-1), NativeScancode(-1) {}
};

// Android data
static double g_Time = 0.0;
static ANativeWindow* g_Window;
static char g_LogTag[] = "ImGuiExample";
static std::map<ImGuiKey, std::queue<KeyEvent>> g_KeyEventQueues; // FIXME: Remove dependency on map and queue once we use upcoming input queue.
static ImGuiKeyModFlags g_KeyModFlags = ImGuiKeyModFlags_None;

static ImGuiKey ImGui_ImplAndroid_KeyCodeToImGuiKey(int32_t key_code)
{
Expand Down Expand Up @@ -176,15 +163,10 @@ int32_t ImGui_ImplAndroid_HandleInputEvent(AInputEvent* input_event)
int32_t event_action = AKeyEvent_getAction(input_event);
int32_t event_meta_state = AKeyEvent_getMetaState(input_event);

g_KeyModFlags = ImGuiKeyModFlags_None;
if ((event_meta_state & AMETA_CTRL_ON) != 0)
g_KeyModFlags |= ImGuiKeyModFlags_Ctrl;
if ((event_meta_state & AMETA_SHIFT_ON) != 0)
g_KeyModFlags |= ImGuiKeyModFlags_Shift;
if ((event_meta_state & AMETA_ALT_ON) != 0)
g_KeyModFlags |= ImGuiKeyModFlags_Alt;
if ((event_meta_state & AMETA_META_ON) != 0)
g_KeyModFlags |= ImGuiKeyModFlags_Super;
io.AddKeyEvent(ImGuiKey_ModCtrl, (event_meta_state & AMETA_CTRL_ON) != 0);
io.AddKeyEvent(ImGuiKey_ModShift, (event_meta_state & AMETA_SHIFT_ON) != 0);
io.AddKeyEvent(ImGuiKey_ModAlt, (event_meta_state & AMETA_ALT_ON) != 0);
io.AddKeyEvent(ImGuiKey_ModSuper, (event_meta_state & AMETA_META_ON) != 0);

switch (event_action)
{
Expand All @@ -197,13 +179,8 @@ int32_t ImGui_ImplAndroid_HandleInputEvent(AInputEvent* input_event)
ImGuiKey key = ImGui_ImplAndroid_KeyCodeToImGuiKey(event_key_code);
if (key != ImGuiKey_None && (event_action == AKEY_EVENT_ACTION_DOWN || event_action == AKEY_EVENT_ACTION_UP))
{
KeyEvent io_event;
io_event.Key = key;
io_event.Down = event_action == AKEY_EVENT_ACTION_DOWN;
io_event.NativeKeycode = event_key_code;
io_event.NativeScancode = event_scan_code;

g_KeyEventQueues[key].push(io_event);
io.AddKeyEvent(key, event_action == AKEY_EVENT_ACTION_DOWN);
io.SetKeyEventNativeData(key, event_key_code, event_scan_code);
}

break;
Expand Down Expand Up @@ -280,20 +257,6 @@ void ImGui_ImplAndroid_NewFrame()
{
ImGuiIO& io = ImGui::GetIO();

// Process queued key events
// FIXME: This is a workaround for multiple key event actions occurring at once (see above) and can be removed once we use upcoming input queue.
for (auto& key_queue : g_KeyEventQueues)
{
if (key_queue.second.empty())
continue;

auto& key_event = key_queue.second.front();
io.AddKeyEvent(key_event.Key, key_event.Down);
io.SetKeyEventNativeData(key_event.Key, key_event.NativeKeycode, key_event.NativeScancode); // To support legacy indexing (<1.87 user code)
key_queue.second.pop();
}
io.AddKeyModsEvent(g_KeyModFlags);

// Setup display size (every frame to accommodate for window resizing)
int32_t window_width = ANativeWindow_getWidth(g_Window);
int32_t window_height = ANativeWindow_getHeight(g_Window);
Expand Down
51 changes: 20 additions & 31 deletions backends/imgui_impl_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago)with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
// 2022-01-12: Inputs: Added basic Platform IME support, hooking the io.SetPlatformImeDataFn() function.
Expand Down Expand Up @@ -664,18 +665,12 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
if (event.type == NSEventTypeFlagsChanged)
{
unsigned short key_code = [event keyCode];
unsigned int flags = [event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask;

ImGuiKeyModFlags imgui_key_mods = ImGuiKeyModFlags_None;
if (flags & NSEventModifierFlagShift)
imgui_key_mods |= ImGuiKeyModFlags_Shift;
if (flags & NSEventModifierFlagControl)
imgui_key_mods |= ImGuiKeyModFlags_Ctrl;
if (flags & NSEventModifierFlagOption)
imgui_key_mods |= ImGuiKeyModFlags_Alt;
if (flags & NSEventModifierFlagCommand)
imgui_key_mods |= ImGuiKeyModFlags_Super;
io.AddKeyModsEvent(imgui_key_mods);
NSEventModifierFlags modifier_flags = [event modifierFlags];

io.AddKeyEvent(ImGuiKey_ModShift, (modifier_flags & NSEventModifierFlagShift) != 0);
io.AddKeyEvent(ImGuiKey_ModCtrl, (modifier_flags & NSEventModifierFlagControl) != 0);
io.AddKeyEvent(ImGuiKey_ModAlt, (modifier_flags & NSEventModifierFlagOption) != 0);
io.AddKeyEvent(ImGuiKey_ModSuper, (modifier_flags & NSEventModifierFlagCommand) != 0);

ImGuiKey key = ImGui_ImplOSX_KeyCodeToImGuiKey(key_code);
if (key != ImGuiKey_None)
Expand All @@ -684,28 +679,22 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
// to use hardware dependent masks to extract that information.
// 'imgui_mask' is left as a fallback.
NSEventModifierFlags mask = 0;
ImGuiKeyModFlags imgui_mask = ImGuiKeyModFlags_None;
switch (key_code)
switch (key)
{
case kVK_Control: mask = 0x0001; imgui_mask = ImGuiKeyModFlags_Ctrl; break;
case kVK_RightControl: mask = 0x2000; imgui_mask = ImGuiKeyModFlags_Ctrl; break;
case kVK_Shift: mask = 0x0002; imgui_mask = ImGuiKeyModFlags_Shift; break;
case kVK_RightShift: mask = 0x0004; imgui_mask = ImGuiKeyModFlags_Shift; break;
case kVK_Command: mask = 0x0008; imgui_mask = ImGuiKeyModFlags_Super; break;
case kVK_RightCommand: mask = 0x0010; imgui_mask = ImGuiKeyModFlags_Super; break;
case kVK_Option: mask = 0x0020; imgui_mask = ImGuiKeyModFlags_Alt; break;
case kVK_RightOption: mask = 0x0040; imgui_mask = ImGuiKeyModFlags_Alt; break;
case ImGuiKey_LeftCtrl: mask = 0x0001; break;
case ImGuiKey_RightCtrl: mask = 0x2000; break;
case ImGuiKey_LeftShift: mask = 0x0002; break;
case ImGuiKey_RightShift: mask = 0x0004; break;
case ImGuiKey_LeftSuper: mask = 0x0008; break;
case ImGuiKey_RightSuper: mask = 0x0010; break;
case ImGuiKey_LeftAlt: mask = 0x0020; break;
case ImGuiKey_RightAlt: mask = 0x0040; break;
default:
return io.WantCaptureKeyboard;
}

if (mask)
{
NSEventModifierFlags modifier_flags = [event modifierFlags];
io.AddKeyEvent(key, (modifier_flags & mask) != 0);
}
else if (imgui_mask)
{
io.AddKeyEvent(key, (imgui_key_mods & imgui_mask) != 0);
}
NSEventModifierFlags modifier_flags = [event modifierFlags];
io.AddKeyEvent(key, (modifier_flags & mask) != 0);
io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code)
}

Expand Down

0 comments on commit 013647e

Please sign in to comment.