Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UI.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*!
  2. * \file src/UI.cpp
  3. * \brief UI interface manager
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include "global.h"
  9. #include "Console.h"
  10. #include "Game.h"
  11. #include "Log.h"
  12. #include "Menu.h"
  13. #include "Render.h"
  14. #include "RunTime.h"
  15. #include "TextureManager.h"
  16. #include "Window.h"
  17. #include "commands/Command.h"
  18. #include "utils/time.h"
  19. #include "UI.h"
  20. #define STB_IMAGE_IMPLEMENTATION
  21. #include "imgui/stb_image.h"
  22. bool UI::visible = false;
  23. unsigned int UI::fontTex;
  24. std::string UI::iniFilename;
  25. std::string UI::logFilename;
  26. bool UI::metaKeyIsActive = false;
  27. std::list<std::tuple<KeyboardButton, bool>> UI::keyboardEvents;
  28. std::list<std::tuple<unsigned int, unsigned int, KeyboardButton, bool>> UI::clickEvents;
  29. std::list<std::tuple<int, int, int, int>> UI::motionEvents;
  30. std::list<std::tuple<int, int>> UI::scrollEvents;
  31. int UI::initialize() {
  32. iniFilename = getRunTime().getBaseDir() + "/imgui.ini";
  33. logFilename = getRunTime().getBaseDir() + "/imgui_log.txt";
  34. ImGuiIO& io = ImGui::GetIO();
  35. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  36. io.DeltaTime = 1.0f / 60.0f;
  37. io.IniFilename = iniFilename.c_str();
  38. io.LogFilename = logFilename.c_str();
  39. io.KeyMap[ImGuiKey_Tab] = tabKey;
  40. io.KeyMap[ImGuiKey_LeftArrow] = leftKey;
  41. io.KeyMap[ImGuiKey_RightArrow] = rightKey;
  42. io.KeyMap[ImGuiKey_UpArrow] = upKey;
  43. io.KeyMap[ImGuiKey_DownArrow] = downKey;
  44. io.KeyMap[ImGuiKey_Home] = homeKey;
  45. io.KeyMap[ImGuiKey_End] = endKey;
  46. io.KeyMap[ImGuiKey_Delete] = delKey;
  47. io.KeyMap[ImGuiKey_Backspace] = backspaceKey;
  48. io.KeyMap[ImGuiKey_Enter] = enterKey;
  49. io.KeyMap[ImGuiKey_Escape] = escapeKey;
  50. io.KeyMap[ImGuiKey_A] = aKey;
  51. io.KeyMap[ImGuiKey_C] = cKey;
  52. io.KeyMap[ImGuiKey_V] = vKey;
  53. io.KeyMap[ImGuiKey_X] = xKey;
  54. io.KeyMap[ImGuiKey_Y] = yKey;
  55. io.KeyMap[ImGuiKey_Z] = zKey;
  56. io.RenderDrawListsFn = UI::renderImGui;
  57. // Load font texture
  58. //! \todo Use our own font subsystem instead of this?
  59. const void* png_data;
  60. unsigned int png_size;
  61. ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
  62. int tex_x, tex_y, tex_comp;
  63. void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
  64. (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
  65. //! \fixme TODO use proper texture slot
  66. fontTex = getTextureManager().loadBufferSlot((unsigned char*)tex_data,
  67. tex_x, tex_y, RGBA, 32, TextureManager::TextureStorage::SYSTEM, -1, false);
  68. stbi_image_free(tex_data);
  69. return 0;
  70. }
  71. void UI::eventsFinished() {
  72. ImGuiIO& io = ImGui::GetIO();
  73. io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
  74. static unsigned long lastTime = 0;
  75. io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
  76. lastTime = systemTimerGet();
  77. if (io.DeltaTime <= 0.0f)
  78. io.DeltaTime = 1.0f / 60.0f;
  79. ImGui::NewFrame();
  80. if (!visible) {
  81. while (!clickEvents.empty()) {
  82. auto i = clickEvents.front();
  83. if (getMenu().isVisible()) {
  84. getMenu().handleMouseClick(std::get<0>(i), std::get<1>(i),
  85. std::get<2>(i), std::get<3>(i));
  86. }
  87. clickEvents.pop_front();
  88. }
  89. while (!motionEvents.empty()) {
  90. auto i = motionEvents.front();
  91. if (!getMenu().isVisible()) {
  92. getGame().handleMouseMotion(std::get<0>(i), std::get<1>(i),
  93. std::get<2>(i), std::get<3>(i));
  94. }
  95. motionEvents.pop_front();
  96. }
  97. while (!scrollEvents.empty()) {
  98. auto i = scrollEvents.front();
  99. if (getMenu().isVisible()) {
  100. getMenu().handleMouseScroll(std::get<0>(i), std::get<1>(i));
  101. }
  102. scrollEvents.pop_front();
  103. }
  104. }
  105. while (!keyboardEvents.empty()) {
  106. auto i = keyboardEvents.front();
  107. if (!visible) {
  108. if (getMenu().isVisible()) {
  109. getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
  110. } else {
  111. for (int n = forwardAction; n < ActionEventCount; n++) {
  112. if (getRunTime().getKeyBinding((ActionEvents)n) == std::get<0>(i))
  113. getGame().handleAction((ActionEvents)n, !std::get<1>(i));
  114. }
  115. }
  116. }
  117. if (std::get<1>(i)) {
  118. if (!visible) {
  119. if (getRunTime().getKeyBinding(menuAction) == std::get<0>(i)) {
  120. getMenu().setVisible(!getMenu().isVisible());
  121. }
  122. }
  123. if ((!io.WantCaptureKeyboard) || (!visible)) {
  124. if (getRunTime().getKeyBinding(debugAction) == std::get<0>(i)) {
  125. if (!metaKeyIsActive)
  126. visible = !visible;
  127. }
  128. }
  129. }
  130. keyboardEvents.pop_front();
  131. }
  132. bool clicked = !clickEvents.empty();
  133. // Only already empty when !visible
  134. if (visible) {
  135. clickEvents.clear();
  136. motionEvents.clear();
  137. scrollEvents.clear();
  138. }
  139. if (visible && (
  140. ((!io.WantCaptureKeyboard) && io.KeysDown[escapeKey])
  141. || ((!io.WantCaptureMouse) && clicked)
  142. )) {
  143. visible = false;
  144. }
  145. if (getWindow().getTextInput() != visible)
  146. getWindow().setTextInput(visible);
  147. bool input = !(visible || getMenu().isVisible());
  148. if (getWindow().getMousegrab() != input)
  149. getWindow().setMousegrab(input);
  150. }
  151. void UI::display() {
  152. if (!visible)
  153. return;
  154. Console::display();
  155. if (ImGui::Begin("Engine")) {
  156. if (ImGui::CollapsingHeader("Info", NULL, true, true)) {
  157. ImGui::Text("Uptime: %lums", systemTimerGet());
  158. }
  159. if (ImGui::CollapsingHeader("GL Textures")) {
  160. static bool game = getGame().isLoaded();
  161. static int index = 0;
  162. static bool visible = false;
  163. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  164. ImGui::SliderInt("", &index, 0, getTextureManager().numTextures(
  165. game ? TextureManager::TextureStorage::GAME
  166. : TextureManager::TextureStorage::SYSTEM) - 1);
  167. ImGui::PopItemWidth();
  168. ImGui::SameLine();
  169. if (ImGui::Checkbox("Game", &game)) {
  170. if (game && !getGame().isLoaded())
  171. game = false;
  172. }
  173. ImGui::SameLine();
  174. if (ImGui::Button("Show")) {
  175. visible = true;
  176. }
  177. ImGui::SameLine();
  178. if (ImGui::Button("Clear")) {
  179. getRender().debugDisplayTexture();
  180. visible = false;
  181. }
  182. if (visible) {
  183. getRender().debugDisplayTexture(index,
  184. game ? TextureManager::TextureStorage::GAME
  185. : TextureManager::TextureStorage::SYSTEM,
  186. ImGui::GetWindowPos().x - ImGui::GetWindowWidth(),
  187. ImGui::GetWindowPos().y,
  188. ImGui::GetWindowWidth(), ImGui::GetWindowWidth());
  189. }
  190. }
  191. if (ImGui::CollapsingHeader("UI Help")) {
  192. ImGui::ShowUserGuide();
  193. }
  194. }
  195. ImGui::End();
  196. ImGui::Render();
  197. }
  198. void UI::shutdown() {
  199. ImGui::Shutdown();
  200. }
  201. void UI::handleKeyboard(KeyboardButton key, bool pressed) {
  202. ImGuiIO& io = ImGui::GetIO();
  203. io.KeysDown[key] = pressed;
  204. io.KeyCtrl = io.KeysDown[leftctrlKey] | io.KeysDown[rightctrlKey];
  205. io.KeyShift = io.KeysDown[leftshiftKey] | io.KeysDown[rightshiftKey];
  206. keyboardEvents.push_back(std::make_tuple(key, pressed));
  207. if ((key == leftguiKey) || (key == rightguiKey))
  208. metaKeyIsActive = pressed;
  209. }
  210. void UI::handleText(char* text, bool notFinished) {
  211. if (notFinished)
  212. return;
  213. ImGuiIO& io = ImGui::GetIO();
  214. while (*text != '\0') {
  215. io.AddInputCharacter(*text);
  216. text++;
  217. }
  218. }
  219. void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  220. ImGuiIO& io = ImGui::GetIO();
  221. io.MousePos = ImVec2((float)x, (float)y);
  222. if (button == leftmouseKey) {
  223. io.MouseDown[0] = !released;
  224. } else if (button == rightmouseKey) {
  225. io.MouseDown[1] = !released;
  226. } else if (button == middlemouseKey) {
  227. io.MouseDown[2] = !released;
  228. } else if (button == fourthmouseKey) {
  229. io.MouseDown[3] = !released;
  230. } else if (button == fifthmouseKey) {
  231. io.MouseDown[4] = !released;
  232. }
  233. clickEvents.push_back(std::make_tuple(x, y, button, released));
  234. }
  235. void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  236. ImGuiIO& io = ImGui::GetIO();
  237. io.MousePos = ImVec2((float)xabs, (float)yabs);
  238. motionEvents.push_back(std::make_tuple(xrel, yrel, xabs, yabs));
  239. }
  240. void UI::handleMouseScroll(int xrel, int yrel) {
  241. ImGuiIO& io = ImGui::GetIO();
  242. io.MouseWheel = (yrel != 0) ? yrel > 0 ? 1 : -1 : 0;
  243. scrollEvents.push_back(std::make_tuple(xrel, yrel));
  244. }
  245. void UI::setVisible(bool v) {
  246. visible = v;
  247. }
  248. bool UI::isVisible() {
  249. return visible;
  250. }
  251. void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
  252. if (cmd_lists_count == 0)
  253. return;
  254. getWindow().glEnter2D();
  255. glDisable(GL_DEPTH_TEST);
  256. glEnable(GL_SCISSOR_TEST);
  257. glEnableClientState(GL_VERTEX_ARRAY);
  258. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  259. glEnableClientState(GL_COLOR_ARRAY);
  260. // Setup texture
  261. getTextureManager().bindTextureId(fontTex, TextureManager::TextureStorage::SYSTEM);
  262. // Render command lists
  263. for (int n = 0; n < cmd_lists_count; n++) {
  264. const ImDrawList* cmd_list = cmd_lists[n];
  265. const unsigned char* vtx_buffer = (const unsigned char*)cmd_list->vtx_buffer.begin();
  266. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer));
  267. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + 8));
  268. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + 16));
  269. int vtx_offset = 0;
  270. const ImDrawCmd* pcmd_end = cmd_list->commands.end();
  271. for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) {
  272. glScissor((int)pcmd->clip_rect.x, (int)(ImGui::GetIO().DisplaySize.y - pcmd->clip_rect.w),
  273. (int)(pcmd->clip_rect.z - pcmd->clip_rect.x),
  274. (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
  275. glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
  276. vtx_offset += pcmd->vtx_count;
  277. }
  278. }
  279. glEnable(GL_DEPTH_TEST);
  280. glDisable(GL_SCISSOR_TEST);
  281. glDisableClientState(GL_VERTEX_ARRAY);
  282. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  283. glDisableClientState(GL_COLOR_ARRAY);
  284. getWindow().glExit2D();
  285. }