2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2015-01-01 18:23:34 +00:00
|
|
|
#include <algorithm>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <map>
|
2018-10-09 14:26:31 +00:00
|
|
|
#include <mutex>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <string>
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2019-07-29 03:04:50 +00:00
|
|
|
#include <imgui.h>
|
2018-10-09 14:26:31 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2019-06-07 22:25:32 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/Timer.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
|
|
|
|
namespace OSD
|
|
|
|
{
|
2018-10-09 14:26:31 +00:00
|
|
|
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
|
|
|
|
{
|
2019-07-29 02:42:42 +00:00
|
|
|
Message() = default;
|
2019-07-29 02:46:08 +00:00
|
|
|
Message(std::string text_, u32 timestamp_, u32 color_)
|
|
|
|
: text(std::move(text_)), timestamp(timestamp_), color(color_)
|
2018-10-09 14:26:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
std::string text;
|
2019-07-29 02:42:42 +00:00
|
|
|
u32 timestamp = 0;
|
|
|
|
u32 color = 0;
|
2018-10-09 14:26:31 +00:00
|
|
|
};
|
2016-02-02 15:35:27 +00:00
|
|
|
static std::multimap<MessageType, Message> s_messages;
|
|
|
|
static std::mutex s_messages_mutex;
|
|
|
|
|
2018-10-09 14:26:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-29 02:46:08 +00:00
|
|
|
void AddTypedMessage(MessageType type, std::string message, u32 ms, u32 rgba)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
2019-07-29 03:08:18 +00:00
|
|
|
std::lock_guard lock{s_messages_mutex};
|
2016-02-02 15:35:27 +00:00
|
|
|
s_messages.erase(type);
|
2019-07-29 02:46:08 +00:00
|
|
|
s_messages.emplace(type, Message(std::move(message), Common::Timer::GetTimeMs() + ms, rgba));
|
2016-02-02 15:35:27 +00:00
|
|
|
}
|
2013-04-24 13:21:54 +00:00
|
|
|
|
2019-07-29 02:46:08 +00:00
|
|
|
void AddMessage(std::string message, u32 ms, u32 rgba)
|
2016-02-02 15:35:27 +00:00
|
|
|
{
|
2019-07-29 03:08:18 +00:00
|
|
|
std::lock_guard lock{s_messages_mutex};
|
2016-02-02 15:35:27 +00:00
|
|
|
s_messages.emplace(MessageType::Typeless,
|
2019-07-29 02:46:08 +00:00
|
|
|
Message(std::move(message), Common::Timer::GetTimeMs() + ms, rgba));
|
2016-02-02 15:35:27 +00:00
|
|
|
}
|
2009-02-23 06:15:48 +00:00
|
|
|
|
|
|
|
void DrawMessages()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!SConfig::GetInstance().bOnScreenDisplayMessages)
|
|
|
|
return;
|
|
|
|
|
|
|
|
{
|
2019-07-29 03:08:18 +00:00
|
|
|
std::lock_guard lock{s_messages_mutex};
|
2016-02-02 15:35:27 +00:00
|
|
|
|
2018-10-09 14:26:31 +00:00
|
|
|
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;
|
2016-02-02 15:35:27 +00:00
|
|
|
|
|
|
|
auto it = s_messages.begin();
|
|
|
|
while (it != s_messages.end())
|
|
|
|
{
|
|
|
|
const Message& msg = it->second;
|
2018-10-09 14:26:31 +00:00
|
|
|
const int time_left = static_cast<int>(msg.timestamp - now);
|
|
|
|
current_y += DrawMessage(index++, msg, ImVec2(current_x, current_y), time_left);
|
2016-02-02 15:35:27 +00:00
|
|
|
|
|
|
|
if (time_left <= 0)
|
|
|
|
it = s_messages.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
2011-02-02 04:40:27 +00:00
|
|
|
void ClearMessages()
|
|
|
|
{
|
2019-07-29 03:08:18 +00:00
|
|
|
std::lock_guard lock{s_messages_mutex};
|
2016-02-02 15:35:27 +00:00
|
|
|
s_messages.clear();
|
2011-02-02 04:40:27 +00:00
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace OSD
|