imgui: handle touch input, set style and don't call al_set_mouse_cursor, which isn't implemented on some platforms

This commit is contained in:
Sebastian Krzyszkowiak 2018-11-29 21:09:20 +01:00
parent 71fec20936
commit 2149c11ace
No known key found for this signature in database
GPG key ID: E8F235CF3BDBC3FF
3 changed files with 48 additions and 33 deletions

View file

@ -1,4 +1,5 @@
// dear imgui: Renderer + Platform Binding for Allegro 5 // dear imgui: Renderer + Platform Binding for Allegro 5
// Adapted from C++ to C for libsuperderpy.
// (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.) // (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.)
// Implemented features: // Implemented features:
@ -42,7 +43,6 @@
static ALLEGRO_DISPLAY* g_Display = NULL; static ALLEGRO_DISPLAY* g_Display = NULL;
static ALLEGRO_BITMAP* g_Texture = NULL; static ALLEGRO_BITMAP* g_Texture = NULL;
static double g_Time = 0.0; static double g_Time = 0.0;
static ALLEGRO_MOUSE_CURSOR* g_MouseCursorInvisible = NULL;
static ALLEGRO_VERTEX_DECL* g_VertexDecl = NULL; static ALLEGRO_VERTEX_DECL* g_VertexDecl = NULL;
static char* g_ClipboardTextData = NULL; static char* g_ClipboardTextData = NULL;
@ -177,12 +177,6 @@ SYMBOL_EXPORT bool ImGui_ImplAllegro5_CreateDeviceObjects() {
io->Fonts->TexID = (void*)cloned_img; io->Fonts->TexID = (void*)cloned_img;
g_Texture = cloned_img; g_Texture = cloned_img;
// Create an invisible mouse cursor
// Because al_hide_mouse_cursor() seems to mess up with the actual inputs..
ALLEGRO_BITMAP* mouse_cursor = al_create_bitmap(8, 8);
g_MouseCursorInvisible = al_create_mouse_cursor(mouse_cursor, 0, 0);
al_destroy_bitmap(mouse_cursor);
return true; return true;
} }
@ -192,10 +186,6 @@ SYMBOL_EXPORT void ImGui_ImplAllegro5_InvalidateDeviceObjects() {
igGetIO()->Fonts->TexID = NULL; igGetIO()->Fonts->TexID = NULL;
g_Texture = NULL; g_Texture = NULL;
} }
if (g_MouseCursorInvisible) {
al_destroy_mouse_cursor(g_MouseCursorInvisible);
g_MouseCursorInvisible = NULL;
}
} }
#if ALLEGRO_HAS_CLIPBOARD #if ALLEGRO_HAS_CLIPBOARD
@ -281,8 +271,46 @@ SYMBOL_EXPORT bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* ev) {
switch (ev->type) { switch (ev->type) {
case ALLEGRO_EVENT_MOUSE_AXES: case ALLEGRO_EVENT_MOUSE_AXES:
io->MouseWheel += ev->mouse.dz; if (ev->mouse.display == g_Display) {
io->MouseWheelH += ev->mouse.dw; io->MouseWheel += ev->mouse.dz;
io->MouseWheelH += ev->mouse.dw;
io->MousePos = (ImVec2){.x = ev->mouse.x, .y = ev->mouse.y};
}
return true;
case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
if (ev->mouse.display == g_Display) {
if (ev->mouse.button <= 5) {
io->MouseDown[ev->mouse.button - 1] = true;
}
}
return true;
case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
if (ev->mouse.display == g_Display) {
if (ev->mouse.button <= 5) {
io->MouseDown[ev->mouse.button - 1] = false;
}
}
return true;
case ALLEGRO_EVENT_TOUCH_MOVE:
if (ev->touch.display == g_Display) {
io->MousePos = (ImVec2){.x = ev->touch.x, .y = ev->touch.y};
}
return true;
case ALLEGRO_EVENT_TOUCH_BEGIN:
if (ev->touch.display == g_Display) {
io->MouseDown[0] = true;
}
return true;
case ALLEGRO_EVENT_TOUCH_END:
case ALLEGRO_EVENT_TOUCH_CANCEL:
if (ev->touch.display == g_Display) {
io->MouseDown[0] = false;
}
return true;
case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
if (ev->mouse.display == g_Display) {
io->MousePos = (ImVec2){.x = -FLT_MAX, .y = -FLT_MAX};
}
return true; return true;
case ALLEGRO_EVENT_KEY_CHAR: case ALLEGRO_EVENT_KEY_CHAR:
if (ev->keyboard.display == g_Display) { if (ev->keyboard.display == g_Display) {
@ -310,8 +338,9 @@ static void ImGui_ImplAllegro5_UpdateMouseCursor() {
ImGuiMouseCursor imgui_cursor = igGetMouseCursor(); ImGuiMouseCursor imgui_cursor = igGetMouseCursor();
if (io->MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) { if (io->MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) {
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
al_set_mouse_cursor(g_Display, g_MouseCursorInvisible); al_hide_mouse_cursor(g_Display);
} else { } else {
al_show_mouse_cursor(g_Display);
ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT; ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
switch (imgui_cursor) { switch (imgui_cursor) {
case ImGuiMouseCursor_TextInput: case ImGuiMouseCursor_TextInput:
@ -363,18 +392,5 @@ SYMBOL_EXPORT void ImGui_ImplAllegro5_NewFrame() {
io->KeyAlt = al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR); io->KeyAlt = al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR);
io->KeySuper = al_key_down(&keys, ALLEGRO_KEY_LWIN) || al_key_down(&keys, ALLEGRO_KEY_RWIN); io->KeySuper = al_key_down(&keys, ALLEGRO_KEY_LWIN) || al_key_down(&keys, ALLEGRO_KEY_RWIN);
ALLEGRO_MOUSE_STATE mouse;
if (keys.display == g_Display) {
al_get_mouse_state(&mouse);
io->MousePos = (ImVec2){.x = mouse.x, .y = mouse.y};
} else {
io->MousePos = (ImVec2){.x = -FLT_MAX, .y = -FLT_MAX};
}
al_get_mouse_state(&mouse);
io->MouseDown[0] = mouse.buttons & (1 << 0);
io->MouseDown[1] = mouse.buttons & (1 << 1);
io->MouseDown[2] = mouse.buttons & (1 << 2);
ImGui_ImplAllegro5_UpdateMouseCursor(); ImGui_ImplAllegro5_UpdateMouseCursor();
} }

View file

@ -377,14 +377,9 @@ SYMBOL_EXPORT int libsuperderpy_start(struct Game* game) {
#ifdef LIBSUPERDERPY_IMGUI #ifdef LIBSUPERDERPY_IMGUI
igCreateContext(NULL); igCreateContext(NULL);
ImGuiIO* io = igGetIO();
io->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
// Setup Platform/Renderer bindings
ImGui_ImplAllegro5_Init(game->display); ImGui_ImplAllegro5_Init(game->display);
// Setup Style
igStyleColorsDark(NULL); igStyleColorsDark(NULL);
igGetStyle()->FrameBorderSize = 1.0;
#endif #endif
return 0; return 0;

View file

@ -383,6 +383,10 @@ static inline bool MainloopEvents(struct Game* game) {
case ALLEGRO_EVENT_MOUSE_AXES: case ALLEGRO_EVENT_MOUSE_AXES:
case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
case ALLEGRO_EVENT_MOUSE_BUTTON_UP: case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
case ALLEGRO_EVENT_TOUCH_BEGIN:
case ALLEGRO_EVENT_TOUCH_CANCEL:
case ALLEGRO_EVENT_TOUCH_END:
case ALLEGRO_EVENT_TOUCH_MOVE:
if (igGetIO()->WantCaptureMouse) { if (igGetIO()->WantCaptureMouse) {
continue; continue;
} }