rlImGui.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /**********************************************************************************************
  2. *
  3. * raylibExtras * Utilities and Shared Components for Raylib
  4. *
  5. * rlImGui * basic ImGui integration
  6. *
  7. * LICENSE: ZLIB
  8. *
  9. * Copyright (c) 2024 Jeffery Myers
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. *
  29. **********************************************************************************************/
  30. #include "rlImGui.h"
  31. #include "imgui_impl_raylib.h"
  32. #include "raylib.h"
  33. #include "rlgl.h"
  34. #include "imgui.h"
  35. #include <math.h>
  36. #include <map>
  37. #include <limits>
  38. #include <cstdint>
  39. #ifndef NO_FONT_AWESOME
  40. #include "extras/FA6FreeSolidFontData.h"
  41. #endif
  42. static ImGuiMouseCursor CurrentMouseCursor = ImGuiMouseCursor_COUNT;
  43. static MouseCursor MouseCursorMap[ImGuiMouseCursor_COUNT];
  44. ImGuiContext* GlobalContext = nullptr;
  45. static std::map<KeyboardKey, ImGuiKey> RaylibKeyMap;
  46. static bool LastFrameFocused = false;
  47. static bool LastControlPressed = false;
  48. static bool LastShiftPressed = false;
  49. static bool LastAltPressed = false;
  50. static bool LastSuperPressed = false;
  51. // internal only functions
  52. bool rlImGuiIsControlDown() { return IsKeyDown(KEY_RIGHT_CONTROL) || IsKeyDown(KEY_LEFT_CONTROL); }
  53. bool rlImGuiIsShiftDown() { return IsKeyDown(KEY_RIGHT_SHIFT) || IsKeyDown(KEY_LEFT_SHIFT); }
  54. bool rlImGuiIsAltDown() { return IsKeyDown(KEY_RIGHT_ALT) || IsKeyDown(KEY_LEFT_ALT); }
  55. bool rlImGuiIsSuperDown() { return IsKeyDown(KEY_RIGHT_SUPER) || IsKeyDown(KEY_LEFT_SUPER); }
  56. struct ImGui_ImplRaylib_Data
  57. {
  58. Texture FontTexture;
  59. };
  60. ImGui_ImplRaylib_Data* ImGui_ImplRaylib_GetBackendData()
  61. {
  62. return ImGui::GetCurrentContext() ? (ImGui_ImplRaylib_Data*)ImGui::GetPlatformIO().Renderer_RenderState : nullptr;
  63. }
  64. void ImGui_ImplRaylib_CreateBackendData()
  65. {
  66. if (!ImGui::GetCurrentContext() || ImGui::GetPlatformIO().Renderer_RenderState)
  67. return;
  68. ImGui::GetPlatformIO().Renderer_RenderState = MemAlloc(sizeof(ImGui_ImplRaylib_Data));
  69. }
  70. void ImGui_ImplRaylib_FreeBackendData()
  71. {
  72. if (!ImGui::GetCurrentContext())
  73. return;
  74. MemFree(ImGui::GetPlatformIO().Renderer_RenderState);
  75. }
  76. void ReloadFonts(void)
  77. {
  78. auto* platData = ImGui_ImplRaylib_GetBackendData();
  79. if (!platData)
  80. return;
  81. ImGuiPlatformIO& platIo = ImGui::GetPlatformIO();
  82. ImGuiIO& io = ImGui::GetIO();
  83. unsigned char* pixels = nullptr;
  84. int width;
  85. int height;
  86. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, nullptr);
  87. Image image = GenImageColor(width, height, BLANK);
  88. memcpy(image.data, pixels, width * height * 4);
  89. if (IsTextureValid(platData->FontTexture))
  90. {
  91. UnloadTexture(platData->FontTexture);
  92. }
  93. platData->FontTexture = LoadTextureFromImage(image);
  94. UnloadImage(image);
  95. io.Fonts->TexID = (ImTextureID)(&platData->FontTexture);
  96. }
  97. static const char* GetClipTextCallback(ImGuiContext*)
  98. {
  99. return GetClipboardText();
  100. }
  101. static void SetClipTextCallback(ImGuiContext*, const char* text)
  102. {
  103. SetClipboardText(text);
  104. }
  105. static void ImGuiNewFrame(float deltaTime)
  106. {
  107. ImGuiIO& io = ImGui::GetIO();
  108. auto* platData = ImGui_ImplRaylib_GetBackendData();
  109. if (!platData)
  110. {
  111. ImGui_ImplRaylib_CreateBackendData();
  112. platData = ImGui_ImplRaylib_GetBackendData();
  113. if (!platData)
  114. return;
  115. }
  116. if (!IsTextureValid(platData->FontTexture))
  117. ReloadFonts();
  118. Vector2 resolutionScale = GetWindowScaleDPI();
  119. #ifndef PLATFORM_DRM
  120. if (IsWindowFullscreen())
  121. {
  122. int monitor = GetCurrentMonitor();
  123. io.DisplaySize.x = float(GetMonitorWidth(monitor));
  124. io.DisplaySize.y = float(GetMonitorHeight(monitor));
  125. }
  126. else
  127. {
  128. io.DisplaySize.x = float(GetScreenWidth());
  129. io.DisplaySize.y = float(GetScreenHeight());
  130. }
  131. #if !defined(__APPLE__)
  132. if (!IsWindowState(FLAG_WINDOW_HIGHDPI))
  133. resolutionScale = Vector2{ 1,1 };
  134. #endif
  135. #else
  136. io.DisplaySize.x = float(GetScreenWidth());
  137. io.DisplaySize.y = float(GetScreenHeight());
  138. #endif
  139. io.DisplayFramebufferScale = ImVec2(resolutionScale.x, resolutionScale.y);
  140. if (deltaTime <= 0)
  141. deltaTime = 0.001f;
  142. io.DeltaTime = deltaTime;
  143. if (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_HasMouseCursors)
  144. {
  145. if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) == 0)
  146. {
  147. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  148. if (imgui_cursor != CurrentMouseCursor || io.MouseDrawCursor)
  149. {
  150. CurrentMouseCursor = imgui_cursor;
  151. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  152. {
  153. HideCursor();
  154. }
  155. else
  156. {
  157. ShowCursor();
  158. if (!(io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange))
  159. {
  160. SetMouseCursor((imgui_cursor > -1 && imgui_cursor < ImGuiMouseCursor_COUNT) ? MouseCursorMap[imgui_cursor] : MOUSE_CURSOR_DEFAULT);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. static void ImGuiTriangleVert(ImDrawVert& idx_vert)
  168. {
  169. Color* c;
  170. c = (Color*)&idx_vert.col;
  171. rlColor4ub(c->r, c->g, c->b, c->a);
  172. rlTexCoord2f(idx_vert.uv.x, idx_vert.uv.y);
  173. rlVertex2f(idx_vert.pos.x, idx_vert.pos.y);
  174. }
  175. static void ImGuiRenderTriangles(unsigned int count, int indexStart, const ImVector<ImDrawIdx>& indexBuffer, const ImVector<ImDrawVert>& vertBuffer, void* texturePtr)
  176. {
  177. if (count < 3)
  178. return;
  179. Texture* texture = (Texture*)texturePtr;
  180. unsigned int textureId = (texture == nullptr) ? 0 : texture->id;
  181. rlBegin(RL_TRIANGLES);
  182. rlSetTexture(textureId);
  183. for (unsigned int i = 0; i <= (count - 3); i += 3)
  184. {
  185. ImDrawIdx indexA = indexBuffer[indexStart + i];
  186. ImDrawIdx indexB = indexBuffer[indexStart + i + 1];
  187. ImDrawIdx indexC = indexBuffer[indexStart + i + 2];
  188. ImDrawVert vertexA = vertBuffer[indexA];
  189. ImDrawVert vertexB = vertBuffer[indexB];
  190. ImDrawVert vertexC = vertBuffer[indexC];
  191. ImGuiTriangleVert(vertexA);
  192. ImGuiTriangleVert(vertexB);
  193. ImGuiTriangleVert(vertexC);
  194. }
  195. rlEnd();
  196. }
  197. static void EnableScissor(float x, float y, float width, float height)
  198. {
  199. rlEnableScissorTest();
  200. ImGuiIO& io = ImGui::GetIO();
  201. ImVec2 scale = io.DisplayFramebufferScale;
  202. #if !defined(__APPLE__)
  203. if (!IsWindowState(FLAG_WINDOW_HIGHDPI))
  204. {
  205. scale.x = 1;
  206. scale.y = 1;
  207. }
  208. #endif
  209. rlScissor((int)(x * scale.x),
  210. int((io.DisplaySize.y - (int)(y + height)) * scale.y),
  211. (int)(width * scale.x),
  212. (int)(height * scale.y));
  213. }
  214. static void SetupMouseCursors(void)
  215. {
  216. MouseCursorMap[ImGuiMouseCursor_Arrow] = MOUSE_CURSOR_ARROW;
  217. MouseCursorMap[ImGuiMouseCursor_TextInput] = MOUSE_CURSOR_IBEAM;
  218. MouseCursorMap[ImGuiMouseCursor_Hand] = MOUSE_CURSOR_POINTING_HAND;
  219. MouseCursorMap[ImGuiMouseCursor_ResizeAll] = MOUSE_CURSOR_RESIZE_ALL;
  220. MouseCursorMap[ImGuiMouseCursor_ResizeEW] = MOUSE_CURSOR_RESIZE_EW;
  221. MouseCursorMap[ImGuiMouseCursor_ResizeNESW] = MOUSE_CURSOR_RESIZE_NESW;
  222. MouseCursorMap[ImGuiMouseCursor_ResizeNS] = MOUSE_CURSOR_RESIZE_NS;
  223. MouseCursorMap[ImGuiMouseCursor_ResizeNWSE] = MOUSE_CURSOR_RESIZE_NWSE;
  224. MouseCursorMap[ImGuiMouseCursor_NotAllowed] = MOUSE_CURSOR_NOT_ALLOWED;
  225. }
  226. void SetupFontAwesome(void)
  227. {
  228. #ifndef NO_FONT_AWESOME
  229. static const ImWchar icons_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
  230. ImFontConfig icons_config;
  231. icons_config.MergeMode = true;
  232. icons_config.PixelSnapH = true;
  233. icons_config.FontDataOwnedByAtlas = false;
  234. icons_config.GlyphMaxAdvanceX = std::numeric_limits<float>::max();
  235. icons_config.RasterizerMultiply = 1.0f;
  236. icons_config.OversampleH = 2;
  237. icons_config.OversampleV = 1;
  238. icons_config.GlyphRanges = icons_ranges;
  239. ImGuiIO& io = ImGui::GetIO();
  240. float size = FONT_AWESOME_ICON_SIZE;
  241. #if !defined(__APPLE__)
  242. if (!IsWindowState(FLAG_WINDOW_HIGHDPI))
  243. size *= GetWindowScaleDPI().y;
  244. icons_config.RasterizerMultiply = GetWindowScaleDPI().y;
  245. #endif
  246. io.Fonts->AddFontFromMemoryCompressedTTF((void*)fa_solid_900_compressed_data, fa_solid_900_compressed_size, size, &icons_config, icons_ranges);
  247. #endif
  248. }
  249. void SetupBackend(void)
  250. {
  251. ImGuiIO& io = ImGui::GetIO();
  252. io.BackendPlatformName = "imgui_impl_raylib";
  253. io.BackendFlags |= ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_HasSetMousePos;
  254. #ifndef PLATFORM_DRM
  255. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
  256. #endif
  257. io.MousePos = ImVec2(0, 0);
  258. ImGuiPlatformIO& platformIO = ImGui::GetPlatformIO();
  259. platformIO.Platform_SetClipboardTextFn = SetClipTextCallback;
  260. platformIO.Platform_GetClipboardTextFn = GetClipTextCallback;
  261. platformIO.Platform_ClipboardUserData = nullptr;
  262. ImGui_ImplRaylib_CreateBackendData();
  263. }
  264. void rlImGuiEndInitImGui(void)
  265. {
  266. ImGui::SetCurrentContext(GlobalContext);
  267. SetupFontAwesome();
  268. SetupMouseCursors();
  269. SetupBackend();
  270. }
  271. static void SetupKeymap(void)
  272. {
  273. if (!RaylibKeyMap.empty())
  274. return;
  275. // build up a map of raylib keys to ImGuiKeys
  276. RaylibKeyMap[KEY_APOSTROPHE] = ImGuiKey_Apostrophe;
  277. RaylibKeyMap[KEY_COMMA] = ImGuiKey_Comma;
  278. RaylibKeyMap[KEY_MINUS] = ImGuiKey_Minus;
  279. RaylibKeyMap[KEY_PERIOD] = ImGuiKey_Period;
  280. RaylibKeyMap[KEY_SLASH] = ImGuiKey_Slash;
  281. RaylibKeyMap[KEY_ZERO] = ImGuiKey_0;
  282. RaylibKeyMap[KEY_ONE] = ImGuiKey_1;
  283. RaylibKeyMap[KEY_TWO] = ImGuiKey_2;
  284. RaylibKeyMap[KEY_THREE] = ImGuiKey_3;
  285. RaylibKeyMap[KEY_FOUR] = ImGuiKey_4;
  286. RaylibKeyMap[KEY_FIVE] = ImGuiKey_5;
  287. RaylibKeyMap[KEY_SIX] = ImGuiKey_6;
  288. RaylibKeyMap[KEY_SEVEN] = ImGuiKey_7;
  289. RaylibKeyMap[KEY_EIGHT] = ImGuiKey_8;
  290. RaylibKeyMap[KEY_NINE] = ImGuiKey_9;
  291. RaylibKeyMap[KEY_SEMICOLON] = ImGuiKey_Semicolon;
  292. RaylibKeyMap[KEY_EQUAL] = ImGuiKey_Equal;
  293. RaylibKeyMap[KEY_A] = ImGuiKey_A;
  294. RaylibKeyMap[KEY_B] = ImGuiKey_B;
  295. RaylibKeyMap[KEY_C] = ImGuiKey_C;
  296. RaylibKeyMap[KEY_D] = ImGuiKey_D;
  297. RaylibKeyMap[KEY_E] = ImGuiKey_E;
  298. RaylibKeyMap[KEY_F] = ImGuiKey_F;
  299. RaylibKeyMap[KEY_G] = ImGuiKey_G;
  300. RaylibKeyMap[KEY_H] = ImGuiKey_H;
  301. RaylibKeyMap[KEY_I] = ImGuiKey_I;
  302. RaylibKeyMap[KEY_J] = ImGuiKey_J;
  303. RaylibKeyMap[KEY_K] = ImGuiKey_K;
  304. RaylibKeyMap[KEY_L] = ImGuiKey_L;
  305. RaylibKeyMap[KEY_M] = ImGuiKey_M;
  306. RaylibKeyMap[KEY_N] = ImGuiKey_N;
  307. RaylibKeyMap[KEY_O] = ImGuiKey_O;
  308. RaylibKeyMap[KEY_P] = ImGuiKey_P;
  309. RaylibKeyMap[KEY_Q] = ImGuiKey_Q;
  310. RaylibKeyMap[KEY_R] = ImGuiKey_R;
  311. RaylibKeyMap[KEY_S] = ImGuiKey_S;
  312. RaylibKeyMap[KEY_T] = ImGuiKey_T;
  313. RaylibKeyMap[KEY_U] = ImGuiKey_U;
  314. RaylibKeyMap[KEY_V] = ImGuiKey_V;
  315. RaylibKeyMap[KEY_W] = ImGuiKey_W;
  316. RaylibKeyMap[KEY_X] = ImGuiKey_X;
  317. RaylibKeyMap[KEY_Y] = ImGuiKey_Y;
  318. RaylibKeyMap[KEY_Z] = ImGuiKey_Z;
  319. RaylibKeyMap[KEY_SPACE] = ImGuiKey_Space;
  320. RaylibKeyMap[KEY_ESCAPE] = ImGuiKey_Escape;
  321. RaylibKeyMap[KEY_ENTER] = ImGuiKey_Enter;
  322. RaylibKeyMap[KEY_TAB] = ImGuiKey_Tab;
  323. RaylibKeyMap[KEY_BACKSPACE] = ImGuiKey_Backspace;
  324. RaylibKeyMap[KEY_INSERT] = ImGuiKey_Insert;
  325. RaylibKeyMap[KEY_DELETE] = ImGuiKey_Delete;
  326. RaylibKeyMap[KEY_RIGHT] = ImGuiKey_RightArrow;
  327. RaylibKeyMap[KEY_LEFT] = ImGuiKey_LeftArrow;
  328. RaylibKeyMap[KEY_DOWN] = ImGuiKey_DownArrow;
  329. RaylibKeyMap[KEY_UP] = ImGuiKey_UpArrow;
  330. RaylibKeyMap[KEY_PAGE_UP] = ImGuiKey_PageUp;
  331. RaylibKeyMap[KEY_PAGE_DOWN] = ImGuiKey_PageDown;
  332. RaylibKeyMap[KEY_HOME] = ImGuiKey_Home;
  333. RaylibKeyMap[KEY_END] = ImGuiKey_End;
  334. RaylibKeyMap[KEY_CAPS_LOCK] = ImGuiKey_CapsLock;
  335. RaylibKeyMap[KEY_SCROLL_LOCK] = ImGuiKey_ScrollLock;
  336. RaylibKeyMap[KEY_NUM_LOCK] = ImGuiKey_NumLock;
  337. RaylibKeyMap[KEY_PRINT_SCREEN] = ImGuiKey_PrintScreen;
  338. RaylibKeyMap[KEY_PAUSE] = ImGuiKey_Pause;
  339. RaylibKeyMap[KEY_F1] = ImGuiKey_F1;
  340. RaylibKeyMap[KEY_F2] = ImGuiKey_F2;
  341. RaylibKeyMap[KEY_F3] = ImGuiKey_F3;
  342. RaylibKeyMap[KEY_F4] = ImGuiKey_F4;
  343. RaylibKeyMap[KEY_F5] = ImGuiKey_F5;
  344. RaylibKeyMap[KEY_F6] = ImGuiKey_F6;
  345. RaylibKeyMap[KEY_F7] = ImGuiKey_F7;
  346. RaylibKeyMap[KEY_F8] = ImGuiKey_F8;
  347. RaylibKeyMap[KEY_F9] = ImGuiKey_F9;
  348. RaylibKeyMap[KEY_F10] = ImGuiKey_F10;
  349. RaylibKeyMap[KEY_F11] = ImGuiKey_F11;
  350. RaylibKeyMap[KEY_F12] = ImGuiKey_F12;
  351. RaylibKeyMap[KEY_LEFT_SHIFT] = ImGuiKey_LeftShift;
  352. RaylibKeyMap[KEY_LEFT_CONTROL] = ImGuiKey_LeftCtrl;
  353. RaylibKeyMap[KEY_LEFT_ALT] = ImGuiKey_LeftAlt;
  354. RaylibKeyMap[KEY_LEFT_SUPER] = ImGuiKey_LeftSuper;
  355. RaylibKeyMap[KEY_RIGHT_SHIFT] = ImGuiKey_RightShift;
  356. RaylibKeyMap[KEY_RIGHT_CONTROL] = ImGuiKey_RightCtrl;
  357. RaylibKeyMap[KEY_RIGHT_ALT] = ImGuiKey_RightAlt;
  358. RaylibKeyMap[KEY_RIGHT_SUPER] = ImGuiKey_RightSuper;
  359. RaylibKeyMap[KEY_KB_MENU] = ImGuiKey_Menu;
  360. RaylibKeyMap[KEY_LEFT_BRACKET] = ImGuiKey_LeftBracket;
  361. RaylibKeyMap[KEY_BACKSLASH] = ImGuiKey_Backslash;
  362. RaylibKeyMap[KEY_RIGHT_BRACKET] = ImGuiKey_RightBracket;
  363. RaylibKeyMap[KEY_GRAVE] = ImGuiKey_GraveAccent;
  364. RaylibKeyMap[KEY_KP_0] = ImGuiKey_Keypad0;
  365. RaylibKeyMap[KEY_KP_1] = ImGuiKey_Keypad1;
  366. RaylibKeyMap[KEY_KP_2] = ImGuiKey_Keypad2;
  367. RaylibKeyMap[KEY_KP_3] = ImGuiKey_Keypad3;
  368. RaylibKeyMap[KEY_KP_4] = ImGuiKey_Keypad4;
  369. RaylibKeyMap[KEY_KP_5] = ImGuiKey_Keypad5;
  370. RaylibKeyMap[KEY_KP_6] = ImGuiKey_Keypad6;
  371. RaylibKeyMap[KEY_KP_7] = ImGuiKey_Keypad7;
  372. RaylibKeyMap[KEY_KP_8] = ImGuiKey_Keypad8;
  373. RaylibKeyMap[KEY_KP_9] = ImGuiKey_Keypad9;
  374. RaylibKeyMap[KEY_KP_DECIMAL] = ImGuiKey_KeypadDecimal;
  375. RaylibKeyMap[KEY_KP_DIVIDE] = ImGuiKey_KeypadDivide;
  376. RaylibKeyMap[KEY_KP_MULTIPLY] = ImGuiKey_KeypadMultiply;
  377. RaylibKeyMap[KEY_KP_SUBTRACT] = ImGuiKey_KeypadSubtract;
  378. RaylibKeyMap[KEY_KP_ADD] = ImGuiKey_KeypadAdd;
  379. RaylibKeyMap[KEY_KP_ENTER] = ImGuiKey_KeypadEnter;
  380. RaylibKeyMap[KEY_KP_EQUAL] = ImGuiKey_KeypadEqual;
  381. }
  382. static void SetupGlobals(void)
  383. {
  384. LastFrameFocused = IsWindowFocused();
  385. LastControlPressed = false;
  386. LastShiftPressed = false;
  387. LastAltPressed = false;
  388. LastSuperPressed = false;
  389. }
  390. void rlImGuiBeginInitImGui(void)
  391. {
  392. SetupGlobals();
  393. if (GlobalContext == nullptr)
  394. GlobalContext = ImGui::CreateContext(nullptr);
  395. SetupKeymap();
  396. ImGuiIO& io = ImGui::GetIO();
  397. ImFontConfig defaultConfig;
  398. static constexpr int DefaultFonSize = 13;
  399. defaultConfig.SizePixels = DefaultFonSize;
  400. #if !defined(__APPLE__)
  401. if (!IsWindowState(FLAG_WINDOW_HIGHDPI))
  402. defaultConfig.SizePixels = ceilf(defaultConfig.SizePixels * GetWindowScaleDPI().y);
  403. defaultConfig.RasterizerMultiply = GetWindowScaleDPI().y;
  404. #endif
  405. defaultConfig.PixelSnapH = true;
  406. io.Fonts->AddFontDefault(&defaultConfig);
  407. }
  408. void rlImGuiSetup(bool dark)
  409. {
  410. rlImGuiBeginInitImGui();
  411. if (dark)
  412. ImGui::StyleColorsDark();
  413. else
  414. ImGui::StyleColorsLight();
  415. rlImGuiEndInitImGui();
  416. }
  417. void rlImGuiReloadFonts(void)
  418. {
  419. ImGui::SetCurrentContext(GlobalContext);
  420. ReloadFonts();
  421. }
  422. void rlImGuiBegin(void)
  423. {
  424. ImGui::SetCurrentContext(GlobalContext);
  425. rlImGuiBeginDelta(GetFrameTime());
  426. }
  427. void rlImGuiBeginDelta(float deltaTime)
  428. {
  429. ImGui::SetCurrentContext(GlobalContext);
  430. ImGuiNewFrame(deltaTime);
  431. ImGui_ImplRaylib_ProcessEvents();
  432. ImGui::NewFrame();
  433. }
  434. void rlImGuiEnd(void)
  435. {
  436. ImGui::SetCurrentContext(GlobalContext);
  437. ImGui::Render();
  438. ImGui_ImplRaylib_RenderDrawData(ImGui::GetDrawData());
  439. }
  440. void rlImGuiShutdown(void)
  441. {
  442. if (GlobalContext == nullptr)
  443. return;
  444. ImGui::SetCurrentContext(GlobalContext);
  445. ImGui_ImplRaylib_Shutdown();
  446. ImGui::DestroyContext(GlobalContext);
  447. GlobalContext = nullptr;
  448. }
  449. void rlImGuiImage(const Texture* image)
  450. {
  451. if (!image)
  452. return;
  453. if (GlobalContext)
  454. ImGui::SetCurrentContext(GlobalContext);
  455. ImGui::Image((ImTextureID)image, ImVec2(float(image->width), float(image->height)));
  456. }
  457. bool rlImGuiImageButton(const char* name, const Texture* image)
  458. {
  459. if (!image)
  460. return false;
  461. if (GlobalContext)
  462. ImGui::SetCurrentContext(GlobalContext);
  463. return ImGui::ImageButton(name, (ImTextureID)image, ImVec2(float(image->width), float(image->height)));
  464. }
  465. bool rlImGuiImageButtonSize(const char* name, const Texture* image, Vector2 size)
  466. {
  467. if (!image)
  468. return false;
  469. if (GlobalContext)
  470. ImGui::SetCurrentContext(GlobalContext);
  471. return ImGui::ImageButton(name, (ImTextureID)image, ImVec2(size.x, size.y));
  472. }
  473. void rlImGuiImageSize(const Texture* image, int width, int height)
  474. {
  475. if (!image)
  476. return;
  477. if (GlobalContext)
  478. ImGui::SetCurrentContext(GlobalContext);
  479. ImGui::Image((ImTextureID)image, ImVec2(float(width), float(height)));
  480. }
  481. void rlImGuiImageSizeV(const Texture* image, Vector2 size)
  482. {
  483. if (!image)
  484. return;
  485. if (GlobalContext)
  486. ImGui::SetCurrentContext(GlobalContext);
  487. ImGui::Image((ImTextureID)image, ImVec2(size.x, size.y));
  488. }
  489. void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect)
  490. {
  491. if (!image)
  492. return;
  493. if (GlobalContext)
  494. ImGui::SetCurrentContext(GlobalContext);
  495. ImVec2 uv0;
  496. ImVec2 uv1;
  497. if (sourceRect.width < 0)
  498. {
  499. uv0.x = -((float)sourceRect.x / image->width);
  500. uv1.x = (uv0.x - (float)(fabs(sourceRect.width) / image->width));
  501. }
  502. else
  503. {
  504. uv0.x = (float)sourceRect.x / image->width;
  505. uv1.x = uv0.x + (float)(sourceRect.width / image->width);
  506. }
  507. if (sourceRect.height < 0)
  508. {
  509. uv0.y = -((float)sourceRect.y / image->height);
  510. uv1.y = (uv0.y - (float)(fabs(sourceRect.height) / image->height));
  511. }
  512. else
  513. {
  514. uv0.y = (float)sourceRect.y / image->height;
  515. uv1.y = uv0.y + (float)(sourceRect.height / image->height);
  516. }
  517. ImGui::Image((ImTextureID)image, ImVec2(float(destWidth), float(destHeight)), uv0, uv1);
  518. }
  519. void rlImGuiImageRenderTexture(const RenderTexture* image)
  520. {
  521. if (!image)
  522. return;
  523. if (GlobalContext)
  524. ImGui::SetCurrentContext(GlobalContext);
  525. rlImGuiImageRect(&image->texture, image->texture.width, image->texture.height, Rectangle{ 0,0, float(image->texture.width), -float(image->texture.height) });
  526. }
  527. void rlImGuiImageRenderTextureFit(const RenderTexture* image, bool center)
  528. {
  529. if (!image)
  530. return;
  531. if (GlobalContext)
  532. ImGui::SetCurrentContext(GlobalContext);
  533. ImVec2 area = ImGui::GetContentRegionAvail();
  534. float scale = area.x / image->texture.width;
  535. float y = image->texture.height * scale;
  536. if (y > area.y)
  537. {
  538. scale = area.y / image->texture.height;
  539. }
  540. int sizeX = int(image->texture.width * scale);
  541. int sizeY = int(image->texture.height * scale);
  542. if (center)
  543. {
  544. ImGui::SetCursorPosX(0);
  545. ImGui::SetCursorPosX(area.x/2 - sizeX/2);
  546. ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (area.y / 2 - sizeY / 2));
  547. }
  548. rlImGuiImageRect(&image->texture, sizeX, sizeY, Rectangle{ 0,0, float(image->texture.width), -float(image->texture.height) });
  549. }
  550. // raw ImGui backend API
  551. bool ImGui_ImplRaylib_Init(void)
  552. {
  553. SetupGlobals();
  554. SetupKeymap();
  555. SetupMouseCursors();
  556. SetupBackend();
  557. return true;
  558. }
  559. void ImGui_ImplRaylib_BuildFontAtlas(void)
  560. {
  561. ReloadFonts();
  562. }
  563. void ImGui_ImplRaylib_Shutdown()
  564. {
  565. ImGuiIO& io =ImGui::GetIO();
  566. auto* plat = ImGui_ImplRaylib_GetBackendData();
  567. if (plat && IsTextureValid(plat->FontTexture))
  568. {
  569. UnloadTexture(plat->FontTexture);
  570. }
  571. ImGui_ImplRaylib_FreeBackendData();
  572. io.Fonts->TexID = 0;
  573. }
  574. void ImGui_ImplRaylib_NewFrame(void)
  575. {
  576. ImGuiNewFrame(GetFrameTime());
  577. }
  578. void ImGui_ImplRaylib_RenderDrawData(ImDrawData* draw_data)
  579. {
  580. rlDrawRenderBatchActive();
  581. rlDisableBackfaceCulling();
  582. for (int l = 0; l < draw_data->CmdListsCount; ++l)
  583. {
  584. const ImDrawList* commandList = draw_data->CmdLists[l];
  585. for (const auto& cmd : commandList->CmdBuffer)
  586. {
  587. EnableScissor(cmd.ClipRect.x - draw_data->DisplayPos.x, cmd.ClipRect.y - draw_data->DisplayPos.y, cmd.ClipRect.z - (cmd.ClipRect.x - draw_data->DisplayPos.x), cmd.ClipRect.w - (cmd.ClipRect.y - draw_data->DisplayPos.y));
  588. if (cmd.UserCallback != nullptr)
  589. {
  590. cmd.UserCallback(commandList, &cmd);
  591. continue;
  592. }
  593. ImGuiRenderTriangles(cmd.ElemCount, cmd.IdxOffset, commandList->IdxBuffer, commandList->VtxBuffer, (Texture2D*)cmd.TextureId);
  594. rlDrawRenderBatchActive();
  595. }
  596. }
  597. rlSetTexture(0);
  598. rlDisableScissorTest();
  599. rlEnableBackfaceCulling();
  600. }
  601. void HandleGamepadButtonEvent(ImGuiIO& io, GamepadButton button, ImGuiKey key)
  602. {
  603. if (IsGamepadButtonPressed(0, button))
  604. io.AddKeyEvent(key, true);
  605. else if (IsGamepadButtonReleased(0, button))
  606. io.AddKeyEvent(key, false);
  607. }
  608. void HandleGamepadStickEvent(ImGuiIO& io, GamepadAxis axis, ImGuiKey negKey, ImGuiKey posKey)
  609. {
  610. constexpr float deadZone = 0.20f;
  611. float axisValue = GetGamepadAxisMovement(0, axis);
  612. io.AddKeyAnalogEvent(negKey, axisValue < -deadZone, axisValue < -deadZone ? -axisValue : 0);
  613. io.AddKeyAnalogEvent(posKey, axisValue > deadZone, axisValue > deadZone ? axisValue : 0);
  614. }
  615. bool ImGui_ImplRaylib_ProcessEvents(void)
  616. {
  617. ImGuiIO& io = ImGui::GetIO();
  618. bool focused = IsWindowFocused();
  619. if (focused != LastFrameFocused)
  620. io.AddFocusEvent(focused);
  621. LastFrameFocused = focused;
  622. // handle the modifyer key events so that shortcuts work
  623. bool ctrlDown = rlImGuiIsControlDown();
  624. if (ctrlDown != LastControlPressed)
  625. io.AddKeyEvent(ImGuiMod_Ctrl, ctrlDown);
  626. LastControlPressed = ctrlDown;
  627. bool shiftDown = rlImGuiIsShiftDown();
  628. if (shiftDown != LastShiftPressed)
  629. io.AddKeyEvent(ImGuiMod_Shift, shiftDown);
  630. LastShiftPressed = shiftDown;
  631. bool altDown = rlImGuiIsAltDown();
  632. if (altDown != LastAltPressed)
  633. io.AddKeyEvent(ImGuiMod_Alt, altDown);
  634. LastAltPressed = altDown;
  635. bool superDown = rlImGuiIsSuperDown();
  636. if (superDown != LastSuperPressed)
  637. io.AddKeyEvent(ImGuiMod_Super, superDown);
  638. LastSuperPressed = superDown;
  639. // walk the keymap and check for up and down events
  640. for (const auto keyItr : RaylibKeyMap)
  641. {
  642. if (IsKeyReleased(keyItr.first))
  643. io.AddKeyEvent(keyItr.second, false);
  644. else if(IsKeyPressed(keyItr.first))
  645. io.AddKeyEvent(keyItr.second, true);
  646. }
  647. if (io.WantCaptureKeyboard)
  648. {
  649. // add the text input in order
  650. unsigned int pressed = GetCharPressed();
  651. while (pressed != 0)
  652. {
  653. io.AddInputCharacter(pressed);
  654. pressed = GetCharPressed();
  655. }
  656. }
  657. if (!io.WantSetMousePos)
  658. {
  659. io.AddMousePosEvent((float)GetMouseX(), (float)GetMouseY());
  660. }
  661. auto setMouseEvent = [&io](int rayMouse, int imGuiMouse)
  662. {
  663. if (IsMouseButtonPressed(rayMouse))
  664. io.AddMouseButtonEvent(imGuiMouse, true);
  665. else if (IsMouseButtonReleased(rayMouse))
  666. io.AddMouseButtonEvent(imGuiMouse, false);
  667. };
  668. setMouseEvent(MOUSE_BUTTON_LEFT, ImGuiMouseButton_Left);
  669. setMouseEvent(MOUSE_BUTTON_RIGHT, ImGuiMouseButton_Right);
  670. setMouseEvent(MOUSE_BUTTON_MIDDLE, ImGuiMouseButton_Middle);
  671. setMouseEvent(MOUSE_BUTTON_FORWARD, ImGuiMouseButton_Middle + 1);
  672. setMouseEvent(MOUSE_BUTTON_BACK, ImGuiMouseButton_Middle + 2);
  673. {
  674. Vector2 mouseWheel = GetMouseWheelMoveV();
  675. io.AddMouseWheelEvent(mouseWheel.x, mouseWheel.y);
  676. }
  677. if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad && IsGamepadAvailable(0))
  678. {
  679. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_LEFT_FACE_UP, ImGuiKey_GamepadDpadUp);
  680. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_LEFT_FACE_RIGHT, ImGuiKey_GamepadDpadRight);
  681. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_LEFT_FACE_DOWN, ImGuiKey_GamepadDpadDown);
  682. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_LEFT_FACE_LEFT, ImGuiKey_GamepadDpadLeft);
  683. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_RIGHT_FACE_UP, ImGuiKey_GamepadFaceUp);
  684. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT, ImGuiKey_GamepadFaceLeft);
  685. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_RIGHT_FACE_DOWN, ImGuiKey_GamepadFaceDown);
  686. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_RIGHT_FACE_LEFT, ImGuiKey_GamepadFaceRight);
  687. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_LEFT_TRIGGER_1, ImGuiKey_GamepadL1);
  688. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_LEFT_TRIGGER_2, ImGuiKey_GamepadL2);
  689. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_RIGHT_TRIGGER_1, ImGuiKey_GamepadR1);
  690. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_RIGHT_TRIGGER_2, ImGuiKey_GamepadR2);
  691. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_LEFT_THUMB, ImGuiKey_GamepadL3);
  692. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_RIGHT_THUMB, ImGuiKey_GamepadR3);
  693. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_MIDDLE_LEFT, ImGuiKey_GamepadStart);
  694. HandleGamepadButtonEvent(io, GAMEPAD_BUTTON_MIDDLE_RIGHT, ImGuiKey_GamepadBack);
  695. // left stick
  696. HandleGamepadStickEvent(io, GAMEPAD_AXIS_LEFT_X, ImGuiKey_GamepadLStickLeft, ImGuiKey_GamepadLStickRight);
  697. HandleGamepadStickEvent(io, GAMEPAD_AXIS_LEFT_Y, ImGuiKey_GamepadLStickUp, ImGuiKey_GamepadLStickDown);
  698. // right stick
  699. HandleGamepadStickEvent(io, GAMEPAD_AXIS_RIGHT_X, ImGuiKey_GamepadRStickLeft, ImGuiKey_GamepadRStickRight);
  700. HandleGamepadStickEvent(io, GAMEPAD_AXIS_RIGHT_Y, ImGuiKey_GamepadRStickUp, ImGuiKey_GamepadRStickDown);
  701. }
  702. return true;
  703. }