rlImGui.cpp 14 KB

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