Open Source Tomb Raider Engine
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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