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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*!
  2. * \file src/UI.cpp
  3. * \brief UI interface manager
  4. *
  5. * \author xythobuz
  6. */
  7. #include "imgui/imgui.h"
  8. #include "global.h"
  9. #include "Camera.h"
  10. #include "Console.h"
  11. #include "Game.h"
  12. #include "Menu.h"
  13. #include "Render.h"
  14. #include "RunTime.h"
  15. #include "SoundManager.h"
  16. #include "TextureManager.h"
  17. #include "system/Window.h"
  18. #include "utils/time.h"
  19. #include "UI.h"
  20. Shader UI::imguiShader;
  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. void UI::setSize(glm::i32vec2 s) {
  31. ImGuiIO& io = ImGui::GetIO();
  32. io.DisplaySize = ImVec2(s.x, s.y);
  33. }
  34. int UI::initialize() {
  35. if (imguiShader.compile(imguiShaderVertex, imguiShaderFragment) < 0)
  36. return -1;
  37. if (imguiShader.addUniform("screen") < 0)
  38. return -2;
  39. if (imguiShader.addUniform("textureSampler") < 0)
  40. return -3;
  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. io.GetClipboardTextFn = Window::getClipboard;
  67. io.SetClipboardTextFn = Window::setClipboard;
  68. io.ImeSetInputScreenPosFn = Window::inputPositionCallback;
  69. // Load font texture
  70. //! \TODO allow loading other TTF fonts
  71. unsigned char* pixels;
  72. int width, height;
  73. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  74. fontTex = TextureManager::loadBufferSlot(pixels, width, height, ColorMode::RGBA, 32,
  75. TextureStorage::SYSTEM, -1, false);
  76. auto bm = TextureManager::getBufferManager(fontTex, TextureStorage::SYSTEM);
  77. io.Fonts->TexID = bm;
  78. // Set up OpenRaider style
  79. ImGuiStyle& style = ImGui::GetStyle();
  80. style.Colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  81. style.Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  82. style.Colors[ImGuiCol_Border] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  83. style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.60f);
  84. style.Colors[ImGuiCol_FrameBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.30f);
  85. style.Colors[ImGuiCol_TitleBg] = ImVec4(0.50f, 0.70f, 1.00f, 0.45f);
  86. style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.65f, 1.00f, 0.20f);
  87. style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.40f, 0.65f, 1.00f, 0.15f);
  88. style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.65f, 1.00f, 0.30f);
  89. style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.65f, 1.00f, 0.40f);
  90. style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(1.00f, 0.16f, 0.16f, 0.40f);
  91. style.Colors[ImGuiCol_ComboBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.99f);
  92. style.Colors[ImGuiCol_CheckHovered] = ImVec4(1.00f, 0.40f, 0.40f, 0.45f);
  93. style.Colors[ImGuiCol_CheckActive] = ImVec4(0.65f, 0.50f, 0.50f, 0.55f);
  94. style.Colors[ImGuiCol_CheckMark] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f);
  95. style.Colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  96. style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(1.00f, 0.20f, 0.22f, 1.00f);
  97. style.Colors[ImGuiCol_Button] = ImVec4(1.00f, 0.20f, 0.20f, 0.60f);
  98. style.Colors[ImGuiCol_ButtonHovered] = ImVec4(1.00f, 0.20f, 0.18f, 1.00f);
  99. style.Colors[ImGuiCol_ButtonActive] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
  100. style.Colors[ImGuiCol_Header] = ImVec4(0.21f, 0.54f, 1.00f, 0.45f);
  101. style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.22f, 0.56f, 1.00f, 0.80f);
  102. style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.00f, 0.31f, 1.00f, 1.00f);
  103. style.Colors[ImGuiCol_Column] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  104. style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.60f, 0.40f, 0.40f, 1.00f);
  105. style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f);
  106. style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  107. style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.60f);
  108. style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.90f);
  109. style.Colors[ImGuiCol_CloseButton] = ImVec4(0.21f, 0.56f, 1.00f, 0.50f);
  110. style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.21f, 0.32f, 1.00f, 0.60f);
  111. style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.70f, 0.70f, 0.70f, 1.00f);
  112. style.Colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  113. style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  114. style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  115. style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
  116. style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f);
  117. style.Colors[ImGuiCol_TooltipBg] = ImVec4(0.05f, 0.05f, 0.10f, 0.90f);
  118. style.WindowPadding = ImVec2(2, 2);
  119. style.WindowRounding = 9;
  120. style.FramePadding = ImVec2(2, 1);
  121. style.ItemSpacing = ImVec2(2, 2);
  122. style.ItemInnerSpacing = ImVec2(1, 1);
  123. style.TouchExtraPadding = ImVec2(0, 0);
  124. style.TreeNodeSpacing = 3;
  125. style.ScrollBarWidth = 10;
  126. return 0;
  127. }
  128. void UI::eventsFinished() {
  129. ImGuiIO& io = ImGui::GetIO();
  130. io.DisplaySize = ImVec2(Window::getSize().x, Window::getSize().y);
  131. static unsigned long lastTime = 0;
  132. io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
  133. lastTime = systemTimerGet();
  134. if (io.DeltaTime <= 0.0f)
  135. io.DeltaTime = 1.0f / 60.0f;
  136. ImGui::NewFrame();
  137. if (!(visible || Console::isVisible())) {
  138. while (!clickEvents.empty()) {
  139. auto i = clickEvents.front();
  140. if (getMenu().isVisible()) {
  141. getMenu().handleMouseClick(std::get<0>(i), std::get<1>(i),
  142. std::get<2>(i), std::get<3>(i));
  143. }
  144. clickEvents.pop_front();
  145. }
  146. while (!motionEvents.empty()) {
  147. auto i = motionEvents.front();
  148. if (!getMenu().isVisible()) {
  149. Game::handleMouseMotion(std::get<0>(i), std::get<1>(i),
  150. std::get<2>(i), std::get<3>(i));
  151. }
  152. motionEvents.pop_front();
  153. }
  154. while (!scrollEvents.empty()) {
  155. auto i = scrollEvents.front();
  156. if (getMenu().isVisible()) {
  157. getMenu().handleMouseScroll(std::get<0>(i), std::get<1>(i));
  158. }
  159. scrollEvents.pop_front();
  160. }
  161. }
  162. while (!keyboardEvents.empty()) {
  163. auto i = keyboardEvents.front();
  164. if (!(visible || Console::isVisible())) {
  165. if (getMenu().isVisible()) {
  166. getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
  167. } else {
  168. for (int n = forwardAction; n < ActionEventCount; n++) {
  169. if (RunTime::getKeyBinding((ActionEvents)n) == std::get<0>(i))
  170. Game::handleAction((ActionEvents)n, !std::get<1>(i));
  171. }
  172. }
  173. }
  174. if (std::get<1>(i)) {
  175. if (!(visible || Console::isVisible())) {
  176. if (RunTime::getKeyBinding(menuAction) == std::get<0>(i)) {
  177. getMenu().setVisible(!getMenu().isVisible());
  178. }
  179. }
  180. if ((!io.WantCaptureKeyboard) || (!(visible || Console::isVisible()))) {
  181. if (!metaKeyIsActive) {
  182. if (RunTime::getKeyBinding(debugAction) == std::get<0>(i)) {
  183. visible = !visible;
  184. }
  185. if (RunTime::getKeyBinding(consoleAction) == std::get<0>(i)) {
  186. Console::setVisible(!Console::isVisible());
  187. }
  188. }
  189. }
  190. }
  191. keyboardEvents.pop_front();
  192. }
  193. bool clicked = !clickEvents.empty();
  194. clickEvents.clear();
  195. motionEvents.clear();
  196. scrollEvents.clear();
  197. if ((visible || Console::isVisible()) && (
  198. ((!io.WantCaptureKeyboard) && io.KeysDown[escapeKey])
  199. || ((!io.WantCaptureMouse) && clicked)
  200. )) {
  201. visible = false;
  202. Console::setVisible(false);
  203. }
  204. if (Window::getTextInput() != (visible || Console::isVisible()))
  205. Window::setTextInput(visible || Console::isVisible());
  206. bool input = !(visible || Console::isVisible() || getMenu().isVisible());
  207. if (Window::getMousegrab() != input)
  208. Window::setMousegrab(input);
  209. io.MouseWheel = 0;
  210. }
  211. void UI::display() {
  212. if (RunTime::getShowFPS()) {
  213. if (ImGui::Begin("Debug Overlay", nullptr, ImVec2(0, 0), 0.3f,
  214. ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
  215. | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
  216. | ImGuiWindowFlags_AlwaysAutoResize)) {
  217. ImGui::Text("%d FPS", RunTime::getFPS());
  218. ImGui::Text("X: %.1f (%.2f)", Camera::getPosition().x, Camera::getRotation().x);
  219. ImGui::Text("Y: %.2f (%.2f)", Camera::getPosition().y, Camera::getRotation().y);
  220. ImGui::Text("Z: %.2f", Camera::getPosition().z);
  221. auto window = ImGui::GetWindowSize();
  222. auto screen = Window::getSize();
  223. ImGui::SetWindowPos(ImVec2(10, screen.y - window.y - 10));
  224. }
  225. ImGui::End();
  226. }
  227. Console::display();
  228. if (!visible) {
  229. ImGui::Render();
  230. return;
  231. }
  232. static bool showTestWindow = false;
  233. static bool showStyleWindow = false;
  234. if (ImGui::Begin("Engine", &visible, ImVec2(400, 400))) {
  235. Render::displayUI();
  236. RunTime::display();
  237. TextureManager::display();
  238. SoundManager::display();
  239. if (ImGui::CollapsingHeader("ImGui/Debug UI Help")) {
  240. ImGui::ShowUserGuide();
  241. ImGui::Separator();
  242. if (ImGui::Button("Show/Hide Test Window")) {
  243. showTestWindow = !showTestWindow;
  244. }
  245. ImGui::SameLine();
  246. if (ImGui::Button("Show/Hide Style Editor")) {
  247. showStyleWindow = !showStyleWindow;
  248. }
  249. }
  250. }
  251. ImGui::End();
  252. if (showTestWindow)
  253. ImGui::ShowTestWindow();
  254. if (showStyleWindow)
  255. ImGui::ShowStyleEditor();
  256. ImGui::Render();
  257. }
  258. void UI::shutdown() {
  259. ImGui::Shutdown();
  260. }
  261. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  262. ImGuiIO& io = ImGui::GetIO();
  263. io.KeysDown[key] = pressed;
  264. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  265. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  266. keyboardEvents.push_back(std::make_tuple(key, pressed));
  267. if ((key == leftguiKey) || (key == rightguiKey))
  268. metaKeyIsActive = pressed;
  269. }
  270. void UI::handleText(char* text, bool notFinished) {
  271. if (notFinished)
  272. return;
  273. ImGuiIO& io = ImGui::GetIO();
  274. while (*text != '\0') {
  275. io.AddInputCharacter(*text);
  276. text++;
  277. }
  278. }
  279. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  280. ImGuiIO& io = ImGui::GetIO();
  281. io.MousePos = ImVec2((float)x, (float)y);
  282. if (button == leftmouseKey) {
  283. io.MouseDown[0] = !released;
  284. } else if (button == rightmouseKey) {
  285. io.MouseDown[1] = !released;
  286. } else if (button == middlemouseKey) {
  287. io.MouseDown[2] = !released;
  288. } else if (button == fourthmouseKey) {
  289. io.MouseDown[3] = !released;
  290. } else if (button == fifthmouseKey) {
  291. io.MouseDown[4] = !released;
  292. }
  293. clickEvents.push_back(std::make_tuple(x, y, button, released));
  294. }
  295. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  296. ImGuiIO& io = ImGui::GetIO();
  297. io.MousePos = ImVec2((float)xabs, (float)yabs);
  298. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  299. }
  300. void UI::handleMouseScroll(int xrel, int yrel) {
  301. ImGuiIO& io = ImGui::GetIO();
  302. io.MouseWheel += yrel;
  303. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  304. }
  305. void UI::handleControllerAxis(float value, KeyboardButton axis) {
  306. Game::handleControllerAxis(value, axis);
  307. }
  308. void UI::handleControllerButton(KeyboardButton button, bool released) {
  309. if (visible || Console::isVisible())
  310. return;
  311. if (getMenu().isVisible()) {
  312. if (button == aButton) {
  313. handleKeyboard(enterKey, !released);
  314. } else if (button == padUp) {
  315. handleKeyboard(upKey, !released);
  316. } else if (button == padDown) {
  317. handleKeyboard(downKey, !released);
  318. } else if (button == padLeft) {
  319. handleKeyboard(leftKey, !released);
  320. } else if (button == padRight) {
  321. handleKeyboard(rightKey, !released);
  322. } else if (button == startButton) {
  323. if (!released)
  324. getMenu().setVisible(false);
  325. }
  326. } else {
  327. if (button == aButton) {
  328. Game::handleAction(jumpAction, released);
  329. } else if (button == bButton) {
  330. Game::handleAction(crouchAction, released);
  331. } else if (button == padUp) {
  332. Game::handleAction(forwardAction, released);
  333. } else if (button == padDown) {
  334. Game::handleAction(backwardAction, released);
  335. } else if (button == padLeft) {
  336. Game::handleAction(leftAction, released);
  337. } else if (button == padRight) {
  338. Game::handleAction(rightAction, released);
  339. } else if (button == startButton) {
  340. if (!released)
  341. getMenu().setVisible(true);
  342. }
  343. }
  344. }
  345. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  346. if (cmd_lists_count == 0)
  347. return;
  348. static ShaderBuffer vert, uv, col;
  349. glEnable(GL_SCISSOR_TEST);
  350. Shader::set2DState(true);
  351. imguiShader.use();
  352. imguiShader.loadUniform(0, Window::getSize());
  353. vert.bindBuffer(0, 2);
  354. uv.bindBuffer(1, 2);
  355. col.bindBuffer(2, 4);
  356. /*! \fixme Don't copy data
  357. * The GL calls and the shaders can probably be slightly altered
  358. * to avoid copying all the vertices, uvs and colors again here.
  359. */
  360. for (int i = 0; i < cmd_lists_count; i++) {
  361. auto& commands = cmd_lists[i]->commands;
  362. auto& buffer = cmd_lists[i]->vtx_buffer;
  363. int offset = 0;
  364. for (int n = 0; n < commands.size(); n++) {
  365. std::vector<glm::vec2> vertices;
  366. std::vector<glm::vec2> uvs;
  367. std::vector<glm::vec4> colors;
  368. for (int v = 0; v < commands[n].vtx_count; v++) {
  369. vertices.push_back(glm::vec2(buffer[offset + v].pos.x, buffer[offset + v].pos.y));
  370. uvs.push_back(glm::vec2(buffer[offset + v].uv.x, buffer[offset + v].uv.y));
  371. float r, g, b, a;
  372. a = ((buffer[offset + v].col & 0xFF000000) >> 24) / 255.0f;
  373. b = ((buffer[offset + v].col & 0x00FF0000) >> 16) / 255.0f;
  374. g = ((buffer[offset + v].col & 0x0000FF00) >> 8) / 255.0f;
  375. r = (buffer[offset + v].col & 0x000000FF) / 255.0f;
  376. colors.push_back(glm::vec4(r, g, b, a));
  377. }
  378. offset += commands[n].vtx_count;
  379. vert.bufferData(vertices);
  380. uv.bufferData(uvs);
  381. col.bufferData(colors);
  382. auto bm = static_cast<BufferManager*>(commands[n].texture_id);
  383. assert(bm != nullptr);
  384. imguiShader.loadUniform(1, bm->getTextureID(), bm->getTextureStorage());
  385. glScissor(commands[n].clip_rect.x,
  386. Window::getSize().y - commands[n].clip_rect.w,
  387. commands[n].clip_rect.z - commands[n].clip_rect.x,
  388. commands[n].clip_rect.w - commands[n].clip_rect.y);
  389. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  390. }
  391. }
  392. vert.unbind(0);
  393. uv.unbind(1);
  394. col.unbind(2);
  395. Shader::set2DState(false);
  396. glDisable(GL_SCISSOR_TEST);
  397. }
  398. // --------------------------------------
  399. // *INDENT-OFF*
  400. const char* UI::imguiShaderVertex = R"!?!(
  401. #version 330 core
  402. layout(location = 0) in vec2 vertexPosition_screen;
  403. layout(location = 1) in vec2 vertexUV;
  404. layout(location = 2) in vec4 vertexColor;
  405. out vec2 UV;
  406. out vec4 FragColor;
  407. uniform vec2 screen;
  408. void main() {
  409. vec2 halfScreen = screen / 2;
  410. vec2 vertexPosition_homogenous = (vertexPosition_screen - halfScreen) / halfScreen;
  411. gl_Position = vec4(vertexPosition_homogenous.x, -vertexPosition_homogenous.y, 0, 1);
  412. UV = vertexUV;
  413. FragColor = vertexColor;
  414. }
  415. )!?!";
  416. const char* UI::imguiShaderFragment = R"!?!(
  417. #version 330 core
  418. in vec2 UV;
  419. in vec4 FragColor;
  420. out vec4 color;
  421. uniform sampler2D textureSampler;
  422. void main() {
  423. color = texture(textureSampler, UV) * FragColor;
  424. }
  425. )!?!";
  426. // --------------------------------------
  427. // *INDENT-ON*