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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 "RunTime.h"
  14. #include "TextureManager.h"
  15. #include "Window.h"
  16. #include "commands/Command.h"
  17. #include "utils/time.h"
  18. #include "UI.h"
  19. #define STB_IMAGE_IMPLEMENTATION
  20. #include "imgui/stb_image.h"
  21. bool UI::visible = false;
  22. unsigned int UI::fontTex;
  23. std::string UI::iniFilename;
  24. std::string UI::logFilename;
  25. bool UI::metaKeyIsActive = false;
  26. std::list<std::tuple<KeyboardButton, bool>> UI::keyboardEvents;
  27. std::list<std::tuple<unsigned int, unsigned int, KeyboardButton, bool>> UI::clickEvents;
  28. std::list<std::tuple<int, int, int, int>> UI::motionEvents;
  29. std::list<std::tuple<int, int>> UI::scrollEvents;
  30. int UI::initialize() {
  31. iniFilename = getRunTime().getBaseDir() + "/imgui.ini";
  32. logFilename = getRunTime().getBaseDir() + "/imgui_log.txt";
  33. ImGuiIO& io = ImGui::GetIO();
  34. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  35. io.DeltaTime = 1.0f / 60.0f;
  36. io.IniFilename = iniFilename.c_str();
  37. io.LogFilename = logFilename.c_str();
  38. io.KeyMap[ImGuiKey_Tab] = tabKey;
  39. io.KeyMap[ImGuiKey_LeftArrow] = leftKey;
  40. io.KeyMap[ImGuiKey_RightArrow] = rightKey;
  41. io.KeyMap[ImGuiKey_UpArrow] = upKey;
  42. io.KeyMap[ImGuiKey_DownArrow] = downKey;
  43. io.KeyMap[ImGuiKey_Home] = homeKey;
  44. io.KeyMap[ImGuiKey_End] = endKey;
  45. io.KeyMap[ImGuiKey_Delete] = delKey;
  46. io.KeyMap[ImGuiKey_Backspace] = backspaceKey;
  47. io.KeyMap[ImGuiKey_Enter] = enterKey;
  48. io.KeyMap[ImGuiKey_Escape] = escapeKey;
  49. io.KeyMap[ImGuiKey_A] = aKey;
  50. io.KeyMap[ImGuiKey_C] = cKey;
  51. io.KeyMap[ImGuiKey_V] = vKey;
  52. io.KeyMap[ImGuiKey_X] = xKey;
  53. io.KeyMap[ImGuiKey_Y] = yKey;
  54. io.KeyMap[ImGuiKey_Z] = zKey;
  55. io.RenderDrawListsFn = UI::renderImGui;
  56. // Load font texture
  57. //! \todo Use our own font subsystem instead of this?
  58. const void* png_data;
  59. unsigned int png_size;
  60. ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
  61. int tex_x, tex_y, tex_comp;
  62. void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
  63. (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
  64. //! \fixme TODO use proper texture slot
  65. fontTex = getTextureManager().loadBufferSlot((unsigned char *)tex_data,
  66. tex_x, tex_y, RGBA, 32, 0, false);
  67. stbi_image_free(tex_data);
  68. return 0;
  69. }
  70. void UI::eventsFinished() {
  71. ImGuiIO& io = ImGui::GetIO();
  72. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  73. static unsigned long lastTime = 0;
  74. io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
  75. lastTime = systemTimerGet();
  76. if (io.DeltaTime <= 0.0f)
  77. io.DeltaTime = 1.0f / 60.0f;
  78. ImGui::NewFrame();
  79. bool clicked = !clickEvents.empty();
  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. if ((!io.WantCaptureKeyboard) || (!visible)) {
  106. while (!keyboardEvents.empty()) {
  107. auto i = keyboardEvents.front();
  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. if (std::get<1>(i)) {
  117. if (getRunTime().getKeyBinding(menuAction) == std::get<0>(i)) {
  118. getMenu().setVisible(!getMenu().isVisible());
  119. visible = false;
  120. } else if (getRunTime().getKeyBinding(debugAction) == std::get<0>(i)) {
  121. if (!metaKeyIsActive)
  122. visible = !visible;
  123. }
  124. }
  125. keyboardEvents.pop_front();
  126. }
  127. }
  128. if ((!io.WantCaptureKeyboard) && (!io.WantCaptureMouse) && visible && clicked) {
  129. visible = false;
  130. }
  131. keyboardEvents.clear();
  132. clickEvents.clear();
  133. motionEvents.clear();
  134. scrollEvents.clear();
  135. if (getWindow().getTextInput() != visible)
  136. getWindow().setTextInput(visible);
  137. bool input = !(visible || getMenu().isVisible());
  138. if (getWindow().getMousegrab() != input)
  139. getWindow().setMousegrab(input);
  140. }
  141. void UI::display() {
  142. if (!visible)
  143. return;
  144. Console::display();
  145. if (ImGui::Begin("Engine/RT")) {
  146. if (ImGui::CollapsingHeader("Engine", NULL, true, true)) {
  147. ImGui::Text("Uptime: %lums", systemTimerGet());
  148. }
  149. if (ImGui::CollapsingHeader("Debug")) {
  150. }
  151. if (ImGui::CollapsingHeader("UI Help")) {
  152. ImGui::ShowUserGuide();
  153. }
  154. }
  155. ImGui::End();
  156. ImGui::Render();
  157. }
  158. void UI::shutdown() {
  159. ImGui::Shutdown();
  160. }
  161. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  162. ImGuiIO& io = ImGui::GetIO();
  163. io.KeysDown[key] = pressed;
  164. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  165. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  166. keyboardEvents.push_back(std::make_tuple(key, pressed));
  167. if ((key == leftguiKey) || (key == rightguiKey))
  168. metaKeyIsActive = pressed;
  169. }
  170. void UI::handleText(char *text, bool notFinished) {
  171. if (notFinished)
  172. return;
  173. ImGuiIO& io = ImGui::GetIO();
  174. while (*text != '\0') {
  175. io.AddInputCharacter(*text);
  176. text++;
  177. }
  178. }
  179. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  180. ImGuiIO& io = ImGui::GetIO();
  181. io.MousePos = ImVec2((float)x, (float)y);
  182. if (button == leftmouseKey) {
  183. io.MouseDown[0] = !released;
  184. } else if (button == rightmouseKey) {
  185. io.MouseDown[1] = !released;
  186. } else if (button == middlemouseKey) {
  187. io.MouseDown[2] = !released;
  188. } else if (button == fourthmouseKey) {
  189. io.MouseDown[3] = !released;
  190. } else if (button == fifthmouseKey) {
  191. io.MouseDown[4] = !released;
  192. }
  193. clickEvents.push_back(std::make_tuple(x, y, button, released));
  194. }
  195. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  196. ImGuiIO& io = ImGui::GetIO();
  197. io.MousePos = ImVec2((float)xabs, (float)yabs);
  198. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  199. }
  200. void UI::handleMouseScroll(int xrel, int yrel) {
  201. ImGuiIO& io = ImGui::GetIO();
  202. io.MouseWheel = (yrel != 0) ? yrel > 0 ? 1 : -1 : 0;
  203. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  204. }
  205. void UI::setVisible(bool v) {
  206. visible = v;
  207. }
  208. bool UI::isVisible() {
  209. return visible;
  210. }
  211. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  212. if (cmd_lists_count == 0)
  213. return;
  214. getWindow().glEnter2D();
  215. glDisable(GL_DEPTH_TEST);
  216. glEnable(GL_SCISSOR_TEST);
  217. glEnableClientState(GL_VERTEX_ARRAY);
  218. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  219. glEnableClientState(GL_COLOR_ARRAY);
  220. // Setup texture
  221. getTextureManager().bindTextureId(fontTex);
  222. // Render command lists
  223. for (int n = 0; n < cmd_lists_count; n++) {
  224. const ImDrawList* cmd_list = cmd_lists[n];
  225. const unsigned char* vtx_buffer = (const unsigned char*)cmd_list->vtx_buffer.begin();
  226. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer));
  227. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer+8));
  228. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer+16));
  229. int vtx_offset = 0;
  230. const ImDrawCmd* pcmd_end = cmd_list->commands.end();
  231. for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) {
  232. glScissor((int)pcmd->clip_rect.x, (int)(ImGui::GetIO().DisplaySize.y - pcmd->clip_rect.w),
  233. (int)(pcmd->clip_rect.z - pcmd->clip_rect.x),
  234. (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
  235. glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
  236. vtx_offset += pcmd->vtx_count;
  237. }
  238. }
  239. glEnable(GL_DEPTH_TEST);
  240. glDisable(GL_SCISSOR_TEST);
  241. glDisableClientState(GL_VERTEX_ARRAY);
  242. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  243. glDisableClientState(GL_COLOR_ARRAY);
  244. getWindow().glExit2D();
  245. }