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

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