Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UI.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*!
  2. * \file src/UI.cpp
  3. * \brief UI interface manager
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include <cstring>
  9. #include "global.h"
  10. #include "Console.h"
  11. #include "Game.h"
  12. #include "Log.h"
  13. #include "Menu.h"
  14. #include "Render.h"
  15. #include "RunTime.h"
  16. #include "SoundManager.h"
  17. #include "TextureManager.h"
  18. #include "World.h"
  19. #include "commands/Command.h"
  20. #include "system/Sound.h"
  21. #include "system/Window.h"
  22. #include "utils/time.h"
  23. #include "UI.h"
  24. #define STB_IMAGE_IMPLEMENTATION
  25. #include "imgui/stb_image.h"
  26. bool UI::visible = false;
  27. unsigned int UI::fontTex;
  28. std::string UI::iniFilename;
  29. std::string UI::logFilename;
  30. bool UI::metaKeyIsActive = false;
  31. std::list<std::tuple<KeyboardButton, bool>> UI::keyboardEvents;
  32. std::list<std::tuple<unsigned int, unsigned int, KeyboardButton, bool>> UI::clickEvents;
  33. std::list<std::tuple<int, int, int, int>> UI::motionEvents;
  34. std::list<std::tuple<int, int>> UI::scrollEvents;
  35. int UI::initialize() {
  36. iniFilename = getRunTime().getBaseDir() + "/imgui.ini";
  37. logFilename = getRunTime().getBaseDir() + "/imgui_log.txt";
  38. ImGuiIO& io = ImGui::GetIO();
  39. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  40. io.DeltaTime = 1.0f / 60.0f;
  41. io.IniFilename = iniFilename.c_str();
  42. io.LogFilename = logFilename.c_str();
  43. io.KeyMap[ImGuiKey_Tab] = tabKey;
  44. io.KeyMap[ImGuiKey_LeftArrow] = leftKey;
  45. io.KeyMap[ImGuiKey_RightArrow] = rightKey;
  46. io.KeyMap[ImGuiKey_UpArrow] = upKey;
  47. io.KeyMap[ImGuiKey_DownArrow] = downKey;
  48. io.KeyMap[ImGuiKey_Home] = homeKey;
  49. io.KeyMap[ImGuiKey_End] = endKey;
  50. io.KeyMap[ImGuiKey_Delete] = delKey;
  51. io.KeyMap[ImGuiKey_Backspace] = backspaceKey;
  52. io.KeyMap[ImGuiKey_Enter] = enterKey;
  53. io.KeyMap[ImGuiKey_Escape] = escapeKey;
  54. io.KeyMap[ImGuiKey_A] = aKey;
  55. io.KeyMap[ImGuiKey_C] = cKey;
  56. io.KeyMap[ImGuiKey_V] = vKey;
  57. io.KeyMap[ImGuiKey_X] = xKey;
  58. io.KeyMap[ImGuiKey_Y] = yKey;
  59. io.KeyMap[ImGuiKey_Z] = zKey;
  60. io.RenderDrawListsFn = UI::renderImGui;
  61. // Load font texture
  62. //! \todo Use our own font subsystem instead of this?
  63. const void* png_data;
  64. unsigned int png_size;
  65. ImGui::GetDefaultFontData(nullptr, nullptr, &png_data, &png_size);
  66. int tex_x, tex_y, tex_comp;
  67. void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
  68. (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
  69. fontTex = getTextureManager().loadBufferSlot((unsigned char*)tex_data,
  70. tex_x, tex_y, TextureManager::ColorMode::RGBA, 32,
  71. TextureManager::TextureStorage::SYSTEM, -1, false);
  72. stbi_image_free(tex_data);
  73. return 0;
  74. }
  75. void UI::eventsFinished() {
  76. ImGuiIO& io = ImGui::GetIO();
  77. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  78. static unsigned long lastTime = 0;
  79. io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
  80. lastTime = systemTimerGet();
  81. if (io.DeltaTime <= 0.0f)
  82. io.DeltaTime = 1.0f / 60.0f;
  83. ImGui::NewFrame();
  84. if (!visible) {
  85. while (!clickEvents.empty()) {
  86. auto i = clickEvents.front();
  87. if (getMenu().isVisible()) {
  88. getMenu().handleMouseClick(std::get<0>(i), std::get<1>(i),
  89. std::get<2>(i), std::get<3>(i));
  90. }
  91. clickEvents.pop_front();
  92. }
  93. while (!motionEvents.empty()) {
  94. auto i = motionEvents.front();
  95. if (!getMenu().isVisible()) {
  96. getGame().handleMouseMotion(std::get<0>(i), std::get<1>(i),
  97. std::get<2>(i), std::get<3>(i));
  98. }
  99. motionEvents.pop_front();
  100. }
  101. while (!scrollEvents.empty()) {
  102. auto i = scrollEvents.front();
  103. if (getMenu().isVisible()) {
  104. getMenu().handleMouseScroll(std::get<0>(i), std::get<1>(i));
  105. }
  106. scrollEvents.pop_front();
  107. }
  108. }
  109. while (!keyboardEvents.empty()) {
  110. auto i = keyboardEvents.front();
  111. if (!visible) {
  112. if (getMenu().isVisible()) {
  113. getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
  114. } else {
  115. for (int n = forwardAction; n < ActionEventCount; n++) {
  116. if (getRunTime().getKeyBinding((ActionEvents)n) == std::get<0>(i))
  117. getGame().handleAction((ActionEvents)n, !std::get<1>(i));
  118. }
  119. }
  120. }
  121. if (std::get<1>(i)) {
  122. if (!visible) {
  123. if (getRunTime().getKeyBinding(menuAction) == std::get<0>(i)) {
  124. getMenu().setVisible(!getMenu().isVisible());
  125. }
  126. }
  127. if ((!io.WantCaptureKeyboard) || (!visible)) {
  128. if (getRunTime().getKeyBinding(debugAction) == std::get<0>(i)) {
  129. if (!metaKeyIsActive)
  130. visible = !visible;
  131. }
  132. }
  133. }
  134. keyboardEvents.pop_front();
  135. }
  136. bool clicked = !clickEvents.empty();
  137. // Only already empty when !visible
  138. if (visible) {
  139. clickEvents.clear();
  140. motionEvents.clear();
  141. scrollEvents.clear();
  142. }
  143. if (visible && (
  144. ((!io.WantCaptureKeyboard) && io.KeysDown[escapeKey])
  145. || ((!io.WantCaptureMouse) && clicked)
  146. )) {
  147. visible = false;
  148. }
  149. if (getWindow().getTextInput() != visible)
  150. getWindow().setTextInput(visible);
  151. bool input = !(visible || getMenu().isVisible());
  152. if (getWindow().getMousegrab() != input)
  153. getWindow().setMousegrab(input);
  154. io.MouseWheel = 0;
  155. }
  156. void UI::display() {
  157. if (!visible)
  158. return;
  159. Console::display();
  160. if (ImGui::Begin("Engine")) {
  161. if (ImGui::CollapsingHeader("Engine Info")) {
  162. ImGui::Text("Uptime: %lums", systemTimerGet());
  163. ImGui::Text("Frames per Second: %luFPS", getRunTime().getFPS());
  164. if (getRunTime().getHistoryFPS().size() > 1) {
  165. static bool scroll = true;
  166. if (scroll) {
  167. int offset = getRunTime().getHistoryFPS().size() - 1;
  168. if (offset > 10)
  169. offset = 10;
  170. ImGui::PlotLines("FPS", &getRunTime().getHistoryFPS()[1],
  171. getRunTime().getHistoryFPS().size() - 1,
  172. getRunTime().getHistoryFPS().size() - offset - 1);
  173. } else {
  174. ImGui::PlotLines("FPS", &getRunTime().getHistoryFPS()[1],
  175. getRunTime().getHistoryFPS().size() - 1);
  176. }
  177. ImGui::SameLine();
  178. ImGui::Checkbox("Scroll##fpsscroll", &scroll);
  179. }
  180. }
  181. if (ImGui::CollapsingHeader("RunTime Settings")) {
  182. bool showFPS = getRunTime().getShowFPS();
  183. if (ImGui::Checkbox("Show FPS##runtime", &showFPS)) {
  184. getRunTime().setShowFPS(showFPS);
  185. }
  186. ImGui::SameLine();
  187. bool running = getRunTime().isRunning();
  188. if (ImGui::Checkbox("Running (!)##runtime", &running)) {
  189. getRunTime().setRunning(running);
  190. }
  191. ImGui::SameLine();
  192. bool sound = Sound::getEnabled();
  193. if (ImGui::Checkbox("Sound##runtime", &sound)) {
  194. Sound::setEnabled(sound);
  195. }
  196. ImGui::SameLine();
  197. bool fullscreen = getWindow().getFullscreen();
  198. if (ImGui::Checkbox("Fullscreen##runtime", &fullscreen)) {
  199. getWindow().setFullscreen(fullscreen);
  200. }
  201. float vol = Sound::getVolume();
  202. if (ImGui::InputFloat("Volume##runtime", &vol, 0.0f, 0.0f, 3,
  203. ImGuiInputTextFlags_EnterReturnsTrue)) {
  204. if (vol < 0.0f)
  205. vol = 0.0f;
  206. if (vol > 1.0f)
  207. vol = 1.0f;
  208. Sound::setVolume(vol);
  209. }
  210. int w = getWindow().getWidth();
  211. if (ImGui::InputInt("Width##runtime", &w, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  212. if (w < 1)
  213. w = 1;
  214. getWindow().setSize(w, getWindow().getHeight());
  215. }
  216. int h = getWindow().getHeight();
  217. if (ImGui::InputInt("Height##runtime", &h, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  218. if (h < 1)
  219. h = 1;
  220. getWindow().setSize(getWindow().getWidth(), h);
  221. }
  222. static int fr = 0;
  223. char buff[1024];
  224. strncpy(buff, getRunTime().getBaseDir().c_str(), 1024);
  225. if (ImGui::InputText("BaseDir##runtime", buff, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  226. getRunTime().setBaseDir(buff);
  227. fr = getRunTime().getFPS();
  228. }
  229. if (fr > 0) {
  230. ImGui::SameLine();
  231. ImGui::Text("Done!##runtime1");
  232. fr--;
  233. }
  234. static int fr2 = 0;
  235. char buff2[1024];
  236. strncpy(buff2, getRunTime().getPakDir().c_str(), 1024);
  237. if (ImGui::InputText("PakDir##runtime", buff2, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  238. getRunTime().setPakDir(buff2);
  239. fr2 = getRunTime().getFPS();
  240. }
  241. if (fr2 > 0) {
  242. ImGui::SameLine();
  243. ImGui::Text("Done!##runtime2");
  244. fr2--;
  245. }
  246. static int fr3 = 0;
  247. char buff3[1024];
  248. strncpy(buff3, getRunTime().getAudioDir().c_str(), 1024);
  249. if (ImGui::InputText("AudioDir##runtime", buff3, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  250. getRunTime().setAudioDir(buff3);
  251. fr3 = getRunTime().getFPS();
  252. }
  253. if (fr3 > 0) {
  254. ImGui::SameLine();
  255. ImGui::Text("Done!##runtime3");
  256. fr3--;
  257. }
  258. static int fr4 = 0;
  259. char buff4[1024];
  260. strncpy(buff4, getRunTime().getDataDir().c_str(), 1024);
  261. if (ImGui::InputText("DataDir##runtime", buff4, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  262. getRunTime().setDataDir(buff4);
  263. fr4 = getRunTime().getFPS();
  264. }
  265. if (fr4 > 0) {
  266. ImGui::SameLine();
  267. ImGui::Text("Done!##runtime4");
  268. fr4--;
  269. }
  270. }
  271. ImGui::Separator();
  272. if (ImGui::CollapsingHeader("Sound Map Player")) {
  273. if (!Sound::getEnabled()) {
  274. ImGui::Text("Please enable Sound first!");
  275. if (ImGui::Button("Enable Sound!")) {
  276. Sound::setEnabled(true);
  277. }
  278. } else if (Sound::numBuffers() == 0) {
  279. ImGui::Text("Please load a level!");
  280. } else {
  281. static int index = 0;
  282. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  283. ImGui::SliderInt("##soundslide", &index, 0, SoundManager::sizeSoundMap() - 1);
  284. ImGui::PopItemWidth();
  285. ImGui::SameLine();
  286. if (ImGui::Button("+##soundplus", ImVec2(0, 0), true)) {
  287. if (index < (SoundManager::sizeSoundMap() - 1))
  288. index++;
  289. else
  290. index = 0;
  291. }
  292. ImGui::SameLine();
  293. if (ImGui::Button("-##soundminus", ImVec2(0, 0), true)) {
  294. if (index > 0)
  295. index--;
  296. else
  297. index = SoundManager::sizeSoundMap() - 1;
  298. }
  299. ImGui::SameLine();
  300. if (ImGui::Button("Play##soundplay")) {
  301. SoundManager::playSound(index);
  302. }
  303. ImGui::Text("Index: %d", SoundManager::getIndex(index));
  304. }
  305. }
  306. /*
  307. static bool visibleTex = false;
  308. static bool visibleTile = false;
  309. static bool visibleAnim = false;
  310. static bool visibleSprite = false;
  311. if (ImGui::CollapsingHeader("Texture Viewer")) {
  312. static bool game = getGame().isLoaded();
  313. static int index = 0;
  314. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  315. ImGui::SliderInt("##texslide", &index, 0, getTextureManager().numTextures(
  316. game ? TextureManager::TextureStorage::GAME
  317. : TextureManager::TextureStorage::SYSTEM) - 1);
  318. ImGui::PopItemWidth();
  319. ImGui::SameLine();
  320. if (ImGui::Button("+##texplus", ImVec2(0, 0), true)) {
  321. if (index < (getTextureManager().numTextures(
  322. game ? TextureManager::TextureStorage::GAME
  323. : TextureManager::TextureStorage::SYSTEM) - 1))
  324. index++;
  325. else
  326. index = 0;
  327. }
  328. ImGui::SameLine();
  329. if (ImGui::Button("-##texminus", ImVec2(0, 0), true)) {
  330. if (index > 0)
  331. index--;
  332. else
  333. index = getTextureManager().numTextures(
  334. game ? TextureManager::TextureStorage::GAME
  335. : TextureManager::TextureStorage::SYSTEM) - 1;
  336. }
  337. ImGui::SameLine();
  338. if ((getTextureManager().numTextures() > 0)) {
  339. ImGui::Checkbox("Game##texgame", &game);
  340. } else {
  341. game = false;
  342. }
  343. ImGui::SameLine();
  344. if (ImGui::Button("Show##texshow")) {
  345. visibleTex = true;
  346. visibleTile = false;
  347. visibleAnim = false;
  348. visibleSprite = false;
  349. }
  350. ImGui::SameLine();
  351. if (ImGui::Button("Clear##texclear")) {
  352. getRender().debugDisplayTexture();
  353. visibleTex = false;
  354. }
  355. if (visibleTex) {
  356. getRender().debugDisplayTexture(index,
  357. game ? TextureManager::TextureStorage::GAME
  358. : TextureManager::TextureStorage::SYSTEM,
  359. ImGui::GetWindowPos().x - ImGui::GetWindowWidth(),
  360. ImGui::GetWindowPos().y,
  361. ImGui::GetWindowWidth(), ImGui::GetWindowWidth());
  362. }
  363. }
  364. if (ImGui::CollapsingHeader("Textile Viewer")) {
  365. if (getTextureManager().numTiles() > 0) {
  366. static int index = 0;
  367. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  368. ImGui::SliderInt("##tileslide", &index, 0, getTextureManager().numTiles() - 1);
  369. ImGui::PopItemWidth();
  370. ImGui::SameLine();
  371. if (ImGui::Button("+##tileplus", ImVec2(0, 0), true)) {
  372. if (index < (getTextureManager().numTiles() - 1))
  373. index++;
  374. else
  375. index = 0;
  376. }
  377. ImGui::SameLine();
  378. if (ImGui::Button("-##tileminus", ImVec2(0, 0), true)) {
  379. if (index > 0)
  380. index--;
  381. else
  382. index = getTextureManager().numTiles() - 1;
  383. }
  384. ImGui::SameLine();
  385. if (ImGui::Button("Show##tileshow")) {
  386. visibleTile = true;
  387. visibleTex = false;
  388. visibleAnim = false;
  389. visibleSprite = false;
  390. }
  391. ImGui::SameLine();
  392. if (ImGui::Button("Clear##tileclear")) {
  393. getRender().debugDisplayTextile();
  394. visibleTile = false;
  395. }
  396. if (visibleTile && (index < getTextureManager().numTiles())) {
  397. ImGui::Text(getTextureManager().getTile(index).isTriangle() ? "Triangle" : "Rectangle");
  398. }
  399. if (visibleTile) {
  400. getRender().debugDisplayTextile(index,
  401. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  402. ImGui::GetWindowPos().y,
  403. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  404. }
  405. } else {
  406. ImGui::Text("Please load a level using the new loader!");
  407. }
  408. }
  409. if (ImGui::CollapsingHeader("Animated Textile Viewer")) {
  410. if (getTextureManager().numAnimatedTiles() > 0) {
  411. static int index = 0;
  412. static int tile = getTextureManager().getFirstTileAnimation(index);
  413. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  414. if (ImGui::SliderInt("##animslide", &index, 0, getTextureManager().numAnimatedTiles() - 1)) {
  415. tile = getTextureManager().getFirstTileAnimation(index);
  416. }
  417. ImGui::PopItemWidth();
  418. ImGui::SameLine();
  419. if (ImGui::Button("+##animplus", ImVec2(0, 0), true)) {
  420. if (index < (getTextureManager().numAnimatedTiles() - 1))
  421. index++;
  422. else
  423. index = 0;
  424. tile = getTextureManager().getFirstTileAnimation(index);
  425. }
  426. ImGui::SameLine();
  427. if (ImGui::Button("-##animminus", ImVec2(0, 0), true)) {
  428. if (index > 0)
  429. index--;
  430. else
  431. index = getTextureManager().numAnimatedTiles() - 1;
  432. tile = getTextureManager().getFirstTileAnimation(index);
  433. }
  434. ImGui::SameLine();
  435. if (ImGui::Button("Show##animshow")) {
  436. visibleAnim = true;
  437. visibleTex = false;
  438. visibleTile = false;
  439. visibleSprite = false;
  440. }
  441. ImGui::SameLine();
  442. if (ImGui::Button("Clear##animclear")) {
  443. getRender().debugDisplayTextile();
  444. visibleAnim = false;
  445. }
  446. if (visibleAnim) {
  447. static int fr = 0;
  448. if (fr > 0) {
  449. fr--;
  450. } else {
  451. getRender().debugDisplayTextile(tile,
  452. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  453. ImGui::GetWindowPos().y,
  454. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  455. fr = getRunTime().getFPS() / 2;
  456. tile = getTextureManager().getNextTileAnimation(tile);
  457. }
  458. ImGui::Text("Current Tile: %d", tile);
  459. }
  460. } else {
  461. ImGui::Text("Please load a level with animated textures!");
  462. }
  463. }
  464. if (ImGui::CollapsingHeader("Sprite Sequence Viewer")) {
  465. if (getWorld().sizeSprite() <= 0) {
  466. ImGui::Text("Please load a level containing sprites!");
  467. } else {
  468. static int index = 0;
  469. static int sprite = 0;
  470. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  471. if (ImGui::SliderInt("##spriteslide", &index, 0, getWorld().sizeSprite() - 1))
  472. sprite = 0;
  473. ImGui::PopItemWidth();
  474. ImGui::SameLine();
  475. if (ImGui::Button("+##spriteplus", ImVec2(0, 0), true)) {
  476. if (index < (getWorld().sizeSprite() - 1))
  477. index++;
  478. else
  479. index = 0;
  480. sprite = 0;
  481. }
  482. ImGui::SameLine();
  483. if (ImGui::Button("-##spriteminus", ImVec2(0, 0), true)) {
  484. if (index > 0)
  485. index--;
  486. else
  487. index = getWorld().sizeSprite() - 1;
  488. sprite = 0;
  489. }
  490. ImGui::SameLine();
  491. if (ImGui::Button("Show##spriteshow")) {
  492. visibleSprite = true;
  493. visibleTex = false;
  494. visibleTile = false;
  495. visibleAnim = false;
  496. sprite = 0;
  497. }
  498. ImGui::SameLine();
  499. if (ImGui::Button("Clear##spriteclear")) {
  500. getRender().debugDisplaySprite();
  501. visibleSprite = false;
  502. }
  503. if (visibleSprite) {
  504. static int fr = 0;
  505. if (fr > 0) {
  506. fr--;
  507. } else {
  508. getRender().debugDisplaySprite(index, sprite,
  509. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  510. ImGui::GetWindowPos().y,
  511. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  512. fr = getRunTime().getFPS() / 10;
  513. if (sprite < (getWorld().getSprite(index).size() - 1))
  514. sprite++;
  515. else
  516. sprite = 0;
  517. }
  518. ImGui::Text("Sprite %d/%d", sprite + 1, getWorld().getSprite(index).size());
  519. }
  520. }
  521. }
  522. */
  523. ImGui::Separator();
  524. if (ImGui::CollapsingHeader("ImGui/Debug UI Help")) {
  525. //ImGui::TextWrapped("DebugViewer Textures/Textiles/Sprites will be drawn on"
  526. // " the left side and scale with the size of this window!");
  527. //ImGui::Separator();
  528. ImGui::ShowUserGuide();
  529. }
  530. }
  531. ImGui::End();
  532. ImGui::Render();
  533. }
  534. void UI::shutdown() {
  535. ImGui::Shutdown();
  536. }
  537. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  538. ImGuiIO& io = ImGui::GetIO();
  539. io.KeysDown[key] = pressed;
  540. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  541. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  542. keyboardEvents.push_back(std::make_tuple(key, pressed));
  543. if ((key == leftguiKey) || (key == rightguiKey))
  544. metaKeyIsActive = pressed;
  545. }
  546. void UI::handleText(char* text, bool notFinished) {
  547. if (notFinished)
  548. return;
  549. ImGuiIO& io = ImGui::GetIO();
  550. while (*text != '\0') {
  551. io.AddInputCharacter(*text);
  552. text++;
  553. }
  554. }
  555. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  556. ImGuiIO& io = ImGui::GetIO();
  557. io.MousePos = ImVec2((float)x, (float)y);
  558. if (button == leftmouseKey) {
  559. io.MouseDown[0] = !released;
  560. } else if (button == rightmouseKey) {
  561. io.MouseDown[1] = !released;
  562. } else if (button == middlemouseKey) {
  563. io.MouseDown[2] = !released;
  564. } else if (button == fourthmouseKey) {
  565. io.MouseDown[3] = !released;
  566. } else if (button == fifthmouseKey) {
  567. io.MouseDown[4] = !released;
  568. }
  569. clickEvents.push_back(std::make_tuple(x, y, button, released));
  570. }
  571. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  572. ImGuiIO& io = ImGui::GetIO();
  573. io.MousePos = ImVec2((float)xabs, (float)yabs);
  574. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  575. }
  576. void UI::handleMouseScroll(int xrel, int yrel) {
  577. ImGuiIO& io = ImGui::GetIO();
  578. io.MouseWheel += yrel;
  579. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  580. }
  581. void UI::setVisible(bool v) {
  582. visible = v;
  583. }
  584. bool UI::isVisible() {
  585. return visible;
  586. }
  587. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  588. if (cmd_lists_count == 0)
  589. return;
  590. glEnable(GL_SCISSOR_TEST);
  591. glDisable(GL_DEPTH_TEST);
  592. Window::imguiShader.use();
  593. glUniform2f(Window::imguiShader.getUniform(0), getWindow().getWidth(), getWindow().getHeight());
  594. getTextureManager().bindTextureId(fontTex, TextureManager::TextureStorage::SYSTEM, 0);
  595. glUniform1i(Window::imguiShader.getUniform(1), 0);
  596. glEnableVertexAttribArray(0); // Vertices
  597. glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(0));
  598. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
  599. glEnableVertexAttribArray(1); // UVs
  600. glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(1));
  601. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
  602. glEnableVertexAttribArray(2); // Colors
  603. glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(2));
  604. glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
  605. std::vector<glm::vec2> vertices;
  606. std::vector<glm::vec2> uvs;
  607. std::vector<glm::vec4> colors;
  608. /*! \fixme Don't copy data
  609. * The GL calls and the shaders can probably be slightly altered
  610. * to avoid copying all the vertices, uvs and colors again here.
  611. */
  612. for (int i = 0; i < cmd_lists_count; i++) {
  613. auto& commands = cmd_lists[i]->commands;
  614. auto& buffer = cmd_lists[i]->vtx_buffer;
  615. int offset = 0;
  616. for (int n = 0; n < commands.size(); n++) {
  617. for (int v = 0; v < commands[n].vtx_count; v++) {
  618. vertices.push_back(glm::vec2(buffer[offset + v].pos.x, buffer[offset + v].pos.y));
  619. uvs.push_back(glm::vec2(buffer[offset + v].uv.x, buffer[offset + v].uv.y));
  620. float r, g, b, a;
  621. a = ((buffer[offset + v].col & 0xFF000000) >> 24) / 255.0f;
  622. b = ((buffer[offset + v].col & 0x00FF0000) >> 16) / 255.0f;
  623. g = ((buffer[offset + v].col & 0x0000FF00) >> 8) / 255.0f;
  624. r = (buffer[offset + v].col & 0x000000FF) / 255.0f;
  625. colors.push_back(glm::vec4(r, g, b, a));
  626. }
  627. offset += commands[n].vtx_count;
  628. glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(0));
  629. glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec2), &vertices[0], GL_STATIC_DRAW);
  630. glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(1));
  631. glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
  632. glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(2));
  633. glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec4), &colors[0], GL_STATIC_DRAW);
  634. glScissor(commands[n].clip_rect.x,
  635. getWindow().getHeight() - commands[n].clip_rect.w,
  636. commands[n].clip_rect.z - commands[n].clip_rect.x,
  637. commands[n].clip_rect.w - commands[n].clip_rect.y);
  638. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  639. vertices.clear();
  640. uvs.clear();
  641. colors.clear();
  642. }
  643. }
  644. glDisableVertexAttribArray(0);
  645. glDisableVertexAttribArray(1);
  646. glDisableVertexAttribArray(2);
  647. glEnable(GL_DEPTH_TEST);
  648. glDisable(GL_SCISSOR_TEST);
  649. }