rlImGui.cpp 24 KB

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