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

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