rlImGui.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /**********************************************************************************************
  2. *
  3. * raylibExtras * Utilities and Shared Components for Raylib
  4. *
  5. * rlImGui * basic ImGui integration
  6. *
  7. * LICENSE: ZLIB
  8. *
  9. * Copyright (c) 2020 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. #ifdef PLATFORM_DESKTOP
  35. #include <GLFW/glfw3.h>
  36. #endif
  37. #include <math.h>
  38. #include <map>
  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. bool rlImGuiIsControlDown() { return IsKeyDown(KEY_RIGHT_CONTROL) || IsKeyDown(KEY_LEFT_CONTROL); }
  52. bool rlImGuiIsShiftDown() { return IsKeyDown(KEY_RIGHT_SHIFT) || IsKeyDown(KEY_LEFT_SHIFT); }
  53. bool rlImGuiIsAltDown() { return IsKeyDown(KEY_RIGHT_ALT) || IsKeyDown(KEY_LEFT_ALT); }
  54. bool rlImGuiIsSuperDown() { return IsKeyDown(KEY_RIGHT_SUPER) || IsKeyDown(KEY_LEFT_SUPER); }
  55. void ReloadFonts()
  56. {
  57. ImGuiIO& io = ImGui::GetIO();
  58. unsigned char* pixels = nullptr;
  59. int width;
  60. int height;
  61. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, nullptr);
  62. Image image = GenImageColor(width, height, BLANK);
  63. memcpy(image.data, pixels, width * height * 4);
  64. Texture2D* fontTexture = (Texture2D*)io.Fonts->TexID;
  65. if (fontTexture && fontTexture->id != 0)
  66. {
  67. UnloadTexture(*fontTexture);
  68. MemFree(fontTexture);
  69. }
  70. fontTexture = (Texture2D*)MemAlloc(sizeof(Texture2D));
  71. *fontTexture = LoadTextureFromImage(image);
  72. UnloadImage(image);
  73. io.Fonts->TexID = fontTexture;
  74. }
  75. static const char* GetClipTextCallback(void*)
  76. {
  77. return GetClipboardText();
  78. }
  79. static void SetClipTextCallback(void*, const char* text)
  80. {
  81. SetClipboardText(text);
  82. }
  83. static void ImGuiNewFrame(float deltaTime)
  84. {
  85. ImGuiIO& io = ImGui::GetIO();
  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. int width = int(io.DisplaySize.x), height = int(io.DisplaySize.y);
  98. #ifdef PLATFORM_DESKTOP
  99. glfwGetFramebufferSize(glfwGetCurrentContext(), &width, &height);
  100. #endif
  101. if (width > 0 && height > 0)
  102. {
  103. io.DisplayFramebufferScale = ImVec2(width / io.DisplaySize.x, height / io.DisplaySize.y);
  104. }
  105. else
  106. {
  107. io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
  108. }
  109. io.DeltaTime = deltaTime;
  110. if (io.WantSetMousePos)
  111. {
  112. SetMousePosition((int)io.MousePos.x, (int)io.MousePos.y);
  113. }
  114. else
  115. {
  116. io.AddMousePosEvent((float)GetMouseX(), (float)GetMouseY());
  117. }
  118. auto setMouseEvent = [&io](int rayMouse, int imGuiMouse)
  119. {
  120. if (IsMouseButtonPressed(rayMouse))
  121. io.AddMouseButtonEvent(imGuiMouse, true);
  122. else if (IsMouseButtonReleased(rayMouse))
  123. io.AddMouseButtonEvent(imGuiMouse, false);
  124. };
  125. setMouseEvent(MOUSE_BUTTON_LEFT, ImGuiMouseButton_Left);
  126. setMouseEvent(MOUSE_BUTTON_RIGHT, ImGuiMouseButton_Right);
  127. setMouseEvent(MOUSE_BUTTON_MIDDLE, ImGuiMouseButton_Middle);
  128. setMouseEvent(MOUSE_BUTTON_FORWARD, ImGuiMouseButton_Middle+1);
  129. setMouseEvent(MOUSE_BUTTON_BACK, ImGuiMouseButton_Middle+2);
  130. {
  131. Vector2 mouseWheel = GetMouseWheelMoveV();
  132. io.AddMouseWheelEvent(mouseWheel.x, mouseWheel.y);
  133. }
  134. if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) == 0)
  135. {
  136. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  137. if (imgui_cursor != CurrentMouseCursor || io.MouseDrawCursor)
  138. {
  139. CurrentMouseCursor = imgui_cursor;
  140. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  141. {
  142. HideCursor();
  143. }
  144. else
  145. {
  146. ShowCursor();
  147. if (!(io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange))
  148. {
  149. SetMouseCursor((imgui_cursor > -1 && imgui_cursor < ImGuiMouseCursor_COUNT) ? MouseCursorMap[imgui_cursor] : MOUSE_CURSOR_DEFAULT);
  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. if (rlCheckRenderBatchLimit(3))
  174. {
  175. rlBegin(RL_TRIANGLES);
  176. rlSetTexture(textureId);
  177. }
  178. ImDrawIdx indexA = indexBuffer[indexStart + i];
  179. ImDrawIdx indexB = indexBuffer[indexStart + i + 1];
  180. ImDrawIdx indexC = indexBuffer[indexStart + i + 2];
  181. ImDrawVert vertexA = vertBuffer[indexA];
  182. ImDrawVert vertexB = vertBuffer[indexB];
  183. ImDrawVert vertexC = vertBuffer[indexC];
  184. ImGuiTriangleVert(vertexA);
  185. ImGuiTriangleVert(vertexB);
  186. ImGuiTriangleVert(vertexC);
  187. }
  188. rlEnd();
  189. }
  190. static void EnableScissor(float x, float y, float width, float height)
  191. {
  192. rlEnableScissorTest();
  193. ImGuiIO& io = ImGui::GetIO();
  194. rlScissor((int)(x * io.DisplayFramebufferScale.x),
  195. int((GetScreenHeight() - (int)(y + height)) * io.DisplayFramebufferScale.y),
  196. (int)(width * io.DisplayFramebufferScale.x),
  197. (int)(height * io.DisplayFramebufferScale.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.GlyphRanges = icons_ranges;
  220. ImGuiIO& io = ImGui::GetIO();
  221. io.Fonts->AddFontFromMemoryCompressedTTF((void*)fa_solid_900_compressed_data, fa_solid_900_compressed_size, FONT_AWESOME_ICON_SIZE, &icons_config, icons_ranges);
  222. #endif
  223. }
  224. void SetupBackend()
  225. {
  226. ImGuiIO& io = ImGui::GetIO();
  227. io.BackendPlatformName = "imgui_impl_raylib";
  228. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
  229. io.MousePos = ImVec2(0, 0);
  230. io.SetClipboardTextFn = SetClipTextCallback;
  231. io.GetClipboardTextFn = GetClipTextCallback;
  232. io.ClipboardUserData = nullptr;
  233. }
  234. void rlImGuiEndInitImGui()
  235. {
  236. ImGui::SetCurrentContext(GlobalContext);
  237. SetupFontAwesome();
  238. SetupMouseCursors();
  239. SetupBackend();
  240. ReloadFonts();
  241. }
  242. static void SetupKeymap()
  243. {
  244. if (!RaylibKeyMap.empty())
  245. return;
  246. // build up a map of raylib keys to ImGuiKeys
  247. RaylibKeyMap[KEY_APOSTROPHE] = ImGuiKey_Apostrophe;
  248. RaylibKeyMap[KEY_COMMA] = ImGuiKey_Comma;
  249. RaylibKeyMap[KEY_MINUS] = ImGuiKey_Minus;
  250. RaylibKeyMap[KEY_PERIOD] = ImGuiKey_Period;
  251. RaylibKeyMap[KEY_SLASH] = ImGuiKey_Slash;
  252. RaylibKeyMap[KEY_ZERO] = ImGuiKey_0;
  253. RaylibKeyMap[KEY_ONE] = ImGuiKey_1;
  254. RaylibKeyMap[KEY_TWO] = ImGuiKey_2;
  255. RaylibKeyMap[KEY_THREE] = ImGuiKey_3;
  256. RaylibKeyMap[KEY_FOUR] = ImGuiKey_4;
  257. RaylibKeyMap[KEY_FIVE] = ImGuiKey_5;
  258. RaylibKeyMap[KEY_SIX] = ImGuiKey_6;
  259. RaylibKeyMap[KEY_SEVEN] = ImGuiKey_7;
  260. RaylibKeyMap[KEY_EIGHT] = ImGuiKey_8;
  261. RaylibKeyMap[KEY_NINE] = ImGuiKey_9;
  262. RaylibKeyMap[KEY_SEMICOLON] = ImGuiKey_Semicolon;
  263. RaylibKeyMap[KEY_EQUAL] = ImGuiKey_Equal;
  264. RaylibKeyMap[KEY_A] = ImGuiKey_A;
  265. RaylibKeyMap[KEY_B] = ImGuiKey_B;
  266. RaylibKeyMap[KEY_C] = ImGuiKey_C;
  267. RaylibKeyMap[KEY_D] = ImGuiKey_D;
  268. RaylibKeyMap[KEY_E] = ImGuiKey_E;
  269. RaylibKeyMap[KEY_F] = ImGuiKey_F;
  270. RaylibKeyMap[KEY_G] = ImGuiKey_G;
  271. RaylibKeyMap[KEY_H] = ImGuiKey_H;
  272. RaylibKeyMap[KEY_I] = ImGuiKey_I;
  273. RaylibKeyMap[KEY_J] = ImGuiKey_J;
  274. RaylibKeyMap[KEY_K] = ImGuiKey_K;
  275. RaylibKeyMap[KEY_L] = ImGuiKey_L;
  276. RaylibKeyMap[KEY_M] = ImGuiKey_M;
  277. RaylibKeyMap[KEY_N] = ImGuiKey_N;
  278. RaylibKeyMap[KEY_O] = ImGuiKey_O;
  279. RaylibKeyMap[KEY_P] = ImGuiKey_P;
  280. RaylibKeyMap[KEY_Q] = ImGuiKey_Q;
  281. RaylibKeyMap[KEY_R] = ImGuiKey_R;
  282. RaylibKeyMap[KEY_S] = ImGuiKey_S;
  283. RaylibKeyMap[KEY_T] = ImGuiKey_T;
  284. RaylibKeyMap[KEY_U] = ImGuiKey_U;
  285. RaylibKeyMap[KEY_V] = ImGuiKey_V;
  286. RaylibKeyMap[KEY_W] = ImGuiKey_W;
  287. RaylibKeyMap[KEY_X] = ImGuiKey_X;
  288. RaylibKeyMap[KEY_Y] = ImGuiKey_Y;
  289. RaylibKeyMap[KEY_Z] = ImGuiKey_Z;
  290. RaylibKeyMap[KEY_SPACE] = ImGuiKey_Space;
  291. RaylibKeyMap[KEY_ESCAPE] = ImGuiKey_Escape;
  292. RaylibKeyMap[KEY_ENTER] = ImGuiKey_Enter;
  293. RaylibKeyMap[KEY_TAB] = ImGuiKey_Tab;
  294. RaylibKeyMap[KEY_BACKSPACE] = ImGuiKey_Backspace;
  295. RaylibKeyMap[KEY_INSERT] = ImGuiKey_Insert;
  296. RaylibKeyMap[KEY_DELETE] = ImGuiKey_Delete;
  297. RaylibKeyMap[KEY_RIGHT] = ImGuiKey_RightArrow;
  298. RaylibKeyMap[KEY_LEFT] = ImGuiKey_LeftArrow;
  299. RaylibKeyMap[KEY_DOWN] = ImGuiKey_DownArrow;
  300. RaylibKeyMap[KEY_UP] = ImGuiKey_UpArrow;
  301. RaylibKeyMap[KEY_PAGE_UP] = ImGuiKey_PageUp;
  302. RaylibKeyMap[KEY_PAGE_DOWN] = ImGuiKey_PageDown;
  303. RaylibKeyMap[KEY_HOME] = ImGuiKey_Home;
  304. RaylibKeyMap[KEY_END] = ImGuiKey_End;
  305. RaylibKeyMap[KEY_CAPS_LOCK] = ImGuiKey_CapsLock;
  306. RaylibKeyMap[KEY_SCROLL_LOCK] = ImGuiKey_ScrollLock;
  307. RaylibKeyMap[KEY_NUM_LOCK] = ImGuiKey_NumLock;
  308. RaylibKeyMap[KEY_PRINT_SCREEN] = ImGuiKey_PrintScreen;
  309. RaylibKeyMap[KEY_PAUSE] = ImGuiKey_Pause;
  310. RaylibKeyMap[KEY_F1] = ImGuiKey_F1;
  311. RaylibKeyMap[KEY_F2] = ImGuiKey_F2;
  312. RaylibKeyMap[KEY_F3] = ImGuiKey_F3;
  313. RaylibKeyMap[KEY_F4] = ImGuiKey_F4;
  314. RaylibKeyMap[KEY_F5] = ImGuiKey_F5;
  315. RaylibKeyMap[KEY_F6] = ImGuiKey_F6;
  316. RaylibKeyMap[KEY_F7] = ImGuiKey_F7;
  317. RaylibKeyMap[KEY_F8] = ImGuiKey_F8;
  318. RaylibKeyMap[KEY_F9] = ImGuiKey_F9;
  319. RaylibKeyMap[KEY_F10] = ImGuiKey_F10;
  320. RaylibKeyMap[KEY_F11] = ImGuiKey_F11;
  321. RaylibKeyMap[KEY_F12] = ImGuiKey_F12;
  322. RaylibKeyMap[KEY_LEFT_SHIFT] = ImGuiKey_LeftShift;
  323. RaylibKeyMap[KEY_LEFT_CONTROL] = ImGuiKey_LeftCtrl;
  324. RaylibKeyMap[KEY_LEFT_ALT] = ImGuiKey_LeftAlt;
  325. RaylibKeyMap[KEY_LEFT_SUPER] = ImGuiKey_LeftSuper;
  326. RaylibKeyMap[KEY_RIGHT_SHIFT] = ImGuiKey_RightShift;
  327. RaylibKeyMap[KEY_RIGHT_CONTROL] = ImGuiKey_RightCtrl;
  328. RaylibKeyMap[KEY_RIGHT_ALT] = ImGuiKey_RightAlt;
  329. RaylibKeyMap[KEY_RIGHT_SUPER] = ImGuiKey_RightSuper;
  330. RaylibKeyMap[KEY_KB_MENU] = ImGuiKey_Menu;
  331. RaylibKeyMap[KEY_LEFT_BRACKET] = ImGuiKey_LeftBracket;
  332. RaylibKeyMap[KEY_BACKSLASH] = ImGuiKey_Backslash;
  333. RaylibKeyMap[KEY_RIGHT_BRACKET] = ImGuiKey_RightBracket;
  334. RaylibKeyMap[KEY_GRAVE] = ImGuiKey_GraveAccent;
  335. RaylibKeyMap[KEY_KP_0] = ImGuiKey_Keypad0;
  336. RaylibKeyMap[KEY_KP_1] = ImGuiKey_Keypad1;
  337. RaylibKeyMap[KEY_KP_2] = ImGuiKey_Keypad2;
  338. RaylibKeyMap[KEY_KP_3] = ImGuiKey_Keypad3;
  339. RaylibKeyMap[KEY_KP_4] = ImGuiKey_Keypad4;
  340. RaylibKeyMap[KEY_KP_5] = ImGuiKey_Keypad5;
  341. RaylibKeyMap[KEY_KP_6] = ImGuiKey_Keypad6;
  342. RaylibKeyMap[KEY_KP_7] = ImGuiKey_Keypad7;
  343. RaylibKeyMap[KEY_KP_8] = ImGuiKey_Keypad8;
  344. RaylibKeyMap[KEY_KP_9] = ImGuiKey_Keypad9;
  345. RaylibKeyMap[KEY_KP_DECIMAL] = ImGuiKey_KeypadDecimal;
  346. RaylibKeyMap[KEY_KP_DIVIDE] = ImGuiKey_KeypadDivide;
  347. RaylibKeyMap[KEY_KP_MULTIPLY] = ImGuiKey_KeypadMultiply;
  348. RaylibKeyMap[KEY_KP_SUBTRACT] = ImGuiKey_KeypadSubtract;
  349. RaylibKeyMap[KEY_KP_ADD] = ImGuiKey_KeypadAdd;
  350. RaylibKeyMap[KEY_KP_ENTER] = ImGuiKey_KeypadEnter;
  351. RaylibKeyMap[KEY_KP_EQUAL] = ImGuiKey_KeypadEqual;
  352. }
  353. static void SetupGlobals()
  354. {
  355. LastFrameFocused = IsWindowFocused();
  356. LastControlPressed = false;
  357. LastShiftPressed = false;
  358. LastAltPressed = false;
  359. LastSuperPressed = false;
  360. }
  361. void rlImGuiBeginInitImGui()
  362. {
  363. SetupGlobals();
  364. GlobalContext = ImGui::CreateContext(nullptr);
  365. SetupKeymap();
  366. ImGuiIO& io = ImGui::GetIO();
  367. io.Fonts->AddFontDefault();
  368. }
  369. void rlImGuiSetup(bool dark)
  370. {
  371. rlImGuiBeginInitImGui();
  372. if (dark)
  373. ImGui::StyleColorsDark();
  374. else
  375. ImGui::StyleColorsLight();
  376. rlImGuiEndInitImGui();
  377. }
  378. void rlImGuiReloadFonts()
  379. {
  380. ImGui::SetCurrentContext(GlobalContext);
  381. ReloadFonts();
  382. }
  383. void rlImGuiBegin()
  384. {
  385. ImGui::SetCurrentContext(GlobalContext);
  386. rlImGuiBeginDelta(GetFrameTime());
  387. }
  388. void rlImGuiBeginDelta(float deltaTime)
  389. {
  390. ImGui::SetCurrentContext(GlobalContext);
  391. ImGuiNewFrame(deltaTime);
  392. ImGui_ImplRaylib_ProcessEvents();
  393. ImGui::NewFrame();
  394. }
  395. void rlImGuiEnd()
  396. {
  397. ImGui::SetCurrentContext(GlobalContext);
  398. ImGui::Render();
  399. ImGui_ImplRaylib_RenderDrawData(ImGui::GetDrawData());
  400. }
  401. void rlImGuiShutdown()
  402. {
  403. ImGui::SetCurrentContext(GlobalContext);
  404. ImGui_ImplRaylib_Shutdown();
  405. ImGui::DestroyContext();
  406. }
  407. void rlImGuiImage(const Texture* image)
  408. {
  409. if (!image)
  410. return;
  411. if (GlobalContext)
  412. ImGui::SetCurrentContext(GlobalContext);
  413. ImGui::Image((ImTextureID)image, ImVec2(float(image->width), float(image->height)));
  414. }
  415. bool rlImGuiImageButton(const char* name, const Texture* image)
  416. {
  417. if (!image)
  418. return false;
  419. if (GlobalContext)
  420. ImGui::SetCurrentContext(GlobalContext);
  421. return ImGui::ImageButton(name, (ImTextureID)image, ImVec2(float(image->width), float(image->height)));
  422. }
  423. bool rlImGuiImageButtonSize(const char* name, const Texture* image, ImVec2 size)
  424. {
  425. if (!image)
  426. return false;
  427. if (GlobalContext)
  428. ImGui::SetCurrentContext(GlobalContext);
  429. return ImGui::ImageButton(name, (ImTextureID)image, size);
  430. }
  431. void rlImGuiImageSize(const Texture* image, int width, int height)
  432. {
  433. if (!image)
  434. return;
  435. if (GlobalContext)
  436. ImGui::SetCurrentContext(GlobalContext);
  437. ImGui::Image((ImTextureID)image, ImVec2(float(width), float(height)));
  438. }
  439. void rlImGuiImageSizeV(const Texture* image, Vector2 size)
  440. {
  441. if (!image)
  442. return;
  443. if (GlobalContext)
  444. ImGui::SetCurrentContext(GlobalContext);
  445. ImGui::Image((ImTextureID)image, ImVec2(size.x, size.y));
  446. }
  447. void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect)
  448. {
  449. if (!image)
  450. return;
  451. if (GlobalContext)
  452. ImGui::SetCurrentContext(GlobalContext);
  453. ImVec2 uv0;
  454. ImVec2 uv1;
  455. if (sourceRect.width < 0)
  456. {
  457. uv0.x = -((float)sourceRect.x / image->width);
  458. uv1.x = (uv0.x - (float)(fabs(sourceRect.width) / image->width));
  459. }
  460. else
  461. {
  462. uv0.x = (float)sourceRect.x / image->width;
  463. uv1.x = uv0.x + (float)(sourceRect.width / image->width);
  464. }
  465. if (sourceRect.height < 0)
  466. {
  467. uv0.y = -((float)sourceRect.y / image->height);
  468. uv1.y = (uv0.y - (float)(fabs(sourceRect.height) / image->height));
  469. }
  470. else
  471. {
  472. uv0.y = (float)sourceRect.y / image->height;
  473. uv1.y = uv0.y + (float)(sourceRect.height / image->height);
  474. }
  475. ImGui::Image((ImTextureID)image, ImVec2(float(destWidth), float(destHeight)), uv0, uv1);
  476. }
  477. void rlImGuiImageRenderTexture(const RenderTexture* image)
  478. {
  479. if (!image)
  480. return;
  481. if (GlobalContext)
  482. ImGui::SetCurrentContext(GlobalContext);
  483. rlImGuiImageRect(&image->texture, image->texture.width, image->texture.height, Rectangle{ 0,0, float(image->texture.width), -float(image->texture.height) });
  484. }
  485. void rlImGuiImageRenderTextureFit(const RenderTexture* image, bool center)
  486. {
  487. if (!image)
  488. return;
  489. if (GlobalContext)
  490. ImGui::SetCurrentContext(GlobalContext);
  491. ImVec2 area = ImGui::GetContentRegionAvail();
  492. float scale = area.x / image->texture.width;
  493. float y = image->texture.height * scale;
  494. if (y > area.y)
  495. {
  496. scale = area.y / image->texture.height;
  497. }
  498. int sizeX = int(image->texture.width * scale);
  499. int sizeY = int(image->texture.height * scale);
  500. if (center)
  501. {
  502. ImGui::SetCursorPosX(0);
  503. ImGui::SetCursorPosX(area.x/2 - sizeX/2);
  504. ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (area.y / 2 - sizeY / 2));
  505. }
  506. rlImGuiImageRect(&image->texture, sizeX, sizeY, Rectangle{ 0,0, float(image->texture.width), -float(image->texture.height) });
  507. }
  508. // raw ImGui backend API
  509. bool ImGui_ImplRaylib_Init()
  510. {
  511. SetupGlobals();
  512. SetupKeymap();
  513. SetupMouseCursors();
  514. SetupBackend();
  515. return true;
  516. }
  517. void Imgui_ImplRaylib_BuildFontAtlas()
  518. {
  519. ReloadFonts();
  520. }
  521. void ImGui_ImplRaylib_Shutdown()
  522. {
  523. ImGuiIO& io =ImGui::GetIO();
  524. Texture2D* fontTexture = (Texture2D*)io.Fonts->TexID;
  525. if (fontTexture)
  526. {
  527. UnloadTexture(*fontTexture);
  528. MemFree(fontTexture);
  529. }
  530. io.Fonts->TexID = 0;
  531. }
  532. void ImGui_ImplRaylib_NewFrame()
  533. {
  534. ImGuiNewFrame(GetFrameTime());
  535. }
  536. void ImGui_ImplRaylib_RenderDrawData(ImDrawData* draw_data)
  537. {
  538. rlDrawRenderBatchActive();
  539. rlDisableBackfaceCulling();
  540. for (int l = 0; l < draw_data->CmdListsCount; ++l)
  541. {
  542. const ImDrawList* commandList = draw_data->CmdLists[l];
  543. for (const auto& cmd : commandList->CmdBuffer)
  544. {
  545. 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));
  546. if (cmd.UserCallback != nullptr)
  547. {
  548. cmd.UserCallback(commandList, &cmd);
  549. continue;
  550. }
  551. ImGuiRenderTriangles(cmd.ElemCount, cmd.IdxOffset, commandList->IdxBuffer, commandList->VtxBuffer, cmd.TextureId);
  552. rlDrawRenderBatchActive();
  553. }
  554. }
  555. rlSetTexture(0);
  556. rlDisableScissorTest();
  557. rlEnableBackfaceCulling();
  558. }
  559. bool ImGui_ImplRaylib_ProcessEvents()
  560. {
  561. ImGuiIO& io = ImGui::GetIO();
  562. bool focused = IsWindowFocused();
  563. if (focused != LastFrameFocused)
  564. io.AddFocusEvent(focused);
  565. LastFrameFocused = focused;
  566. // handle the modifyer key events so that shortcuts work
  567. bool ctrlDown = rlImGuiIsControlDown();
  568. if (ctrlDown != LastControlPressed)
  569. io.AddKeyEvent(ImGuiMod_Ctrl, ctrlDown);
  570. LastControlPressed = ctrlDown;
  571. bool shiftDown = rlImGuiIsShiftDown();
  572. if (shiftDown != LastShiftPressed)
  573. io.AddKeyEvent(ImGuiMod_Shift, shiftDown);
  574. LastShiftPressed = shiftDown;
  575. bool altDown = rlImGuiIsAltDown();
  576. if (altDown != LastAltPressed)
  577. io.AddKeyEvent(ImGuiMod_Alt, altDown);
  578. LastAltPressed = altDown;
  579. bool superDown = rlImGuiIsSuperDown();
  580. if (superDown != LastSuperPressed)
  581. io.AddKeyEvent(ImGuiMod_Super, superDown);
  582. LastSuperPressed = superDown;
  583. // get the pressed keys, they are in event order
  584. int keyId = GetKeyPressed();
  585. while (keyId != 0)
  586. {
  587. auto keyItr = RaylibKeyMap.find(KeyboardKey(keyId));
  588. if (keyItr != RaylibKeyMap.end())
  589. io.AddKeyEvent(keyItr->second, true);
  590. keyId = GetKeyPressed();
  591. }
  592. // look for any keys that were down last frame and see if they were down and are released
  593. for (const auto keyItr : RaylibKeyMap)
  594. {
  595. if (IsKeyReleased(keyItr.first))
  596. io.AddKeyEvent(keyItr.second, false);
  597. }
  598. // add the text input in order
  599. unsigned int pressed = GetCharPressed();
  600. while (pressed != 0)
  601. {
  602. io.AddInputCharacter(pressed);
  603. pressed = GetCharPressed();
  604. }
  605. return true;
  606. }