Open Source Tomb Raider Engine
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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