Open Source Tomb Raider Engine
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. if (!visible) {
  80. while (!clickEvents.empty()) {
  81. auto i = clickEvents.front();
  82. if (getMenu().isVisible()) {
  83. getMenu().handleMouseClick(std::get<0>(i), std::get<1>(i),
  84. std::get<2>(i), std::get<3>(i));
  85. }
  86. clickEvents.pop_front();
  87. }
  88. while (!motionEvents.empty()) {
  89. auto i = motionEvents.front();
  90. if (!getMenu().isVisible()) {
  91. getGame().handleMouseMotion(std::get<0>(i), std::get<1>(i),
  92. std::get<2>(i), std::get<3>(i));
  93. }
  94. motionEvents.pop_front();
  95. }
  96. while (!scrollEvents.empty()) {
  97. auto i = scrollEvents.front();
  98. if (getMenu().isVisible()) {
  99. getMenu().handleMouseScroll(std::get<0>(i), std::get<1>(i));
  100. }
  101. scrollEvents.pop_front();
  102. }
  103. }
  104. while (!keyboardEvents.empty()) {
  105. auto i = keyboardEvents.front();
  106. if (!visible) {
  107. if (getMenu().isVisible()) {
  108. getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
  109. } else {
  110. for (int n = forwardAction; n < ActionEventCount; n++) {
  111. if (getRunTime().getKeyBinding((ActionEvents)n) == std::get<0>(i))
  112. getGame().handleAction((ActionEvents)n, !std::get<1>(i));
  113. }
  114. }
  115. }
  116. if (std::get<1>(i)) {
  117. if (!visible) {
  118. if (getRunTime().getKeyBinding(menuAction) == std::get<0>(i)) {
  119. getMenu().setVisible(!getMenu().isVisible());
  120. }
  121. }
  122. if ((!io.WantCaptureKeyboard) || (!visible)) {
  123. if (getRunTime().getKeyBinding(debugAction) == std::get<0>(i)) {
  124. if (!metaKeyIsActive)
  125. visible = !visible;
  126. }
  127. }
  128. }
  129. keyboardEvents.pop_front();
  130. }
  131. bool clicked = !clickEvents.empty();
  132. // Only already empty when !visible
  133. if (visible) {
  134. clickEvents.clear();
  135. motionEvents.clear();
  136. scrollEvents.clear();
  137. }
  138. if (visible && (
  139. ((!io.WantCaptureKeyboard) && io.KeysDown[escapeKey])
  140. || ((!io.WantCaptureMouse) && clicked)
  141. )) {
  142. visible = false;
  143. }
  144. if (getWindow().getTextInput() != visible)
  145. getWindow().setTextInput(visible);
  146. bool input = !(visible || getMenu().isVisible());
  147. if (getWindow().getMousegrab() != input)
  148. getWindow().setMousegrab(input);
  149. }
  150. void UI::display() {
  151. if (!visible)
  152. return;
  153. Console::display();
  154. if (ImGui::Begin("Engine/RT")) {
  155. if (ImGui::CollapsingHeader("Engine", NULL, true, true)) {
  156. ImGui::Text("Uptime: %lums", systemTimerGet());
  157. }
  158. if (ImGui::CollapsingHeader("Debug")) {
  159. }
  160. if (ImGui::CollapsingHeader("UI Help")) {
  161. ImGui::ShowUserGuide();
  162. }
  163. }
  164. ImGui::End();
  165. ImGui::Render();
  166. }
  167. void UI::shutdown() {
  168. ImGui::Shutdown();
  169. }
  170. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  171. ImGuiIO& io = ImGui::GetIO();
  172. io.KeysDown[key] = pressed;
  173. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  174. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  175. keyboardEvents.push_back(std::make_tuple(key, pressed));
  176. if ((key == leftguiKey) || (key == rightguiKey))
  177. metaKeyIsActive = pressed;
  178. }
  179. void UI::handleText(char *text, bool notFinished) {
  180. if (notFinished)
  181. return;
  182. ImGuiIO& io = ImGui::GetIO();
  183. while (*text != '\0') {
  184. io.AddInputCharacter(*text);
  185. text++;
  186. }
  187. }
  188. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  189. ImGuiIO& io = ImGui::GetIO();
  190. io.MousePos = ImVec2((float)x, (float)y);
  191. if (button == leftmouseKey) {
  192. io.MouseDown[0] = !released;
  193. } else if (button == rightmouseKey) {
  194. io.MouseDown[1] = !released;
  195. } else if (button == middlemouseKey) {
  196. io.MouseDown[2] = !released;
  197. } else if (button == fourthmouseKey) {
  198. io.MouseDown[3] = !released;
  199. } else if (button == fifthmouseKey) {
  200. io.MouseDown[4] = !released;
  201. }
  202. clickEvents.push_back(std::make_tuple(x, y, button, released));
  203. }
  204. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  205. ImGuiIO& io = ImGui::GetIO();
  206. io.MousePos = ImVec2((float)xabs, (float)yabs);
  207. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  208. }
  209. void UI::handleMouseScroll(int xrel, int yrel) {
  210. ImGuiIO& io = ImGui::GetIO();
  211. io.MouseWheel = (yrel != 0) ? yrel > 0 ? 1 : -1 : 0;
  212. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  213. }
  214. void UI::setVisible(bool v) {
  215. visible = v;
  216. }
  217. bool UI::isVisible() {
  218. return visible;
  219. }
  220. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  221. if (cmd_lists_count == 0)
  222. return;
  223. getWindow().glEnter2D();
  224. glDisable(GL_DEPTH_TEST);
  225. glEnable(GL_SCISSOR_TEST);
  226. glEnableClientState(GL_VERTEX_ARRAY);
  227. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  228. glEnableClientState(GL_COLOR_ARRAY);
  229. // Setup texture
  230. getTextureManager().bindTextureId(fontTex);
  231. // Render command lists
  232. for (int n = 0; n < cmd_lists_count; n++) {
  233. const ImDrawList* cmd_list = cmd_lists[n];
  234. const unsigned char* vtx_buffer = (const unsigned char*)cmd_list->vtx_buffer.begin();
  235. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer));
  236. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer+8));
  237. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer+16));
  238. int vtx_offset = 0;
  239. const ImDrawCmd* pcmd_end = cmd_list->commands.end();
  240. for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) {
  241. glScissor((int)pcmd->clip_rect.x, (int)(ImGui::GetIO().DisplaySize.y - pcmd->clip_rect.w),
  242. (int)(pcmd->clip_rect.z - pcmd->clip_rect.x),
  243. (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
  244. glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
  245. vtx_offset += pcmd->vtx_count;
  246. }
  247. }
  248. glEnable(GL_DEPTH_TEST);
  249. glDisable(GL_SCISSOR_TEST);
  250. glDisableClientState(GL_VERTEX_ARRAY);
  251. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  252. glDisableClientState(GL_COLOR_ARRAY);
  253. getWindow().glExit2D();
  254. }