| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | #include "imgui.h" |
| 68 | #ifndef IMGUI_DISABLE |
| 69 | #include "imgui_impl_allegro5.h" |
| 70 | #include <stdint.h> |
| 71 | #include <cstring> |
| 72 | |
| 73 | |
| 74 | #include <allegro5/allegro.h> |
| 75 | #include <allegro5/allegro_primitives.h> |
| 76 | #ifdef _WIN32 |
| 77 | #include <allegro5/allegro_windows.h> |
| 78 | #endif |
| 79 | #define ALLEGRO_HAS_CLIPBOARD ((ALLEGRO_VERSION_INT & ~ALLEGRO_UNSTABLE_BIT) >= ((5 << 24) | (1 << 16) | (12 << 8))) |
| 80 | #define ALLEGRO_HAS_DRAW_INDEXED_PRIM ((ALLEGRO_VERSION_INT & ~ALLEGRO_UNSTABLE_BIT) >= ((5 << 24) | (2 << 16) | ( 5 << 8))) |
| 81 | |
| 82 | |
| 83 | #ifdef _MSC_VER |
| 84 | #pragma warning (disable: 4127) |
| 85 | #endif |
| 86 | |
| 87 | struct ImDrawVertAllegro |
| 88 | { |
| 89 | ImVec2 pos; |
| 90 | ImVec2 uv; |
| 91 | ALLEGRO_COLOR col; |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | #define DRAW_VERT_IMGUI_TO_ALLEGRO(DST, SRC) { (DST)->pos = (SRC)->pos; (DST)->uv = (SRC)->uv; unsigned char* c = (unsigned char*)&(SRC)->col; (DST)->col = al_map_rgba(c[0], c[1], c[2], c[3]); } |
| 99 | |
| 100 | |
| 101 | struct ImGui_ImplAllegro5_Data |
| 102 | { |
| 103 | ALLEGRO_DISPLAY* Display; |
| 104 | ALLEGRO_BITMAP* Texture; |
| 105 | double Time; |
| 106 | ALLEGRO_MOUSE_CURSOR* MouseCursorInvisible; |
| 107 | ALLEGRO_VERTEX_DECL* VertexDecl; |
| 108 | char* ClipboardTextData; |
| 109 | ImGuiMouseCursor LastCursor; |
| 110 | |
| 111 | ImVector<ImDrawVertAllegro> BufVertices; |
| 112 | ImVector<int> BufIndices; |
| 113 | |
| 114 | ImGui_ImplAllegro5_Data() { memset((void*)this, 0, sizeof(*this)); } |
| 115 | }; |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | static ImGui_ImplAllegro5_Data* ImGui_ImplAllegro5_GetBackendData() { return ImGui::GetCurrentContext() ? (ImGui_ImplAllegro5_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; } |
| 121 | |
| 122 | static void ImGui_ImplAllegro5_SetupRenderState(ImDrawData* draw_data) |
| 123 | { |
| 124 | |
| 125 | al_set_separate_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); |
| 126 | |
| 127 | |
| 128 | |
| 129 | { |
| 130 | float L = draw_data->DisplayPos.x; |
| 131 | float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; |
| 132 | float T = draw_data->DisplayPos.y; |
| 133 | float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; |
| 134 | ALLEGRO_TRANSFORM transform; |
| 135 | al_identity_transform(&transform); |
| 136 | al_use_transform(&transform); |
| 137 | al_orthographic_transform(&transform, L, T, 1.0f, R, B, -1.0f); |
| 138 | al_use_projection_transform(&transform); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | static void ImGui_ImplAllegro5_DrawCallback_ResetRenderState(const ImDrawList*, const ImDrawCmd*) {} |
| 144 | |
| 145 | |
| 146 | void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data) |
| 147 | { |
| 148 | |
| 149 | if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) |
| 150 | return; |
| 151 | |
| 152 | |
| 153 | |
| 154 | if (draw_data->Textures != nullptr) |
| 155 | for (ImTextureData* tex : *draw_data->Textures) |
| 156 | if (tex->Status != ImTextureStatus_OK) |
| 157 | ImGui_ImplAllegro5_UpdateTexture(tex); |
| 158 | |
| 159 | |
| 160 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 161 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 162 | ALLEGRO_TRANSFORM last_transform = *al_get_current_transform(); |
| 163 | ALLEGRO_TRANSFORM last_projection_transform = *al_get_current_projection_transform(); |
| 164 | int last_clip_x, last_clip_y, last_clip_w, last_clip_h; |
| 165 | al_get_clipping_rectangle(&last_clip_x, &last_clip_y, &last_clip_w, &last_clip_h); |
| 166 | int last_blender_op, last_blender_src, last_blender_dst; |
| 167 | al_get_blender(&last_blender_op, &last_blender_src, &last_blender_dst); |
| 168 | |
| 169 | |
| 170 | ImGui_ImplAllegro5_SetupRenderState(draw_data); |
| 171 | |
| 172 | |
| 173 | for (const ImDrawList* draw_list : draw_data->CmdLists) |
| 174 | { |
| 175 | ImVector<ImDrawVertAllegro>& vertices = bd->BufVertices; |
| 176 | #if ALLEGRO_HAS_DRAW_INDEXED_PRIM |
| 177 | vertices.resize(draw_list->VtxBuffer.Size); |
| 178 | for (int i = 0; i < draw_list->VtxBuffer.Size; i++) |
| 179 | { |
| 180 | const ImDrawVert* src_v = &draw_list->VtxBuffer[i]; |
| 181 | ImDrawVertAllegro* dst_v = &vertices[i]; |
| 182 | DRAW_VERT_IMGUI_TO_ALLEGRO(dst_v, src_v); |
| 183 | } |
| 184 | const int* indices = nullptr; |
| 185 | if (sizeof(ImDrawIdx) == 2) |
| 186 | { |
| 187 | |
| 188 | |
| 189 | |
| 190 | bd->BufIndices.resize(draw_list->IdxBuffer.Size); |
| 191 | for (int i = 0; i < draw_list->IdxBuffer.Size; ++i) |
| 192 | bd->BufIndices[i] = (int)draw_list->IdxBuffer.Data[i]; |
| 193 | indices = bd->BufIndices.Data; |
| 194 | } |
| 195 | else if (sizeof(ImDrawIdx) == 4) |
| 196 | { |
| 197 | indices = (const int*)draw_list->IdxBuffer.Data; |
| 198 | } |
| 199 | #else |
| 200 | |
| 201 | vertices.resize(draw_list->IdxBuffer.Size); |
| 202 | for (int i = 0; i < draw_list->IdxBuffer.Size; i++) |
| 203 | { |
| 204 | const ImDrawVert* src_v = &draw_list->VtxBuffer[draw_list->IdxBuffer[i]]; |
| 205 | ImDrawVertAllegro* dst_v = &vertices[i]; |
| 206 | DRAW_VERT_IMGUI_TO_ALLEGRO(dst_v, src_v); |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | |
| 211 | ImVec2 clip_off = draw_data->DisplayPos; |
| 212 | for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) |
| 213 | { |
| 214 | const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; |
| 215 | if (pcmd->UserCallback) |
| 216 | { |
| 217 | |
| 218 | if (pcmd->UserCallback == ImGui_ImplAllegro5_DrawCallback_ResetRenderState) |
| 219 | ImGui_ImplAllegro5_SetupRenderState(draw_data); |
| 220 | else |
| 221 | pcmd->UserCallback(draw_list, pcmd); |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | |
| 226 | ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y); |
| 227 | ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y); |
| 228 | if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) |
| 229 | continue; |
| 230 | |
| 231 | |
| 232 | ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->GetTexID(); |
| 233 | al_set_clipping_rectangle(clip_min.x, clip_min.y, clip_max.x - clip_min.x, clip_max.y - clip_min.y); |
| 234 | #if ALLEGRO_HAS_DRAW_INDEXED_PRIM |
| 235 | al_draw_indexed_prim(&vertices[0], bd->VertexDecl, texture, &indices[pcmd->IdxOffset], pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST); |
| 236 | #else |
| 237 | al_draw_prim(&vertices[0], bd->VertexDecl, texture, pcmd->IdxOffset, pcmd->IdxOffset + pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST); |
| 238 | #endif |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | al_set_blender(last_blender_op, last_blender_src, last_blender_dst); |
| 245 | al_set_clipping_rectangle(last_clip_x, last_clip_y, last_clip_w, last_clip_h); |
| 246 | al_use_transform(&last_transform); |
| 247 | al_use_projection_transform(&last_projection_transform); |
| 248 | } |
| 249 | |
| 250 | bool ImGui_ImplAllegro5_CreateDeviceObjects() |
| 251 | { |
| 252 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 253 | |
| 254 | |
| 255 | |
| 256 | ALLEGRO_BITMAP* mouse_cursor = al_create_bitmap(8, 8); |
| 257 | bd->MouseCursorInvisible = al_create_mouse_cursor(mouse_cursor, 0, 0); |
| 258 | al_destroy_bitmap(mouse_cursor); |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | void ImGui_ImplAllegro5_UpdateTexture(ImTextureData* tex) |
| 264 | { |
| 265 | if (tex->Status == ImTextureStatus_WantCreate) |
| 266 | { |
| 267 | |
| 268 | |
| 269 | IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr); |
| 270 | IM_ASSERT(tex->Format == ImTextureFormat_RGBA32); |
| 271 | |
| 272 | |
| 273 | |
| 274 | const int new_bitmap_flags = al_get_new_bitmap_flags(); |
| 275 | int new_bitmap_format = al_get_new_bitmap_format(); |
| 276 | al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP | ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); |
| 277 | al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE); |
| 278 | ALLEGRO_BITMAP* cpu_bitmap = al_create_bitmap(tex->Width, tex->Height); |
| 279 | IM_ASSERT(cpu_bitmap != nullptr && "Backend failed to create texture!"); |
| 280 | |
| 281 | |
| 282 | ALLEGRO_LOCKED_REGION* locked_region = al_lock_bitmap(cpu_bitmap, al_get_bitmap_format(cpu_bitmap), ALLEGRO_LOCK_WRITEONLY); |
| 283 | IM_ASSERT(locked_region != nullptr && "Backend failed to create texture!"); |
| 284 | memcpy(locked_region->data, tex->GetPixels(), tex->GetSizeInBytes()); |
| 285 | al_unlock_bitmap(cpu_bitmap); |
| 286 | |
| 287 | |
| 288 | al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); |
| 289 | al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA); |
| 290 | ALLEGRO_BITMAP* gpu_bitmap = al_clone_bitmap(cpu_bitmap); |
| 291 | al_destroy_bitmap(cpu_bitmap); |
| 292 | IM_ASSERT(gpu_bitmap != nullptr && "Backend failed to create texture!"); |
| 293 | |
| 294 | al_set_new_bitmap_flags(new_bitmap_flags); |
| 295 | al_set_new_bitmap_format(new_bitmap_format); |
| 296 | |
| 297 | |
| 298 | tex->SetTexID((ImTextureID)(intptr_t)gpu_bitmap); |
| 299 | tex->SetStatus(ImTextureStatus_OK); |
| 300 | } |
| 301 | else if (tex->Status == ImTextureStatus_WantUpdates) |
| 302 | { |
| 303 | |
| 304 | |
| 305 | ImTextureRect r = tex->UpdateRect; |
| 306 | ALLEGRO_BITMAP* gpu_bitmap = (ALLEGRO_BITMAP*)(intptr_t)tex->TexID; |
| 307 | ALLEGRO_LOCKED_REGION* locked_region = al_lock_bitmap_region(gpu_bitmap, r.x, r.y, r.w, r.h, al_get_bitmap_format(gpu_bitmap), ALLEGRO_LOCK_WRITEONLY); |
| 308 | IM_ASSERT(locked_region && "Backend failed to update texture!"); |
| 309 | for (int y = 0; y < r.h; y++) |
| 310 | memcpy((unsigned char*)locked_region->data + locked_region->pitch * y, tex->GetPixelsAt(r.x, r.y + y), r.w * tex->BytesPerPixel); |
| 311 | al_unlock_bitmap(gpu_bitmap); |
| 312 | tex->SetStatus(ImTextureStatus_OK); |
| 313 | } |
| 314 | else if (tex->Status == ImTextureStatus_WantDestroy) |
| 315 | { |
| 316 | ALLEGRO_BITMAP* backend_tex = (ALLEGRO_BITMAP*)(intptr_t)tex->TexID; |
| 317 | if (backend_tex) |
| 318 | al_destroy_bitmap(backend_tex); |
| 319 | |
| 320 | |
| 321 | tex->SetTexID(ImTextureID_Invalid); |
| 322 | tex->SetStatus(ImTextureStatus_Destroyed); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void ImGui_ImplAllegro5_InvalidateDeviceObjects() |
| 327 | { |
| 328 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 329 | |
| 330 | |
| 331 | for (ImTextureData* tex : ImGui::GetPlatformIO().Textures) |
| 332 | if (tex->RefCount == 1) |
| 333 | { |
| 334 | tex->SetStatus(ImTextureStatus_WantDestroy); |
| 335 | ImGui_ImplAllegro5_UpdateTexture(tex); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | if (bd->MouseCursorInvisible) |
| 340 | { |
| 341 | al_destroy_mouse_cursor(bd->MouseCursorInvisible); |
| 342 | bd->MouseCursorInvisible = nullptr; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | #if ALLEGRO_HAS_CLIPBOARD |
| 347 | static const char* ImGui_ImplAllegro5_GetClipboardText(ImGuiContext*) |
| 348 | { |
| 349 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 350 | if (bd->ClipboardTextData) |
| 351 | al_free(bd->ClipboardTextData); |
| 352 | bd->ClipboardTextData = al_get_clipboard_text(bd->Display); |
| 353 | return bd->ClipboardTextData; |
| 354 | } |
| 355 | |
| 356 | static void ImGui_ImplAllegro5_SetClipboardText(ImGuiContext*, const char* text) |
| 357 | { |
| 358 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 359 | al_set_clipboard_text(bd->Display, text); |
| 360 | } |
| 361 | #endif |
| 362 | |
| 363 | |
| 364 | ImGuiKey ImGui_ImplAllegro5_KeyCodeToImGuiKey(int key_code); |
| 365 | ImGuiKey ImGui_ImplAllegro5_KeyCodeToImGuiKey(int key_code) |
| 366 | { |
| 367 | switch (key_code) |
| 368 | { |
| 369 | case ALLEGRO_KEY_TAB: return ImGuiKey_Tab; |
| 370 | case ALLEGRO_KEY_LEFT: return ImGuiKey_LeftArrow; |
| 371 | case ALLEGRO_KEY_RIGHT: return ImGuiKey_RightArrow; |
| 372 | case ALLEGRO_KEY_UP: return ImGuiKey_UpArrow; |
| 373 | case ALLEGRO_KEY_DOWN: return ImGuiKey_DownArrow; |
| 374 | case ALLEGRO_KEY_PGUP: return ImGuiKey_PageUp; |
| 375 | case ALLEGRO_KEY_PGDN: return ImGuiKey_PageDown; |
| 376 | case ALLEGRO_KEY_HOME: return ImGuiKey_Home; |
| 377 | case ALLEGRO_KEY_END: return ImGuiKey_End; |
| 378 | case ALLEGRO_KEY_INSERT: return ImGuiKey_Insert; |
| 379 | case ALLEGRO_KEY_DELETE: return ImGuiKey_Delete; |
| 380 | case ALLEGRO_KEY_BACKSPACE: return ImGuiKey_Backspace; |
| 381 | case ALLEGRO_KEY_SPACE: return ImGuiKey_Space; |
| 382 | case ALLEGRO_KEY_ENTER: return ImGuiKey_Enter; |
| 383 | case ALLEGRO_KEY_ESCAPE: return ImGuiKey_Escape; |
| 384 | case ALLEGRO_KEY_QUOTE: return ImGuiKey_Apostrophe; |
| 385 | case ALLEGRO_KEY_COMMA: return ImGuiKey_Comma; |
| 386 | case ALLEGRO_KEY_MINUS: return ImGuiKey_Minus; |
| 387 | case ALLEGRO_KEY_FULLSTOP: return ImGuiKey_Period; |
| 388 | case ALLEGRO_KEY_SLASH: return ImGuiKey_Slash; |
| 389 | case ALLEGRO_KEY_SEMICOLON: return ImGuiKey_Semicolon; |
| 390 | case ALLEGRO_KEY_EQUALS: return ImGuiKey_Equal; |
| 391 | case ALLEGRO_KEY_OPENBRACE: return ImGuiKey_LeftBracket; |
| 392 | case ALLEGRO_KEY_BACKSLASH: return ImGuiKey_Backslash; |
| 393 | case ALLEGRO_KEY_CLOSEBRACE: return ImGuiKey_RightBracket; |
| 394 | case ALLEGRO_KEY_TILDE: return ImGuiKey_GraveAccent; |
| 395 | case ALLEGRO_KEY_CAPSLOCK: return ImGuiKey_CapsLock; |
| 396 | case ALLEGRO_KEY_SCROLLLOCK: return ImGuiKey_ScrollLock; |
| 397 | case ALLEGRO_KEY_NUMLOCK: return ImGuiKey_NumLock; |
| 398 | case ALLEGRO_KEY_PRINTSCREEN: return ImGuiKey_PrintScreen; |
| 399 | case ALLEGRO_KEY_PAUSE: return ImGuiKey_Pause; |
| 400 | case ALLEGRO_KEY_PAD_0: return ImGuiKey_Keypad0; |
| 401 | case ALLEGRO_KEY_PAD_1: return ImGuiKey_Keypad1; |
| 402 | case ALLEGRO_KEY_PAD_2: return ImGuiKey_Keypad2; |
| 403 | case ALLEGRO_KEY_PAD_3: return ImGuiKey_Keypad3; |
| 404 | case ALLEGRO_KEY_PAD_4: return ImGuiKey_Keypad4; |
| 405 | case ALLEGRO_KEY_PAD_5: return ImGuiKey_Keypad5; |
| 406 | case ALLEGRO_KEY_PAD_6: return ImGuiKey_Keypad6; |
| 407 | case ALLEGRO_KEY_PAD_7: return ImGuiKey_Keypad7; |
| 408 | case ALLEGRO_KEY_PAD_8: return ImGuiKey_Keypad8; |
| 409 | case ALLEGRO_KEY_PAD_9: return ImGuiKey_Keypad9; |
| 410 | case ALLEGRO_KEY_PAD_DELETE: return ImGuiKey_KeypadDecimal; |
| 411 | case ALLEGRO_KEY_PAD_SLASH: return ImGuiKey_KeypadDivide; |
| 412 | case ALLEGRO_KEY_PAD_ASTERISK: return ImGuiKey_KeypadMultiply; |
| 413 | case ALLEGRO_KEY_PAD_MINUS: return ImGuiKey_KeypadSubtract; |
| 414 | case ALLEGRO_KEY_PAD_PLUS: return ImGuiKey_KeypadAdd; |
| 415 | case ALLEGRO_KEY_PAD_ENTER: return ImGuiKey_KeypadEnter; |
| 416 | case ALLEGRO_KEY_PAD_EQUALS: return ImGuiKey_KeypadEqual; |
| 417 | case ALLEGRO_KEY_LCTRL: return ImGuiKey_LeftCtrl; |
| 418 | case ALLEGRO_KEY_LSHIFT: return ImGuiKey_LeftShift; |
| 419 | case ALLEGRO_KEY_ALT: return ImGuiKey_LeftAlt; |
| 420 | case ALLEGRO_KEY_LWIN: return ImGuiKey_LeftSuper; |
| 421 | case ALLEGRO_KEY_RCTRL: return ImGuiKey_RightCtrl; |
| 422 | case ALLEGRO_KEY_RSHIFT: return ImGuiKey_RightShift; |
| 423 | case ALLEGRO_KEY_ALTGR: return ImGuiKey_RightAlt; |
| 424 | case ALLEGRO_KEY_RWIN: return ImGuiKey_RightSuper; |
| 425 | case ALLEGRO_KEY_MENU: return ImGuiKey_Menu; |
| 426 | case ALLEGRO_KEY_0: return ImGuiKey_0; |
| 427 | case ALLEGRO_KEY_1: return ImGuiKey_1; |
| 428 | case ALLEGRO_KEY_2: return ImGuiKey_2; |
| 429 | case ALLEGRO_KEY_3: return ImGuiKey_3; |
| 430 | case ALLEGRO_KEY_4: return ImGuiKey_4; |
| 431 | case ALLEGRO_KEY_5: return ImGuiKey_5; |
| 432 | case ALLEGRO_KEY_6: return ImGuiKey_6; |
| 433 | case ALLEGRO_KEY_7: return ImGuiKey_7; |
| 434 | case ALLEGRO_KEY_8: return ImGuiKey_8; |
| 435 | case ALLEGRO_KEY_9: return ImGuiKey_9; |
| 436 | case ALLEGRO_KEY_A: return ImGuiKey_A; |
| 437 | case ALLEGRO_KEY_B: return ImGuiKey_B; |
| 438 | case ALLEGRO_KEY_C: return ImGuiKey_C; |
| 439 | case ALLEGRO_KEY_D: return ImGuiKey_D; |
| 440 | case ALLEGRO_KEY_E: return ImGuiKey_E; |
| 441 | case ALLEGRO_KEY_F: return ImGuiKey_F; |
| 442 | case ALLEGRO_KEY_G: return ImGuiKey_G; |
| 443 | case ALLEGRO_KEY_H: return ImGuiKey_H; |
| 444 | case ALLEGRO_KEY_I: return ImGuiKey_I; |
| 445 | case ALLEGRO_KEY_J: return ImGuiKey_J; |
| 446 | case ALLEGRO_KEY_K: return ImGuiKey_K; |
| 447 | case ALLEGRO_KEY_L: return ImGuiKey_L; |
| 448 | case ALLEGRO_KEY_M: return ImGuiKey_M; |
| 449 | case ALLEGRO_KEY_N: return ImGuiKey_N; |
| 450 | case ALLEGRO_KEY_O: return ImGuiKey_O; |
| 451 | case ALLEGRO_KEY_P: return ImGuiKey_P; |
| 452 | case ALLEGRO_KEY_Q: return ImGuiKey_Q; |
| 453 | case ALLEGRO_KEY_R: return ImGuiKey_R; |
| 454 | case ALLEGRO_KEY_S: return ImGuiKey_S; |
| 455 | case ALLEGRO_KEY_T: return ImGuiKey_T; |
| 456 | case ALLEGRO_KEY_U: return ImGuiKey_U; |
| 457 | case ALLEGRO_KEY_V: return ImGuiKey_V; |
| 458 | case ALLEGRO_KEY_W: return ImGuiKey_W; |
| 459 | case ALLEGRO_KEY_X: return ImGuiKey_X; |
| 460 | case ALLEGRO_KEY_Y: return ImGuiKey_Y; |
| 461 | case ALLEGRO_KEY_Z: return ImGuiKey_Z; |
| 462 | case ALLEGRO_KEY_F1: return ImGuiKey_F1; |
| 463 | case ALLEGRO_KEY_F2: return ImGuiKey_F2; |
| 464 | case ALLEGRO_KEY_F3: return ImGuiKey_F3; |
| 465 | case ALLEGRO_KEY_F4: return ImGuiKey_F4; |
| 466 | case ALLEGRO_KEY_F5: return ImGuiKey_F5; |
| 467 | case ALLEGRO_KEY_F6: return ImGuiKey_F6; |
| 468 | case ALLEGRO_KEY_F7: return ImGuiKey_F7; |
| 469 | case ALLEGRO_KEY_F8: return ImGuiKey_F8; |
| 470 | case ALLEGRO_KEY_F9: return ImGuiKey_F9; |
| 471 | case ALLEGRO_KEY_F10: return ImGuiKey_F10; |
| 472 | case ALLEGRO_KEY_F11: return ImGuiKey_F11; |
| 473 | case ALLEGRO_KEY_F12: return ImGuiKey_F12; |
| 474 | default: return ImGuiKey_None; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display) |
| 479 | { |
| 480 | ImGuiIO& io = ImGui::GetIO(); |
| 481 | IMGUI_CHECKVERSION(); |
| 482 | IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); |
| 483 | |
| 484 | |
| 485 | ImGui_ImplAllegro5_Data* bd = IM_NEW(ImGui_ImplAllegro5_Data)(); |
| 486 | io.BackendPlatformUserData = (void*)bd; |
| 487 | io.BackendPlatformName = io.BackendRendererName = "imgui_impl_allegro5"; |
| 488 | io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; |
| 489 | io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; |
| 490 | |
| 491 | bd->LastCursor = ALLEGRO_SYSTEM_MOUSE_CURSOR_NONE; |
| 492 | |
| 493 | ImGui_ImplAllegro5_SetDisplay(display); |
| 494 | |
| 495 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 496 | #if ALLEGRO_HAS_CLIPBOARD |
| 497 | platform_io.Platform_SetClipboardTextFn = ImGui_ImplAllegro5_SetClipboardText; |
| 498 | platform_io.Platform_GetClipboardTextFn = ImGui_ImplAllegro5_GetClipboardText; |
| 499 | #endif |
| 500 | platform_io.DrawCallback_ResetRenderState = ImGui_ImplAllegro5_DrawCallback_ResetRenderState; |
| 501 | |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | void ImGui_ImplAllegro5_Shutdown() |
| 506 | { |
| 507 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 508 | IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); |
| 509 | ImGuiIO& io = ImGui::GetIO(); |
| 510 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 511 | |
| 512 | ImGui_ImplAllegro5_InvalidateDeviceObjects(); |
| 513 | if (bd->VertexDecl) |
| 514 | al_destroy_vertex_decl(bd->VertexDecl); |
| 515 | if (bd->ClipboardTextData) |
| 516 | al_free(bd->ClipboardTextData); |
| 517 | |
| 518 | io.BackendPlatformName = io.BackendRendererName = nullptr; |
| 519 | io.BackendPlatformUserData = nullptr; |
| 520 | io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_RendererHasTextures); |
| 521 | platform_io.ClearRendererHandlers(); |
| 522 | platform_io.ClearPlatformHandlers(); |
| 523 | IM_DELETE(bd); |
| 524 | } |
| 525 | |
| 526 | void ImGui_ImplAllegro5_SetDisplay(ALLEGRO_DISPLAY* display) |
| 527 | { |
| 528 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 529 | bd->Display = display; |
| 530 | |
| 531 | if (bd->VertexDecl) |
| 532 | { |
| 533 | al_destroy_vertex_decl(bd->VertexDecl); |
| 534 | bd->VertexDecl = nullptr; |
| 535 | } |
| 536 | |
| 537 | if (bd->Display && !bd->VertexDecl) |
| 538 | { |
| 539 | |
| 540 | |
| 541 | |
| 542 | ALLEGRO_VERTEX_ELEMENT elems[] = |
| 543 | { |
| 544 | { ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, pos) }, |
| 545 | { ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, uv) }, |
| 546 | { ALLEGRO_PRIM_COLOR_ATTR, 0, offsetof(ImDrawVertAllegro, col) }, |
| 547 | { 0, 0, 0 } |
| 548 | }; |
| 549 | bd->VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro)); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | |
| 554 | static void ImGui_ImplAllegro5_UpdateKeyModifiers() |
| 555 | { |
| 556 | ImGuiIO& io = ImGui::GetIO(); |
| 557 | ALLEGRO_KEYBOARD_STATE keys; |
| 558 | al_get_keyboard_state(&keys); |
| 559 | io.AddKeyEvent(ImGuiMod_Ctrl, al_key_down(&keys, ALLEGRO_KEY_LCTRL) || al_key_down(&keys, ALLEGRO_KEY_RCTRL)); |
| 560 | io.AddKeyEvent(ImGuiMod_Shift, al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT)); |
| 561 | io.AddKeyEvent(ImGuiMod_Alt, al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR)); |
| 562 | io.AddKeyEvent(ImGuiMod_Super, al_key_down(&keys, ALLEGRO_KEY_LWIN) || al_key_down(&keys, ALLEGRO_KEY_RWIN)); |
| 563 | } |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* ev) |
| 570 | { |
| 571 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 572 | IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplAllegro5_Init()?"); |
| 573 | ImGuiIO& io = ImGui::GetIO(); |
| 574 | |
| 575 | switch (ev->type) |
| 576 | { |
| 577 | case ALLEGRO_EVENT_MOUSE_AXES: |
| 578 | if (ev->mouse.display == bd->Display) |
| 579 | { |
| 580 | io.AddMousePosEvent(ev->mouse.x, ev->mouse.y); |
| 581 | io.AddMouseWheelEvent(-ev->mouse.dw, ev->mouse.dz); |
| 582 | } |
| 583 | return true; |
| 584 | case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: |
| 585 | case ALLEGRO_EVENT_MOUSE_BUTTON_UP: |
| 586 | if (ev->mouse.display == bd->Display && ev->mouse.button > 0 && ev->mouse.button <= 5) |
| 587 | io.AddMouseButtonEvent(ev->mouse.button - 1, ev->type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN); |
| 588 | return true; |
| 589 | case ALLEGRO_EVENT_TOUCH_MOVE: |
| 590 | if (ev->touch.display == bd->Display) |
| 591 | io.AddMousePosEvent(ev->touch.x, ev->touch.y); |
| 592 | return true; |
| 593 | case ALLEGRO_EVENT_TOUCH_BEGIN: |
| 594 | case ALLEGRO_EVENT_TOUCH_END: |
| 595 | case ALLEGRO_EVENT_TOUCH_CANCEL: |
| 596 | if (ev->touch.display == bd->Display && ev->touch.primary) |
| 597 | io.AddMouseButtonEvent(0, ev->type == ALLEGRO_EVENT_TOUCH_BEGIN); |
| 598 | return true; |
| 599 | case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY: |
| 600 | if (ev->mouse.display == bd->Display) |
| 601 | io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); |
| 602 | return true; |
| 603 | case ALLEGRO_EVENT_KEY_CHAR: |
| 604 | if (ev->keyboard.display == bd->Display) |
| 605 | if (ev->keyboard.unichar != 0) |
| 606 | io.AddInputCharacter((unsigned int)ev->keyboard.unichar); |
| 607 | return true; |
| 608 | case ALLEGRO_EVENT_KEY_DOWN: |
| 609 | case ALLEGRO_EVENT_KEY_UP: |
| 610 | if (ev->keyboard.display == bd->Display) |
| 611 | { |
| 612 | ImGui_ImplAllegro5_UpdateKeyModifiers(); |
| 613 | ImGuiKey key = ImGui_ImplAllegro5_KeyCodeToImGuiKey(ev->keyboard.keycode); |
| 614 | io.AddKeyEvent(key, (ev->type == ALLEGRO_EVENT_KEY_DOWN)); |
| 615 | io.SetKeyEventNativeData(key, ev->keyboard.keycode, -1); |
| 616 | } |
| 617 | return true; |
| 618 | case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT: |
| 619 | if (ev->display.source == bd->Display) |
| 620 | io.AddFocusEvent(false); |
| 621 | return true; |
| 622 | case ALLEGRO_EVENT_DISPLAY_SWITCH_IN: |
| 623 | if (ev->display.source == bd->Display) |
| 624 | { |
| 625 | io.AddFocusEvent(true); |
| 626 | #if defined(ALLEGRO_UNSTABLE) |
| 627 | al_clear_keyboard_state(bd->Display); |
| 628 | #endif |
| 629 | } |
| 630 | return true; |
| 631 | } |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | static void ImGui_ImplAllegro5_UpdateMouseCursor() |
| 636 | { |
| 637 | ImGuiIO& io = ImGui::GetIO(); |
| 638 | if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) |
| 639 | return; |
| 640 | |
| 641 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 642 | ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); |
| 643 | |
| 644 | |
| 645 | if (io.MouseDrawCursor) |
| 646 | imgui_cursor = ImGuiMouseCursor_None; |
| 647 | |
| 648 | if (bd->LastCursor == imgui_cursor) |
| 649 | return; |
| 650 | bd->LastCursor = imgui_cursor; |
| 651 | if (imgui_cursor == ImGuiMouseCursor_None) |
| 652 | { |
| 653 | al_set_mouse_cursor(bd->Display, bd->MouseCursorInvisible); |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT; |
| 658 | switch (imgui_cursor) |
| 659 | { |
| 660 | case ImGuiMouseCursor_TextInput: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT; break; |
| 661 | case ImGuiMouseCursor_ResizeAll: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE; break; |
| 662 | case ImGuiMouseCursor_ResizeNS: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N; break; |
| 663 | case ImGuiMouseCursor_ResizeEW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E; break; |
| 664 | case ImGuiMouseCursor_ResizeNESW: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE; break; |
| 665 | case ImGuiMouseCursor_ResizeNWSE: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW; break; |
| 666 | case ImGuiMouseCursor_Wait: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_BUSY; break; |
| 667 | case ImGuiMouseCursor_Progress: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_PROGRESS; break; |
| 668 | case ImGuiMouseCursor_NotAllowed: cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_UNAVAILABLE; break; |
| 669 | } |
| 670 | al_set_system_mouse_cursor(bd->Display, cursor_id); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | void ImGui_ImplAllegro5_NewFrame() |
| 675 | { |
| 676 | ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData(); |
| 677 | IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplAllegro5_Init()?"); |
| 678 | |
| 679 | if (!bd->MouseCursorInvisible) |
| 680 | ImGui_ImplAllegro5_CreateDeviceObjects(); |
| 681 | |
| 682 | |
| 683 | ImGuiIO& io = ImGui::GetIO(); |
| 684 | int w, h; |
| 685 | w = al_get_display_width(bd->Display); |
| 686 | h = al_get_display_height(bd->Display); |
| 687 | io.DisplaySize = ImVec2((float)w, (float)h); |
| 688 | |
| 689 | |
| 690 | double current_time = al_get_time(); |
| 691 | io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f); |
| 692 | bd->Time = current_time; |
| 693 | |
| 694 | |
| 695 | #ifdef _WIN32 |
| 696 | io.AddKeyEvent(ImGuiKey_PrintScreen, (::GetAsyncKeyState(VK_SNAPSHOT) & 0x8000) != 0); |
| 697 | #endif |
| 698 | |
| 699 | |
| 700 | ImGui_ImplAllegro5_UpdateMouseCursor(); |
| 701 | } |
| 702 | |
| 703 | |
| 704 | |
| 705 | #endif |