mirror of
https://github.com/Drezil/imgui.git
synced 2024-11-26 05:27:01 +00:00
Examples: OSX: Fixed obsolete enums. Fixed keyboard mapping. Added missing keyboard modifiers. (#1873)
This commit is contained in:
parent
fac0c801da
commit
e54b61aa0f
@ -191,7 +191,7 @@
|
|||||||
|
|
||||||
appMenu = [[NSMenu alloc] initWithTitle:@"ImGui OSX+OpenGL2 Example"];
|
appMenu = [[NSMenu alloc] initWithTitle:@"ImGui OSX+OpenGL2 Example"];
|
||||||
menuItem = [appMenu addItemWithTitle:@"Quit ImGui OSX+OpenGL2 Example" action:@selector(terminate:) keyEquivalent:@"q"];
|
menuItem = [appMenu addItemWithTitle:@"Quit ImGui OSX+OpenGL2 Example" action:@selector(terminate:) keyEquivalent:@"q"];
|
||||||
[menuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
|
[menuItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
|
||||||
|
|
||||||
menuItem = [[NSMenuItem alloc] init];
|
menuItem = [[NSMenuItem alloc] init];
|
||||||
[menuItem setSubmenu:appMenu];
|
[menuItem setSubmenu:appMenu];
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
// ImGui Platform Binding for: OSX / Cocoa
|
// ImGui Platform Binding for: OSX / Cocoa
|
||||||
// This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan..)
|
// This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan..)
|
||||||
|
|
||||||
// FIXME-OSX: Keyboard mapping is weird.
|
// Issues:
|
||||||
// FIXME-OSX: Mouse cursor shape support (see https://github.com/glfw/glfw/issues/427)
|
// [ ] Platform: CTRL+Tab can't be read.
|
||||||
// FIXME-OSX: Test renderer backend combination, e.g. OpenGL3
|
// [ ] Platform: Mouse cursor shapes are not supported (see end of https://github.com/glfw/glfw/issues/427)
|
||||||
|
// [ ] Test with another renderer back-end than OpenGL2. e.g. OpenGL3.
|
||||||
|
|
||||||
#include "imgui.h"
|
#include "imgui.h"
|
||||||
#include "imgui_impl_osx.h"
|
#include "imgui_impl_osx.h"
|
||||||
@ -11,7 +12,7 @@
|
|||||||
|
|
||||||
// CHANGELOG
|
// CHANGELOG
|
||||||
// (minor and older changes stripped away, please see git history for details)
|
// (minor and older changes stripped away, please see git history for details)
|
||||||
// 2018-XX-XX: Initial version.
|
// 2018-06-XX: Initial version.
|
||||||
|
|
||||||
// Data
|
// Data
|
||||||
static clock_t g_Time = 0;
|
static clock_t g_Time = 0;
|
||||||
@ -28,31 +29,28 @@ bool ImGui_ImplOSX_Init()
|
|||||||
//io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
|
//io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
|
||||||
|
|
||||||
// Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
|
// Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
|
||||||
io.KeyMap[ImGuiKey_Tab] = 9;
|
const int offset_for_function_keys = 256 - 0xF700;
|
||||||
io.KeyMap[ImGuiKey_LeftArrow] = ImGuiKey_LeftArrow;
|
io.KeyMap[ImGuiKey_Tab] = '\t';
|
||||||
io.KeyMap[ImGuiKey_RightArrow] = ImGuiKey_RightArrow;
|
io.KeyMap[ImGuiKey_LeftArrow] = NSLeftArrowFunctionKey + offset_for_function_keys;
|
||||||
io.KeyMap[ImGuiKey_UpArrow] = ImGuiKey_UpArrow;
|
io.KeyMap[ImGuiKey_RightArrow] = NSRightArrowFunctionKey + offset_for_function_keys;
|
||||||
io.KeyMap[ImGuiKey_DownArrow] = ImGuiKey_DownArrow;
|
io.KeyMap[ImGuiKey_UpArrow] = NSUpArrowFunctionKey + offset_for_function_keys;
|
||||||
io.KeyMap[ImGuiKey_PageUp] = ImGuiKey_PageUp;
|
io.KeyMap[ImGuiKey_DownArrow] = NSDownArrowFunctionKey + offset_for_function_keys;
|
||||||
io.KeyMap[ImGuiKey_PageDown] = ImGuiKey_PageDown;
|
io.KeyMap[ImGuiKey_PageUp] = NSPageUpFunctionKey + offset_for_function_keys;
|
||||||
io.KeyMap[ImGuiKey_Home] = ImGuiKey_Home;
|
io.KeyMap[ImGuiKey_PageDown] = NSPageDownFunctionKey + offset_for_function_keys;
|
||||||
io.KeyMap[ImGuiKey_End] = ImGuiKey_End;
|
io.KeyMap[ImGuiKey_Home] = NSHomeFunctionKey + offset_for_function_keys;
|
||||||
io.KeyMap[ImGuiKey_Insert] = ImGuiKey_Insert;
|
io.KeyMap[ImGuiKey_End] = NSEndFunctionKey + offset_for_function_keys;
|
||||||
io.KeyMap[ImGuiKey_Delete] = ImGuiKey_Delete;
|
io.KeyMap[ImGuiKey_Insert] = NSInsertFunctionKey + offset_for_function_keys;
|
||||||
|
io.KeyMap[ImGuiKey_Delete] = NSDeleteFunctionKey + offset_for_function_keys;
|
||||||
io.KeyMap[ImGuiKey_Backspace] = 127;
|
io.KeyMap[ImGuiKey_Backspace] = 127;
|
||||||
io.KeyMap[ImGuiKey_Space] = 32;
|
io.KeyMap[ImGuiKey_Space] = 32;
|
||||||
io.KeyMap[ImGuiKey_Enter] = 13;
|
io.KeyMap[ImGuiKey_Enter] = 13;
|
||||||
io.KeyMap[ImGuiKey_Escape] = 27;
|
io.KeyMap[ImGuiKey_Escape] = 27;
|
||||||
io.KeyMap[ImGuiKey_A] = 'a';
|
io.KeyMap[ImGuiKey_A] = 'A';
|
||||||
io.KeyMap[ImGuiKey_C] = 'c';
|
io.KeyMap[ImGuiKey_C] = 'C';
|
||||||
io.KeyMap[ImGuiKey_V] = 'v';
|
io.KeyMap[ImGuiKey_V] = 'V';
|
||||||
io.KeyMap[ImGuiKey_X] = 'x';
|
io.KeyMap[ImGuiKey_X] = 'X';
|
||||||
io.KeyMap[ImGuiKey_Y] = 'y';
|
io.KeyMap[ImGuiKey_Y] = 'Y';
|
||||||
io.KeyMap[ImGuiKey_Z] = 'z';
|
io.KeyMap[ImGuiKey_Z] = 'Z';
|
||||||
|
|
||||||
// Time elapsed since last frame, in seconds
|
|
||||||
// (in this sample app we'll override this every frame because our time step is variable)
|
|
||||||
io.DeltaTime = 1.0f/60.0f;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -63,9 +61,8 @@ void ImGui_ImplOSX_Shutdown()
|
|||||||
|
|
||||||
void ImGui_ImplOSX_NewFrame(NSOpenGLView* view)
|
void ImGui_ImplOSX_NewFrame(NSOpenGLView* view)
|
||||||
{
|
{
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
|
||||||
|
|
||||||
// Setup display size
|
// Setup display size
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
const float dpi = [view.window backingScaleFactor];
|
const float dpi = [view.window backingScaleFactor];
|
||||||
io.DisplaySize = ImVec2((float)view.bounds.size.width, (float)view.bounds.size.height);
|
io.DisplaySize = ImVec2((float)view.bounds.size.width, (float)view.bounds.size.height);
|
||||||
io.DisplayFramebufferScale = ImVec2(dpi, dpi);
|
io.DisplayFramebufferScale = ImVec2(dpi, dpi);
|
||||||
@ -86,47 +83,26 @@ void ImGui_ImplOSX_NewFrame(NSOpenGLView* view)
|
|||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME-OSX: Store in io.KeysDown[]
|
static int mapCharacterToKey(int c)
|
||||||
static bool mapKeymap(int* keymap)
|
|
||||||
{
|
{
|
||||||
if (*keymap == NSUpArrowFunctionKey)
|
if (c >= 'a' && c <= 'z')
|
||||||
*keymap = ImGuiKey_LeftArrow;
|
return c - 'a' + 'A';
|
||||||
else if (*keymap == NSDownArrowFunctionKey)
|
if (c == 25) // SHIFT+TAB -> TAB
|
||||||
*keymap = ImGuiKey_DownArrow;
|
return 9;
|
||||||
else if (*keymap == NSLeftArrowFunctionKey)
|
if (c >= 0 && c < 256)
|
||||||
*keymap = ImGuiKey_LeftArrow;
|
return c;
|
||||||
else if (*keymap == NSRightArrowFunctionKey)
|
if (c >= 0xF700 && c < 0xF700 + 256)
|
||||||
*keymap = ImGuiKey_RightArrow;
|
return c - 0xF700 + 256;
|
||||||
else if (*keymap == NSHomeFunctionKey)
|
return -1;
|
||||||
*keymap = ImGuiKey_Home;
|
|
||||||
else if (*keymap == NSEndFunctionKey)
|
|
||||||
*keymap = ImGuiKey_End;
|
|
||||||
else if (*keymap == NSDeleteFunctionKey)
|
|
||||||
*keymap = ImGuiKey_Delete;
|
|
||||||
else if (*keymap == 25) // SHIFT + TAB
|
|
||||||
*keymap = 9; // TAB
|
|
||||||
else
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ResetKeys()
|
// Reset the non-characters keys
|
||||||
|
static void resetKeys()
|
||||||
{
|
{
|
||||||
// FIXME-OSX: Mapping
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_A]] = false;
|
for (int n = 0; n < IM_ARRAYSIZE(io.KeyMap); n++)
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_C]] = false;
|
if (io.KeyMap[n] != -1 && io.KeyMap[n] >= 256)
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_V]] = false;
|
io.KeysDown[io.KeyMap[n]] = false;
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_X]] = false;
|
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_Y]] = false;
|
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_Z]] = false;
|
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_LeftArrow]] = false;
|
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_RightArrow]] = false;
|
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_Tab]] = false;
|
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_UpArrow]] = false;
|
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_DownArrow]] = false;
|
|
||||||
io.KeysDown[io.KeyMap[ImGuiKey_Tab]] = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ImGui_ImplOSX_HandleEvent(NSEvent* event)
|
bool ImGui_ImplOSX_HandleEvent(NSEvent* event)
|
||||||
@ -186,16 +162,16 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event)
|
|||||||
int len = (int)[str length];
|
int len = (int)[str length];
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
int keymap = [str characterAtIndex:i];
|
int c = [str characterAtIndex:i];
|
||||||
if (mapKeymap(&keymap) && !io.KeyCtrl)
|
if (c < 0xF700 && !io.KeyCtrl)
|
||||||
io.AddInputCharacter(keymap);
|
io.AddInputCharacter(c);
|
||||||
if (keymap < 512)
|
|
||||||
{
|
|
||||||
// We must reset in case we're pressing a sequence of special keys while keeping the command pressed
|
// We must reset in case we're pressing a sequence of special keys while keeping the command pressed
|
||||||
if (io.KeyCtrl)
|
int key = mapCharacterToKey(c);
|
||||||
ResetKeys();
|
if (key != -1 && key < 256 && !io.KeyCtrl)
|
||||||
io.KeysDown[keymap] = true;
|
resetKeys();
|
||||||
}
|
if (key != -1)
|
||||||
|
io.KeysDown[key] = true;
|
||||||
}
|
}
|
||||||
return io.WantCaptureKeyboard;
|
return io.WantCaptureKeyboard;
|
||||||
}
|
}
|
||||||
@ -206,29 +182,31 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event)
|
|||||||
int len = (int)[str length];
|
int len = (int)[str length];
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
int keymap = [str characterAtIndex:i];
|
int c = [str characterAtIndex:i];
|
||||||
mapKeymap(&keymap);
|
int key = mapCharacterToKey(c);
|
||||||
if (keymap < 512)
|
if (key != -1)
|
||||||
io.KeysDown[keymap] = false;
|
io.KeysDown[key] = false;
|
||||||
}
|
}
|
||||||
return io.WantCaptureKeyboard;
|
return io.WantCaptureKeyboard;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type == NSEventTypeFlagsChanged)
|
if (event.type == NSEventTypeFlagsChanged)
|
||||||
{
|
{
|
||||||
unsigned int flags;
|
|
||||||
flags = [event modifierFlags] & NSDeviceIndependentModifierFlagsMask;
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
bool wasKeyShift= io.KeyShift;
|
unsigned int flags = [event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask;
|
||||||
bool wasKeyCtrl = io.KeyCtrl;
|
|
||||||
io.KeyShift = flags & NSShiftKeyMask;
|
bool oldKeyCtrl = io.KeyCtrl;
|
||||||
io.KeyCtrl = flags & NSCommandKeyMask;
|
bool oldKeyShift = io.KeyShift;
|
||||||
bool keyShiftReleased = wasKeyShift && !io.KeyShift;
|
bool oldKeyAlt = io.KeyAlt;
|
||||||
bool keyCtrlReleased = wasKeyCtrl && !io.KeyCtrl;
|
bool oldKeySuper = io.KeySuper;
|
||||||
|
io.KeyCtrl = flags & NSEventModifierFlagControl;
|
||||||
|
io.KeyShift = flags & NSEventModifierFlagShift;
|
||||||
|
io.KeyAlt = flags & NSEventModifierFlagOption;
|
||||||
|
io.KeySuper = flags & NSEventModifierFlagCommand;
|
||||||
|
|
||||||
// We must reset them as we will not receive any keyUp event if they where pressed during shift or command
|
// We must reset them as we will not receive any keyUp event if they where pressed during shift or command
|
||||||
if (keyShiftReleased || keyCtrlReleased)
|
if ((oldKeyShift && !io.KeyShift) || (oldKeyCtrl && !io.KeyCtrl))
|
||||||
ResetKeys();
|
resetKeys();
|
||||||
return io.WantCaptureKeyboard;
|
return io.WantCaptureKeyboard;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user