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

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