rlImGui.cpp 25 KB

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