rlImGui.cpp 14 KB

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