HostInterface: Move OSD messages to base class

This commit is contained in:
Connor McLaughlin
2019-12-01 21:33:56 +10:00
parent 0a6b913536
commit 299ee05cd9
4 changed files with 76 additions and 75 deletions

View File

@ -7,6 +7,7 @@
#include "host_display.h"
#include "system.h"
#include <filesystem>
#include <imgui.h>
Log_SetChannel(HostInterface);
#ifdef _WIN32
@ -82,7 +83,6 @@ void HostInterface::ResetSystem()
AddOSDMessage("System reset.");
}
void HostInterface::DestroySystem()
{
m_system.reset();
@ -100,6 +100,63 @@ void HostInterface::ReportMessage(const char* message)
Log_InfoPrintf(message);
}
void HostInterface::AddOSDMessage(const char* message, float duration /*= 2.0f*/)
{
OSDMessage msg;
msg.text = message;
msg.duration = duration;
std::unique_lock<std::mutex> lock(m_osd_messages_lock);
m_osd_messages.push_back(std::move(msg));
}
void HostInterface::DrawOSDMessages()
{
constexpr ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoNav |
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoFocusOnAppearing;
std::unique_lock<std::mutex> lock(m_osd_messages_lock);
const float scale = ImGui::GetIO().DisplayFramebufferScale.x;
auto iter = m_osd_messages.begin();
float position_x = 10.0f * scale;
float position_y = (10.0f + (m_settings.display_fullscreen ? 0.0f : 20.0f)) * scale;
u32 index = 0;
while (iter != m_osd_messages.end())
{
const OSDMessage& msg = *iter;
const double time = msg.time.GetTimeSeconds();
const float time_remaining = static_cast<float>(msg.duration - time);
if (time_remaining <= 0.0f)
{
iter = m_osd_messages.erase(iter);
continue;
}
const float opacity = std::min(time_remaining, 1.0f);
ImGui::SetNextWindowPos(ImVec2(position_x, position_y));
ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f));
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, opacity);
if (ImGui::Begin(SmallString::FromFormat("osd_%u", index++), nullptr, window_flags))
{
ImGui::TextUnformatted(msg.text.c_str());
position_y += ImGui::GetWindowSize().y + (4.0f * scale);
}
ImGui::End();
ImGui::PopStyleVar();
++iter;
}
}
void HostInterface::ClearImGuiFocus()
{
ImGui::SetWindowFocus(nullptr);
}
std::optional<std::vector<u8>> HostInterface::GetBIOSImage(ConsoleRegion region)
{
// Try the other default filenames in the directory of the configured BIOS.

View File

@ -3,8 +3,11 @@
#include "settings.h"
#include "types.h"
#include <chrono>
#include <deque>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <vector>
class AudioStream;
@ -38,8 +41,8 @@ public:
virtual void ReportError(const char* message);
virtual void ReportMessage(const char* message);
// Adds OSD messages, duration is in seconds.
virtual void AddOSDMessage(const char* message, float duration = 2.0f) = 0;
/// Adds OSD messages, duration is in seconds.
void AddOSDMessage(const char* message, float duration = 2.0f);
/// Loads the BIOS image for the specified region.
virtual std::optional<std::vector<u8>> GetBIOSImage(ConsoleRegion region);
@ -50,6 +53,13 @@ public:
protected:
using ThrottleClock = std::chrono::steady_clock;
struct OSDMessage
{
std::string text;
Timer time;
float duration;
};
/// Connects controllers. TODO: Clean this up later...
virtual void ConnectControllers();
@ -58,6 +68,9 @@ protected:
void UpdateSpeedLimiterState();
void DrawOSDMessages();
void ClearImGuiFocus();
void UpdatePerformanceCounters();
void ResetPerformanceCounters();
@ -79,6 +92,9 @@ protected:
u32 m_last_global_tick_counter = 0;
Timer m_fps_timer;
std::deque<OSDMessage> m_osd_messages;
std::mutex m_osd_messages_lock;
bool m_paused = false;
bool m_speed_limiter_temp_disabled = false;
bool m_speed_limiter_enabled = false;