rlImGui.cpp 13 KB

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