rlImGui.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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.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 Texture2D FontTexture;
  43. static ImGuiMouseCursor CurrentMouseCursor = ImGuiMouseCursor_COUNT;
  44. static MouseCursor MouseCursorMap[ImGuiMouseCursor_COUNT];
  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. static const char* rlImGuiGetClipText(void*)
  56. {
  57. return GetClipboardText();
  58. }
  59. static void rlImGuiSetClipText(void*, const char* text)
  60. {
  61. SetClipboardText(text);
  62. }
  63. static void rlImGuiNewFrame()
  64. {
  65. ImGuiIO& io = ImGui::GetIO();
  66. if (IsWindowFullscreen())
  67. {
  68. int monitor = GetCurrentMonitor();
  69. io.DisplaySize.x = float(GetMonitorWidth(monitor));
  70. io.DisplaySize.y = float(GetMonitorHeight(monitor));
  71. }
  72. else
  73. {
  74. io.DisplaySize.x = float(GetScreenWidth());
  75. io.DisplaySize.y = float(GetScreenHeight());
  76. }
  77. int width = int(io.DisplaySize.x), height = int(io.DisplaySize.y);
  78. #ifdef PLATFORM_DESKTOP
  79. glfwGetFramebufferSize(glfwGetCurrentContext(), &width, &height);
  80. #endif
  81. if (width > 0 && height > 0) {
  82. io.DisplayFramebufferScale = ImVec2(width / io.DisplaySize.x, height / io.DisplaySize.y);
  83. }
  84. else {
  85. io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
  86. }
  87. io.DeltaTime = GetFrameTime();
  88. if (io.WantSetMousePos)
  89. {
  90. SetMousePosition((int)io.MousePos.x, (int)io.MousePos.y);
  91. }
  92. else
  93. {
  94. io.MousePos.x = (float)GetMouseX();
  95. io.MousePos.y = (float)GetMouseY();
  96. }
  97. io.MouseDown[0] = IsMouseButtonDown(MOUSE_LEFT_BUTTON);
  98. io.MouseDown[1] = IsMouseButtonDown(MOUSE_RIGHT_BUTTON);
  99. io.MouseDown[2] = IsMouseButtonDown(MOUSE_MIDDLE_BUTTON);
  100. {
  101. Vector2 mouseWheel = GetMouseWheelMoveV();
  102. io.MouseWheel += mouseWheel.y;
  103. io.MouseWheelH += mouseWheel.x;
  104. }
  105. if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) == 0)
  106. {
  107. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  108. if (imgui_cursor != CurrentMouseCursor || io.MouseDrawCursor)
  109. {
  110. CurrentMouseCursor = imgui_cursor;
  111. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  112. {
  113. HideCursor();
  114. }
  115. else
  116. {
  117. ShowCursor();
  118. if (!(io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange))
  119. {
  120. SetMouseCursor((imgui_cursor > -1 && imgui_cursor < ImGuiMouseCursor_COUNT) ? MouseCursorMap[imgui_cursor] : MOUSE_CURSOR_DEFAULT);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. static void rlImGuiEvents()
  127. {
  128. ImGuiIO& io = ImGui::GetIO();
  129. bool focused = IsWindowFocused();
  130. if (focused != LastFrameFocused)
  131. io.AddFocusEvent(focused);
  132. LastFrameFocused = focused;
  133. // handle the modifyer key events so that shortcuts work
  134. bool ctrlDown = rlImGuiIsControlDown();
  135. if (ctrlDown != LastControlPressed)
  136. io.AddKeyEvent(ImGuiMod_Ctrl, ctrlDown);
  137. LastControlPressed = ctrlDown;
  138. bool shiftDown = rlImGuiIsShiftDown();
  139. if (shiftDown != LastShiftPressed)
  140. io.AddKeyEvent(ImGuiMod_Shift, ctrlDown);
  141. LastShiftPressed = shiftDown;
  142. bool altDown = rlImGuiIsAltDown();
  143. if (altDown != LastAltPressed)
  144. io.AddKeyEvent(ImGuiMod_Alt, altDown);
  145. LastAltPressed = altDown;
  146. bool superDown = rlImGuiIsSuperDown();
  147. if (superDown != LastSuperPressed)
  148. io.AddKeyEvent(ImGuiMod_Super, ctrlDown);
  149. LastSuperPressed = superDown;
  150. // get the pressed keys, they are in event order
  151. int keyId = GetKeyPressed();
  152. while (keyId != 0)
  153. {
  154. auto keyItr = RaylibKeyMap.find(KeyboardKey(keyId));
  155. if (keyItr != RaylibKeyMap.end())
  156. io.AddKeyEvent(keyItr->second, true);
  157. keyId = GetKeyPressed();
  158. }
  159. for (auto keyItr : RaylibKeyMap)
  160. io.KeysData[keyItr.second].Down = IsKeyDown(keyItr.first);
  161. // look for any keys that were down last frame and see if they were down and are released
  162. for (const auto keyItr : RaylibKeyMap)
  163. {
  164. if (IsKeyReleased(keyItr.first))
  165. io.AddKeyEvent(keyItr.second, false);
  166. }
  167. // add the text input in order
  168. unsigned int pressed = GetCharPressed();
  169. while (pressed != 0)
  170. {
  171. io.AddInputCharacter(pressed);
  172. pressed = GetCharPressed();
  173. }
  174. }
  175. static void rlImGuiTriangleVert(ImDrawVert& idx_vert)
  176. {
  177. Color* c;
  178. c = (Color*)&idx_vert.col;
  179. rlColor4ub(c->r, c->g, c->b, c->a);
  180. rlTexCoord2f(idx_vert.uv.x, idx_vert.uv.y);
  181. rlVertex2f(idx_vert.pos.x, idx_vert.pos.y);
  182. }
  183. static void rlImGuiRenderTriangles(unsigned int count, int indexStart, const ImVector<ImDrawIdx>& indexBuffer, const ImVector<ImDrawVert>& vertBuffer, void* texturePtr)
  184. {
  185. if (count < 3)
  186. return;
  187. Texture* texture = (Texture*)texturePtr;
  188. unsigned int textureId = (texture == nullptr) ? 0 : texture->id;
  189. rlBegin(RL_TRIANGLES);
  190. rlSetTexture(textureId);
  191. for (unsigned int i = 0; i <= (count - 3); i += 3)
  192. {
  193. if (rlCheckRenderBatchLimit(3))
  194. {
  195. rlBegin(RL_TRIANGLES);
  196. rlSetTexture(textureId);
  197. }
  198. ImDrawIdx indexA = indexBuffer[indexStart + i];
  199. ImDrawIdx indexB = indexBuffer[indexStart + i + 1];
  200. ImDrawIdx indexC = indexBuffer[indexStart + i + 2];
  201. ImDrawVert vertexA = vertBuffer[indexA];
  202. ImDrawVert vertexB = vertBuffer[indexB];
  203. ImDrawVert vertexC = vertBuffer[indexC];
  204. rlImGuiTriangleVert(vertexA);
  205. rlImGuiTriangleVert(vertexB);
  206. rlImGuiTriangleVert(vertexC);
  207. }
  208. rlEnd();
  209. }
  210. static void EnableScissor(float x, float y, float width, float height)
  211. {
  212. rlEnableScissorTest();
  213. ImGuiIO& io = ImGui::GetIO();
  214. rlScissor((int)(x * io.DisplayFramebufferScale.x),
  215. int((GetScreenHeight() - (int)(y + height)) * io.DisplayFramebufferScale.y),
  216. (int)(width * io.DisplayFramebufferScale.x),
  217. (int)(height * io.DisplayFramebufferScale.y));
  218. }
  219. static void rlRenderData(ImDrawData* data)
  220. {
  221. rlDrawRenderBatchActive();
  222. rlDisableBackfaceCulling();
  223. for (int l = 0; l < data->CmdListsCount; ++l)
  224. {
  225. const ImDrawList* commandList = data->CmdLists[l];
  226. for (const auto& cmd : commandList->CmdBuffer)
  227. {
  228. EnableScissor(cmd.ClipRect.x - data->DisplayPos.x, cmd.ClipRect.y - data->DisplayPos.y, cmd.ClipRect.z - (cmd.ClipRect.x - data->DisplayPos.x), cmd.ClipRect.w - (cmd.ClipRect.y - data->DisplayPos.y));
  229. if (cmd.UserCallback != nullptr)
  230. {
  231. cmd.UserCallback(commandList, &cmd);
  232. continue;
  233. }
  234. rlImGuiRenderTriangles(cmd.ElemCount, cmd.IdxOffset, commandList->IdxBuffer, commandList->VtxBuffer, cmd.TextureId);
  235. rlDrawRenderBatchActive();
  236. }
  237. }
  238. rlSetTexture(0);
  239. rlDisableScissorTest();
  240. rlEnableBackfaceCulling();
  241. }
  242. void SetupMouseCursors()
  243. {
  244. MouseCursorMap[ImGuiMouseCursor_Arrow] = MOUSE_CURSOR_ARROW;
  245. MouseCursorMap[ImGuiMouseCursor_TextInput] = MOUSE_CURSOR_IBEAM;
  246. MouseCursorMap[ImGuiMouseCursor_Hand] = MOUSE_CURSOR_POINTING_HAND;
  247. MouseCursorMap[ImGuiMouseCursor_ResizeAll] = MOUSE_CURSOR_RESIZE_ALL;
  248. MouseCursorMap[ImGuiMouseCursor_ResizeEW] = MOUSE_CURSOR_RESIZE_EW;
  249. MouseCursorMap[ImGuiMouseCursor_ResizeNESW] = MOUSE_CURSOR_RESIZE_NESW;
  250. MouseCursorMap[ImGuiMouseCursor_ResizeNS] = MOUSE_CURSOR_RESIZE_NS;
  251. MouseCursorMap[ImGuiMouseCursor_ResizeNWSE] = MOUSE_CURSOR_RESIZE_NWSE;
  252. MouseCursorMap[ImGuiMouseCursor_NotAllowed] = MOUSE_CURSOR_NOT_ALLOWED;
  253. }
  254. void rlImGuiEndInitImGui()
  255. {
  256. SetupMouseCursors();
  257. ImGuiIO& io = ImGui::GetIO();
  258. io.BackendPlatformName = "imgui_impl_raylib";
  259. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
  260. io.MousePos = ImVec2(0, 0);
  261. io.SetClipboardTextFn = rlImGuiSetClipText;
  262. io.GetClipboardTextFn = rlImGuiGetClipText;
  263. io.ClipboardUserData = nullptr;
  264. rlImGuiReloadFonts();
  265. }
  266. void rlSetupKeymap()
  267. {
  268. if (!RaylibKeyMap.empty())
  269. return;
  270. // build up a map of raylib keys to ImGuiKeys
  271. RaylibKeyMap[KEY_APOSTROPHE] = ImGuiKey_Apostrophe;
  272. RaylibKeyMap[KEY_COMMA] = ImGuiKey_Comma;
  273. RaylibKeyMap[KEY_MINUS] = ImGuiKey_Minus;
  274. RaylibKeyMap[KEY_PERIOD] = ImGuiKey_Period;
  275. RaylibKeyMap[KEY_SLASH] = ImGuiKey_Slash;
  276. RaylibKeyMap[KEY_ZERO] = ImGuiKey_0;
  277. RaylibKeyMap[KEY_ONE] = ImGuiKey_1;
  278. RaylibKeyMap[KEY_TWO] = ImGuiKey_2;
  279. RaylibKeyMap[KEY_THREE] = ImGuiKey_3;
  280. RaylibKeyMap[KEY_FOUR] = ImGuiKey_4;
  281. RaylibKeyMap[KEY_FIVE] = ImGuiKey_5;
  282. RaylibKeyMap[KEY_SIX] = ImGuiKey_6;
  283. RaylibKeyMap[KEY_SEVEN] = ImGuiKey_7;
  284. RaylibKeyMap[KEY_EIGHT] = ImGuiKey_8;
  285. RaylibKeyMap[KEY_NINE] = ImGuiKey_9;
  286. RaylibKeyMap[KEY_SEMICOLON] = ImGuiKey_Semicolon;
  287. RaylibKeyMap[KEY_EQUAL] = ImGuiKey_Equal;
  288. RaylibKeyMap[KEY_A] = ImGuiKey_A;
  289. RaylibKeyMap[KEY_B] = ImGuiKey_B;
  290. RaylibKeyMap[KEY_C] = ImGuiKey_C;
  291. RaylibKeyMap[KEY_D] = ImGuiKey_D;
  292. RaylibKeyMap[KEY_E] = ImGuiKey_E;
  293. RaylibKeyMap[KEY_F] = ImGuiKey_F;
  294. RaylibKeyMap[KEY_G] = ImGuiKey_G;
  295. RaylibKeyMap[KEY_H] = ImGuiKey_H;
  296. RaylibKeyMap[KEY_I] = ImGuiKey_I;
  297. RaylibKeyMap[KEY_J] = ImGuiKey_J;
  298. RaylibKeyMap[KEY_K] = ImGuiKey_K;
  299. RaylibKeyMap[KEY_L] = ImGuiKey_L;
  300. RaylibKeyMap[KEY_M] = ImGuiKey_M;
  301. RaylibKeyMap[KEY_N] = ImGuiKey_N;
  302. RaylibKeyMap[KEY_O] = ImGuiKey_O;
  303. RaylibKeyMap[KEY_P] = ImGuiKey_P;
  304. RaylibKeyMap[KEY_Q] = ImGuiKey_Q;
  305. RaylibKeyMap[KEY_R] = ImGuiKey_R;
  306. RaylibKeyMap[KEY_S] = ImGuiKey_S;
  307. RaylibKeyMap[KEY_T] = ImGuiKey_T;
  308. RaylibKeyMap[KEY_U] = ImGuiKey_U;
  309. RaylibKeyMap[KEY_V] = ImGuiKey_V;
  310. RaylibKeyMap[KEY_W] = ImGuiKey_W;
  311. RaylibKeyMap[KEY_X] = ImGuiKey_X;
  312. RaylibKeyMap[KEY_Y] = ImGuiKey_Y;
  313. RaylibKeyMap[KEY_Z] = ImGuiKey_Z;
  314. RaylibKeyMap[KEY_SPACE] = ImGuiKey_Space;
  315. RaylibKeyMap[KEY_ESCAPE] = ImGuiKey_Escape;
  316. RaylibKeyMap[KEY_ENTER] = ImGuiKey_Enter;
  317. RaylibKeyMap[KEY_TAB] = ImGuiKey_Tab;
  318. RaylibKeyMap[KEY_BACKSPACE] = ImGuiKey_Backspace;
  319. RaylibKeyMap[KEY_INSERT] = ImGuiKey_Insert;
  320. RaylibKeyMap[KEY_DELETE] = ImGuiKey_Delete;
  321. RaylibKeyMap[KEY_RIGHT] = ImGuiKey_RightArrow;
  322. RaylibKeyMap[KEY_LEFT] = ImGuiKey_LeftArrow;
  323. RaylibKeyMap[KEY_DOWN] = ImGuiKey_DownArrow;
  324. RaylibKeyMap[KEY_UP] = ImGuiKey_UpArrow;
  325. RaylibKeyMap[KEY_PAGE_UP] = ImGuiKey_PageUp;
  326. RaylibKeyMap[KEY_PAGE_DOWN] = ImGuiKey_PageDown;
  327. RaylibKeyMap[KEY_HOME] = ImGuiKey_Home;
  328. RaylibKeyMap[KEY_END] = ImGuiKey_End;
  329. RaylibKeyMap[KEY_CAPS_LOCK] = ImGuiKey_CapsLock;
  330. RaylibKeyMap[KEY_SCROLL_LOCK] = ImGuiKey_ScrollLock;
  331. RaylibKeyMap[KEY_NUM_LOCK] = ImGuiKey_NumLock;
  332. RaylibKeyMap[KEY_PRINT_SCREEN] = ImGuiKey_PrintScreen;
  333. RaylibKeyMap[KEY_PAUSE] = ImGuiKey_Pause;
  334. RaylibKeyMap[KEY_F1] = ImGuiKey_F1;
  335. RaylibKeyMap[KEY_F2] = ImGuiKey_F2;
  336. RaylibKeyMap[KEY_F3] = ImGuiKey_F3;
  337. RaylibKeyMap[KEY_F4] = ImGuiKey_F4;
  338. RaylibKeyMap[KEY_F5] = ImGuiKey_F5;
  339. RaylibKeyMap[KEY_F6] = ImGuiKey_F6;
  340. RaylibKeyMap[KEY_F7] = ImGuiKey_F7;
  341. RaylibKeyMap[KEY_F8] = ImGuiKey_F8;
  342. RaylibKeyMap[KEY_F9] = ImGuiKey_F9;
  343. RaylibKeyMap[KEY_F10] = ImGuiKey_F10;
  344. RaylibKeyMap[KEY_F11] = ImGuiKey_F11;
  345. RaylibKeyMap[KEY_F12] = ImGuiKey_F12;
  346. RaylibKeyMap[KEY_LEFT_SHIFT] = ImGuiKey_LeftShift;
  347. RaylibKeyMap[KEY_LEFT_CONTROL] = ImGuiKey_LeftCtrl;
  348. RaylibKeyMap[KEY_LEFT_ALT] = ImGuiKey_LeftAlt;
  349. RaylibKeyMap[KEY_LEFT_SUPER] = ImGuiKey_LeftSuper;
  350. RaylibKeyMap[KEY_RIGHT_SHIFT] = ImGuiKey_RightShift;
  351. RaylibKeyMap[KEY_RIGHT_CONTROL] = ImGuiKey_RightCtrl;
  352. RaylibKeyMap[KEY_RIGHT_ALT] = ImGuiKey_RightAlt;
  353. RaylibKeyMap[KEY_RIGHT_SUPER] = ImGuiKey_RightSuper;
  354. RaylibKeyMap[KEY_KB_MENU] = ImGuiKey_Menu;
  355. RaylibKeyMap[KEY_LEFT_BRACKET] = ImGuiKey_LeftBracket;
  356. RaylibKeyMap[KEY_BACKSLASH] = ImGuiKey_Backslash;
  357. RaylibKeyMap[KEY_RIGHT_BRACKET] = ImGuiKey_RightBracket;
  358. RaylibKeyMap[KEY_GRAVE] = ImGuiKey_GraveAccent;
  359. RaylibKeyMap[KEY_KP_0] = ImGuiKey_Keypad0;
  360. RaylibKeyMap[KEY_KP_1] = ImGuiKey_Keypad1;
  361. RaylibKeyMap[KEY_KP_2] = ImGuiKey_Keypad2;
  362. RaylibKeyMap[KEY_KP_3] = ImGuiKey_Keypad3;
  363. RaylibKeyMap[KEY_KP_4] = ImGuiKey_Keypad4;
  364. RaylibKeyMap[KEY_KP_5] = ImGuiKey_Keypad5;
  365. RaylibKeyMap[KEY_KP_6] = ImGuiKey_Keypad6;
  366. RaylibKeyMap[KEY_KP_7] = ImGuiKey_Keypad7;
  367. RaylibKeyMap[KEY_KP_8] = ImGuiKey_Keypad8;
  368. RaylibKeyMap[KEY_KP_9] = ImGuiKey_Keypad9;
  369. RaylibKeyMap[KEY_KP_DECIMAL] = ImGuiKey_KeypadDecimal;
  370. RaylibKeyMap[KEY_KP_DIVIDE] = ImGuiKey_KeypadDivide;
  371. RaylibKeyMap[KEY_KP_MULTIPLY] = ImGuiKey_KeypadMultiply;
  372. RaylibKeyMap[KEY_KP_SUBTRACT] = ImGuiKey_KeypadSubtract;
  373. RaylibKeyMap[KEY_KP_ADD] = ImGuiKey_KeypadAdd;
  374. RaylibKeyMap[KEY_KP_ENTER] = ImGuiKey_KeypadEnter;
  375. RaylibKeyMap[KEY_KP_EQUAL] = ImGuiKey_KeypadEqual;
  376. }
  377. void rlImGuiBeginInitImGui()
  378. {
  379. rlSetupKeymap();
  380. ImGui::CreateContext(nullptr);
  381. }
  382. void rlImGuiSetup(bool dark)
  383. {
  384. LastFrameFocused = IsWindowFocused();
  385. LastControlPressed = false;
  386. LastShiftPressed = false;
  387. LastAltPressed = false;
  388. LastSuperPressed = false;
  389. rlImGuiBeginInitImGui();
  390. if (dark)
  391. ImGui::StyleColorsDark();
  392. else
  393. ImGui::StyleColorsLight();
  394. ImGuiIO& io = ImGui::GetIO();
  395. io.Fonts->AddFontDefault();
  396. #ifndef NO_FONT_AWESOME
  397. static const ImWchar icons_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
  398. ImFontConfig icons_config;
  399. icons_config.MergeMode = true;
  400. icons_config.PixelSnapH = true;
  401. icons_config.FontDataOwnedByAtlas = false;
  402. io.Fonts->AddFontFromMemoryCompressedTTF((void*)fa_solid_900_compressed_data, fa_solid_900_compressed_size, FONT_AWESOME_ICON_SIZE, &icons_config, icons_ranges);
  403. #endif
  404. rlImGuiEndInitImGui();
  405. }
  406. void rlImGuiReloadFonts()
  407. {
  408. ImGuiIO& io = ImGui::GetIO();
  409. unsigned char* pixels = nullptr;
  410. int width;
  411. int height;
  412. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, nullptr);
  413. Image image = GenImageColor(width, height, BLANK);
  414. memcpy(image.data, pixels, width * height * 4);
  415. if (FontTexture.id != 0)
  416. UnloadTexture(FontTexture);
  417. FontTexture = LoadTextureFromImage(image);
  418. UnloadImage(image);
  419. io.Fonts->TexID = &FontTexture;
  420. }
  421. void rlImGuiBegin()
  422. {
  423. rlImGuiNewFrame();
  424. rlImGuiEvents();
  425. ImGui::NewFrame();
  426. }
  427. void rlImGuiEnd()
  428. {
  429. ImGui::Render();
  430. rlRenderData(ImGui::GetDrawData());
  431. }
  432. void rlImGuiShutdown()
  433. {
  434. UnloadTexture(FontTexture);
  435. ImGui::DestroyContext();
  436. }
  437. void rlImGuiImage(const Texture* image)
  438. {
  439. ImGui::Image((ImTextureID)image, ImVec2(float(image->width), float(image->height)));
  440. }
  441. bool rlImGuiImageButton(const char* name, const Texture* image)
  442. {
  443. return ImGui::ImageButton(name, (ImTextureID)image, ImVec2(float(image->width), float(image->height)));
  444. }
  445. bool rlImGuiImageButtonSize(const char* name, const Texture* image, ImVec2 size)
  446. {
  447. return ImGui::ImageButton(name, (ImTextureID)image, size);
  448. }
  449. void rlImGuiImageSize(const Texture* image, int width, int height)
  450. {
  451. ImGui::Image((ImTextureID)image, ImVec2(float(width), float(height)));
  452. }
  453. void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect)
  454. {
  455. ImVec2 uv0;
  456. ImVec2 uv1;
  457. if (sourceRect.width < 0)
  458. {
  459. uv0.x = -((float)sourceRect.x / image->width);
  460. uv1.x = (uv0.x - (float)(fabs(sourceRect.width) / image->width));
  461. }
  462. else
  463. {
  464. uv0.x = (float)sourceRect.x / image->width;
  465. uv1.x = uv0.x + (float)(sourceRect.width / image->width);
  466. }
  467. if (sourceRect.height < 0)
  468. {
  469. uv0.y = -((float)sourceRect.y / image->height);
  470. uv1.y = (uv0.y - (float)(fabs(sourceRect.height) / image->height));
  471. }
  472. else
  473. {
  474. uv0.y = (float)sourceRect.y / image->height;
  475. uv1.y = uv0.y + (float)(sourceRect.height / image->height);
  476. }
  477. ImGui::Image((ImTextureID)image, ImVec2(float(destWidth), float(destHeight)), uv0, uv1);
  478. }