diff options
| author | spycrab <spycrab@users.noreply.github.com> | 2019-01-25 15:08:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-25 15:08:40 +0100 |
| commit | 6962d5bc5217562b0200a47968e013e9c820feeb (patch) | |
| tree | 2b6a1de08745ae7c5afe1c492446547c3edb8c30 /Source/Core/VideoCommon/OnScreenDisplay.cpp | |
| parent | e060b133e36f5bb0bda909186084d0d933e9c538 (diff) | |
| parent | 774480ba234c788d2973cf6a3ccd1cd4c3fcc673 (diff) | |
Merge pull request #7478 from stenzek/imgui
Replace raster font with dear imgui
Diffstat (limited to 'Source/Core/VideoCommon/OnScreenDisplay.cpp')
| -rw-r--r-- | Source/Core/VideoCommon/OnScreenDisplay.cpp | 100 |
1 files changed, 65 insertions, 35 deletions
diff --git a/Source/Core/VideoCommon/OnScreenDisplay.cpp b/Source/Core/VideoCommon/OnScreenDisplay.cpp index 62997cd150..d6f9b38a3e 100644 --- a/Source/Core/VideoCommon/OnScreenDisplay.cpp +++ b/Source/Core/VideoCommon/OnScreenDisplay.cpp @@ -5,22 +5,79 @@ #include <algorithm> #include <list> #include <map> +#include <mutex> #include <string> +#include "imgui.h" + #include "Common/CommonTypes.h" #include "Common/Timer.h" #include "Core/ConfigManager.h" #include "VideoCommon/OnScreenDisplay.h" -#include "VideoCommon/RenderBase.h" namespace OSD { -static std::multimap<CallbackType, Callback> s_callbacks; +constexpr float LEFT_MARGIN = 10.0f; // Pixels to the left of OSD messages. +constexpr float TOP_MARGIN = 10.0f; // Pixels above the first OSD message. +constexpr float WINDOW_PADDING = 4.0f; // Pixels between subsequent OSD messages. + +struct Message +{ + Message() {} + Message(const std::string& text_, u32 timestamp_, u32 color_) + : text(text_), timestamp(timestamp_), color(color_) + { + } + std::string text; + u32 timestamp; + u32 color; +}; static std::multimap<MessageType, Message> s_messages; static std::mutex s_messages_mutex; +static ImVec4 RGBAToImVec4(const u32 rgba) +{ + return ImVec4(static_cast<float>((rgba >> 16) & 0xFF) / 255.0f, + static_cast<float>((rgba >> 8) & 0xFF) / 255.0f, + static_cast<float>((rgba >> 0) & 0xFF) / 255.0f, + static_cast<float>((rgba >> 24) & 0xFF) / 255.0f); +} + +static float DrawMessage(int index, const Message& msg, const ImVec2& position, int time_left) +{ + // We have to provide a window name, and these shouldn't be duplicated. + // So instead, we generate a name based on the number of messages drawn. + const std::string window_name = StringFromFormat("osd_%d", index); + + // The size must be reset, otherwise the length of old messages could influence new ones. + ImGui::SetNextWindowPos(position); + ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f)); + + // Gradually fade old messages away. + const float alpha = std::min(1.0f, std::max(0.0f, time_left / 1024.0f)); + ImGui::PushStyleVar(ImGuiStyleVar_Alpha, alpha); + + float window_height = 0.0f; + if (ImGui::Begin(window_name.c_str(), nullptr, + ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings | + ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoNav | + ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoFocusOnAppearing)) + { + // Use %s in case message contains %. + ImGui::TextColored(RGBAToImVec4(msg.color), "%s", msg.text.c_str()); + window_height = + ImGui::GetWindowSize().y + (WINDOW_PADDING * ImGui::GetIO().DisplayFramebufferScale.y); + } + + ImGui::End(); + ImGui::PopStyleVar(); + + return window_height; +} + void AddTypedMessage(MessageType type, const std::string& message, u32 ms, u32 rgba) { std::lock_guard<std::mutex> lock(s_messages_mutex); @@ -35,14 +92,6 @@ void AddMessage(const std::string& message, u32 ms, u32 rgba) Message(message, Common::Timer::GetTimeMs() + ms, rgba)); } -void DrawMessage(const Message& msg, int top, int left, int time_left) -{ - float alpha = std::min(1.0f, std::max(0.0f, time_left / 1024.0f)); - u32 color = (msg.m_rgba & 0xFFFFFF) | ((u32)((msg.m_rgba >> 24) * alpha) << 24); - - g_renderer->RenderText(msg.m_str, left, top, color); -} - void DrawMessages() { if (!SConfig::GetInstance().bOnScreenDisplayMessages) @@ -51,21 +100,22 @@ void DrawMessages() { std::lock_guard<std::mutex> lock(s_messages_mutex); - u32 now = Common::Timer::GetTimeMs(); - int left = 20, top = 35; + const u32 now = Common::Timer::GetTimeMs(); + float current_x = LEFT_MARGIN * ImGui::GetIO().DisplayFramebufferScale.x; + float current_y = TOP_MARGIN * ImGui::GetIO().DisplayFramebufferScale.y; + int index = 0; auto it = s_messages.begin(); while (it != s_messages.end()) { const Message& msg = it->second; - int time_left = (int)(msg.m_timestamp - now); - DrawMessage(msg, top, left, time_left); + const int time_left = static_cast<int>(msg.timestamp - now); + current_y += DrawMessage(index++, msg, ImVec2(current_x, current_y), time_left); if (time_left <= 0) it = s_messages.erase(it); else ++it; - top += 15; } } } @@ -75,24 +125,4 @@ void ClearMessages() std::lock_guard<std::mutex> lock(s_messages_mutex); s_messages.clear(); } - -// On-Screen Display Callbacks -void AddCallback(CallbackType type, Callback cb) -{ - s_callbacks.emplace(type, cb); -} - -void DoCallbacks(CallbackType type) -{ - auto it_bounds = s_callbacks.equal_range(type); - for (auto it = it_bounds.first; it != it_bounds.second; ++it) - { - it->second(); - } - - // Wipe all callbacks on shutdown - if (type == CallbackType::Shutdown) - s_callbacks.clear(); } - -} // namespace |
