2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-02-22 23:15:48 -07:00
|
|
|
|
2019-11-22 15:10:41 -07:00
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
|
|
|
|
2015-01-01 11:23:34 -07:00
|
|
|
#include <algorithm>
|
2020-09-12 09:56:47 -06:00
|
|
|
#include <atomic>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <map>
|
2018-10-09 08:26:31 -06:00
|
|
|
#include <mutex>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <string>
|
2009-02-22 23:15:48 -07:00
|
|
|
|
2019-11-22 15:10:41 -07:00
|
|
|
#include <fmt/format.h>
|
2019-07-28 21:04:50 -06:00
|
|
|
#include <imgui.h>
|
2018-10-09 08:26:31 -06:00
|
|
|
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-07-20 03:22:53 -06:00
|
|
|
#include "Common/Config/Config.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/Timer.h"
|
2009-02-22 23:15:48 -07:00
|
|
|
|
2020-07-20 03:22:53 -06:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
|
2023-10-16 17:42:22 -06:00
|
|
|
#include "VideoCommon/AbstractGfx.h"
|
|
|
|
#include "VideoCommon/AbstractTexture.h"
|
2024-05-12 22:15:21 -06:00
|
|
|
#include "VideoCommon/Assets/CustomTextureData.h"
|
2023-10-16 17:42:22 -06:00
|
|
|
#include "VideoCommon/TextureConfig.h"
|
|
|
|
|
2009-02-21 05:53:10 -07:00
|
|
|
namespace OSD
|
|
|
|
{
|
2020-07-01 13:47:51 -06: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.
|
|
|
|
constexpr float MESSAGE_FADE_TIME = 1000.f; // Ms to fade OSD messages at the end of their life.
|
|
|
|
constexpr float MESSAGE_DROP_TIME = 5000.f; // Ms to drop OSD messages that has yet to ever render.
|
2018-10-09 08:26:31 -06:00
|
|
|
|
2020-09-12 09:56:47 -06:00
|
|
|
static std::atomic<int> s_obscured_pixels_left = 0;
|
|
|
|
static std::atomic<int> s_obscured_pixels_top = 0;
|
|
|
|
|
2018-10-09 08:26:31 -06:00
|
|
|
struct Message
|
|
|
|
{
|
2019-07-28 20:42:42 -06:00
|
|
|
Message() = default;
|
2024-05-12 22:15:21 -06:00
|
|
|
Message(std::string text_, u32 duration_, u32 color_,
|
|
|
|
const VideoCommon::CustomTextureData::ArraySlice::Level* icon_ = nullptr)
|
|
|
|
: text(std::move(text_)), duration(duration_), color(color_), icon(icon_)
|
2018-10-09 08:26:31 -06:00
|
|
|
{
|
2022-07-18 12:48:20 -06:00
|
|
|
timer.Start();
|
2018-10-09 08:26:31 -06:00
|
|
|
}
|
2022-07-18 12:48:20 -06:00
|
|
|
s64 TimeRemaining() const { return duration - timer.ElapsedMs(); }
|
2018-10-09 08:26:31 -06:00
|
|
|
std::string text;
|
2022-07-18 12:48:20 -06:00
|
|
|
Common::Timer timer;
|
2020-07-01 13:47:51 -06:00
|
|
|
u32 duration = 0;
|
|
|
|
bool ever_drawn = false;
|
2023-10-16 17:42:22 -06:00
|
|
|
bool should_discard = false;
|
2019-07-28 20:42:42 -06:00
|
|
|
u32 color = 0;
|
2024-05-12 22:15:21 -06:00
|
|
|
const VideoCommon::CustomTextureData::ArraySlice::Level* icon;
|
2023-10-16 17:42:22 -06:00
|
|
|
std::unique_ptr<AbstractTexture> texture;
|
2018-10-09 08:26:31 -06:00
|
|
|
};
|
2016-02-02 08:35:27 -07:00
|
|
|
static std::multimap<MessageType, Message> s_messages;
|
|
|
|
static std::mutex s_messages_mutex;
|
|
|
|
|
2020-08-01 17:31:05 -06:00
|
|
|
static ImVec4 ARGBToImVec4(const u32 argb)
|
2018-10-09 08:26:31 -06:00
|
|
|
{
|
2020-08-01 17:31:05 -06:00
|
|
|
return ImVec4(static_cast<float>((argb >> 16) & 0xFF) / 255.0f,
|
|
|
|
static_cast<float>((argb >> 8) & 0xFF) / 255.0f,
|
|
|
|
static_cast<float>((argb >> 0) & 0xFF) / 255.0f,
|
|
|
|
static_cast<float>((argb >> 24) & 0xFF) / 255.0f);
|
2018-10-09 08:26:31 -06:00
|
|
|
}
|
|
|
|
|
2020-07-01 13:47:51 -06:00
|
|
|
static float DrawMessage(int index, Message& msg, const ImVec2& position, int time_left)
|
2018-10-09 08:26:31 -06:00
|
|
|
{
|
|
|
|
// 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.
|
2019-11-22 15:10:41 -07:00
|
|
|
const std::string window_name = fmt::format("osd_{}", index);
|
2018-10-09 08:26:31 -06:00
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
2020-07-01 13:47:51 -06:00
|
|
|
// Gradually fade old messages away (except in their first frame)
|
|
|
|
const float fade_time = std::max(std::min(MESSAGE_FADE_TIME, (float)msg.duration), 1.f);
|
|
|
|
const float alpha = std::clamp(time_left / fade_time, 0.f, 1.f);
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, msg.ever_drawn ? alpha : 1.0);
|
2018-10-09 08:26:31 -06:00
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2023-10-16 17:42:22 -06:00
|
|
|
if (msg.icon)
|
|
|
|
{
|
|
|
|
if (!msg.texture)
|
|
|
|
{
|
|
|
|
const u32 width = msg.icon->width;
|
|
|
|
const u32 height = msg.icon->height;
|
2023-12-09 18:00:11 -07:00
|
|
|
TextureConfig tex_config(width, height, 1, 1, 1, AbstractTextureFormat::RGBA8, 0,
|
|
|
|
AbstractTextureType::Texture_2DArray);
|
2023-10-16 17:42:22 -06:00
|
|
|
msg.texture = g_gfx->CreateTexture(tex_config);
|
|
|
|
if (msg.texture)
|
|
|
|
{
|
2024-05-12 22:15:21 -06:00
|
|
|
msg.texture->Load(0, width, height, width, msg.icon->data.data(),
|
2023-10-16 17:42:22 -06:00
|
|
|
sizeof(u32) * width * height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// don't try again next time
|
2024-05-12 22:15:21 -06:00
|
|
|
msg.icon = nullptr;
|
2023-10-16 17:42:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.texture)
|
|
|
|
{
|
|
|
|
ImGui::Image(msg.texture.get(), ImVec2(static_cast<float>(msg.icon->width),
|
|
|
|
static_cast<float>(msg.icon->height)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 08:26:31 -06:00
|
|
|
// Use %s in case message contains %.
|
2020-08-01 17:31:05 -06:00
|
|
|
ImGui::TextColored(ARGBToImVec4(msg.color), "%s", msg.text.c_str());
|
2018-10-09 08:26:31 -06:00
|
|
|
window_height =
|
|
|
|
ImGui::GetWindowSize().y + (WINDOW_PADDING * ImGui::GetIO().DisplayFramebufferScale.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::End();
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
|
2020-07-01 13:47:51 -06:00
|
|
|
msg.ever_drawn = true;
|
|
|
|
|
2018-10-09 08:26:31 -06:00
|
|
|
return window_height;
|
|
|
|
}
|
|
|
|
|
2023-10-16 17:42:22 -06:00
|
|
|
void AddTypedMessage(MessageType type, std::string message, u32 ms, u32 argb,
|
2024-05-12 22:15:21 -06:00
|
|
|
const VideoCommon::CustomTextureData::ArraySlice::Level* icon)
|
2009-02-21 05:53:10 -07:00
|
|
|
{
|
2019-07-28 21:08:18 -06:00
|
|
|
std::lock_guard lock{s_messages_mutex};
|
2023-10-16 17:42:22 -06:00
|
|
|
|
|
|
|
// A message may hold a reference to a texture that can only be destroyed on the video thread, so
|
|
|
|
// only mark the old typed message (if any) for removal. It will be discarded on the next call to
|
|
|
|
// DrawMessages().
|
|
|
|
auto range = s_messages.equal_range(type);
|
|
|
|
for (auto it = range.first; it != range.second; ++it)
|
|
|
|
it->second.should_discard = true;
|
|
|
|
|
|
|
|
s_messages.emplace(type, Message(std::move(message), ms, argb, std::move(icon)));
|
2016-02-02 08:35:27 -07:00
|
|
|
}
|
2013-04-24 07:21:54 -06:00
|
|
|
|
2024-05-12 22:15:21 -06:00
|
|
|
void AddMessage(std::string message, u32 ms, u32 argb,
|
|
|
|
const VideoCommon::CustomTextureData::ArraySlice::Level* icon)
|
2016-02-02 08:35:27 -07:00
|
|
|
{
|
2019-07-28 21:08:18 -06:00
|
|
|
std::lock_guard lock{s_messages_mutex};
|
2023-10-16 17:42:22 -06:00
|
|
|
s_messages.emplace(MessageType::Typeless, Message(std::move(message), ms, argb, std::move(icon)));
|
2016-02-02 08:35:27 -07:00
|
|
|
}
|
2009-02-22 23:15:48 -07:00
|
|
|
|
2009-02-21 05:53:10 -07:00
|
|
|
void DrawMessages()
|
|
|
|
{
|
2020-07-20 03:22:53 -06:00
|
|
|
const bool draw_messages = Config::Get(Config::MAIN_OSD_MESSAGES);
|
2020-09-12 09:56:47 -06:00
|
|
|
const float current_x =
|
|
|
|
LEFT_MARGIN * ImGui::GetIO().DisplayFramebufferScale.x + s_obscured_pixels_left;
|
|
|
|
float current_y = TOP_MARGIN * ImGui::GetIO().DisplayFramebufferScale.y + s_obscured_pixels_top;
|
2020-01-24 10:00:58 -07:00
|
|
|
int index = 0;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2020-01-24 10:00:58 -07:00
|
|
|
std::lock_guard lock{s_messages_mutex};
|
2016-02-02 08:35:27 -07:00
|
|
|
|
2020-01-24 10:00:58 -07:00
|
|
|
for (auto it = s_messages.begin(); it != s_messages.end();)
|
|
|
|
{
|
2020-07-01 13:47:51 -06:00
|
|
|
Message& msg = it->second;
|
2023-10-16 17:42:22 -06:00
|
|
|
if (msg.should_discard)
|
|
|
|
{
|
|
|
|
it = s_messages.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-18 12:48:20 -06:00
|
|
|
const s64 time_left = msg.TimeRemaining();
|
2016-02-02 08:35:27 -07:00
|
|
|
|
2020-07-01 13:47:51 -06:00
|
|
|
// Make sure we draw them at least once if they were printed with 0ms,
|
|
|
|
// unless enough time has expired, in that case, we drop them
|
|
|
|
if (time_left <= 0 && (msg.ever_drawn || -time_left >= MESSAGE_DROP_TIME))
|
2016-02-02 08:35:27 -07:00
|
|
|
{
|
2020-01-24 10:00:58 -07:00
|
|
|
it = s_messages.erase(it);
|
|
|
|
continue;
|
2016-02-02 08:35:27 -07:00
|
|
|
}
|
2020-01-24 10:00:58 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (draw_messages)
|
|
|
|
current_y += DrawMessage(index++, msg, ImVec2(current_x, current_y), time_left);
|
2009-02-21 05:53:10 -07:00
|
|
|
}
|
|
|
|
}
|
2009-02-22 23:15:48 -07:00
|
|
|
|
2011-02-01 21:40:27 -07:00
|
|
|
void ClearMessages()
|
|
|
|
{
|
2019-07-28 21:08:18 -06:00
|
|
|
std::lock_guard lock{s_messages_mutex};
|
2016-02-02 08:35:27 -07:00
|
|
|
s_messages.clear();
|
2011-02-01 21:40:27 -07:00
|
|
|
}
|
2020-09-12 09:56:47 -06:00
|
|
|
|
|
|
|
void SetObscuredPixelsLeft(int width)
|
|
|
|
{
|
|
|
|
s_obscured_pixels_left = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetObscuredPixelsTop(int height)
|
|
|
|
{
|
|
|
|
s_obscured_pixels_top = height;
|
|
|
|
}
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace OSD
|