569 lines
20 KiB
C++
569 lines
20 KiB
C++
#include "UhdkPayload.hpp"
|
|
// Example-local platform backend. Docking is supported; OS multi-viewports are not.
|
|
// Clipboard reads are asynchronous: ImGui's synchronous getter uses a cache,
|
|
// refreshed on focus and every 250 ms. No native waits occur inside GL frames.
|
|
// IME commits/candidate positioning work; preedit rendering and replacement of
|
|
// surrounding text are not implemented (ImGui owns the editable text buffers).
|
|
#include "imgui.h"
|
|
#include "imgui_impl_uhdk.h"
|
|
#include <cfloat>
|
|
#include <string>
|
|
|
|
struct ImGui_ImplUHDK_Data {
|
|
uhdk_session *session = nullptr;
|
|
uhdk_window *window = nullptr;
|
|
uint64_t time = 0;
|
|
uint64_t clipboard_time = 0;
|
|
uint64_t acknowledged_input = 0;
|
|
uhdk_request_tag clipboard_tag = 0;
|
|
bool clipboard_pending = false;
|
|
bool discard_clipboard = false;
|
|
std::string clipboard;
|
|
uhdk_modifier_flags modifiers = 0;
|
|
unsigned mouse_buttons = 0;
|
|
ImGuiMouseCursor cursor = ImGuiMouseCursor_COUNT;
|
|
bool focused = false;
|
|
bool text_enabled = false;
|
|
bool composing = false;
|
|
bool macos = false;
|
|
uhdk_rect caret = {};
|
|
};
|
|
|
|
static ImGui_ImplUHDK_Data *Data() {
|
|
return static_cast<ImGui_ImplUHDK_Data *>(ImGui::GetIO().BackendPlatformUserData);
|
|
}
|
|
|
|
static void Modifiers(uhdk_modifier_flags mods) {
|
|
ImGuiIO &io = ImGui::GetIO();
|
|
io.AddKeyEvent(ImGuiMod_Ctrl, (mods & UHDK_MOD_CONTROL) != 0);
|
|
io.AddKeyEvent(ImGuiMod_Shift, (mods & UHDK_MOD_SHIFT) != 0);
|
|
io.AddKeyEvent(ImGuiMod_Alt, (mods & UHDK_MOD_ALT) != 0);
|
|
io.AddKeyEvent(ImGuiMod_Super, (mods & UHDK_MOD_SUPER) != 0);
|
|
}
|
|
|
|
static ImGuiKey Key(uint32_t physical, uhdk_key logical) {
|
|
// USB HID usages, including modifiers and keypad. Text comes from TEXT_COMMIT.
|
|
if (physical >= 0x04 && physical <= 0x1d)
|
|
return ImGuiKey(ImGuiKey_A + physical - 0x04);
|
|
if (physical >= 0x1e && physical <= 0x26)
|
|
return ImGuiKey(ImGuiKey_1 + physical - 0x1e);
|
|
if (physical >= 0x3a && physical <= 0x45)
|
|
return ImGuiKey(ImGuiKey_F1 + physical - 0x3a);
|
|
if (physical >= 0x68 && physical <= 0x73)
|
|
return ImGuiKey(ImGuiKey_F13 + physical - 0x68);
|
|
if (physical >= 0x59 && physical <= 0x61)
|
|
return ImGuiKey(ImGuiKey_Keypad1 + physical - 0x59);
|
|
switch (physical) {
|
|
case 0x27:
|
|
return ImGuiKey_0;
|
|
case 0x28:
|
|
return ImGuiKey_Enter;
|
|
case 0x29:
|
|
return ImGuiKey_Escape;
|
|
case 0x2a:
|
|
return ImGuiKey_Backspace;
|
|
case 0x2b:
|
|
return ImGuiKey_Tab;
|
|
case 0x2c:
|
|
return ImGuiKey_Space;
|
|
case 0x2d:
|
|
return ImGuiKey_Minus;
|
|
case 0x2e:
|
|
return ImGuiKey_Equal;
|
|
case 0x2f:
|
|
return ImGuiKey_LeftBracket;
|
|
case 0x30:
|
|
return ImGuiKey_RightBracket;
|
|
case 0x31:
|
|
return ImGuiKey_Backslash;
|
|
case 0x33:
|
|
return ImGuiKey_Semicolon;
|
|
case 0x34:
|
|
return ImGuiKey_Apostrophe;
|
|
case 0x35:
|
|
return ImGuiKey_GraveAccent;
|
|
case 0x36:
|
|
return ImGuiKey_Comma;
|
|
case 0x37:
|
|
return ImGuiKey_Period;
|
|
case 0x38:
|
|
return ImGuiKey_Slash;
|
|
case 0x39:
|
|
return ImGuiKey_CapsLock;
|
|
case 0x46:
|
|
return ImGuiKey_PrintScreen;
|
|
case 0x47:
|
|
return ImGuiKey_ScrollLock;
|
|
case 0x48:
|
|
return ImGuiKey_Pause;
|
|
case 0x49:
|
|
return ImGuiKey_Insert;
|
|
case 0x4a:
|
|
return ImGuiKey_Home;
|
|
case 0x4b:
|
|
return ImGuiKey_PageUp;
|
|
case 0x4c:
|
|
return ImGuiKey_Delete;
|
|
case 0x4d:
|
|
return ImGuiKey_End;
|
|
case 0x4e:
|
|
return ImGuiKey_PageDown;
|
|
case 0x4f:
|
|
return ImGuiKey_RightArrow;
|
|
case 0x50:
|
|
return ImGuiKey_LeftArrow;
|
|
case 0x51:
|
|
return ImGuiKey_DownArrow;
|
|
case 0x52:
|
|
return ImGuiKey_UpArrow;
|
|
case 0x53:
|
|
return ImGuiKey_NumLock;
|
|
case 0x54:
|
|
return ImGuiKey_KeypadDivide;
|
|
case 0x55:
|
|
return ImGuiKey_KeypadMultiply;
|
|
case 0x56:
|
|
return ImGuiKey_KeypadSubtract;
|
|
case 0x57:
|
|
return ImGuiKey_KeypadAdd;
|
|
case 0x58:
|
|
return ImGuiKey_KeypadEnter;
|
|
case 0x62:
|
|
return ImGuiKey_Keypad0;
|
|
case 0x63:
|
|
return ImGuiKey_KeypadDecimal;
|
|
case 0x64:
|
|
return ImGuiKey_Oem102;
|
|
case 0x65:
|
|
return ImGuiKey_Menu;
|
|
case 0x67:
|
|
return ImGuiKey_KeypadEqual;
|
|
case 0xe0:
|
|
return ImGuiKey_LeftCtrl;
|
|
case 0xe1:
|
|
return ImGuiKey_LeftShift;
|
|
case 0xe2:
|
|
return ImGuiKey_LeftAlt;
|
|
case 0xe3:
|
|
return ImGuiKey_LeftSuper;
|
|
case 0xe4:
|
|
return ImGuiKey_RightCtrl;
|
|
case 0xe5:
|
|
return ImGuiKey_RightShift;
|
|
case 0xe6:
|
|
return ImGuiKey_RightAlt;
|
|
case 0xe7:
|
|
return ImGuiKey_RightSuper;
|
|
default:
|
|
break;
|
|
}
|
|
if (logical >= 'a' && logical <= 'z')
|
|
return ImGuiKey(ImGuiKey_A + logical - 'a');
|
|
if (logical >= 'A' && logical <= 'Z')
|
|
return ImGuiKey(ImGuiKey_A + logical - 'A');
|
|
return ImGuiKey_None;
|
|
}
|
|
|
|
static bool NativeEditKey(ImGuiKey key) {
|
|
return key == ImGuiKey_Backspace || key == ImGuiKey_Delete || key == ImGuiKey_LeftArrow ||
|
|
key == ImGuiKey_RightArrow || key == ImGuiKey_UpArrow || key == ImGuiKey_DownArrow ||
|
|
key == ImGuiKey_Home || key == ImGuiKey_End || key == ImGuiKey_Enter ||
|
|
key == ImGuiKey_KeypadEnter || key == ImGuiKey_Tab || key == ImGuiKey_PageUp ||
|
|
key == ImGuiKey_PageDown || key == ImGuiKey_Escape;
|
|
}
|
|
|
|
static void Edit(const uhdk_event &event) {
|
|
auto *bd = Data();
|
|
ImGuiKey key = ImGuiKey_None;
|
|
uhdk_modifier_flags mods =
|
|
UhdkData<uhdk_edit_event>(event).extend_selection ? UHDK_MOD_SHIFT : 0;
|
|
const auto shortcut = bd->macos ? UHDK_MOD_SUPER : UHDK_MOD_CONTROL;
|
|
switch (UhdkData<uhdk_edit_event>(event).command) {
|
|
case UHDK_EDIT_COPY:
|
|
key = ImGuiKey_C;
|
|
mods |= shortcut;
|
|
break;
|
|
case UHDK_EDIT_CUT:
|
|
key = ImGuiKey_X;
|
|
mods |= shortcut;
|
|
break;
|
|
case UHDK_EDIT_PASTE:
|
|
key = ImGuiKey_V;
|
|
mods |= shortcut;
|
|
break;
|
|
case UHDK_EDIT_SELECT_ALL:
|
|
key = ImGuiKey_A;
|
|
mods |= shortcut;
|
|
break;
|
|
case UHDK_EDIT_UNDO:
|
|
key = ImGuiKey_Z;
|
|
mods |= shortcut;
|
|
break;
|
|
case UHDK_EDIT_REDO:
|
|
key = ImGuiKey_Z;
|
|
mods |= shortcut | UHDK_MOD_SHIFT;
|
|
break;
|
|
case UHDK_EDIT_DELETE_WORD_BACKWARD:
|
|
key = ImGuiKey_Backspace;
|
|
mods |= bd->macos ? UHDK_MOD_ALT : UHDK_MOD_CONTROL;
|
|
break;
|
|
case UHDK_EDIT_DELETE_WORD_FORWARD:
|
|
key = ImGuiKey_Delete;
|
|
mods |= bd->macos ? UHDK_MOD_ALT : UHDK_MOD_CONTROL;
|
|
break;
|
|
case UHDK_EDIT_DELETE_LINE_START:
|
|
if (!bd->macos)
|
|
return;
|
|
key = ImGuiKey_Backspace;
|
|
mods |= UHDK_MOD_SUPER;
|
|
break;
|
|
case UHDK_EDIT_DELETE_BACKWARD:
|
|
key = ImGuiKey_Backspace;
|
|
break;
|
|
case UHDK_EDIT_DELETE_FORWARD:
|
|
key = ImGuiKey_Delete;
|
|
break;
|
|
case UHDK_EDIT_MOVE_LEFT:
|
|
case UHDK_EDIT_MOVE_BACKWARD:
|
|
key = ImGuiKey_LeftArrow;
|
|
break;
|
|
case UHDK_EDIT_MOVE_RIGHT:
|
|
case UHDK_EDIT_MOVE_FORWARD:
|
|
key = ImGuiKey_RightArrow;
|
|
break;
|
|
case UHDK_EDIT_MOVE_UP:
|
|
key = ImGuiKey_UpArrow;
|
|
break;
|
|
case UHDK_EDIT_MOVE_DOWN:
|
|
key = ImGuiKey_DownArrow;
|
|
break;
|
|
case UHDK_EDIT_LINE_LEFT_END:
|
|
case UHDK_EDIT_LINE_START:
|
|
key = ImGuiKey_Home;
|
|
break;
|
|
case UHDK_EDIT_LINE_RIGHT_END:
|
|
case UHDK_EDIT_LINE_END:
|
|
key = ImGuiKey_End;
|
|
break;
|
|
case UHDK_EDIT_DOCUMENT_START:
|
|
key = ImGuiKey_Home;
|
|
mods |= shortcut;
|
|
break;
|
|
case UHDK_EDIT_DOCUMENT_END:
|
|
key = ImGuiKey_End;
|
|
mods |= shortcut;
|
|
break;
|
|
case UHDK_EDIT_WORD_LEFT:
|
|
case UHDK_EDIT_WORD_BACKWARD:
|
|
key = ImGuiKey_LeftArrow;
|
|
mods |= bd->macos ? UHDK_MOD_ALT : UHDK_MOD_CONTROL;
|
|
break;
|
|
case UHDK_EDIT_WORD_RIGHT:
|
|
case UHDK_EDIT_WORD_FORWARD:
|
|
key = ImGuiKey_RightArrow;
|
|
mods |= bd->macos ? UHDK_MOD_ALT : UHDK_MOD_CONTROL;
|
|
break;
|
|
case UHDK_EDIT_PAGE_UP:
|
|
key = ImGuiKey_PageUp;
|
|
break;
|
|
case UHDK_EDIT_PAGE_DOWN:
|
|
key = ImGuiKey_PageDown;
|
|
break;
|
|
case UHDK_EDIT_CANCEL:
|
|
key = ImGuiKey_Escape;
|
|
break;
|
|
case UHDK_EDIT_LINE_BREAK:
|
|
key = ImGuiKey_Enter;
|
|
mods |= UHDK_MOD_SHIFT;
|
|
break;
|
|
case UHDK_EDIT_BACKTAB:
|
|
key = ImGuiKey_Tab;
|
|
mods |= UHDK_MOD_SHIFT;
|
|
break;
|
|
case UHDK_EDIT_NEWLINE:
|
|
key = ImGuiKey_Enter;
|
|
mods |= bd->modifiers & UHDK_MOD_CONTROL;
|
|
break;
|
|
case UHDK_EDIT_TAB:
|
|
key = ImGuiKey_Tab;
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
Modifiers(mods);
|
|
ImGui::GetIO().AddKeyEvent(key, true);
|
|
ImGui::GetIO().AddKeyEvent(key, false);
|
|
Modifiers(bd->modifiers);
|
|
}
|
|
|
|
static const char *GetClipboard(ImGuiContext *) {
|
|
return Data()->clipboard.c_str();
|
|
}
|
|
|
|
static void SetClipboard(ImGuiContext *, const char *text) {
|
|
auto *bd = Data();
|
|
// Cancel an older read so its eventual completion cannot overwrite this copy.
|
|
if (bd->clipboard_pending)
|
|
uhdk_request_cancel(bd->session, bd->clipboard_tag);
|
|
bd->discard_clipboard = bd->clipboard_pending;
|
|
bd->clipboard.assign(text);
|
|
bd->clipboard_time = uhdk_get_time_ns(bd->session);
|
|
uhdk_clipboard_write_text(bd->session, {text, bd->clipboard.size()});
|
|
}
|
|
|
|
static void SetImeData(ImGuiContext *, ImGuiViewport *, ImGuiPlatformImeData *ime) {
|
|
auto *bd = Data();
|
|
if (bd->text_enabled != ime->WantTextInput) {
|
|
if (uhdk_window_set_text_input(bd->window, ime->WantTextInput) == UHDK_STATUS_OK)
|
|
bd->text_enabled = ime->WantTextInput;
|
|
}
|
|
bd->caret = {ime->InputPos.x, ime->InputPos.y, 1.0f, ime->InputLineHeight};
|
|
}
|
|
|
|
bool ImGui_ImplUHDK_Init(uhdk_session *session, uhdk_request_tag clipboard_tag) {
|
|
uhdk_window *window = uhdk_get_window(session);
|
|
ImGuiIO &io = ImGui::GetIO();
|
|
IM_ASSERT(io.BackendPlatformUserData == nullptr);
|
|
uhdk_window_metrics metrics = {};
|
|
metrics.struct_size = sizeof(metrics);
|
|
if (!session || !clipboard_tag || !window ||
|
|
uhdk_window_get_metrics(window, &metrics) != UHDK_STATUS_OK)
|
|
return false;
|
|
auto *bd = IM_NEW(ImGui_ImplUHDK_Data)();
|
|
bd->session = session;
|
|
bd->clipboard_tag = clipboard_tag;
|
|
bd->window = window;
|
|
bd->focused = metrics.focused;
|
|
bd->macos = uhdk_get_backend(session) == UHDK_BACKEND_MACOS;
|
|
io.BackendPlatformUserData = bd;
|
|
io.BackendPlatformName = "imgui_impl_uhdk";
|
|
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
|
|
io.ConfigMacOSXBehaviors = bd->macos;
|
|
io.AddFocusEvent(bd->focused);
|
|
auto &platform = ImGui::GetPlatformIO();
|
|
platform.Platform_GetClipboardTextFn = GetClipboard;
|
|
platform.Platform_SetClipboardTextFn = SetClipboard;
|
|
platform.Platform_SetImeDataFn = SetImeData;
|
|
ImGui::GetMainViewport()->PlatformHandle = window;
|
|
return true;
|
|
}
|
|
|
|
void ImGui_ImplUHDK_Shutdown() {
|
|
auto *bd = Data();
|
|
IM_ASSERT(bd);
|
|
if (bd->clipboard_pending)
|
|
uhdk_request_cancel(bd->session, bd->clipboard_tag);
|
|
uhdk_window_set_text_input(bd->window, false);
|
|
uhdk_window_set_pointer_mode(bd->window, UHDK_POINTER_NORMAL);
|
|
uhdk_window_set_cursor_shape(bd->window, UHDK_CURSOR_ARROW);
|
|
auto &platform = ImGui::GetPlatformIO();
|
|
platform.Platform_GetClipboardTextFn = nullptr;
|
|
platform.Platform_SetClipboardTextFn = nullptr;
|
|
platform.Platform_SetImeDataFn = nullptr;
|
|
ImGui::GetMainViewport()->PlatformHandle = nullptr;
|
|
ImGuiIO &io = ImGui::GetIO();
|
|
io.BackendFlags &= ~ImGuiBackendFlags_HasMouseCursors;
|
|
io.BackendPlatformName = nullptr;
|
|
io.BackendPlatformUserData = nullptr;
|
|
IM_DELETE(bd);
|
|
}
|
|
|
|
void ImGui_ImplUHDK_ProcessEvent(const uhdk_event *event) {
|
|
auto *bd = Data();
|
|
ImGuiIO &io = ImGui::GetIO();
|
|
if (event->type == UHDK_EVENT_REQUEST_DONE && bd->clipboard_pending &&
|
|
UhdkData<uhdk_request_result>(*event).tag == bd->clipboard_tag) {
|
|
const auto &result = UhdkData<uhdk_request_result>(*event);
|
|
bd->clipboard_pending = false;
|
|
if (bd->discard_clipboard) {
|
|
bd->discard_clipboard = false;
|
|
return;
|
|
}
|
|
bd->clipboard.clear();
|
|
if (result.status == UHDK_STATUS_OK && UhdkResult<uhdk_text>(result).len)
|
|
bd->clipboard.assign(
|
|
UhdkResult<uhdk_text>(result).ptr, size_t(UhdkResult<uhdk_text>(result).len)
|
|
);
|
|
return;
|
|
}
|
|
if (event->window != bd->window)
|
|
return;
|
|
switch (event->type) {
|
|
case UHDK_EVENT_WINDOW_METRICS:
|
|
if (bd->focused != bool(UhdkData<uhdk_window_metrics>(*event).focused)) {
|
|
bd->focused = UhdkData<uhdk_window_metrics>(*event).focused;
|
|
io.AddFocusEvent(bd->focused);
|
|
bd->clipboard_time = 0;
|
|
if (!bd->focused) {
|
|
bd->mouse_buttons = 0;
|
|
bd->modifiers = 0;
|
|
bd->composing = false;
|
|
}
|
|
}
|
|
break;
|
|
case UHDK_EVENT_POINTER_ENTER:
|
|
case UHDK_EVENT_POINTER_MOVE:
|
|
io.AddMousePosEvent(
|
|
UhdkData<uhdk_pointer_event>(*event).position.x,
|
|
UhdkData<uhdk_pointer_event>(*event).position.y
|
|
);
|
|
break;
|
|
case UHDK_EVENT_POINTER_LEAVE:
|
|
if (!bd->mouse_buttons)
|
|
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
|
|
break;
|
|
case UHDK_EVENT_POINTER_BUTTON: {
|
|
const auto &pointer = UhdkData<uhdk_pointer_event>(*event);
|
|
int button = int(pointer.button) - 1;
|
|
if (button < 0 || button >= 5)
|
|
break;
|
|
bd->modifiers = pointer.modifiers;
|
|
Modifiers(bd->modifiers);
|
|
io.AddMousePosEvent(pointer.position.x, pointer.position.y);
|
|
io.AddMouseButtonEvent(button, pointer.pressed);
|
|
unsigned before = bd->mouse_buttons;
|
|
if (pointer.pressed)
|
|
bd->mouse_buttons |= 1u << button;
|
|
else
|
|
bd->mouse_buttons &= ~(1u << button);
|
|
if (bool(before) != bool(bd->mouse_buttons)) {
|
|
uhdk_window_set_pointer_mode(
|
|
bd->window, bd->mouse_buttons ? UHDK_POINTER_CAPTURE : UHDK_POINTER_NORMAL
|
|
);
|
|
}
|
|
break;
|
|
}
|
|
case UHDK_EVENT_POINTER_MODE_CHANGED:
|
|
if (UhdkData<uhdk_pointer_mode_event>(*event).mode == UHDK_POINTER_NORMAL) {
|
|
for (int i = 0; i < 5; ++i)
|
|
if (bd->mouse_buttons & (1u << i))
|
|
io.AddMouseButtonEvent(i, false);
|
|
bd->mouse_buttons = 0;
|
|
}
|
|
break;
|
|
case UHDK_EVENT_SCROLL: {
|
|
// UHDK uses right/down-positive deltas; ImGui uses left/up-positive.
|
|
float unit = UhdkData<uhdk_scroll_event>(*event).unit == UHDK_SCROLL_LOGICAL_PIXELS
|
|
? 1.0f / 40.0f
|
|
: 1.0f;
|
|
io.AddMouseWheelEvent(
|
|
-UhdkData<uhdk_scroll_event>(*event).delta.x * unit,
|
|
-UhdkData<uhdk_scroll_event>(*event).delta.y * unit
|
|
);
|
|
break;
|
|
}
|
|
case UHDK_EVENT_KEY: {
|
|
const auto &key = UhdkData<uhdk_key_event>(*event);
|
|
bd->modifiers = key.modifiers;
|
|
Modifiers(bd->modifiers);
|
|
ImGuiKey mapped = Key(key.physical_key, key.logical_key);
|
|
// Native EDIT_COMMAND drives editing/repeat while text input is enabled.
|
|
bool native_edit = bd->text_enabled && NativeEditKey(mapped);
|
|
if (bd->text_enabled && !bd->macos) {
|
|
const bool ctrl = (key.modifiers & UHDK_MOD_CONTROL) != 0;
|
|
const bool shift = (key.modifiers & UHDK_MOD_SHIFT) != 0;
|
|
const bool reserved = (key.modifiers & (UHDK_MOD_ALT | UHDK_MOD_SUPER)) != 0;
|
|
const bool clipboard =
|
|
ctrl &&
|
|
((!shift && (mapped == ImGuiKey_C || mapped == ImGuiKey_X || mapped == ImGuiKey_V ||
|
|
mapped == ImGuiKey_A || mapped == ImGuiKey_Y)) ||
|
|
mapped == ImGuiKey_Z);
|
|
const bool insert_alias = mapped == ImGuiKey_Insert && ctrl != shift;
|
|
native_edit = !reserved && (native_edit || clipboard || insert_alias);
|
|
if (ctrl && (mapped == ImGuiKey_Tab || mapped == ImGuiKey_Enter ||
|
|
mapped == ImGuiKey_KeypadEnter || mapped == ImGuiKey_PageUp ||
|
|
mapped == ImGuiKey_PageDown))
|
|
native_edit = false;
|
|
if ((ctrl || shift) && mapped == ImGuiKey_Escape)
|
|
native_edit = false;
|
|
}
|
|
if (mapped != ImGuiKey_None && !native_edit && !key.repeat &&
|
|
(!bd->composing || !key.pressed))
|
|
io.AddKeyEvent(mapped, key.pressed);
|
|
break;
|
|
}
|
|
case UHDK_EVENT_TEXT_COMMIT:
|
|
bd->acknowledged_input = event->input_id;
|
|
if (UhdkData<uhdk_text_event>(*event).text.len) {
|
|
std::string text(
|
|
UhdkData<uhdk_text_event>(*event).text.ptr,
|
|
size_t(UhdkData<uhdk_text_event>(*event).text.len)
|
|
);
|
|
io.AddInputCharactersUTF8(text.c_str());
|
|
}
|
|
break;
|
|
case UHDK_EVENT_TEXT_PREEDIT:
|
|
bd->acknowledged_input = event->input_id;
|
|
bd->composing = true;
|
|
break;
|
|
case UHDK_EVENT_TEXT_COMPOSITION_END:
|
|
bd->acknowledged_input = event->input_id;
|
|
bd->composing = false;
|
|
break;
|
|
case UHDK_EVENT_EDIT_COMMAND:
|
|
bd->acknowledged_input = event->input_id;
|
|
Edit(*event);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ImGui_ImplUHDK_NewFrame() {
|
|
auto *bd = Data();
|
|
ImGuiIO &io = ImGui::GetIO();
|
|
IM_ASSERT(!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable));
|
|
uhdk_window_metrics metrics = {};
|
|
metrics.struct_size = sizeof(metrics);
|
|
if (uhdk_window_get_metrics(bd->window, &metrics) == UHDK_STATUS_OK) {
|
|
io.DisplaySize = ImVec2(metrics.logical_size.width, metrics.logical_size.height);
|
|
if (io.DisplaySize.x > 0 && io.DisplaySize.y > 0)
|
|
io.DisplayFramebufferScale = ImVec2(
|
|
metrics.framebuffer_width / io.DisplaySize.x,
|
|
metrics.framebuffer_height / io.DisplaySize.y
|
|
);
|
|
}
|
|
uint64_t now = uhdk_get_time_ns(bd->session);
|
|
io.DeltaTime = bd->time && now > bd->time ? float(double(now - bd->time) / 1e9) : 1.0f / 60.0f;
|
|
bd->time = now;
|
|
if (bd->focused && !bd->clipboard_pending &&
|
|
(!bd->clipboard_time || now - bd->clipboard_time >= 250000000)) {
|
|
bd->clipboard_pending =
|
|
uhdk_clipboard_read_text_async(bd->session, 8 * 1024 * 1024, bd->clipboard_tag) ==
|
|
UHDK_STATUS_OK;
|
|
bd->clipboard_time = now;
|
|
}
|
|
if (!(io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)) {
|
|
ImGuiMouseCursor cursor =
|
|
io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
|
|
if (cursor != bd->cursor) {
|
|
static const uhdk_cursor_shape shapes[] = {
|
|
UHDK_CURSOR_ARROW,
|
|
UHDK_CURSOR_TEXT,
|
|
UHDK_CURSOR_MOVE,
|
|
UHDK_CURSOR_RESIZE_VERTICAL,
|
|
UHDK_CURSOR_RESIZE_HORIZONTAL,
|
|
UHDK_CURSOR_RESIZE_NE_SW,
|
|
UHDK_CURSOR_RESIZE_NW_SE,
|
|
UHDK_CURSOR_HAND,
|
|
UHDK_CURSOR_WAIT,
|
|
UHDK_CURSOR_WAIT,
|
|
UHDK_CURSOR_NOT_ALLOWED,
|
|
};
|
|
static_assert(IM_ARRAYSIZE(shapes) == ImGuiMouseCursor_COUNT, "Update cursor mapping");
|
|
if (uhdk_window_set_cursor_shape(
|
|
bd->window,
|
|
cursor == ImGuiMouseCursor_None ? UHDK_CURSOR_HIDDEN : shapes[cursor]
|
|
) == UHDK_STATUS_OK)
|
|
bd->cursor = cursor;
|
|
}
|
|
}
|
|
if (bd->text_enabled) {
|
|
uhdk_text_state state = {};
|
|
state.struct_size = sizeof(state);
|
|
state.acknowledged_input_id = bd->acknowledged_input;
|
|
state.caret = bd->caret;
|
|
uhdk_window_set_text_state(bd->window, &state);
|
|
}
|
|
}
|