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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*!
  2. * \file src/UI.cpp
  3. * \brief UI interface manager
  4. *
  5. * \author xythobuz
  6. */
  7. #include "imgui/imgui.h"
  8. #include "stb/stb_image.h"
  9. #include "global.h"
  10. #include "Camera.h"
  11. #include "Console.h"
  12. #include "Game.h"
  13. #include "Log.h"
  14. #include "Menu.h"
  15. #include "Render.h"
  16. #include "RunTime.h"
  17. #include "SoundManager.h"
  18. #include "TextureManager.h"
  19. #include "World.h"
  20. #include "commands/Command.h"
  21. #include "system/Sound.h"
  22. #include "system/Window.h"
  23. #include "utils/time.h"
  24. #include "UI.h"
  25. Shader UI::imguiShader;
  26. bool UI::visible = false;
  27. unsigned int UI::fontTex;
  28. std::string UI::iniFilename;
  29. std::string UI::logFilename;
  30. bool UI::metaKeyIsActive = false;
  31. std::list<std::tuple<KeyboardButton, bool>> UI::keyboardEvents;
  32. std::list<std::tuple<unsigned int, unsigned int, KeyboardButton, bool>> UI::clickEvents;
  33. std::list<std::tuple<int, int, int, int>> UI::motionEvents;
  34. std::list<std::tuple<int, int>> UI::scrollEvents;
  35. void UI::setSize(glm::i32vec2 s) {
  36. ImGuiIO& io = ImGui::GetIO();
  37. io.DisplaySize = ImVec2(s.x, s.y);
  38. }
  39. int UI::initialize() {
  40. if (imguiShader.compile(imguiShaderVertex, imguiShaderFragment) < 0)
  41. return -1;
  42. if (imguiShader.addUniform("screen") < 0)
  43. return -2;
  44. if (imguiShader.addUniform("textureSampler") < 0)
  45. return -3;
  46. iniFilename = RunTime::getBaseDir() + "/imgui.ini";
  47. logFilename = RunTime::getBaseDir() + "/imgui_log.txt";
  48. ImGuiIO& io = ImGui::GetIO();
  49. io.DisplaySize = ImVec2(Window::getSize().x, Window::getSize().y);
  50. io.DeltaTime = 1.0f / 60.0f;
  51. io.IniFilename = iniFilename.c_str();
  52. io.LogFilename = logFilename.c_str();
  53. io.KeyMap[ImGuiKey_Tab] = tabKey;
  54. io.KeyMap[ImGuiKey_LeftArrow] = leftKey;
  55. io.KeyMap[ImGuiKey_RightArrow] = rightKey;
  56. io.KeyMap[ImGuiKey_UpArrow] = upKey;
  57. io.KeyMap[ImGuiKey_DownArrow] = downKey;
  58. io.KeyMap[ImGuiKey_Home] = homeKey;
  59. io.KeyMap[ImGuiKey_End] = endKey;
  60. io.KeyMap[ImGuiKey_Delete] = delKey;
  61. io.KeyMap[ImGuiKey_Backspace] = backspaceKey;
  62. io.KeyMap[ImGuiKey_Enter] = enterKey;
  63. io.KeyMap[ImGuiKey_Escape] = escapeKey;
  64. io.KeyMap[ImGuiKey_A] = aKey;
  65. io.KeyMap[ImGuiKey_C] = cKey;
  66. io.KeyMap[ImGuiKey_V] = vKey;
  67. io.KeyMap[ImGuiKey_X] = xKey;
  68. io.KeyMap[ImGuiKey_Y] = yKey;
  69. io.KeyMap[ImGuiKey_Z] = zKey;
  70. io.RenderDrawListsFn = UI::renderImGui;
  71. io.GetClipboardTextFn = Window::getClipboard;
  72. io.SetClipboardTextFn = Window::setClipboard;
  73. // Load font texture
  74. //! \todo Use our own font subsystem instead of this?
  75. const void* png_data;
  76. unsigned int png_size;
  77. ImGui::GetDefaultFontData(nullptr, nullptr, &png_data, &png_size);
  78. int tex_x, tex_y, tex_comp;
  79. void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
  80. (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
  81. fontTex = TextureManager::loadBufferSlot((unsigned char*)tex_data,
  82. tex_x, tex_y, ColorMode::RGBA, 32,
  83. TextureStorage::SYSTEM, -1, false);
  84. stbi_image_free(tex_data);
  85. // Set up OpenRaider style
  86. ImGuiStyle& style = ImGui::GetStyle();
  87. style.Colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  88. style.Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  89. style.Colors[ImGuiCol_Border] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  90. style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.60f);
  91. style.Colors[ImGuiCol_FrameBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.30f);
  92. style.Colors[ImGuiCol_TitleBg] = ImVec4(0.50f, 0.70f, 1.00f, 0.45f);
  93. style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.65f, 1.00f, 0.20f);
  94. style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.40f, 0.65f, 1.00f, 0.15f);
  95. style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.65f, 1.00f, 0.30f);
  96. style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.65f, 1.00f, 0.40f);
  97. style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(1.00f, 0.16f, 0.16f, 0.40f);
  98. style.Colors[ImGuiCol_ComboBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.99f);
  99. style.Colors[ImGuiCol_CheckHovered] = ImVec4(1.00f, 0.40f, 0.40f, 0.45f);
  100. style.Colors[ImGuiCol_CheckActive] = ImVec4(0.65f, 0.50f, 0.50f, 0.55f);
  101. style.Colors[ImGuiCol_CheckMark] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f);
  102. style.Colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  103. style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(1.00f, 0.20f, 0.22f, 1.00f);
  104. style.Colors[ImGuiCol_Button] = ImVec4(1.00f, 0.20f, 0.20f, 0.60f);
  105. style.Colors[ImGuiCol_ButtonHovered] = ImVec4(1.00f, 0.20f, 0.18f, 1.00f);
  106. style.Colors[ImGuiCol_ButtonActive] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
  107. style.Colors[ImGuiCol_Header] = ImVec4(0.21f, 0.54f, 1.00f, 0.45f);
  108. style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.22f, 0.56f, 1.00f, 0.80f);
  109. style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.00f, 0.31f, 1.00f, 1.00f);
  110. style.Colors[ImGuiCol_Column] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  111. style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.60f, 0.40f, 0.40f, 1.00f);
  112. style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f);
  113. style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
  114. style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.60f);
  115. style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.90f);
  116. style.Colors[ImGuiCol_CloseButton] = ImVec4(0.21f, 0.56f, 1.00f, 0.50f);
  117. style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.21f, 0.32f, 1.00f, 0.60f);
  118. style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.70f, 0.70f, 0.70f, 1.00f);
  119. style.Colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  120. style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  121. style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  122. style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
  123. style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f);
  124. style.Colors[ImGuiCol_TooltipBg] = ImVec4(0.05f, 0.05f, 0.10f, 0.90f);
  125. style.WindowPadding = ImVec2(2, 2);
  126. style.WindowRounding = 9;
  127. style.FramePadding = ImVec2(2, 1);
  128. style.ItemSpacing = ImVec2(2, 2);
  129. style.ItemInnerSpacing = ImVec2(1, 1);
  130. style.TouchExtraPadding = ImVec2(0, 0);
  131. style.TreeNodeSpacing = 3;
  132. style.ScrollBarWidth = 10;
  133. return 0;
  134. }
  135. void UI::eventsFinished() {
  136. ImGuiIO& io = ImGui::GetIO();
  137. io.DisplaySize = ImVec2(Window::getSize().x, Window::getSize().y);
  138. static unsigned long lastTime = 0;
  139. io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
  140. lastTime = systemTimerGet();
  141. if (io.DeltaTime <= 0.0f)
  142. io.DeltaTime = 1.0f / 60.0f;
  143. ImGui::NewFrame();
  144. if (!(visible || Console::isVisible())) {
  145. while (!clickEvents.empty()) {
  146. auto i = clickEvents.front();
  147. if (getMenu().isVisible()) {
  148. getMenu().handleMouseClick(std::get<0>(i), std::get<1>(i),
  149. std::get<2>(i), std::get<3>(i));
  150. }
  151. clickEvents.pop_front();
  152. }
  153. while (!motionEvents.empty()) {
  154. auto i = motionEvents.front();
  155. if (!getMenu().isVisible()) {
  156. Game::handleMouseMotion(std::get<0>(i), std::get<1>(i),
  157. std::get<2>(i), std::get<3>(i));
  158. }
  159. motionEvents.pop_front();
  160. }
  161. while (!scrollEvents.empty()) {
  162. auto i = scrollEvents.front();
  163. if (getMenu().isVisible()) {
  164. getMenu().handleMouseScroll(std::get<0>(i), std::get<1>(i));
  165. }
  166. scrollEvents.pop_front();
  167. }
  168. }
  169. while (!keyboardEvents.empty()) {
  170. auto i = keyboardEvents.front();
  171. if (!(visible || Console::isVisible())) {
  172. if (getMenu().isVisible()) {
  173. getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
  174. } else {
  175. for (int n = forwardAction; n < ActionEventCount; n++) {
  176. if (RunTime::getKeyBinding((ActionEvents)n) == std::get<0>(i))
  177. Game::handleAction((ActionEvents)n, !std::get<1>(i));
  178. }
  179. }
  180. }
  181. if (std::get<1>(i)) {
  182. if (!(visible || Console::isVisible())) {
  183. if (RunTime::getKeyBinding(menuAction) == std::get<0>(i)) {
  184. getMenu().setVisible(!getMenu().isVisible());
  185. }
  186. }
  187. if ((!io.WantCaptureKeyboard) || (!(visible || Console::isVisible()))) {
  188. if (!metaKeyIsActive) {
  189. if (RunTime::getKeyBinding(debugAction) == std::get<0>(i)) {
  190. visible = !visible;
  191. }
  192. if (RunTime::getKeyBinding(consoleAction) == std::get<0>(i)) {
  193. Console::setVisible(!Console::isVisible());
  194. }
  195. }
  196. }
  197. }
  198. keyboardEvents.pop_front();
  199. }
  200. bool clicked = !clickEvents.empty();
  201. clickEvents.clear();
  202. motionEvents.clear();
  203. scrollEvents.clear();
  204. if ((visible || Console::isVisible()) && (
  205. ((!io.WantCaptureKeyboard) && io.KeysDown[escapeKey])
  206. || ((!io.WantCaptureMouse) && clicked)
  207. )) {
  208. visible = false;
  209. Console::setVisible(false);
  210. }
  211. if (Window::getTextInput() != (visible || Console::isVisible()))
  212. Window::setTextInput(visible || Console::isVisible());
  213. bool input = !(visible || Console::isVisible() || getMenu().isVisible());
  214. if (Window::getMousegrab() != input)
  215. Window::setMousegrab(input);
  216. io.MouseWheel = 0;
  217. }
  218. void UI::display() {
  219. if (RunTime::getShowFPS()) {
  220. if (ImGui::Begin("Debug Overlay", nullptr, ImVec2(0,0), 0.3f,
  221. ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
  222. | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
  223. | ImGuiWindowFlags_AlwaysAutoResize)) {
  224. ImGui::Text("%d FPS", RunTime::getFPS());
  225. ImGui::Text("X: %.1f (%.2f)", Camera::getPosition().x, Camera::getRotation().x);
  226. ImGui::Text("Y: %.2f (%.2f)", Camera::getPosition().y, Camera::getRotation().y);
  227. ImGui::Text("Z: %.2f", Camera::getPosition().z);
  228. auto window = ImGui::GetWindowSize();
  229. auto screen = Window::getSize();
  230. ImGui::SetWindowPos(ImVec2(10, screen.y - window.y - 10));
  231. }
  232. ImGui::End();
  233. }
  234. Console::display();
  235. if (!visible) {
  236. ImGui::Render();
  237. return;
  238. }
  239. static bool showTestWindow = false;
  240. static bool showStyleWindow = false;
  241. if (ImGui::Begin("Engine", &visible, ImVec2(400, 400))) {
  242. Render::displayUI();
  243. RunTime::display();
  244. SoundManager::display();
  245. /*
  246. static bool visibleTex = false;
  247. static bool visibleTile = false;
  248. static bool visibleAnim = false;
  249. static bool visibleSprite = false;
  250. if (ImGui::CollapsingHeader("Texture Viewer")) {
  251. static bool game = Game::isLoaded();
  252. static int index = 0;
  253. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  254. ImGui::SliderInt("##texslide", &index, 0, TextureManager::.numTextures(
  255. game ? TextureStorage::GAME : TextureStorage::SYSTEM) - 1);
  256. ImGui::PopItemWidth();
  257. ImGui::SameLine();
  258. if (ImGui::Button("+##texplus", ImVec2(0, 0), true)) {
  259. if (index < (TextureManager::numTextures(
  260. game ? TextureStorage::GAME : TextureStorage::SYSTEM) - 1))
  261. index++;
  262. else
  263. index = 0;
  264. }
  265. ImGui::SameLine();
  266. if (ImGui::Button("-##texminus", ImVec2(0, 0), true)) {
  267. if (index > 0)
  268. index--;
  269. else
  270. index = TextureManager::numTextures(
  271. game ? TextureStorage::GAME : TextureStorage::SYSTEM) - 1;
  272. }
  273. ImGui::SameLine();
  274. if ((TextureManager::numTextures() > 0)) {
  275. ImGui::Checkbox("Game##texgame", &game);
  276. } else {
  277. game = false;
  278. }
  279. ImGui::SameLine();
  280. if (ImGui::Button("Show##texshow")) {
  281. visibleTex = true;
  282. visibleTile = false;
  283. visibleAnim = false;
  284. visibleSprite = false;
  285. }
  286. ImGui::SameLine();
  287. if (ImGui::Button("Clear##texclear")) {
  288. getRender().debugDisplayTexture();
  289. visibleTex = false;
  290. }
  291. if (visibleTex) {
  292. getRender().debugDisplayTexture(index,
  293. game ? TextureStorage::GAME : TextureStorage::SYSTEM,
  294. ImGui::GetWindowPos().x - ImGui::GetWindowWidth(),
  295. ImGui::GetWindowPos().y,
  296. ImGui::GetWindowWidth(), ImGui::GetWindowWidth());
  297. }
  298. }
  299. if (ImGui::CollapsingHeader("Textile Viewer")) {
  300. if (TextureManager::numTiles() > 0) {
  301. static int index = 0;
  302. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  303. ImGui::SliderInt("##tileslide", &index, 0, TextureManager::numTiles() - 1);
  304. ImGui::PopItemWidth();
  305. ImGui::SameLine();
  306. if (ImGui::Button("+##tileplus", ImVec2(0, 0), true)) {
  307. if (index < (TextureManager::numTiles() - 1))
  308. index++;
  309. else
  310. index = 0;
  311. }
  312. ImGui::SameLine();
  313. if (ImGui::Button("-##tileminus", ImVec2(0, 0), true)) {
  314. if (index > 0)
  315. index--;
  316. else
  317. index = TextureManager::numTiles() - 1;
  318. }
  319. ImGui::SameLine();
  320. if (ImGui::Button("Show##tileshow")) {
  321. visibleTile = true;
  322. visibleTex = false;
  323. visibleAnim = false;
  324. visibleSprite = false;
  325. }
  326. ImGui::SameLine();
  327. if (ImGui::Button("Clear##tileclear")) {
  328. getRender().debugDisplayTextile();
  329. visibleTile = false;
  330. }
  331. if (visibleTile && (index < TextureManager::numTiles())) {
  332. ImGui::Text(TextureManager::.getTile(index).isTriangle() ? "Triangle" : "Rectangle");
  333. }
  334. if (visibleTile) {
  335. getRender().debugDisplayTextile(index,
  336. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  337. ImGui::GetWindowPos().y,
  338. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  339. }
  340. } else {
  341. ImGui::Text("Please load a level using the new loader!");
  342. }
  343. }
  344. if (ImGui::CollapsingHeader("Animated Textile Viewer")) {
  345. if (TextureManager::.numAnimatedTiles() > 0) {
  346. static int index = 0;
  347. static int tile = TextureManager::.getFirstTileAnimation(index);
  348. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  349. if (ImGui::SliderInt("##animslide", &index, 0, TextureManager::.numAnimatedTiles() - 1)) {
  350. tile = TextureManager::.getFirstTileAnimation(index);
  351. }
  352. ImGui::PopItemWidth();
  353. ImGui::SameLine();
  354. if (ImGui::Button("+##animplus", ImVec2(0, 0), true)) {
  355. if (index < (TextureManager::.numAnimatedTiles() - 1))
  356. index++;
  357. else
  358. index = 0;
  359. tile = TextureManager::.getFirstTileAnimation(index);
  360. }
  361. ImGui::SameLine();
  362. if (ImGui::Button("-##animminus", ImVec2(0, 0), true)) {
  363. if (index > 0)
  364. index--;
  365. else
  366. index = TextureManager::.numAnimatedTiles() - 1;
  367. tile = TextureManager::.getFirstTileAnimation(index);
  368. }
  369. ImGui::SameLine();
  370. if (ImGui::Button("Show##animshow")) {
  371. visibleAnim = true;
  372. visibleTex = false;
  373. visibleTile = false;
  374. visibleSprite = false;
  375. }
  376. ImGui::SameLine();
  377. if (ImGui::Button("Clear##animclear")) {
  378. getRender().debugDisplayTextile();
  379. visibleAnim = false;
  380. }
  381. if (visibleAnim) {
  382. static int fr = 0;
  383. if (fr > 0) {
  384. fr--;
  385. } else {
  386. getRender().debugDisplayTextile(tile,
  387. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  388. ImGui::GetWindowPos().y,
  389. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  390. fr = RunTime::getFPS() / 2;
  391. tile = TextureManager::.getNextTileAnimation(tile);
  392. }
  393. ImGui::Text("Current Tile: %d", tile);
  394. }
  395. } else {
  396. ImGui::Text("Please load a level with animated textures!");
  397. }
  398. }
  399. if (ImGui::CollapsingHeader("Sprite Sequence Viewer")) {
  400. if (getWorld().sizeSprite() <= 0) {
  401. ImGui::Text("Please load a level containing sprites!");
  402. } else {
  403. static int index = 0;
  404. static int sprite = 0;
  405. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  406. if (ImGui::SliderInt("##spriteslide", &index, 0, getWorld().sizeSprite() - 1))
  407. sprite = 0;
  408. ImGui::PopItemWidth();
  409. ImGui::SameLine();
  410. if (ImGui::Button("+##spriteplus", ImVec2(0, 0), true)) {
  411. if (index < (getWorld().sizeSprite() - 1))
  412. index++;
  413. else
  414. index = 0;
  415. sprite = 0;
  416. }
  417. ImGui::SameLine();
  418. if (ImGui::Button("-##spriteminus", ImVec2(0, 0), true)) {
  419. if (index > 0)
  420. index--;
  421. else
  422. index = getWorld().sizeSprite() - 1;
  423. sprite = 0;
  424. }
  425. ImGui::SameLine();
  426. if (ImGui::Button("Show##spriteshow")) {
  427. visibleSprite = true;
  428. visibleTex = false;
  429. visibleTile = false;
  430. visibleAnim = false;
  431. sprite = 0;
  432. }
  433. ImGui::SameLine();
  434. if (ImGui::Button("Clear##spriteclear")) {
  435. getRender().debugDisplaySprite();
  436. visibleSprite = false;
  437. }
  438. if (visibleSprite) {
  439. static int fr = 0;
  440. if (fr > 0) {
  441. fr--;
  442. } else {
  443. getRender().debugDisplaySprite(index, sprite,
  444. ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
  445. ImGui::GetWindowPos().y,
  446. (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
  447. fr = RunTime::getFPS() / 10;
  448. if (sprite < (getWorld().getSprite(index).size() - 1))
  449. sprite++;
  450. else
  451. sprite = 0;
  452. }
  453. ImGui::Text("Sprite %d/%d", sprite + 1, getWorld().getSprite(index).size());
  454. }
  455. }
  456. }
  457. */
  458. if (ImGui::CollapsingHeader("ImGui/Debug UI Help")) {
  459. //ImGui::TextWrapped("DebugViewer Textures/Textiles/Sprites will be drawn on"
  460. // " the left side and scale with the size of this window!");
  461. //ImGui::Separator();
  462. ImGui::ShowUserGuide();
  463. ImGui::Separator();
  464. if (ImGui::Button("Show/Hide Test Window")) {
  465. showTestWindow = !showTestWindow;
  466. }
  467. ImGui::SameLine();
  468. if (ImGui::Button("Show/Hide Style Editor")) {
  469. showStyleWindow = !showStyleWindow;
  470. }
  471. }
  472. }
  473. ImGui::End();
  474. if (showTestWindow)
  475. ImGui::ShowTestWindow();
  476. if (showStyleWindow)
  477. ImGui::ShowStyleEditor();
  478. ImGui::Render();
  479. }
  480. void UI::shutdown() {
  481. ImGui::Shutdown();
  482. }
  483. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  484. ImGuiIO& io = ImGui::GetIO();
  485. io.KeysDown[key] = pressed;
  486. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  487. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  488. keyboardEvents.push_back(std::make_tuple(key, pressed));
  489. if ((key == leftguiKey) || (key == rightguiKey))
  490. metaKeyIsActive = pressed;
  491. }
  492. void UI::handleText(char* text, bool notFinished) {
  493. if (notFinished)
  494. return;
  495. ImGuiIO& io = ImGui::GetIO();
  496. while (*text != '\0') {
  497. io.AddInputCharacter(*text);
  498. text++;
  499. }
  500. }
  501. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  502. ImGuiIO& io = ImGui::GetIO();
  503. io.MousePos = ImVec2((float)x, (float)y);
  504. if (button == leftmouseKey) {
  505. io.MouseDown[0] = !released;
  506. } else if (button == rightmouseKey) {
  507. io.MouseDown[1] = !released;
  508. } else if (button == middlemouseKey) {
  509. io.MouseDown[2] = !released;
  510. } else if (button == fourthmouseKey) {
  511. io.MouseDown[3] = !released;
  512. } else if (button == fifthmouseKey) {
  513. io.MouseDown[4] = !released;
  514. }
  515. clickEvents.push_back(std::make_tuple(x, y, button, released));
  516. }
  517. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  518. ImGuiIO& io = ImGui::GetIO();
  519. io.MousePos = ImVec2((float)xabs, (float)yabs);
  520. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  521. }
  522. void UI::handleMouseScroll(int xrel, int yrel) {
  523. ImGuiIO& io = ImGui::GetIO();
  524. io.MouseWheel += yrel;
  525. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  526. }
  527. void UI::handleControllerAxis(float value, KeyboardButton axis) {
  528. Game::handleControllerAxis(value, axis);
  529. }
  530. void UI::handleControllerButton(KeyboardButton button, bool released) {
  531. Game::handleControllerButton(button, released);
  532. }
  533. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  534. if (cmd_lists_count == 0)
  535. return;
  536. static ShaderBuffer vert, uv, col;
  537. glEnable(GL_SCISSOR_TEST);
  538. Shader::set2DState(true);
  539. imguiShader.use();
  540. imguiShader.loadUniform(0, Window::getSize());
  541. imguiShader.loadUniform(1, fontTex, TextureStorage::SYSTEM);
  542. vert.bindBuffer(0, 2);
  543. uv.bindBuffer(1, 2);
  544. col.bindBuffer(2, 4);
  545. /*! \fixme Don't copy data
  546. * The GL calls and the shaders can probably be slightly altered
  547. * to avoid copying all the vertices, uvs and colors again here.
  548. */
  549. for (int i = 0; i < cmd_lists_count; i++) {
  550. auto& commands = cmd_lists[i]->commands;
  551. auto& buffer = cmd_lists[i]->vtx_buffer;
  552. int offset = 0;
  553. for (int n = 0; n < commands.size(); n++) {
  554. std::vector<glm::vec2> vertices;
  555. std::vector<glm::vec2> uvs;
  556. std::vector<glm::vec4> colors;
  557. for (int v = 0; v < commands[n].vtx_count; v++) {
  558. vertices.push_back(glm::vec2(buffer[offset + v].pos.x, buffer[offset + v].pos.y));
  559. uvs.push_back(glm::vec2(buffer[offset + v].uv.x, buffer[offset + v].uv.y));
  560. float r, g, b, a;
  561. a = ((buffer[offset + v].col & 0xFF000000) >> 24) / 255.0f;
  562. b = ((buffer[offset + v].col & 0x00FF0000) >> 16) / 255.0f;
  563. g = ((buffer[offset + v].col & 0x0000FF00) >> 8) / 255.0f;
  564. r = (buffer[offset + v].col & 0x000000FF) / 255.0f;
  565. colors.push_back(glm::vec4(r, g, b, a));
  566. }
  567. offset += commands[n].vtx_count;
  568. vert.bufferData(vertices);
  569. uv.bufferData(uvs);
  570. col.bufferData(colors);
  571. glScissor(commands[n].clip_rect.x,
  572. Window::getSize().y - commands[n].clip_rect.w,
  573. commands[n].clip_rect.z - commands[n].clip_rect.x,
  574. commands[n].clip_rect.w - commands[n].clip_rect.y);
  575. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  576. }
  577. }
  578. vert.unbind(0);
  579. uv.unbind(1);
  580. col.unbind(2);
  581. Shader::set2DState(false);
  582. glDisable(GL_SCISSOR_TEST);
  583. }
  584. // --------------------------------------
  585. // *INDENT-OFF*
  586. const char* UI::imguiShaderVertex = R"!?!(
  587. #version 330 core
  588. layout(location = 0) in vec2 vertexPosition_screen;
  589. layout(location = 1) in vec2 vertexUV;
  590. layout(location = 2) in vec4 vertexColor;
  591. out vec2 UV;
  592. out vec4 FragColor;
  593. uniform vec2 screen;
  594. void main() {
  595. vec2 halfScreen = screen / 2;
  596. vec2 vertexPosition_homogenous = (vertexPosition_screen - halfScreen) / halfScreen;
  597. gl_Position = vec4(vertexPosition_homogenous.x, -vertexPosition_homogenous.y, 0, 1);
  598. UV = vertexUV;
  599. FragColor = vertexColor;
  600. }
  601. )!?!";
  602. const char* UI::imguiShaderFragment = R"!?!(
  603. #version 330 core
  604. in vec2 UV;
  605. in vec4 FragColor;
  606. out vec4 color;
  607. uniform sampler2D textureSampler;
  608. void main() {
  609. color = texture(textureSampler, UV) * FragColor;
  610. }
  611. )!?!";
  612. // --------------------------------------
  613. // *INDENT-ON*