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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*!
  2. * \file src/UI.cpp
  3. * \brief UI/Event 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 "Log.h"
  13. #include "Menu.h"
  14. #include "Render.h"
  15. #include "RunTime.h"
  16. #include "SoundManager.h"
  17. #include "TextureManager.h"
  18. #include "World.h"
  19. #include "system/Sound.h"
  20. #include "system/Window.h"
  21. #include "utils/time.h"
  22. #include "UI.h"
  23. #include <glm/gtc/matrix_transform.hpp>
  24. Shader UI::imguiShader;
  25. bool UI::visible = false;
  26. unsigned int UI::fontTex;
  27. std::string UI::iniFilename;
  28. std::string UI::logFilename;
  29. bool UI::metaKeyIsActive = false;
  30. std::list<std::tuple<KeyboardButton, bool>> UI::keyboardEvents;
  31. std::list<std::tuple<unsigned int, unsigned int, KeyboardButton, bool>> UI::clickEvents;
  32. std::list<std::tuple<int, int, int, int>> UI::motionEvents;
  33. std::list<std::tuple<int, int>> UI::scrollEvents;
  34. void UI::setSize(glm::i32vec2 s) {
  35. ImGuiIO& io = ImGui::GetIO();
  36. io.DisplaySize = ImVec2(s.x, s.y);
  37. }
  38. int UI::initialize() {
  39. if (imguiShader.compile(imguiShaderVertex, imguiShaderFragment) < 0)
  40. return -1;
  41. if (imguiShader.addUniform("screen") < 0)
  42. return -2;
  43. if (imguiShader.addUniform("textureSampler") < 0)
  44. return -3;
  45. iniFilename = RunTime::getBaseDir() + "/imgui.ini";
  46. logFilename = RunTime::getBaseDir() + "/imgui_log.txt";
  47. ImGuiIO& io = ImGui::GetIO();
  48. io.DisplaySize = ImVec2(Window::getSize().x, Window::getSize().y);
  49. io.DeltaTime = 1.0f / 60.0f;
  50. io.IniFilename = iniFilename.c_str();
  51. io.LogFilename = logFilename.c_str();
  52. io.KeyMap[ImGuiKey_Tab] = tabKey;
  53. io.KeyMap[ImGuiKey_LeftArrow] = leftKey;
  54. io.KeyMap[ImGuiKey_RightArrow] = rightKey;
  55. io.KeyMap[ImGuiKey_UpArrow] = upKey;
  56. io.KeyMap[ImGuiKey_DownArrow] = downKey;
  57. io.KeyMap[ImGuiKey_Home] = homeKey;
  58. io.KeyMap[ImGuiKey_End] = endKey;
  59. io.KeyMap[ImGuiKey_Delete] = delKey;
  60. io.KeyMap[ImGuiKey_Backspace] = backspaceKey;
  61. io.KeyMap[ImGuiKey_Enter] = enterKey;
  62. io.KeyMap[ImGuiKey_Escape] = escapeKey;
  63. io.KeyMap[ImGuiKey_A] = aKey;
  64. io.KeyMap[ImGuiKey_C] = cKey;
  65. io.KeyMap[ImGuiKey_V] = vKey;
  66. io.KeyMap[ImGuiKey_X] = xKey;
  67. io.KeyMap[ImGuiKey_Y] = yKey;
  68. io.KeyMap[ImGuiKey_Z] = zKey;
  69. io.RenderDrawListsFn = UI::renderImGui;
  70. io.GetClipboardTextFn = Window::getClipboard;
  71. io.SetClipboardTextFn = Window::setClipboard;
  72. io.ImeSetInputScreenPosFn = Window::inputPositionCallback;
  73. // Load font texture
  74. //! \fixme allow loading other TTF fonts
  75. unsigned char* pixels;
  76. int width, height;
  77. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  78. fontTex = TextureManager::loadBufferSlot(pixels, width, height, ColorMode::RGBA, 32,
  79. TextureStorage::SYSTEM, -1, true);
  80. auto bm = TextureManager::getBufferManager(fontTex, TextureStorage::SYSTEM);
  81. io.Fonts->TexID = bm;
  82. // Set up OpenRaider style
  83. ImGuiStyle& style = ImGui::GetStyle();
  84. style.Colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  85. style.Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  86. style.Colors[ImGuiCol_Border] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  87. style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.60f);
  88. style.Colors[ImGuiCol_FrameBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.30f);
  89. style.Colors[ImGuiCol_TitleBg] = ImVec4(0.50f, 0.70f, 1.00f, 0.45f);
  90. style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.65f, 1.00f, 0.20f);
  91. style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.40f, 0.65f, 1.00f, 0.15f);
  92. style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.65f, 1.00f, 0.30f);
  93. style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.65f, 1.00f, 0.40f);
  94. style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(1.00f, 0.16f, 0.16f, 0.40f);
  95. style.Colors[ImGuiCol_ComboBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.99f);
  96. style.Colors[ImGuiCol_CheckHovered] = ImVec4(1.00f, 0.40f, 0.40f, 0.45f);
  97. style.Colors[ImGuiCol_CheckActive] = ImVec4(0.65f, 0.50f, 0.50f, 0.55f);
  98. style.Colors[ImGuiCol_CheckMark] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f);
  99. style.Colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  100. style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(1.00f, 0.20f, 0.22f, 1.00f);
  101. style.Colors[ImGuiCol_Button] = ImVec4(1.00f, 0.20f, 0.20f, 0.60f);
  102. style.Colors[ImGuiCol_ButtonHovered] = ImVec4(1.00f, 0.20f, 0.18f, 1.00f);
  103. style.Colors[ImGuiCol_ButtonActive] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
  104. style.Colors[ImGuiCol_Header] = ImVec4(0.21f, 0.54f, 1.00f, 0.45f);
  105. style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.22f, 0.56f, 1.00f, 0.80f);
  106. style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.00f, 0.31f, 1.00f, 1.00f);
  107. style.Colors[ImGuiCol_Column] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  108. style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.60f, 0.40f, 0.40f, 1.00f);
  109. style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f);
  110. style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  111. style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.60f);
  112. style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.90f);
  113. style.Colors[ImGuiCol_CloseButton] = ImVec4(0.21f, 0.56f, 1.00f, 0.50f);
  114. style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.21f, 0.32f, 1.00f, 0.60f);
  115. style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.70f, 0.70f, 0.70f, 1.00f);
  116. style.Colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  117. style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  118. style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  119. style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
  120. style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f);
  121. style.Colors[ImGuiCol_TooltipBg] = ImVec4(0.05f, 0.05f, 0.10f, 0.90f);
  122. style.WindowPadding = ImVec2(2, 2);
  123. style.WindowRounding = 9;
  124. style.FramePadding = ImVec2(2, 1);
  125. style.ItemSpacing = ImVec2(2, 2);
  126. style.ItemInnerSpacing = ImVec2(1, 1);
  127. style.TouchExtraPadding = ImVec2(0, 0);
  128. style.IndentSpacing = 3;
  129. style.ScrollbarWidth = 10;
  130. return 0;
  131. }
  132. void UI::eventsFinished() {
  133. ImGuiIO& io = ImGui::GetIO();
  134. io.DisplaySize = ImVec2(Window::getSize().x, Window::getSize().y);
  135. static unsigned long lastTime = 0;
  136. io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
  137. lastTime = systemTimerGet();
  138. if (io.DeltaTime <= 0.0f)
  139. io.DeltaTime = 1.0f / 60.0f;
  140. ImGui::NewFrame();
  141. if (!(visible || Console::isVisible() || Menu::isVisible())) {
  142. while (!motionEvents.empty()) {
  143. auto i = motionEvents.front();
  144. Game::handleMouseMotion(std::get<0>(i), std::get<1>(i),
  145. std::get<2>(i), std::get<3>(i));
  146. motionEvents.pop_front();
  147. }
  148. }
  149. while (!keyboardEvents.empty()) {
  150. auto i = keyboardEvents.front();
  151. if (!(visible || Console::isVisible() || Menu::isVisible())) {
  152. for (int n = forwardAction; n < ActionEventCount; n++) {
  153. if (RunTime::getKeyBinding((ActionEvents)n) == std::get<0>(i))
  154. Game::handleAction((ActionEvents)n, !std::get<1>(i));
  155. }
  156. }
  157. if (std::get<1>(i)) {
  158. if ((!io.WantCaptureKeyboard) || (!(visible || Console::isVisible() || Menu::isVisible()))) {
  159. if (!metaKeyIsActive) {
  160. if (RunTime::getKeyBinding(debugAction) == std::get<0>(i)) {
  161. visible = !visible;
  162. }
  163. if (RunTime::getKeyBinding(consoleAction) == std::get<0>(i)) {
  164. Console::setVisible(!Console::isVisible());
  165. }
  166. if (RunTime::getKeyBinding(menuAction) == std::get<0>(i)) {
  167. Menu::setVisible(!Menu::isVisible());
  168. }
  169. }
  170. }
  171. }
  172. keyboardEvents.pop_front();
  173. }
  174. bool clicked = !clickEvents.empty();
  175. clickEvents.clear();
  176. motionEvents.clear();
  177. scrollEvents.clear();
  178. // Allow clicking into the game to hide debug UI
  179. if ((visible || Console::isVisible()) && ((!io.WantCaptureMouse) && clicked)) {
  180. visible = false;
  181. Console::setVisible(false);
  182. }
  183. if (Window::getTextInput() != (visible || Console::isVisible() || Menu::isVisible()))
  184. Window::setTextInput(visible || Console::isVisible() || Menu::isVisible());
  185. bool input = !(visible || Console::isVisible() || Menu::isVisible());
  186. if (Window::getMousegrab() != input)
  187. Window::setMousegrab(input);
  188. io.MouseWheel = 0;
  189. }
  190. void UI::display() {
  191. if (RunTime::getShowFPS() && (!Menu::isVisible())) {
  192. if (ImGui::Begin("Debug Overlay", nullptr, ImVec2(0, 0), -1.0f,
  193. ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
  194. | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
  195. | ImGuiWindowFlags_AlwaysAutoResize)) {
  196. ImGui::Text("%d FPS", RunTime::getFPS());
  197. ImGui::Text("X: %.1f (%.2f)", Camera::getPosition().x, Camera::getRotation().x);
  198. ImGui::Text("Y: %.2f (%.2f)", Camera::getPosition().y, Camera::getRotation().y);
  199. ImGui::Text("Z: %.2f (%d)", Camera::getPosition().z, Camera::getRoom());
  200. auto window = ImGui::GetWindowSize();
  201. auto screen = Window::getSize();
  202. //ImGui::SetWindowPos(ImVec2(10, screen.y - window.y - 10));
  203. }
  204. ImGui::End();
  205. }
  206. if (Game::isLoaded())
  207. Camera::displayUI();
  208. Console::display();
  209. Menu::display();
  210. if (!visible) {
  211. ImGui::Render();
  212. return;
  213. }
  214. static bool showTestWindow = false;
  215. static bool showStyleWindow = false;
  216. if (ImGui::Begin("Engine", &visible, ImVec2(500, 600))) {
  217. Render::displayUI();
  218. RunTime::display();
  219. TextureManager::display();
  220. SoundManager::display();
  221. if (ImGui::CollapsingHeader("Library Versions")) {
  222. ImGui::TextWrapped("%s", VERSION);
  223. ImGui::Separator();
  224. ImGui::TextWrapped("ImGui v%s", IMGUI_VERSION);
  225. ImGui::TextWrapped("%s", Window::getVersion(false).c_str());
  226. ImGui::TextWrapped("%s", Shader::getVersion(false).c_str());
  227. ImGui::TextWrapped("%s", Sound::getVersion(false).c_str());
  228. ImGui::TextWrapped("GLM v%d.%d.%d", (GLM_VERSION / 100),
  229. ((GLM_VERSION % 100) / 10), (GLM_VERSION % 10));
  230. ImGui::Separator();
  231. ImGui::TextWrapped("ImGui v%s", ImGui::GetVersion());
  232. ImGui::TextWrapped("%s", Window::getVersion(true).c_str());
  233. ImGui::TextWrapped("%s", Shader::getVersion(true).c_str());
  234. ImGui::TextWrapped("%s", Sound::getVersion(true).c_str());
  235. }
  236. if (ImGui::CollapsingHeader("ImGui/Debug UI Help")) {
  237. ImGui::ShowUserGuide();
  238. ImGui::Separator();
  239. if (ImGui::Button("Show/Hide Test Window")) {
  240. showTestWindow = !showTestWindow;
  241. }
  242. ImGui::SameLine();
  243. if (ImGui::Button("Show/Hide Style Editor")) {
  244. showStyleWindow = !showStyleWindow;
  245. }
  246. }
  247. if (ImGui::CollapsingHeader("ShaderTexture Test")) {
  248. static ShaderTexture* st = nullptr;
  249. static int index = 0;
  250. ImGui::SliderInt("SkeletalModel", &index, 0, getWorld().sizeSkeletalModel() - 1);
  251. static glm::mat4 MVP(1.0f);
  252. static bool dirty = true, redraw = false;
  253. static float zoom = 1.0f;
  254. static float pos[3];
  255. static float rot[3];
  256. if (ImGui::SliderFloat3("Position", pos, -100.0f, 100.0f)) {
  257. //dirty = true;
  258. }
  259. if (ImGui::SliderFloat3("Rotation", rot, -6.0f, 6.0f)) {
  260. //dirty = true;
  261. }
  262. if (ImGui::SliderFloat("Zoom", &zoom, -1.0f, 2.0f)) {
  263. //dirty = true;
  264. }
  265. if (dirty) {
  266. static glm::mat4 projection = glm::perspective(45.0f, 1.0f, 0.1f, 2000.0f);
  267. glm::mat4 rotateX = glm::rotate(glm::mat4(1.0f), rot[0], glm::vec3(1.0f, 0.0f, 0.0f));
  268. glm::mat4 rotateY = glm::rotate(glm::mat4(1.0f), rot[1], glm::vec3(0.0f, 1.0f, 0.0f));
  269. glm::mat4 rotateZ = glm::rotate(glm::mat4(1.0f), rot[2], glm::vec3(0.0f, 0.0f, 1.0f));
  270. glm::mat4 rotate = rotateZ * rotateX * rotateY;
  271. glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(pos[0], pos[1], pos[2]));
  272. //glm::mat4 view = glm::inverse(rotate * translate);
  273. glm::mat4 view = rotate * translate;
  274. glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(zoom, zoom, zoom));
  275. MVP = projection * view * scale;
  276. redraw = true;
  277. dirty = false;
  278. }
  279. if ((index >= 0) && (index < getWorld().sizeSkeletalModel())) {
  280. auto& sm = getWorld().getSkeletalModel(index);
  281. if ((sm.size() > 0) && (sm.get(0).size() > 0)) {
  282. if (ImGui::Button("(Re)Create ShaderTexture...")) {
  283. if (st != nullptr) {
  284. delete st;
  285. st = nullptr;
  286. }
  287. if (st == nullptr) {
  288. st = new ShaderTexture();
  289. redraw = true;
  290. }
  291. }
  292. if ((st != nullptr) && redraw) {
  293. st->clear();
  294. sm.display(MVP, 0, 0, st);
  295. redraw = false;
  296. Log::get(LOG_DEBUG) << "Rendered new ShaderTexture!" << Log::endl;
  297. }
  298. } else {
  299. ImGui::Text("Selected SkeletalModel has no animation/frame!");
  300. }
  301. } else {
  302. ImGui::Text("No SkeletalModels loaded!");
  303. }
  304. if (st != nullptr) {
  305. auto bm = TextureManager::getBufferManager(st->getTexture(), TextureStorage::SYSTEM);
  306. ImGui::Image(bm, ImVec2(ImGui::GetColumnWidth() * 2 / 3,
  307. ImGui::GetColumnWidth() * 2 / 3));
  308. }
  309. }
  310. }
  311. ImGui::End();
  312. if (showTestWindow)
  313. ImGui::ShowTestWindow();
  314. if (showStyleWindow)
  315. ImGui::ShowStyleEditor();
  316. ImGui::Render();
  317. }
  318. void UI::shutdown() {
  319. ImGui::Shutdown();
  320. }
  321. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  322. ImGuiIO& io = ImGui::GetIO();
  323. io.KeysDown[key] = pressed;
  324. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  325. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  326. io.KeyAlt = io.KeysDown[leftaltKey] | io.KeysDown[rightaltKey];
  327. keyboardEvents.push_back(std::make_tuple(key, pressed));
  328. if ((key == leftguiKey) || (key == rightguiKey))
  329. metaKeyIsActive = pressed;
  330. }
  331. void UI::handleText(char* text, bool notFinished) {
  332. if (notFinished)
  333. return;
  334. ImGuiIO& io = ImGui::GetIO();
  335. while (*text != '\0') {
  336. io.AddInputCharacter(*text);
  337. text++;
  338. }
  339. }
  340. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  341. ImGuiIO& io = ImGui::GetIO();
  342. io.MousePos = ImVec2((float)x, (float)y);
  343. if (button == leftmouseKey) {
  344. io.MouseDown[0] = !released;
  345. } else if (button == rightmouseKey) {
  346. io.MouseDown[1] = !released;
  347. } else if (button == middlemouseKey) {
  348. io.MouseDown[2] = !released;
  349. } else if (button == fourthmouseKey) {
  350. io.MouseDown[3] = !released;
  351. } else if (button == fifthmouseKey) {
  352. io.MouseDown[4] = !released;
  353. }
  354. clickEvents.push_back(std::make_tuple(x, y, button, released));
  355. }
  356. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  357. ImGuiIO& io = ImGui::GetIO();
  358. io.MousePos = ImVec2((float)xabs, (float)yabs);
  359. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  360. }
  361. void UI::handleMouseScroll(int xrel, int yrel) {
  362. ImGuiIO& io = ImGui::GetIO();
  363. io.MouseWheel += yrel;
  364. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  365. }
  366. void UI::handleControllerAxis(float value, KeyboardButton axis) {
  367. Game::handleControllerAxis(value, axis);
  368. }
  369. void UI::handleControllerButton(KeyboardButton button, bool released) {
  370. if (visible || Console::isVisible())
  371. return;
  372. if (Menu::isVisible()) {
  373. if (button == aButton) {
  374. handleKeyboard(enterKey, !released);
  375. } else if (button == padUp) {
  376. handleKeyboard(upKey, !released);
  377. } else if (button == padDown) {
  378. handleKeyboard(downKey, !released);
  379. } else if (button == padLeft) {
  380. handleKeyboard(leftKey, !released);
  381. } else if (button == padRight) {
  382. handleKeyboard(rightKey, !released);
  383. } else if (button == startButton) {
  384. if (!released)
  385. Menu::setVisible(false);
  386. }
  387. } else {
  388. if (button == aButton) {
  389. Game::handleAction(jumpAction, released);
  390. } else if (button == bButton) {
  391. Game::handleAction(crouchAction, released);
  392. } else if (button == xButton) {
  393. Game::handleAction(useAction, released);
  394. } else if (button == yButton) {
  395. Game::handleAction(holsterAction, released);
  396. } else if (button == padUp) {
  397. Game::handleAction(forwardAction, released);
  398. } else if (button == padDown) {
  399. Game::handleAction(backwardAction, released);
  400. } else if (button == padLeft) {
  401. Game::handleAction(leftAction, released);
  402. } else if (button == padRight) {
  403. Game::handleAction(rightAction, released);
  404. } else if (button == leftShoulder) {
  405. Game::handleAction(walkAction, released);
  406. } else if (button == startButton) {
  407. if (!released)
  408. Menu::setVisible(true);
  409. }
  410. }
  411. }
  412. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  413. if (cmd_lists_count == 0)
  414. return;
  415. static ShaderBuffer vert, uv, col;
  416. glEnable(GL_SCISSOR_TEST);
  417. Shader::set2DState(true);
  418. imguiShader.use();
  419. imguiShader.loadUniform(0, Window::getSize());
  420. vert.bindBuffer(0, 2);
  421. uv.bindBuffer(1, 2);
  422. col.bindBuffer(2, 4);
  423. /*! \fixme Don't copy data
  424. * The GL calls and the shaders can probably be slightly altered
  425. * to avoid copying all the vertices, uvs and colors again here.
  426. */
  427. for (int i = 0; i < cmd_lists_count; i++) {
  428. auto& commands = cmd_lists[i]->commands;
  429. auto& buffer = cmd_lists[i]->vtx_buffer;
  430. int offset = 0;
  431. for (int n = 0; n < commands.size(); n++) {
  432. std::vector<glm::vec2> vertices;
  433. std::vector<glm::vec2> uvs;
  434. std::vector<glm::vec4> colors;
  435. for (int v = 0; v < commands[n].vtx_count; v++) {
  436. vertices.push_back(glm::vec2(buffer[offset + v].pos.x, buffer[offset + v].pos.y));
  437. uvs.push_back(glm::vec2(buffer[offset + v].uv.x, buffer[offset + v].uv.y));
  438. float r, g, b, a;
  439. a = ((buffer[offset + v].col & 0xFF000000) >> 24) / 255.0f;
  440. b = ((buffer[offset + v].col & 0x00FF0000) >> 16) / 255.0f;
  441. g = ((buffer[offset + v].col & 0x0000FF00) >> 8) / 255.0f;
  442. r = (buffer[offset + v].col & 0x000000FF) / 255.0f;
  443. colors.push_back(glm::vec4(r, g, b, a));
  444. }
  445. offset += commands[n].vtx_count;
  446. vert.bufferData(vertices);
  447. uv.bufferData(uvs);
  448. col.bufferData(colors);
  449. auto bm = static_cast<BufferManager*>(commands[n].texture_id);
  450. assert(bm != nullptr);
  451. imguiShader.loadUniform(1, bm->getTextureID(), bm->getTextureStorage());
  452. glScissor(commands[n].clip_rect.x,
  453. Window::getSize().y - commands[n].clip_rect.w,
  454. commands[n].clip_rect.z - commands[n].clip_rect.x,
  455. commands[n].clip_rect.w - commands[n].clip_rect.y);
  456. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  457. }
  458. }
  459. vert.unbind(0);
  460. uv.unbind(1);
  461. col.unbind(2);
  462. Shader::set2DState(false);
  463. glDisable(GL_SCISSOR_TEST);
  464. }
  465. // --------------------------------------
  466. // *INDENT-OFF*
  467. const char* UI::imguiShaderVertex = R"!?!(
  468. #version 330 core
  469. layout(location = 0) in vec2 vertexPosition_screen;
  470. layout(location = 1) in vec2 vertexUV;
  471. layout(location = 2) in vec4 vertexColor;
  472. out vec2 UV;
  473. out vec4 FragColor;
  474. uniform vec2 screen;
  475. void main() {
  476. vec2 halfScreen = screen / 2;
  477. vec2 vertexPosition_homogenous = (vertexPosition_screen - halfScreen) / halfScreen;
  478. gl_Position = vec4(vertexPosition_homogenous.x, -vertexPosition_homogenous.y, 0, 1);
  479. UV = vertexUV;
  480. FragColor = vertexColor;
  481. }
  482. )!?!";
  483. const char* UI::imguiShaderFragment = R"!?!(
  484. #version 330 core
  485. in vec2 UV;
  486. in vec4 FragColor;
  487. out vec4 color;
  488. uniform sampler2D textureSampler;
  489. void main() {
  490. color = texture(textureSampler, UV) * FragColor;
  491. }
  492. )!?!";
  493. // --------------------------------------
  494. // *INDENT-ON*