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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*!
  2. * \file src/UI.cpp
  3. * \brief UI interface manager
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include "global.h"
  9. #include "Console.h"
  10. #include "Game.h"
  11. #include "Log.h"
  12. #include "Menu.h"
  13. #include "Render.h"
  14. #include "RunTime.h"
  15. #include "Sound.h"
  16. #include "TextureManager.h"
  17. #include "Window.h"
  18. #include "commands/Command.h"
  19. #include "utils/time.h"
  20. #include "UI.h"
  21. #define STB_IMAGE_IMPLEMENTATION
  22. #include "imgui/stb_image.h"
  23. bool UI::visible = false;
  24. unsigned int UI::fontTex;
  25. std::string UI::iniFilename;
  26. std::string UI::logFilename;
  27. bool UI::metaKeyIsActive = false;
  28. std::list<std::tuple<KeyboardButton, bool>> UI::keyboardEvents;
  29. std::list<std::tuple<unsigned int, unsigned int, KeyboardButton, bool>> UI::clickEvents;
  30. std::list<std::tuple<int, int, int, int>> UI::motionEvents;
  31. std::list<std::tuple<int, int>> UI::scrollEvents;
  32. int UI::initialize() {
  33. iniFilename = getRunTime().getBaseDir() + "/imgui.ini";
  34. logFilename = getRunTime().getBaseDir() + "/imgui_log.txt";
  35. ImGuiIO& io = ImGui::GetIO();
  36. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  37. io.DeltaTime = 1.0f / 60.0f;
  38. io.IniFilename = iniFilename.c_str();
  39. io.LogFilename = logFilename.c_str();
  40. io.KeyMap[ImGuiKey_Tab] = tabKey;
  41. io.KeyMap[ImGuiKey_LeftArrow] = leftKey;
  42. io.KeyMap[ImGuiKey_RightArrow] = rightKey;
  43. io.KeyMap[ImGuiKey_UpArrow] = upKey;
  44. io.KeyMap[ImGuiKey_DownArrow] = downKey;
  45. io.KeyMap[ImGuiKey_Home] = homeKey;
  46. io.KeyMap[ImGuiKey_End] = endKey;
  47. io.KeyMap[ImGuiKey_Delete] = delKey;
  48. io.KeyMap[ImGuiKey_Backspace] = backspaceKey;
  49. io.KeyMap[ImGuiKey_Enter] = enterKey;
  50. io.KeyMap[ImGuiKey_Escape] = escapeKey;
  51. io.KeyMap[ImGuiKey_A] = aKey;
  52. io.KeyMap[ImGuiKey_C] = cKey;
  53. io.KeyMap[ImGuiKey_V] = vKey;
  54. io.KeyMap[ImGuiKey_X] = xKey;
  55. io.KeyMap[ImGuiKey_Y] = yKey;
  56. io.KeyMap[ImGuiKey_Z] = zKey;
  57. io.RenderDrawListsFn = UI::renderImGui;
  58. // Load font texture
  59. //! \todo Use our own font subsystem instead of this?
  60. const void* png_data;
  61. unsigned int png_size;
  62. ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
  63. int tex_x, tex_y, tex_comp;
  64. void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
  65. (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
  66. //! \fixme TODO use proper texture slot
  67. fontTex = getTextureManager().loadBufferSlot((unsigned char*)tex_data,
  68. tex_x, tex_y, RGBA, 32, TextureManager::TextureStorage::SYSTEM, -1, false);
  69. stbi_image_free(tex_data);
  70. return 0;
  71. }
  72. void UI::eventsFinished() {
  73. ImGuiIO& io = ImGui::GetIO();
  74. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  75. static unsigned long lastTime = 0;
  76. io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
  77. lastTime = systemTimerGet();
  78. if (io.DeltaTime <= 0.0f)
  79. io.DeltaTime = 1.0f / 60.0f;
  80. ImGui::NewFrame();
  81. if (!visible) {
  82. while (!clickEvents.empty()) {
  83. auto i = clickEvents.front();
  84. if (getMenu().isVisible()) {
  85. getMenu().handleMouseClick(std::get<0>(i), std::get<1>(i),
  86. std::get<2>(i), std::get<3>(i));
  87. }
  88. clickEvents.pop_front();
  89. }
  90. while (!motionEvents.empty()) {
  91. auto i = motionEvents.front();
  92. if (!getMenu().isVisible()) {
  93. getGame().handleMouseMotion(std::get<0>(i), std::get<1>(i),
  94. std::get<2>(i), std::get<3>(i));
  95. }
  96. motionEvents.pop_front();
  97. }
  98. while (!scrollEvents.empty()) {
  99. auto i = scrollEvents.front();
  100. if (getMenu().isVisible()) {
  101. getMenu().handleMouseScroll(std::get<0>(i), std::get<1>(i));
  102. }
  103. scrollEvents.pop_front();
  104. }
  105. }
  106. while (!keyboardEvents.empty()) {
  107. auto i = keyboardEvents.front();
  108. if (!visible) {
  109. if (getMenu().isVisible()) {
  110. getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
  111. } else {
  112. for (int n = forwardAction; n < ActionEventCount; n++) {
  113. if (getRunTime().getKeyBinding((ActionEvents)n) == std::get<0>(i))
  114. getGame().handleAction((ActionEvents)n, !std::get<1>(i));
  115. }
  116. }
  117. }
  118. if (std::get<1>(i)) {
  119. if (!visible) {
  120. if (getRunTime().getKeyBinding(menuAction) == std::get<0>(i)) {
  121. getMenu().setVisible(!getMenu().isVisible());
  122. }
  123. }
  124. if ((!io.WantCaptureKeyboard) || (!visible)) {
  125. if (getRunTime().getKeyBinding(debugAction) == std::get<0>(i)) {
  126. if (!metaKeyIsActive)
  127. visible = !visible;
  128. }
  129. }
  130. }
  131. keyboardEvents.pop_front();
  132. }
  133. bool clicked = !clickEvents.empty();
  134. // Only already empty when !visible
  135. if (visible) {
  136. clickEvents.clear();
  137. motionEvents.clear();
  138. scrollEvents.clear();
  139. }
  140. if (visible && (
  141. ((!io.WantCaptureKeyboard) && io.KeysDown[escapeKey])
  142. || ((!io.WantCaptureMouse) && clicked)
  143. )) {
  144. visible = false;
  145. }
  146. if (getWindow().getTextInput() != visible)
  147. getWindow().setTextInput(visible);
  148. bool input = !(visible || getMenu().isVisible());
  149. if (getWindow().getMousegrab() != input)
  150. getWindow().setMousegrab(input);
  151. }
  152. void UI::display() {
  153. if (!visible)
  154. return;
  155. Console::display();
  156. if (ImGui::Begin("Engine")) {
  157. if (ImGui::CollapsingHeader("RunTime Info")) {
  158. ImGui::Text("Uptime: %lums", systemTimerGet());
  159. ImGui::Text("Frames per Second: %luFPS", getRunTime().getFPS());
  160. if (getRunTime().getHistoryFPS().size() > 1) {
  161. static bool scroll = true;
  162. if (scroll) {
  163. int offset = getRunTime().getHistoryFPS().size() - 1;
  164. if (offset > 15)
  165. offset = 15;
  166. ImGui::PlotLines("FPS", &getRunTime().getHistoryFPS()[1],
  167. getRunTime().getHistoryFPS().size() - 1,
  168. getRunTime().getHistoryFPS().size() - offset - 1);
  169. } else {
  170. ImGui::PlotLines("FPS", &getRunTime().getHistoryFPS()[1],
  171. getRunTime().getHistoryFPS().size() - 1);
  172. }
  173. ImGui::SameLine();
  174. ImGui::Checkbox("Scroll##fpsscroll", &scroll);
  175. }
  176. }
  177. static bool visibleTex = false;
  178. static bool visibleTile = false;
  179. if (ImGui::CollapsingHeader("Texture Viewer")) {
  180. static bool game = getGame().isLoaded();
  181. static int index = 0;
  182. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  183. ImGui::SliderInt("##texslide", &index, 0, getTextureManager().numTextures(
  184. game ? TextureManager::TextureStorage::GAME
  185. : TextureManager::TextureStorage::SYSTEM) - 1);
  186. ImGui::PopItemWidth();
  187. ImGui::SameLine();
  188. if (ImGui::Button("+##texplus", ImVec2(0, 0), true)) {
  189. if (index < (getTextureManager().numTextures(
  190. game ? TextureManager::TextureStorage::GAME
  191. : TextureManager::TextureStorage::SYSTEM) - 1))
  192. index++;
  193. else
  194. index = 0;
  195. }
  196. ImGui::SameLine();
  197. if (ImGui::Button("-##texminus", ImVec2(0, 0), true)) {
  198. if (index > 0)
  199. index--;
  200. else
  201. index = getTextureManager().numTextures(
  202. game ? TextureManager::TextureStorage::GAME
  203. : TextureManager::TextureStorage::SYSTEM) - 1;
  204. }
  205. ImGui::SameLine();
  206. if ((getTextureManager().numTextures() > 0)) {
  207. ImGui::Checkbox("Game##texgame", &game);
  208. } else {
  209. game = false;
  210. }
  211. ImGui::SameLine();
  212. if (ImGui::Button("Show##texshow")) {
  213. visibleTex = true;
  214. visibleTile = false;
  215. }
  216. ImGui::SameLine();
  217. if (ImGui::Button("Clear##texclear")) {
  218. getRender().debugDisplayTexture();
  219. visibleTex = false;
  220. }
  221. if (visibleTex) {
  222. getRender().debugDisplayTexture(index,
  223. game ? TextureManager::TextureStorage::GAME
  224. : TextureManager::TextureStorage::SYSTEM,
  225. ImGui::GetWindowPos().x - ImGui::GetWindowWidth(),
  226. ImGui::GetWindowPos().y,
  227. ImGui::GetWindowWidth(), ImGui::GetWindowWidth());
  228. }
  229. }
  230. if (ImGui::CollapsingHeader("Textile Viewer")) {
  231. if (getTextureManager().numTiles() > 0) {
  232. static int index = 0;
  233. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  234. ImGui::SliderInt("##tileslide", &index, 0, getTextureManager().numTiles() - 1);
  235. ImGui::PopItemWidth();
  236. ImGui::SameLine();
  237. if (ImGui::Button("+##tileplus", ImVec2(0, 0), true)) {
  238. if (index < (getTextureManager().numTiles() - 1))
  239. index++;
  240. else
  241. index = 0;
  242. }
  243. ImGui::SameLine();
  244. if (ImGui::Button("-##tileminus", ImVec2(0, 0), true)) {
  245. if (index > 0)
  246. index--;
  247. else
  248. index = getTextureManager().numTiles() - 1;
  249. }
  250. ImGui::SameLine();
  251. if (ImGui::Button("Show##tileshow")) {
  252. visibleTile = true;
  253. visibleTex = 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!");
  271. }
  272. }
  273. if (ImGui::CollapsingHeader("SoundSample Player")) {
  274. if (!getSound().getEnabled()) {
  275. ImGui::Text("Please enable Sound before loading a level!");
  276. if (ImGui::Button("Enable Sound!")) {
  277. getSound().setEnabled(true);
  278. }
  279. } else if (getSound().registeredSources() == 0) {
  280. ImGui::Text("Please load a level!");
  281. } else {
  282. static int index = 0;
  283. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  284. ImGui::SliderInt("##soundslide", &index, 0, getSound().registeredSources() - 1);
  285. ImGui::PopItemWidth();
  286. ImGui::SameLine();
  287. if (ImGui::Button("+##soundplus", ImVec2(0, 0), true)) {
  288. if (index < (getSound().registeredSources() - 1))
  289. index++;
  290. else
  291. index = 0;
  292. }
  293. ImGui::SameLine();
  294. if (ImGui::Button("-##soundminus", ImVec2(0, 0), true)) {
  295. if (index > 0)
  296. index--;
  297. else
  298. index = getSound().registeredSources() - 1;
  299. }
  300. ImGui::SameLine();
  301. if (ImGui::Button("Play##soundplay")) {
  302. getSound().play(index);
  303. }
  304. }
  305. }
  306. /*
  307. if (ImGui::CollapsingHeader("UI Help")) {
  308. ImGui::ShowUserGuide();
  309. }
  310. */
  311. }
  312. ImGui::End();
  313. ImGui::Render();
  314. }
  315. void UI::shutdown() {
  316. ImGui::Shutdown();
  317. }
  318. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  319. ImGuiIO& io = ImGui::GetIO();
  320. io.KeysDown[key] = pressed;
  321. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  322. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  323. keyboardEvents.push_back(std::make_tuple(key, pressed));
  324. if ((key == leftguiKey) || (key == rightguiKey))
  325. metaKeyIsActive = pressed;
  326. }
  327. void UI::handleText(char* text, bool notFinished) {
  328. if (notFinished)
  329. return;
  330. ImGuiIO& io = ImGui::GetIO();
  331. while (*text != '\0') {
  332. io.AddInputCharacter(*text);
  333. text++;
  334. }
  335. }
  336. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  337. ImGuiIO& io = ImGui::GetIO();
  338. io.MousePos = ImVec2((float)x, (float)y);
  339. if (button == leftmouseKey) {
  340. io.MouseDown[0] = !released;
  341. } else if (button == rightmouseKey) {
  342. io.MouseDown[1] = !released;
  343. } else if (button == middlemouseKey) {
  344. io.MouseDown[2] = !released;
  345. } else if (button == fourthmouseKey) {
  346. io.MouseDown[3] = !released;
  347. } else if (button == fifthmouseKey) {
  348. io.MouseDown[4] = !released;
  349. }
  350. clickEvents.push_back(std::make_tuple(x, y, button, released));
  351. }
  352. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  353. ImGuiIO& io = ImGui::GetIO();
  354. io.MousePos = ImVec2((float)xabs, (float)yabs);
  355. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  356. }
  357. void UI::handleMouseScroll(int xrel, int yrel) {
  358. ImGuiIO& io = ImGui::GetIO();
  359. io.MouseWheel = (yrel != 0) ? yrel > 0 ? 1 : -1 : 0;
  360. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  361. }
  362. void UI::setVisible(bool v) {
  363. visible = v;
  364. }
  365. bool UI::isVisible() {
  366. return visible;
  367. }
  368. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  369. if (cmd_lists_count == 0)
  370. return;
  371. getWindow().glEnter2D();
  372. glDisable(GL_DEPTH_TEST);
  373. glEnable(GL_SCISSOR_TEST);
  374. glEnableClientState(GL_VERTEX_ARRAY);
  375. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  376. glEnableClientState(GL_COLOR_ARRAY);
  377. // Setup texture
  378. getTextureManager().bindTextureId(fontTex, TextureManager::TextureStorage::SYSTEM);
  379. // Render command lists
  380. for (int n = 0; n < cmd_lists_count; n++) {
  381. const ImDrawList* cmd_list = cmd_lists[n];
  382. const unsigned char* vtx_buffer = (const unsigned char*)cmd_list->vtx_buffer.begin();
  383. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer));
  384. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + 8));
  385. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + 16));
  386. int vtx_offset = 0;
  387. const ImDrawCmd* pcmd_end = cmd_list->commands.end();
  388. for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) {
  389. glScissor((int)pcmd->clip_rect.x, (int)(ImGui::GetIO().DisplaySize.y - pcmd->clip_rect.w),
  390. (int)(pcmd->clip_rect.z - pcmd->clip_rect.x),
  391. (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
  392. glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
  393. vtx_offset += pcmd->vtx_count;
  394. }
  395. }
  396. glEnable(GL_DEPTH_TEST);
  397. glDisable(GL_SCISSOR_TEST);
  398. glDisableClientState(GL_VERTEX_ARRAY);
  399. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  400. glDisableClientState(GL_COLOR_ARRAY);
  401. getWindow().glExit2D();
  402. }