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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. void UI::setSize(glm::i32vec2 s) {
  37. ImGuiIO& io = ImGui::GetIO();
  38. io.DisplaySize = ImVec2(s.x, s.y);
  39. }
  40. int UI::initialize() {
  41. iniFilename = RunTime::getBaseDir() + "/imgui.ini";
  42. logFilename = RunTime::getBaseDir() + "/imgui_log.txt";
  43. ImGuiIO& io = ImGui::GetIO();
  44. io.DisplaySize = ImVec2(Window::getSize().x, Window::getSize().y);
  45. io.DeltaTime = 1.0f / 60.0f;
  46. io.IniFilename = iniFilename.c_str();
  47. io.LogFilename = logFilename.c_str();
  48. io.KeyMap[ImGuiKey_Tab] = tabKey;
  49. io.KeyMap[ImGuiKey_LeftArrow] = leftKey;
  50. io.KeyMap[ImGuiKey_RightArrow] = rightKey;
  51. io.KeyMap[ImGuiKey_UpArrow] = upKey;
  52. io.KeyMap[ImGuiKey_DownArrow] = downKey;
  53. io.KeyMap[ImGuiKey_Home] = homeKey;
  54. io.KeyMap[ImGuiKey_End] = endKey;
  55. io.KeyMap[ImGuiKey_Delete] = delKey;
  56. io.KeyMap[ImGuiKey_Backspace] = backspaceKey;
  57. io.KeyMap[ImGuiKey_Enter] = enterKey;
  58. io.KeyMap[ImGuiKey_Escape] = escapeKey;
  59. io.KeyMap[ImGuiKey_A] = aKey;
  60. io.KeyMap[ImGuiKey_C] = cKey;
  61. io.KeyMap[ImGuiKey_V] = vKey;
  62. io.KeyMap[ImGuiKey_X] = xKey;
  63. io.KeyMap[ImGuiKey_Y] = yKey;
  64. io.KeyMap[ImGuiKey_Z] = zKey;
  65. io.RenderDrawListsFn = UI::renderImGui;
  66. // Load font texture
  67. //! \todo Use our own font subsystem instead of this?
  68. const void* png_data;
  69. unsigned int png_size;
  70. ImGui::GetDefaultFontData(nullptr, nullptr, &png_data, &png_size);
  71. int tex_x, tex_y, tex_comp;
  72. void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
  73. (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
  74. fontTex = getTextureManager().loadBufferSlot((unsigned char*)tex_data,
  75. tex_x, tex_y, TextureManager::ColorMode::RGBA, 32,
  76. TextureManager::TextureStorage::SYSTEM, -1, false);
  77. stbi_image_free(tex_data);
  78. return 0;
  79. }
  80. void UI::eventsFinished() {
  81. ImGuiIO& io = ImGui::GetIO();
  82. io.DisplaySize = ImVec2(Window::getSize().x, Window::getSize().y);
  83. static unsigned long lastTime = 0;
  84. io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
  85. lastTime = systemTimerGet();
  86. if (io.DeltaTime <= 0.0f)
  87. io.DeltaTime = 1.0f / 60.0f;
  88. ImGui::NewFrame();
  89. if (!visible) {
  90. while (!clickEvents.empty()) {
  91. auto i = clickEvents.front();
  92. if (getMenu().isVisible()) {
  93. getMenu().handleMouseClick(std::get<0>(i), std::get<1>(i),
  94. std::get<2>(i), std::get<3>(i));
  95. }
  96. clickEvents.pop_front();
  97. }
  98. while (!motionEvents.empty()) {
  99. auto i = motionEvents.front();
  100. if (!getMenu().isVisible()) {
  101. getGame().handleMouseMotion(std::get<0>(i), std::get<1>(i),
  102. std::get<2>(i), std::get<3>(i));
  103. }
  104. motionEvents.pop_front();
  105. }
  106. while (!scrollEvents.empty()) {
  107. auto i = scrollEvents.front();
  108. if (getMenu().isVisible()) {
  109. getMenu().handleMouseScroll(std::get<0>(i), std::get<1>(i));
  110. }
  111. scrollEvents.pop_front();
  112. }
  113. }
  114. while (!keyboardEvents.empty()) {
  115. auto i = keyboardEvents.front();
  116. if (!visible) {
  117. if (getMenu().isVisible()) {
  118. getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
  119. } else {
  120. for (int n = forwardAction; n < ActionEventCount; n++) {
  121. if (RunTime::getKeyBinding((ActionEvents)n) == std::get<0>(i))
  122. getGame().handleAction((ActionEvents)n, !std::get<1>(i));
  123. }
  124. }
  125. }
  126. if (std::get<1>(i)) {
  127. if (!visible) {
  128. if (RunTime::getKeyBinding(menuAction) == std::get<0>(i)) {
  129. getMenu().setVisible(!getMenu().isVisible());
  130. }
  131. }
  132. if ((!io.WantCaptureKeyboard) || (!visible)) {
  133. if (RunTime::getKeyBinding(debugAction) == std::get<0>(i)) {
  134. if (!metaKeyIsActive)
  135. visible = !visible;
  136. }
  137. }
  138. }
  139. keyboardEvents.pop_front();
  140. }
  141. bool clicked = !clickEvents.empty();
  142. // Only already empty when !visible
  143. if (visible) {
  144. clickEvents.clear();
  145. motionEvents.clear();
  146. scrollEvents.clear();
  147. }
  148. if (visible && (
  149. ((!io.WantCaptureKeyboard) && io.KeysDown[escapeKey])
  150. || ((!io.WantCaptureMouse) && clicked)
  151. )) {
  152. visible = false;
  153. }
  154. if (Window::getTextInput() != visible)
  155. Window::setTextInput(visible);
  156. bool input = !(visible || getMenu().isVisible());
  157. if (Window::getMousegrab() != input)
  158. Window::setMousegrab(input);
  159. io.MouseWheel = 0;
  160. }
  161. void UI::display() {
  162. if (!visible)
  163. return;
  164. Console::display();
  165. static bool showTestWindow = false;
  166. if (ImGui::Begin("Engine")) {
  167. Render::displayUI();
  168. RunTime::display();
  169. SoundManager::display();
  170. /*
  171. static bool visibleTex = false;
  172. static bool visibleTile = false;
  173. static bool visibleAnim = false;
  174. static bool visibleSprite = false;
  175. if (ImGui::CollapsingHeader("Texture Viewer")) {
  176. static bool game = getGame().isLoaded();
  177. static int index = 0;
  178. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  179. ImGui::SliderInt("##texslide", &index, 0, getTextureManager().numTextures(
  180. game ? TextureManager::TextureStorage::GAME
  181. : TextureManager::TextureStorage::SYSTEM) - 1);
  182. ImGui::PopItemWidth();
  183. ImGui::SameLine();
  184. if (ImGui::Button("+##texplus", ImVec2(0, 0), true)) {
  185. if (index < (getTextureManager().numTextures(
  186. game ? TextureManager::TextureStorage::GAME
  187. : TextureManager::TextureStorage::SYSTEM) - 1))
  188. index++;
  189. else
  190. index = 0;
  191. }
  192. ImGui::SameLine();
  193. if (ImGui::Button("-##texminus", ImVec2(0, 0), true)) {
  194. if (index > 0)
  195. index--;
  196. else
  197. index = getTextureManager().numTextures(
  198. game ? TextureManager::TextureStorage::GAME
  199. : TextureManager::TextureStorage::SYSTEM) - 1;
  200. }
  201. ImGui::SameLine();
  202. if ((getTextureManager().numTextures() > 0)) {
  203. ImGui::Checkbox("Game##texgame", &game);
  204. } else {
  205. game = false;
  206. }
  207. ImGui::SameLine();
  208. if (ImGui::Button("Show##texshow")) {
  209. visibleTex = true;
  210. visibleTile = false;
  211. visibleAnim = false;
  212. visibleSprite = false;
  213. }
  214. ImGui::SameLine();
  215. if (ImGui::Button("Clear##texclear")) {
  216. getRender().debugDisplayTexture();
  217. visibleTex = false;
  218. }
  219. if (visibleTex) {
  220. getRender().debugDisplayTexture(index,
  221. game ? TextureManager::TextureStorage::GAME
  222. : TextureManager::TextureStorage::SYSTEM,
  223. ImGui::GetWindowPos().x - ImGui::GetWindowWidth(),
  224. ImGui::GetWindowPos().y,
  225. ImGui::GetWindowWidth(), ImGui::GetWindowWidth());
  226. }
  227. }
  228. if (ImGui::CollapsingHeader("Textile Viewer")) {
  229. if (getTextureManager().numTiles() > 0) {
  230. static int index = 0;
  231. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  232. ImGui::SliderInt("##tileslide", &index, 0, getTextureManager().numTiles() - 1);
  233. ImGui::PopItemWidth();
  234. ImGui::SameLine();
  235. if (ImGui::Button("+##tileplus", ImVec2(0, 0), true)) {
  236. if (index < (getTextureManager().numTiles() - 1))
  237. index++;
  238. else
  239. index = 0;
  240. }
  241. ImGui::SameLine();
  242. if (ImGui::Button("-##tileminus", ImVec2(0, 0), true)) {
  243. if (index > 0)
  244. index--;
  245. else
  246. index = getTextureManager().numTiles() - 1;
  247. }
  248. ImGui::SameLine();
  249. if (ImGui::Button("Show##tileshow")) {
  250. visibleTile = true;
  251. visibleTex = false;
  252. visibleAnim = false;
  253. visibleSprite = false;
  254. }
  255. ImGui::SameLine();
  256. if (ImGui::Button("Clear##tileclear")) {
  257. getRender().debugDisplayTextile();
  258. visibleTile = false;
  259. }
  260. if (visibleTile && (index < getTextureManager().numTiles())) {
  261. ImGui::Text(getTextureManager().getTile(index).isTriangle() ? "Triangle" : "Rectangle");
  262. }
  263. if (visibleTile) {
  264. getRender().debugDisplayTextile(index,
  265. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  266. ImGui::GetWindowPos().y,
  267. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  268. }
  269. } else {
  270. ImGui::Text("Please load a level using the new loader!");
  271. }
  272. }
  273. if (ImGui::CollapsingHeader("Animated Textile Viewer")) {
  274. if (getTextureManager().numAnimatedTiles() > 0) {
  275. static int index = 0;
  276. static int tile = getTextureManager().getFirstTileAnimation(index);
  277. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  278. if (ImGui::SliderInt("##animslide", &index, 0, getTextureManager().numAnimatedTiles() - 1)) {
  279. tile = getTextureManager().getFirstTileAnimation(index);
  280. }
  281. ImGui::PopItemWidth();
  282. ImGui::SameLine();
  283. if (ImGui::Button("+##animplus", ImVec2(0, 0), true)) {
  284. if (index < (getTextureManager().numAnimatedTiles() - 1))
  285. index++;
  286. else
  287. index = 0;
  288. tile = getTextureManager().getFirstTileAnimation(index);
  289. }
  290. ImGui::SameLine();
  291. if (ImGui::Button("-##animminus", ImVec2(0, 0), true)) {
  292. if (index > 0)
  293. index--;
  294. else
  295. index = getTextureManager().numAnimatedTiles() - 1;
  296. tile = getTextureManager().getFirstTileAnimation(index);
  297. }
  298. ImGui::SameLine();
  299. if (ImGui::Button("Show##animshow")) {
  300. visibleAnim = true;
  301. visibleTex = false;
  302. visibleTile = false;
  303. visibleSprite = false;
  304. }
  305. ImGui::SameLine();
  306. if (ImGui::Button("Clear##animclear")) {
  307. getRender().debugDisplayTextile();
  308. visibleAnim = false;
  309. }
  310. if (visibleAnim) {
  311. static int fr = 0;
  312. if (fr > 0) {
  313. fr--;
  314. } else {
  315. getRender().debugDisplayTextile(tile,
  316. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  317. ImGui::GetWindowPos().y,
  318. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  319. fr = RunTime::getFPS() / 2;
  320. tile = getTextureManager().getNextTileAnimation(tile);
  321. }
  322. ImGui::Text("Current Tile: %d", tile);
  323. }
  324. } else {
  325. ImGui::Text("Please load a level with animated textures!");
  326. }
  327. }
  328. if (ImGui::CollapsingHeader("Sprite Sequence Viewer")) {
  329. if (getWorld().sizeSprite() <= 0) {
  330. ImGui::Text("Please load a level containing sprites!");
  331. } else {
  332. static int index = 0;
  333. static int sprite = 0;
  334. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  335. if (ImGui::SliderInt("##spriteslide", &index, 0, getWorld().sizeSprite() - 1))
  336. sprite = 0;
  337. ImGui::PopItemWidth();
  338. ImGui::SameLine();
  339. if (ImGui::Button("+##spriteplus", ImVec2(0, 0), true)) {
  340. if (index < (getWorld().sizeSprite() - 1))
  341. index++;
  342. else
  343. index = 0;
  344. sprite = 0;
  345. }
  346. ImGui::SameLine();
  347. if (ImGui::Button("-##spriteminus", ImVec2(0, 0), true)) {
  348. if (index > 0)
  349. index--;
  350. else
  351. index = getWorld().sizeSprite() - 1;
  352. sprite = 0;
  353. }
  354. ImGui::SameLine();
  355. if (ImGui::Button("Show##spriteshow")) {
  356. visibleSprite = true;
  357. visibleTex = false;
  358. visibleTile = false;
  359. visibleAnim = false;
  360. sprite = 0;
  361. }
  362. ImGui::SameLine();
  363. if (ImGui::Button("Clear##spriteclear")) {
  364. getRender().debugDisplaySprite();
  365. visibleSprite = false;
  366. }
  367. if (visibleSprite) {
  368. static int fr = 0;
  369. if (fr > 0) {
  370. fr--;
  371. } else {
  372. getRender().debugDisplaySprite(index, sprite,
  373. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  374. ImGui::GetWindowPos().y,
  375. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  376. fr = RunTime::getFPS() / 10;
  377. if (sprite < (getWorld().getSprite(index).size() - 1))
  378. sprite++;
  379. else
  380. sprite = 0;
  381. }
  382. ImGui::Text("Sprite %d/%d", sprite + 1, getWorld().getSprite(index).size());
  383. }
  384. }
  385. }
  386. */
  387. if (ImGui::CollapsingHeader("ImGui/Debug UI Help")) {
  388. //ImGui::TextWrapped("DebugViewer Textures/Textiles/Sprites will be drawn on"
  389. // " the left side and scale with the size of this window!");
  390. //ImGui::Separator();
  391. ImGui::ShowUserGuide();
  392. ImGui::Separator();
  393. if (ImGui::Button("Show/Hide Test Window")) {
  394. showTestWindow = !showTestWindow;
  395. }
  396. }
  397. }
  398. ImGui::End();
  399. if (showTestWindow)
  400. ImGui::ShowTestWindow();
  401. ImGui::Render();
  402. }
  403. void UI::shutdown() {
  404. ImGui::Shutdown();
  405. }
  406. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  407. ImGuiIO& io = ImGui::GetIO();
  408. io.KeysDown[key] = pressed;
  409. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  410. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  411. keyboardEvents.push_back(std::make_tuple(key, pressed));
  412. if ((key == leftguiKey) || (key == rightguiKey))
  413. metaKeyIsActive = pressed;
  414. }
  415. void UI::handleText(char* text, bool notFinished) {
  416. if (notFinished)
  417. return;
  418. ImGuiIO& io = ImGui::GetIO();
  419. while (*text != '\0') {
  420. io.AddInputCharacter(*text);
  421. text++;
  422. }
  423. }
  424. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  425. ImGuiIO& io = ImGui::GetIO();
  426. io.MousePos = ImVec2((float)x, (float)y);
  427. if (button == leftmouseKey) {
  428. io.MouseDown[0] = !released;
  429. } else if (button == rightmouseKey) {
  430. io.MouseDown[1] = !released;
  431. } else if (button == middlemouseKey) {
  432. io.MouseDown[2] = !released;
  433. } else if (button == fourthmouseKey) {
  434. io.MouseDown[3] = !released;
  435. } else if (button == fifthmouseKey) {
  436. io.MouseDown[4] = !released;
  437. }
  438. clickEvents.push_back(std::make_tuple(x, y, button, released));
  439. }
  440. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  441. ImGuiIO& io = ImGui::GetIO();
  442. io.MousePos = ImVec2((float)xabs, (float)yabs);
  443. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  444. }
  445. void UI::handleMouseScroll(int xrel, int yrel) {
  446. ImGuiIO& io = ImGui::GetIO();
  447. io.MouseWheel += yrel;
  448. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  449. }
  450. void UI::handleControllerAxis(float value, KeyboardButton axis) {
  451. getGame().handleControllerAxis(value, axis);
  452. }
  453. void UI::handleControllerButton(KeyboardButton button, bool released) {
  454. getGame().handleControllerButton(button, released);
  455. }
  456. void UI::setVisible(bool v) {
  457. visible = v;
  458. }
  459. bool UI::isVisible() {
  460. return visible;
  461. }
  462. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  463. if (cmd_lists_count == 0)
  464. return;
  465. glEnable(GL_SCISSOR_TEST);
  466. glDisable(GL_DEPTH_TEST);
  467. Window::imguiShader.use();
  468. Window::imguiShader.loadUniform(0, Window::getSize());
  469. Window::imguiShader.loadUniform(1, fontTex, TextureManager::TextureStorage::SYSTEM);
  470. Window::imguiShader.bindBuffer(0, 0, 2);
  471. Window::imguiShader.bindBuffer(1, 1, 2);
  472. Window::imguiShader.bindBuffer(2, 2, 4);
  473. std::vector<glm::vec2> vertices;
  474. std::vector<glm::vec2> uvs;
  475. std::vector<glm::vec4> colors;
  476. /*! \fixme Don't copy data
  477. * The GL calls and the shaders can probably be slightly altered
  478. * to avoid copying all the vertices, uvs and colors again here.
  479. */
  480. for (int i = 0; i < cmd_lists_count; i++) {
  481. auto& commands = cmd_lists[i]->commands;
  482. auto& buffer = cmd_lists[i]->vtx_buffer;
  483. int offset = 0;
  484. for (int n = 0; n < commands.size(); n++) {
  485. for (int v = 0; v < commands[n].vtx_count; v++) {
  486. vertices.push_back(glm::vec2(buffer[offset + v].pos.x, buffer[offset + v].pos.y));
  487. uvs.push_back(glm::vec2(buffer[offset + v].uv.x, buffer[offset + v].uv.y));
  488. float r, g, b, a;
  489. a = ((buffer[offset + v].col & 0xFF000000) >> 24) / 255.0f;
  490. b = ((buffer[offset + v].col & 0x00FF0000) >> 16) / 255.0f;
  491. g = ((buffer[offset + v].col & 0x0000FF00) >> 8) / 255.0f;
  492. r = (buffer[offset + v].col & 0x000000FF) / 255.0f;
  493. colors.push_back(glm::vec4(r, g, b, a));
  494. }
  495. offset += commands[n].vtx_count;
  496. Window::imguiShader.bufferData(0, vertices);
  497. Window::imguiShader.bufferData(1, uvs);
  498. Window::imguiShader.bufferData(2, colors);
  499. glScissor(commands[n].clip_rect.x,
  500. Window::getSize().y - commands[n].clip_rect.w,
  501. commands[n].clip_rect.z - commands[n].clip_rect.x,
  502. commands[n].clip_rect.w - commands[n].clip_rect.y);
  503. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  504. vertices.clear();
  505. uvs.clear();
  506. colors.clear();
  507. }
  508. }
  509. Window::imguiShader.disableAttribs();
  510. glEnable(GL_DEPTH_TEST);
  511. glDisable(GL_SCISSOR_TEST);
  512. }