rlImGui.cpp 20 KB

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