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

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