rlImGui.cpp 13 KB

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