rlImGui.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. if (count < 3)
  228. return;
  229. Texture* texture = (Texture*)texturePtr;
  230. unsigned int textureId = (texture == nullptr) ? 0 : texture->id;
  231. rlBegin(RL_TRIANGLES);
  232. rlSetTexture(textureId);
  233. for (unsigned int i = 0; i <= (count - 3); i += 3)
  234. {
  235. if(rlCheckRenderBatchLimit(3))
  236. {
  237. rlBegin(RL_TRIANGLES);
  238. rlSetTexture(textureId);
  239. }
  240. ImDrawIdx indexA = indexBuffer[indexStart + i];
  241. ImDrawIdx indexB = indexBuffer[indexStart + i + 1];
  242. ImDrawIdx indexC = indexBuffer[indexStart + i + 2];
  243. ImDrawVert vertexA = vertBuffer[indexA];
  244. ImDrawVert vertexB = vertBuffer[indexB];
  245. ImDrawVert vertexC = vertBuffer[indexC];
  246. rlImGuiTriangleVert(vertexA);
  247. rlImGuiTriangleVert(vertexB);
  248. rlImGuiTriangleVert(vertexC);
  249. }
  250. rlEnd();
  251. }
  252. static void EnableScissor(float x, float y, float width, float height)
  253. {
  254. rlEnableScissorTest();
  255. rlScissor((int)x, GetScreenHeight() - (int)(y + height), (int)width, (int)height);
  256. }
  257. static void rlRenderData(ImDrawData* data)
  258. {
  259. rlDrawRenderBatchActive();
  260. rlDisableBackfaceCulling();
  261. for (int l = 0; l < data->CmdListsCount; ++l)
  262. {
  263. const ImDrawList* commandList = data->CmdLists[l];
  264. for (const auto& cmd : commandList->CmdBuffer)
  265. {
  266. 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));
  267. if (cmd.UserCallback != nullptr)
  268. {
  269. cmd.UserCallback(commandList, &cmd);
  270. continue;
  271. }
  272. rlImGuiRenderTriangles(cmd.ElemCount, cmd.IdxOffset, commandList->IdxBuffer, commandList->VtxBuffer, cmd.TextureId);
  273. rlDrawRenderBatchActive();
  274. }
  275. }
  276. rlSetTexture(0);
  277. rlDisableScissorTest();
  278. rlEnableBackfaceCulling();
  279. }
  280. void SetupMouseCursors()
  281. {
  282. MouseCursorMap.clear();
  283. MouseCursorMap[ImGuiMouseCursor_Arrow] = MOUSE_CURSOR_ARROW;
  284. MouseCursorMap[ImGuiMouseCursor_TextInput] = MOUSE_CURSOR_IBEAM;
  285. MouseCursorMap[ImGuiMouseCursor_Hand] = MOUSE_CURSOR_POINTING_HAND;
  286. MouseCursorMap[ImGuiMouseCursor_ResizeAll] = MOUSE_CURSOR_RESIZE_ALL;
  287. MouseCursorMap[ImGuiMouseCursor_ResizeEW] = MOUSE_CURSOR_RESIZE_EW;
  288. MouseCursorMap[ImGuiMouseCursor_ResizeNESW] = MOUSE_CURSOR_RESIZE_NESW;
  289. MouseCursorMap[ImGuiMouseCursor_ResizeNS] = MOUSE_CURSOR_RESIZE_NS;
  290. MouseCursorMap[ImGuiMouseCursor_ResizeNWSE] = MOUSE_CURSOR_RESIZE_NWSE;
  291. MouseCursorMap[ImGuiMouseCursor_NotAllowed] = MOUSE_CURSOR_NOT_ALLOWED;
  292. }
  293. void rlImGuiBeginInitImGui()
  294. {
  295. ImGui::CreateContext(nullptr);
  296. }
  297. void rlImGuiEndInitImGui()
  298. {
  299. SetupMouseCursors();
  300. ImGuiIO& io = ImGui::GetIO();
  301. io.BackendPlatformName = "imgui_impl_raylib";
  302. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
  303. io.KeyMap[ImGuiKey_Tab] = KEY_TAB;
  304. io.KeyMap[ImGuiKey_LeftArrow] = KEY_LEFT;
  305. io.KeyMap[ImGuiKey_RightArrow] = KEY_RIGHT;
  306. io.KeyMap[ImGuiKey_UpArrow] = KEY_UP;
  307. io.KeyMap[ImGuiKey_DownArrow] = KEY_DOWN;
  308. io.KeyMap[ImGuiKey_PageUp] = KEY_PAGE_DOWN;
  309. io.KeyMap[ImGuiKey_PageDown] = KEY_PAGE_UP;
  310. io.KeyMap[ImGuiKey_Home] = KEY_HOME;
  311. io.KeyMap[ImGuiKey_End] = KEY_END;
  312. io.KeyMap[ImGuiKey_Insert] = KEY_INSERT;
  313. io.KeyMap[ImGuiKey_Delete] = KEY_DELETE;
  314. io.KeyMap[ImGuiKey_Backspace] = KEY_BACKSPACE;
  315. io.KeyMap[ImGuiKey_Space] = KEY_SPACE;
  316. io.KeyMap[ImGuiKey_Enter] = KEY_ENTER;
  317. io.KeyMap[ImGuiKey_Escape] = KEY_ESCAPE;
  318. io.KeyMap[ImGuiKey_KeyPadEnter] = KEY_KP_ENTER;
  319. io.KeyMap[ImGuiKey_A] = KEY_A;
  320. io.KeyMap[ImGuiKey_C] = KEY_C;
  321. io.KeyMap[ImGuiKey_V] = KEY_V;
  322. io.KeyMap[ImGuiKey_X] = KEY_X;
  323. io.KeyMap[ImGuiKey_Y] = KEY_Y;
  324. io.KeyMap[ImGuiKey_Z] = KEY_Z;
  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. rlImGuiEndInitImGui();
  339. }
  340. void rlImGuiReloadFonts()
  341. {
  342. ImGuiIO& io = ImGui::GetIO();
  343. unsigned char* pixels = nullptr;
  344. int width;
  345. int height;
  346. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, nullptr);
  347. Image image = GenImageColor(width, height, BLANK);
  348. memcpy(image.data, pixels, width * height * 4);
  349. if (FontTexture.id != 0)
  350. UnloadTexture(FontTexture);
  351. FontTexture = LoadTextureFromImage(image);
  352. UnloadImage(image);
  353. io.Fonts->TexID = &FontTexture;
  354. }
  355. void rlImGuiBegin()
  356. {
  357. rlImGuiNewFrame();
  358. rlImGuiEvents();
  359. ImGui::NewFrame();
  360. }
  361. void rlImGuiEnd()
  362. {
  363. ImGui::Render();
  364. rlRenderData(ImGui::GetDrawData());
  365. }
  366. void rlImGuiShutdown()
  367. {
  368. for (const auto& tx : LoadedTextures)
  369. UnloadTexture(tx);
  370. UnloadTexture(FontTexture);
  371. LoadedTextures.clear();
  372. }
  373. void rlImGuiImage(const Texture *image)
  374. {
  375. ImGui::Image((ImTextureID)image, ImVec2(float(image->width), float(image->height)));
  376. }
  377. bool rlImGuiImageButton(const Texture *image) {
  378. return ImGui::ImageButton((ImTextureID)image, ImVec2(float(image->width), float(image->height)));
  379. }
  380. void rlImGuiImageSize(const Texture *image, int width, int height)
  381. {
  382. ImGui::Image((ImTextureID)image, ImVec2(float(width), float(height)));
  383. }
  384. void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect)
  385. {
  386. ImVec2 uv0;
  387. ImVec2 uv1;
  388. if (sourceRect.width < 0)
  389. {
  390. uv0.x = -((float)sourceRect.x / image->width);
  391. uv1.x = (uv0.x - (float)(fabs(sourceRect.width) / image->width));
  392. }
  393. else
  394. {
  395. uv0.x = (float)sourceRect.x / image->width;
  396. uv1.x = uv0.x + (float)(sourceRect.width / image->width);
  397. }
  398. if (sourceRect.height < 0)
  399. {
  400. uv0.y = -((float)sourceRect.y / image->height);
  401. uv1.y = (uv0.y - (float)(fabs(sourceRect.height) / image->height));
  402. }
  403. else
  404. {
  405. uv0.y = (float)sourceRect.y / image->height;
  406. uv1.y = uv0.y + (float)(sourceRect.height / image->height);
  407. }
  408. ImGui::Image((ImTextureID)image, ImVec2(float(destWidth), float(destHeight)),uv0,uv1);
  409. }