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 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*!
  2. * \file src/UI.cpp
  3. * \brief UI interface manager
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include <cstring>
  9. #include "imgui/imgui.h"
  10. #include "stb/stb_image.h"
  11. #include "global.h"
  12. #include "Camera.h"
  13. #include "Console.h"
  14. #include "Game.h"
  15. #include "Log.h"
  16. #include "Menu.h"
  17. #include "Render.h"
  18. #include "RunTime.h"
  19. #include "SoundManager.h"
  20. #include "TextureManager.h"
  21. #include "World.h"
  22. #include "commands/Command.h"
  23. #include "system/Sound.h"
  24. #include "system/Window.h"
  25. #include "utils/time.h"
  26. #include "UI.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 = RunTime::getBaseDir() + "/imgui.ini";
  38. logFilename = RunTime::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 (RunTime::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 (RunTime::getKeyBinding(menuAction) == std::get<0>(i)) {
  125. getMenu().setVisible(!getMenu().isVisible());
  126. }
  127. }
  128. if ((!io.WantCaptureKeyboard) || (!visible)) {
  129. if (RunTime::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. static bool showTestWindow = false;
  162. if (ImGui::Begin("Engine")) {
  163. Render::displayUI();
  164. RunTime::display();
  165. SoundManager::display();
  166. /*
  167. static bool visibleTex = false;
  168. static bool visibleTile = false;
  169. static bool visibleAnim = false;
  170. static bool visibleSprite = false;
  171. if (ImGui::CollapsingHeader("Texture Viewer")) {
  172. static bool game = getGame().isLoaded();
  173. static int index = 0;
  174. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  175. ImGui::SliderInt("##texslide", &index, 0, getTextureManager().numTextures(
  176. game ? TextureManager::TextureStorage::GAME
  177. : TextureManager::TextureStorage::SYSTEM) - 1);
  178. ImGui::PopItemWidth();
  179. ImGui::SameLine();
  180. if (ImGui::Button("+##texplus", ImVec2(0, 0), true)) {
  181. if (index < (getTextureManager().numTextures(
  182. game ? TextureManager::TextureStorage::GAME
  183. : TextureManager::TextureStorage::SYSTEM) - 1))
  184. index++;
  185. else
  186. index = 0;
  187. }
  188. ImGui::SameLine();
  189. if (ImGui::Button("-##texminus", ImVec2(0, 0), true)) {
  190. if (index > 0)
  191. index--;
  192. else
  193. index = getTextureManager().numTextures(
  194. game ? TextureManager::TextureStorage::GAME
  195. : TextureManager::TextureStorage::SYSTEM) - 1;
  196. }
  197. ImGui::SameLine();
  198. if ((getTextureManager().numTextures() > 0)) {
  199. ImGui::Checkbox("Game##texgame", &game);
  200. } else {
  201. game = false;
  202. }
  203. ImGui::SameLine();
  204. if (ImGui::Button("Show##texshow")) {
  205. visibleTex = true;
  206. visibleTile = false;
  207. visibleAnim = false;
  208. visibleSprite = false;
  209. }
  210. ImGui::SameLine();
  211. if (ImGui::Button("Clear##texclear")) {
  212. getRender().debugDisplayTexture();
  213. visibleTex = false;
  214. }
  215. if (visibleTex) {
  216. getRender().debugDisplayTexture(index,
  217. game ? TextureManager::TextureStorage::GAME
  218. : TextureManager::TextureStorage::SYSTEM,
  219. ImGui::GetWindowPos().x - ImGui::GetWindowWidth(),
  220. ImGui::GetWindowPos().y,
  221. ImGui::GetWindowWidth(), ImGui::GetWindowWidth());
  222. }
  223. }
  224. if (ImGui::CollapsingHeader("Textile Viewer")) {
  225. if (getTextureManager().numTiles() > 0) {
  226. static int index = 0;
  227. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  228. ImGui::SliderInt("##tileslide", &index, 0, getTextureManager().numTiles() - 1);
  229. ImGui::PopItemWidth();
  230. ImGui::SameLine();
  231. if (ImGui::Button("+##tileplus", ImVec2(0, 0), true)) {
  232. if (index < (getTextureManager().numTiles() - 1))
  233. index++;
  234. else
  235. index = 0;
  236. }
  237. ImGui::SameLine();
  238. if (ImGui::Button("-##tileminus", ImVec2(0, 0), true)) {
  239. if (index > 0)
  240. index--;
  241. else
  242. index = getTextureManager().numTiles() - 1;
  243. }
  244. ImGui::SameLine();
  245. if (ImGui::Button("Show##tileshow")) {
  246. visibleTile = true;
  247. visibleTex = false;
  248. visibleAnim = false;
  249. visibleSprite = false;
  250. }
  251. ImGui::SameLine();
  252. if (ImGui::Button("Clear##tileclear")) {
  253. getRender().debugDisplayTextile();
  254. visibleTile = false;
  255. }
  256. if (visibleTile && (index < getTextureManager().numTiles())) {
  257. ImGui::Text(getTextureManager().getTile(index).isTriangle() ? "Triangle" : "Rectangle");
  258. }
  259. if (visibleTile) {
  260. getRender().debugDisplayTextile(index,
  261. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  262. ImGui::GetWindowPos().y,
  263. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  264. }
  265. } else {
  266. ImGui::Text("Please load a level using the new loader!");
  267. }
  268. }
  269. if (ImGui::CollapsingHeader("Animated Textile Viewer")) {
  270. if (getTextureManager().numAnimatedTiles() > 0) {
  271. static int index = 0;
  272. static int tile = getTextureManager().getFirstTileAnimation(index);
  273. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  274. if (ImGui::SliderInt("##animslide", &index, 0, getTextureManager().numAnimatedTiles() - 1)) {
  275. tile = getTextureManager().getFirstTileAnimation(index);
  276. }
  277. ImGui::PopItemWidth();
  278. ImGui::SameLine();
  279. if (ImGui::Button("+##animplus", ImVec2(0, 0), true)) {
  280. if (index < (getTextureManager().numAnimatedTiles() - 1))
  281. index++;
  282. else
  283. index = 0;
  284. tile = getTextureManager().getFirstTileAnimation(index);
  285. }
  286. ImGui::SameLine();
  287. if (ImGui::Button("-##animminus", ImVec2(0, 0), true)) {
  288. if (index > 0)
  289. index--;
  290. else
  291. index = getTextureManager().numAnimatedTiles() - 1;
  292. tile = getTextureManager().getFirstTileAnimation(index);
  293. }
  294. ImGui::SameLine();
  295. if (ImGui::Button("Show##animshow")) {
  296. visibleAnim = true;
  297. visibleTex = false;
  298. visibleTile = false;
  299. visibleSprite = false;
  300. }
  301. ImGui::SameLine();
  302. if (ImGui::Button("Clear##animclear")) {
  303. getRender().debugDisplayTextile();
  304. visibleAnim = false;
  305. }
  306. if (visibleAnim) {
  307. static int fr = 0;
  308. if (fr > 0) {
  309. fr--;
  310. } else {
  311. getRender().debugDisplayTextile(tile,
  312. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  313. ImGui::GetWindowPos().y,
  314. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  315. fr = RunTime::getFPS() / 2;
  316. tile = getTextureManager().getNextTileAnimation(tile);
  317. }
  318. ImGui::Text("Current Tile: %d", tile);
  319. }
  320. } else {
  321. ImGui::Text("Please load a level with animated textures!");
  322. }
  323. }
  324. if (ImGui::CollapsingHeader("Sprite Sequence Viewer")) {
  325. if (getWorld().sizeSprite() <= 0) {
  326. ImGui::Text("Please load a level containing sprites!");
  327. } else {
  328. static int index = 0;
  329. static int sprite = 0;
  330. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  331. if (ImGui::SliderInt("##spriteslide", &index, 0, getWorld().sizeSprite() - 1))
  332. sprite = 0;
  333. ImGui::PopItemWidth();
  334. ImGui::SameLine();
  335. if (ImGui::Button("+##spriteplus", ImVec2(0, 0), true)) {
  336. if (index < (getWorld().sizeSprite() - 1))
  337. index++;
  338. else
  339. index = 0;
  340. sprite = 0;
  341. }
  342. ImGui::SameLine();
  343. if (ImGui::Button("-##spriteminus", ImVec2(0, 0), true)) {
  344. if (index > 0)
  345. index--;
  346. else
  347. index = getWorld().sizeSprite() - 1;
  348. sprite = 0;
  349. }
  350. ImGui::SameLine();
  351. if (ImGui::Button("Show##spriteshow")) {
  352. visibleSprite = true;
  353. visibleTex = false;
  354. visibleTile = false;
  355. visibleAnim = false;
  356. sprite = 0;
  357. }
  358. ImGui::SameLine();
  359. if (ImGui::Button("Clear##spriteclear")) {
  360. getRender().debugDisplaySprite();
  361. visibleSprite = false;
  362. }
  363. if (visibleSprite) {
  364. static int fr = 0;
  365. if (fr > 0) {
  366. fr--;
  367. } else {
  368. getRender().debugDisplaySprite(index, sprite,
  369. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  370. ImGui::GetWindowPos().y,
  371. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  372. fr = RunTime::getFPS() / 10;
  373. if (sprite < (getWorld().getSprite(index).size() - 1))
  374. sprite++;
  375. else
  376. sprite = 0;
  377. }
  378. ImGui::Text("Sprite %d/%d", sprite + 1, getWorld().getSprite(index).size());
  379. }
  380. }
  381. }
  382. */
  383. if (ImGui::CollapsingHeader("ImGui/Debug UI Help")) {
  384. //ImGui::TextWrapped("DebugViewer Textures/Textiles/Sprites will be drawn on"
  385. // " the left side and scale with the size of this window!");
  386. //ImGui::Separator();
  387. ImGui::ShowUserGuide();
  388. ImGui::Separator();
  389. if (ImGui::Button("Show/Hide Test Window")) {
  390. showTestWindow = !showTestWindow;
  391. }
  392. }
  393. }
  394. ImGui::End();
  395. if (showTestWindow)
  396. ImGui::ShowTestWindow();
  397. ImGui::Render();
  398. }
  399. void UI::shutdown() {
  400. ImGui::Shutdown();
  401. }
  402. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  403. ImGuiIO& io = ImGui::GetIO();
  404. io.KeysDown[key] = pressed;
  405. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  406. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  407. keyboardEvents.push_back(std::make_tuple(key, pressed));
  408. if ((key == leftguiKey) || (key == rightguiKey))
  409. metaKeyIsActive = pressed;
  410. }
  411. void UI::handleText(char* text, bool notFinished) {
  412. if (notFinished)
  413. return;
  414. ImGuiIO& io = ImGui::GetIO();
  415. while (*text != '\0') {
  416. io.AddInputCharacter(*text);
  417. text++;
  418. }
  419. }
  420. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  421. ImGuiIO& io = ImGui::GetIO();
  422. io.MousePos = ImVec2((float)x, (float)y);
  423. if (button == leftmouseKey) {
  424. io.MouseDown[0] = !released;
  425. } else if (button == rightmouseKey) {
  426. io.MouseDown[1] = !released;
  427. } else if (button == middlemouseKey) {
  428. io.MouseDown[2] = !released;
  429. } else if (button == fourthmouseKey) {
  430. io.MouseDown[3] = !released;
  431. } else if (button == fifthmouseKey) {
  432. io.MouseDown[4] = !released;
  433. }
  434. clickEvents.push_back(std::make_tuple(x, y, button, released));
  435. }
  436. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  437. ImGuiIO& io = ImGui::GetIO();
  438. io.MousePos = ImVec2((float)xabs, (float)yabs);
  439. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  440. }
  441. void UI::handleMouseScroll(int xrel, int yrel) {
  442. ImGuiIO& io = ImGui::GetIO();
  443. io.MouseWheel += yrel;
  444. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  445. }
  446. void UI::handleControllerAxis(float value, KeyboardButton axis) {
  447. getGame().handleControllerAxis(value, axis);
  448. }
  449. void UI::handleControllerButton(KeyboardButton button, bool released) {
  450. getGame().handleControllerButton(button, released);
  451. }
  452. void UI::setVisible(bool v) {
  453. visible = v;
  454. }
  455. bool UI::isVisible() {
  456. return visible;
  457. }
  458. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  459. if (cmd_lists_count == 0)
  460. return;
  461. glEnable(GL_SCISSOR_TEST);
  462. glDisable(GL_DEPTH_TEST);
  463. Window::imguiShader.use();
  464. Window::imguiShader.loadUniform(0, Window::getSize());
  465. Window::imguiShader.loadUniform(1, fontTex, TextureManager::TextureStorage::SYSTEM);
  466. Window::imguiShader.bindBuffer(0, 0, 2);
  467. Window::imguiShader.bindBuffer(1, 1, 2);
  468. Window::imguiShader.bindBuffer(2, 2, 4);
  469. std::vector<glm::vec2> vertices;
  470. std::vector<glm::vec2> uvs;
  471. std::vector<glm::vec4> colors;
  472. /*! \fixme Don't copy data
  473. * The GL calls and the shaders can probably be slightly altered
  474. * to avoid copying all the vertices, uvs and colors again here.
  475. */
  476. for (int i = 0; i < cmd_lists_count; i++) {
  477. auto& commands = cmd_lists[i]->commands;
  478. auto& buffer = cmd_lists[i]->vtx_buffer;
  479. int offset = 0;
  480. for (int n = 0; n < commands.size(); n++) {
  481. for (int v = 0; v < commands[n].vtx_count; v++) {
  482. vertices.push_back(glm::vec2(buffer[offset + v].pos.x, buffer[offset + v].pos.y));
  483. uvs.push_back(glm::vec2(buffer[offset + v].uv.x, buffer[offset + v].uv.y));
  484. float r, g, b, a;
  485. a = ((buffer[offset + v].col & 0xFF000000) >> 24) / 255.0f;
  486. b = ((buffer[offset + v].col & 0x00FF0000) >> 16) / 255.0f;
  487. g = ((buffer[offset + v].col & 0x0000FF00) >> 8) / 255.0f;
  488. r = (buffer[offset + v].col & 0x000000FF) / 255.0f;
  489. colors.push_back(glm::vec4(r, g, b, a));
  490. }
  491. offset += commands[n].vtx_count;
  492. Window::imguiShader.bufferData(0, vertices);
  493. Window::imguiShader.bufferData(1, uvs);
  494. Window::imguiShader.bufferData(2, colors);
  495. glScissor(commands[n].clip_rect.x,
  496. Window::getSize().y - commands[n].clip_rect.w,
  497. commands[n].clip_rect.z - commands[n].clip_rect.x,
  498. commands[n].clip_rect.w - commands[n].clip_rect.y);
  499. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  500. vertices.clear();
  501. uvs.clear();
  502. colors.clear();
  503. }
  504. }
  505. Window::imguiShader.disableAttribs();
  506. glEnable(GL_DEPTH_TEST);
  507. glDisable(GL_SCISSOR_TEST);
  508. }