Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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