/** ****************************************************************************** * Xenia : Xbox 360 Emulator Research Project * ****************************************************************************** * Copyright 2022 Ben Vanik. All rights reserved. * * Released under the BSD license - see LICENSE in the root for more details. * ****************************************************************************** */ #include "xenia/ui/imgui_drawer.h" #include #include #include "third_party/imgui/imgui.h" #include "xenia/base/assert.h" #include "xenia/base/clock.h" #include "xenia/base/logging.h" #include "xenia/base/math.h" #include "xenia/ui/imgui_dialog.h" #include "xenia/ui/imgui_notification.h" #include "xenia/ui/resources.h" #include "xenia/ui/ui_event.h" #include "xenia/ui/window.h" #define STB_IMAGE_IMPLEMENTATION #include "third_party/stb/stb_image.h" #if XE_PLATFORM_WIN32 #include #endif DEFINE_path( custom_font_path, "", "Allows user to load custom font and use it instead of default one.", "UI"); DEFINE_uint32(font_size, 14, "Allows user to set custom font size.", "UI"); UPDATE_from_uint32(font_size, 2024, 8, 31, 20, 12); namespace xe { namespace ui { // File: 'ProggyTiny.ttf' (35656 bytes) // Exported using binary_to_compressed_c.cpp const char kProggyTinyCompressedDataBase85[10950 + 1] = R"(7])#######LJg=:'/###[),##/l:$#Q6>##5[n42>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCm<]vf.r$<$u7k;hb';9C'mm?]XmKVeU2cD4Eo3R/[WB]b(MC;$jPfY.;h^`ItLw6Lh2TlS+f-s$o6Q#X14,MZ[Z##UE31#J&###Q-F%b>-nw'w++GM-]u)Nx0#,M[LH>#Zsvx+6O_^#l(FS7f`C_&E?g'&kcg-6Y,/;M#@2G`Bf%=(`5LF%fv$8#,+[0#veg>$EB&sQSSDgEKnIS7EM9>Z,KO_/78pQWqJE#$nt-4$F&###E`J&#uU'B#*9D6N;@;=-:U>hL&Y5<-%A9;-Y+Z&P^)9##._L&#awnk+ib*.-Z06X1>LcA#'rB#$o4ve6)fbA#kt72LN.72L=CG&#*iXZWt(F,>>#_03:)`(@2L@^x4Sj2B@PN#[xO8QkJNRR()N@#f.Mr#)t-L5FGMm8#&#/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>>#`bW-?4Ne_&6Ne_&6Ne_&lM4;-xCJcM6X;uM6X;uM(.a..^2TkL%oR(#;u.T%eAr%4tJ8&><1=GHZ_+m9/#H1F^R#SC#*N=BA9(D?v[UiFk-c/>tBc/>`9IL2a)Ph#WL](#O:Jr1Btu+#TH4.#a5C/#vS3rL<1^NMowY.##t6qLw`5oL_R#.#2HwqLUwXrLp/w+#ALx>-1xRu-'*IqL@KCsLB@]qL]cYs-dpao7Om#K)l?1?%;LuDNH@H>#/X-TI(;P>#,Gc>#0Su>#4`1?#8lC?#xL$#B.`$#F:r$#JF.%#NR@%#R_R%#Vke%#Zww%#_-4TR-&Mglr-k'MS.o?.5/sWel/wpEM0%3'/1)K^f1-d>G21&v(35>V`39V7A4=onx4A1OY5EI0;6Ibgr6M$HS7Q<)58UT`l8Ym@M9^/x.:bGXf:f`9G;jxp((F+;?,_br?0wBS@49$5A8QZlAQ#]V-kw:8.o9ro.sQRP/wj320%-ki0)EKJ1-^,,21vcc258DD39P%&4=i[]4A+=>5ECtu5I[TV6Mt587Q6mo7tB'DW-fJcMxUq4S=Gj(N=eC]OkKu=Yc/;ip3#T(j:6s7R`?U+rH#5PSpL7]bIFtIqmW:YYdQqFrhod(WEH1VdDMSrZ>vViBn_t.CTp;JCbMMrdku.Sek+f4ft(XfCsOFlfOuo7[&+T.q6jHPA$HHPB*QHPC0ZHPD6dHPD3Q-P_aQL2RemoveInputListener(this); if (internal_state_) { ImGui::SetCurrentContext(internal_state_); if (touch_pointer_id_ == TouchEvent::kPointerIDNone && ImGui::IsAnyMouseDown()) { window_->ReleaseMouse(); } } } if (internal_state_) { ImGui::DestroyContext(internal_state_); internal_state_ = nullptr; } } void ImGuiDrawer::AddDialog(ImGuiDialog* dialog) { assert_not_null(dialog); // Check if already added. if (std::find(dialogs_.cbegin(), dialogs_.cend(), dialog) != dialogs_.cend()) { return; } if (dialogs_.empty() && !IsDrawingDialogs()) { // First dialog added. !IsDrawingDialogs() is also checked because in a // situation of removing the only dialog, then adding a dialog, from within // a dialog's Draw function, re-registering the ImGuiDrawer may result in // ImGui being drawn multiple times in the current frame. window_->AddInputListener(this, z_order_); if (presenter_) { presenter_->AddUIDrawerFromUIThread(this, z_order_); } } dialogs_.push_back(dialog); } void ImGuiDrawer::RemoveDialog(ImGuiDialog* dialog) { assert_not_null(dialog); auto it = std::find(dialogs_.cbegin(), dialogs_.cend(), dialog); if (it == dialogs_.cend()) { return; } if (IsDrawingDialogs()) { // Actualize the next dialog index after the erasure from the vector. size_t existing_index = size_t(std::distance(dialogs_.cbegin(), it)); if (dialog_loop_next_index_ > existing_index) { --dialog_loop_next_index_; } } dialogs_.erase(it); DetachIfLastWindowRemoved(); } void ImGuiDrawer::AddNotification(ImGuiNotification* dialog) { assert_not_null(dialog); // Check if already added. if (std::find(notifications_.cbegin(), notifications_.cend(), dialog) != notifications_.cend()) { return; } if (notifications_.empty()) { if (presenter_) { presenter_->AddUIDrawerFromUIThread(this, z_order_); } } notifications_.push_back(dialog); } void ImGuiDrawer::RemoveNotification(ImGuiNotification* dialog) { assert_not_null(dialog); auto it = std::find(notifications_.cbegin(), notifications_.cend(), dialog); if (it == notifications_.cend()) { return; } notifications_.erase(it); DetachIfLastWindowRemoved(); } void ImGuiDrawer::Initialize() { // Setup ImGui internal state. // This will give us state we can swap to the ImGui globals when in use. internal_state_ = ImGui::CreateContext(); ImGui::SetCurrentContext(internal_state_); const float font_size = std::max((float)cvars::font_size, 8.f); const float title_font_size = font_size + 6.f; InitializeFonts(font_size); InitializeFonts(title_font_size); auto& style = ImGui::GetStyle(); style.ScrollbarRounding = 6.0f; style.WindowRounding = 6.0f; style.PopupRounding = 6.0f; style.TabRounding = 6.0f; style.Colors[ImGuiCol_Text] = ImVec4(0.89f, 0.90f, 0.90f, 1.00f); style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); style.Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.06f, 0.00f, 1.00f); style.Colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); style.Colors[ImGuiCol_Border] = ImVec4(0.00f, 0.35f, 0.00f, 1.00f); style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); style.Colors[ImGuiCol_FrameBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.30f); style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.90f, 0.80f, 0.80f, 0.40f); style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.90f, 0.65f, 0.65f, 0.45f); style.Colors[ImGuiCol_TitleBg] = ImVec4(0.00f, 0.40f, 0.00f, 1.00f); style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.33f, 0.00f, 1.00f); style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.16f, 0.65f, 0.00f, 1.00f); style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.00f, 0.35f, 0.00f, 1.00f); style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.00f, 0.40f, 0.11f, 0.59f); style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.00f, 0.68f, 0.00f, 0.68f); style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.00f, 1.00f, 0.15f, 0.62f); style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.00f, 0.91f, 0.09f, 0.40f); style.Colors[ImGuiCol_PopupBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.99f); style.Colors[ImGuiCol_CheckMark] = ImVec4(0.74f, 0.90f, 0.72f, 0.50f); style.Colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f); style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.34f, 0.75f, 0.11f, 1.00f); style.Colors[ImGuiCol_Button] = ImVec4(0.15f, 0.56f, 0.11f, 0.60f); style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.19f, 0.72f, 0.09f, 1.00f); style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.19f, 0.60f, 0.09f, 1.00f); style.Colors[ImGuiCol_Header] = ImVec4(0.00f, 0.40f, 0.00f, 0.71f); style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.00f, 0.60f, 0.26f, 0.80f); style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.00f, 0.75f, 0.00f, 0.80f); style.Colors[ImGuiCol_Separator] = ImVec4(0.00f, 0.35f, 0.00f, 1.00f); style.Colors[ImGuiCol_SeparatorHovered] = ImVec4(0.36f, 0.89f, 0.38f, 1.00f); style.Colors[ImGuiCol_SeparatorActive] = ImVec4(0.13f, 0.50f, 0.11f, 1.00f); style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f); style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.60f); style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.90f); style.Colors[ImGuiCol_Tab] = style.Colors[ImGuiCol_Button]; style.Colors[ImGuiCol_TabHovered] = style.Colors[ImGuiCol_ButtonHovered]; style.Colors[ImGuiCol_TabActive] = style.Colors[ImGuiCol_ButtonActive]; style.Colors[ImGuiCol_TabUnfocused] = style.Colors[ImGuiCol_FrameBg]; style.Colors[ImGuiCol_TabUnfocusedActive] = style.Colors[ImGuiCol_FrameBgHovered]; style.Colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 1.00f, 0.00f, 0.21f); style.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); frame_time_tick_frequency_ = double(Clock::QueryHostTickFrequency()); last_frame_time_ticks_ = Clock::QueryHostTickCount(); touch_pointer_id_ = TouchEvent::kPointerIDNone; reset_mouse_position_after_next_frame_ = false; } std::optional ImGuiDrawer::VirtualKeyToImGuiKey(VirtualKey vkey) { static const std::map map = { {ui::VirtualKey::kTab, ImGuiKey_Tab}, {ui::VirtualKey::kLeft, ImGuiKey_LeftArrow}, {ui::VirtualKey::kRight, ImGuiKey_RightArrow}, {ui::VirtualKey::kUp, ImGuiKey_UpArrow}, {ui::VirtualKey::kDown, ImGuiKey_DownArrow}, {ui::VirtualKey::kHome, ImGuiKey_Home}, {ui::VirtualKey::kEnd, ImGuiKey_End}, {ui::VirtualKey::kDelete, ImGuiKey_Delete}, {ui::VirtualKey::kBack, ImGuiKey_Backspace}, {ui::VirtualKey::kReturn, ImGuiKey_Enter}, {ui::VirtualKey::kEscape, ImGuiKey_Escape}, {ui::VirtualKey::kA, ImGuiKey_A}, {ui::VirtualKey::kC, ImGuiKey_C}, {ui::VirtualKey::kV, ImGuiKey_V}, {ui::VirtualKey::kX, ImGuiKey_X}, {ui::VirtualKey::kY, ImGuiKey_Y}, {ui::VirtualKey::kZ, ImGuiKey_Z}, }; if (auto search = map.find(vkey); search != map.end()) { return search->second; } else { return std::nullopt; } } std::map> ImGuiDrawer::LoadIcons( IconsData data) { std::map> icons_; if (!immediate_drawer_) { return icons_; } int width, height, channels; for (const auto& icon : data) { unsigned char* image_data = stbi_load_from_memory(icon.second.first, icon.second.second, &width, &height, &channels, STBI_rgb_alpha); icons_[icon.first] = (immediate_drawer_->CreateTexture( width, height, ImmediateTextureFilter::kLinear, true, reinterpret_cast(image_data))); } return icons_; } void ImGuiDrawer::SetupNotificationTextures() { if (!immediate_drawer_) { return; } // We're including 4th to include all visible for (uint8_t i = 0; i <= 4; i++) { if (notification_icons.size() < i) { break; } unsigned char* image_data; int width, height, channels; const auto user_icon = notification_icons.at(i); image_data = stbi_load_from_memory(user_icon.first, user_icon.second, &width, &height, &channels, STBI_rgb_alpha); notification_icon_textures_.push_back(immediate_drawer_->CreateTexture( width, height, ImmediateTextureFilter::kLinear, true, reinterpret_cast(image_data))); } } static const ImWchar font_glyph_ranges[] = { 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x0370, 0x03FF, // Greek 0x0400, 0x04FF, // Cyrillic 0x2000, 0x206F, // General Punctuation 0, }; bool ImGuiDrawer::LoadCustomFont(ImGuiIO& io, ImFontConfig& font_config, float font_size) { if (cvars::custom_font_path.empty()) { return false; } if (!std::filesystem::exists(cvars::custom_font_path)) { return false; } const std::string font_path = xe::path_to_utf8(cvars::custom_font_path); ImFont* font = io.Fonts->AddFontFromFileTTF(font_path.c_str(), font_size, &font_config, font_glyph_ranges); io.Fonts->Build(); if (!font->IsLoaded()) { XELOGE("Failed to load custom font: {}", font_path); io.Fonts->Clear(); return false; } return true; } bool ImGuiDrawer::LoadWindowsFont(ImGuiIO& io, ImFontConfig& font_config, float font_size) { #if XE_PLATFORM_WIN32 PWSTR fonts_dir; HRESULT result = SHGetKnownFolderPath(FOLDERID_Fonts, 0, NULL, &fonts_dir); if (FAILED(result)) { CoTaskMemFree(static_cast(fonts_dir)); XELOGW("Unable to find Windows fonts directory"); return false; } std::filesystem::path font_path = std::wstring(fonts_dir); font_path.append("tahoma.ttf"); if (!std::filesystem::exists(font_path)) { return false; } ImFont* font = io.Fonts->AddFontFromFileTTF(xe::path_to_utf8(font_path).c_str(), font_size, &font_config, font_glyph_ranges); io.Fonts->Build(); // Something went wrong while loading custom font. Probably corrupted. if (!font->IsLoaded()) { XELOGE("Failed to load custom font: {}", font_path); io.Fonts->Clear(); } CoTaskMemFree(static_cast(fonts_dir)); return true; #endif return false; } bool ImGuiDrawer::LoadJapaneseFont(ImGuiIO& io, float font_size) { // TODO(benvanik): jp font on other platforms? #if XE_PLATFORM_WIN32 PWSTR fonts_dir; HRESULT result = SHGetKnownFolderPath(FOLDERID_Fonts, 0, NULL, &fonts_dir); if (FAILED(result)) { XELOGW("Unable to find Windows fonts directory"); return false; } std::filesystem::path jp_font_path = std::wstring(fonts_dir); jp_font_path.append("msgothic.ttc"); if (std::filesystem::exists(jp_font_path)) { ImFontConfig jp_font_config; jp_font_config.MergeMode = true; jp_font_config.OversampleH = jp_font_config.OversampleV = 2; jp_font_config.PixelSnapH = true; jp_font_config.FontNo = 0; io.Fonts->AddFontFromFileTTF(xe::path_to_utf8(jp_font_path).c_str(), font_size, &jp_font_config, io.Fonts->GetGlyphRangesJapanese()); } else { XELOGW("Unable to load Japanese font; JP characters will be boxes"); } CoTaskMemFree(static_cast(fonts_dir)); return true; #endif return false; }; void ImGuiDrawer::InitializeFonts(const float font_size) { auto& io = ImGui::GetIO(); // TODO(gibbed): disable imgui.ini saving for now, // imgui assumes paths are char* so we can't throw a good path at it on // Windows. io.IniFilename = nullptr; ImFontConfig font_config; font_config.OversampleH = font_config.OversampleV = 2; font_config.PixelSnapH = true; bool is_font_loaded = LoadCustomFont(io, font_config, font_size); if (!is_font_loaded) { is_font_loaded = LoadWindowsFont(io, font_config, font_size); } if (io.Fonts->Fonts.empty()) { io.Fonts->AddFontFromMemoryCompressedBase85TTF( kProggyTinyCompressedDataBase85, font_size, &font_config, io.Fonts->GetGlyphRangesDefault()); } LoadJapaneseFont(io, font_size); } void ImGuiDrawer::SetupFontTexture() { if (font_texture_ || !immediate_drawer_) { return; } ImGuiIO& io = GetIO(); unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); font_texture_ = immediate_drawer_->CreateTexture( width, height, ImmediateTextureFilter::kLinear, true, reinterpret_cast(pixels)); io.Fonts->TexID = reinterpret_cast(font_texture_.get()); } void ImGuiDrawer::SetPresenter(Presenter* new_presenter) { if (presenter_) { if (presenter_ == new_presenter) { return; } if (!dialogs_.empty()) { presenter_->RemoveUIDrawerFromUIThread(this); } ImGuiIO& io = GetIO(); } presenter_ = new_presenter; if (presenter_) { if (!dialogs_.empty()) { presenter_->AddUIDrawerFromUIThread(this, z_order_); } } } void ImGuiDrawer::SetImmediateDrawer(ImmediateDrawer* new_immediate_drawer) { if (immediate_drawer_ == new_immediate_drawer) { return; } if (immediate_drawer_) { GetIO().Fonts->TexID = static_cast(nullptr); font_texture_.reset(); notification_icon_textures_.clear(); } immediate_drawer_ = new_immediate_drawer; if (immediate_drawer_) { SetupFontTexture(); SetupNotificationTextures(); // Load locked achievement icon. unsigned char* image_data; int width, height, channels; image_data = stbi_load_from_memory(locked_achievement_icon.first, locked_achievement_icon.second, &width, &height, &channels, STBI_rgb_alpha); locked_achievement_icon_ = immediate_drawer_->CreateTexture( width, height, ImmediateTextureFilter::kLinear, true, reinterpret_cast(image_data)); } } void ImGuiDrawer::Draw(UIDrawContext& ui_draw_context) { // Drawing of anything is initiated by the presenter. assert_not_null(presenter_); if (!immediate_drawer_) { // A presenter has been attached, but an immediate drawer hasn't been // attached yet. return; } if (dialogs_.empty() && notifications_.empty()) { return; } ImGui::SetCurrentContext(internal_state_); ImGuiIO& io = ImGui::GetIO(); uint64_t current_frame_time_ticks = Clock::QueryHostTickCount(); io.DeltaTime = float(double(current_frame_time_ticks - last_frame_time_ticks_) / frame_time_tick_frequency_); if (!(io.DeltaTime > 0.0f) || current_frame_time_ticks < last_frame_time_ticks_) { // For safety as Dear ImGui doesn't allow non-positive DeltaTime. Using the // same default value as in the official samples. io.DeltaTime = 1.0f / 60.0f; } last_frame_time_ticks_ = current_frame_time_ticks; float physical_to_logical = float(window_->GetMediumDpi()) / float(window_->GetDpi()); io.DisplaySize.x = window_->GetActualPhysicalWidth() * physical_to_logical; io.DisplaySize.y = window_->GetActualPhysicalHeight() * physical_to_logical; ImGui::NewFrame(); assert_true(!IsDrawingDialogs()); dialog_loop_next_index_ = 0; while (dialog_loop_next_index_ < dialogs_.size()) { dialogs_[dialog_loop_next_index_++]->Draw(); } dialog_loop_next_index_ = SIZE_MAX; if (!notifications_.empty()) { std::vector guest_notifications = {}; std::vector host_notifications = {}; std::copy_if(notifications_.cbegin(), notifications_.cend(), std::back_inserter(guest_notifications), [](ui::ImGuiNotification* notification) { return notification->GetNotificationType() == NotificationType::Guest; }); std::copy_if(notifications_.cbegin(), notifications_.cend(), std::back_inserter(host_notifications), [](ui::ImGuiNotification* notification) { return notification->GetNotificationType() == NotificationType::Host; }); if (guest_notifications.size() > 0) { guest_notifications.at(0)->Draw(); } if (host_notifications.size() > 0) { host_notifications.at(0)->Draw(); if (host_notifications.size() > 1) { host_notifications.at(0)->SetDeletionPending(); } } } ImGui::Render(); ImDrawData* draw_data = ImGui::GetDrawData(); if (draw_data) { RenderDrawLists(draw_data, ui_draw_context); } if (reset_mouse_position_after_next_frame_) { reset_mouse_position_after_next_frame_ = false; io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); } // Detaching is deferred if the last dialog is removed during drawing, perform // it now if needed. DetachIfLastWindowRemoved(); if (!dialogs_.empty() || !notifications_.empty()) { // Repaint (and handle input) continuously if still active. presenter_->RequestUIPaintFromUIThread(); } } void ImGuiDrawer::ClearDialogs() { size_t dialog_loop = 0; while (dialog_loop < dialogs_.size()) { RemoveDialog(dialogs_[dialog_loop++]); } } void ImGuiDrawer::RenderDrawLists(ImDrawData* data, UIDrawContext& ui_draw_context) { ImGuiIO& io = ImGui::GetIO(); immediate_drawer_->Begin(ui_draw_context, io.DisplaySize.x, io.DisplaySize.y); for (int i = 0; i < data->CmdListsCount; ++i) { const auto cmd_list = data->CmdLists[i]; ImmediateDrawBatch batch; batch.vertices = reinterpret_cast(cmd_list->VtxBuffer.Data); batch.vertex_count = cmd_list->VtxBuffer.size(); batch.indices = cmd_list->IdxBuffer.Data; batch.index_count = cmd_list->IdxBuffer.size(); immediate_drawer_->BeginDrawBatch(batch); for (int j = 0; j < cmd_list->CmdBuffer.size(); ++j) { const auto& cmd = cmd_list->CmdBuffer[j]; ImmediateDraw draw; draw.primitive_type = ImmediatePrimitiveType::kTriangles; draw.count = cmd.ElemCount; draw.index_offset = cmd.IdxOffset; draw.texture = reinterpret_cast(cmd.TextureId); draw.scissor = true; draw.scissor_left = cmd.ClipRect.x; draw.scissor_top = cmd.ClipRect.y; draw.scissor_right = cmd.ClipRect.z; draw.scissor_bottom = cmd.ClipRect.w; immediate_drawer_->Draw(draw); } immediate_drawer_->EndDrawBatch(); } immediate_drawer_->End(); } ImGuiIO& ImGuiDrawer::GetIO() { ImGui::SetCurrentContext(internal_state_); return ImGui::GetIO(); } void ImGuiDrawer::OnKeyDown(KeyEvent& e) { OnKey(e, true); } void ImGuiDrawer::OnKeyUp(KeyEvent& e) { OnKey(e, false); } void ImGuiDrawer::OnKeyChar(KeyEvent& e) { auto& io = GetIO(); // TODO(Triang3l): Accept the Unicode character. unsigned int character = static_cast(e.virtual_key()); if (character > 0 && character < 0x10000) { io.AddInputCharacter(character); e.set_handled(true); } } void ImGuiDrawer::OnMouseDown(MouseEvent& e) { SwitchToPhysicalMouseAndUpdateMousePosition(e); auto& io = GetIO(); int button = -1; switch (e.button()) { case xe::ui::MouseEvent::Button::kLeft: { button = 0; break; } case xe::ui::MouseEvent::Button::kRight: { button = 1; break; } default: { // Ignored. break; } } if (button >= 0 && button < std::size(io.MouseDown)) { if (!io.MouseDown[button]) { if (!ImGui::IsAnyMouseDown()) { window_->CaptureMouse(); } io.MouseDown[button] = true; } } } void ImGuiDrawer::OnMouseMove(MouseEvent& e) { SwitchToPhysicalMouseAndUpdateMousePosition(e); } void ImGuiDrawer::OnMouseUp(MouseEvent& e) { SwitchToPhysicalMouseAndUpdateMousePosition(e); auto& io = GetIO(); int button = -1; switch (e.button()) { case xe::ui::MouseEvent::Button::kLeft: { button = 0; break; } case xe::ui::MouseEvent::Button::kRight: { button = 1; break; } default: { // Ignored. break; } } if (button >= 0 && button < std::size(io.MouseDown)) { if (io.MouseDown[button]) { io.MouseDown[button] = false; if (!ImGui::IsAnyMouseDown()) { window_->ReleaseMouse(); } } } } void ImGuiDrawer::OnMouseWheel(MouseEvent& e) { SwitchToPhysicalMouseAndUpdateMousePosition(e); auto& io = GetIO(); io.MouseWheel += float(e.scroll_y()) / float(MouseEvent::kScrollPerDetent); } void ImGuiDrawer::OnTouchEvent(TouchEvent& e) { auto& io = GetIO(); TouchEvent::Action action = e.action(); uint32_t pointer_id = e.pointer_id(); if (action == TouchEvent::Action::kDown) { // The latest pointer needs to be controlling the ImGui mouse. if (touch_pointer_id_ == TouchEvent::kPointerIDNone) { // Switching from the mouse to touch input. if (ImGui::IsAnyMouseDown()) { std::memset(io.MouseDown, 0, sizeof(io.MouseDown)); window_->ReleaseMouse(); } } touch_pointer_id_ = pointer_id; } else { if (pointer_id != touch_pointer_id_) { return; } } UpdateMousePosition(e.x(), e.y()); if (action == TouchEvent::Action::kUp || action == TouchEvent::Action::kCancel) { io.MouseDown[0] = false; touch_pointer_id_ = TouchEvent::kPointerIDNone; // Make sure that after a touch, the ImGui mouse isn't hovering over // anything. reset_mouse_position_after_next_frame_ = true; } else { io.MouseDown[0] = true; reset_mouse_position_after_next_frame_ = false; } } void ImGuiDrawer::ClearInput() { auto& io = GetIO(); if (touch_pointer_id_ == TouchEvent::kPointerIDNone && ImGui::IsAnyMouseDown()) { window_->ReleaseMouse(); } io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); std::memset(io.MouseDown, 0, sizeof(io.MouseDown)); io.ClearInputKeys(); io.ClearInputCharacters(); touch_pointer_id_ = TouchEvent::kPointerIDNone; reset_mouse_position_after_next_frame_ = false; } void ImGuiDrawer::OnKey(KeyEvent& e, bool is_down) { auto& io = GetIO(); const VirtualKey virtual_key = e.virtual_key(); if (auto imGuiKey = VirtualKeyToImGuiKey(virtual_key); imGuiKey) { io.AddKeyEvent(*imGuiKey, is_down); } switch (virtual_key) { case VirtualKey::kShift: io.KeyShift = is_down; break; case VirtualKey::kControl: io.KeyCtrl = is_down; break; case VirtualKey::kMenu: // FIXME(Triang3l): Doesn't work in xenia-ui-window-demo. io.KeyAlt = is_down; break; case VirtualKey::kLWin: io.KeySuper = is_down; break; default: break; } } void ImGuiDrawer::UpdateMousePosition(float x, float y) { auto& io = GetIO(); float physical_to_logical = float(window_->GetMediumDpi()) / float(window_->GetDpi()); io.MousePos.x = x * physical_to_logical; io.MousePos.y = y * physical_to_logical; } void ImGuiDrawer::SwitchToPhysicalMouseAndUpdateMousePosition( const MouseEvent& e) { if (touch_pointer_id_ != TouchEvent::kPointerIDNone) { touch_pointer_id_ = TouchEvent::kPointerIDNone; auto& io = GetIO(); std::memset(io.MouseDown, 0, sizeof(io.MouseDown)); // Nothing needs to be done regarding CaptureMouse and ReleaseMouse - all // buttons as well as mouse capture have been released when switching to // touch input, the mouse is never captured during touch input, and now // resetting to no buttons down (therefore not capturing). } reset_mouse_position_after_next_frame_ = false; UpdateMousePosition(float(e.x()), float(e.y())); } void ImGuiDrawer::DetachIfLastWindowRemoved() { // IsDrawingDialogs() is also checked because in a situation of removing the // only dialog, then adding a dialog, from within a dialog's Draw function, // re-registering the ImGuiDrawer may result in ImGui being drawn multiple // times in the current frame. if (!dialogs_.empty() || !notifications_.empty() || IsDrawingDialogs()) { return; } if (presenter_) { presenter_->RemoveUIDrawerFromUIThread(this); } window_->RemoveInputListener(this); // Clear all input since no input will be received anymore, and when the // drawer becomes active again, it'd have an outdated input state otherwise // which will be persistent until new events actualize individual input // properties. ClearInput(); } } // namespace ui } // namespace xe