rlImGui.cpp 25 KB

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