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

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