rlImGui.cpp 16 KB

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