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>
|
2009-02-23 06:15:48 +00:00
|
|
|
#include <list>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.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"
|
|
|
|
#include "VideoCommon/RenderBase.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
|
|
|
|
namespace OSD
|
|
|
|
{
|
2016-02-02 15:35:27 +00:00
|
|
|
static std::multimap<CallbackType, Callback> s_callbacks;
|
|
|
|
static std::multimap<MessageType, Message> s_messages;
|
|
|
|
static std::mutex s_messages_mutex;
|
|
|
|
|
2016-10-04 02:37:18 +00:00
|
|
|
static std::string CleanMessage(std::string message)
|
|
|
|
{
|
|
|
|
std::replace_if(message.begin(), message.end(), [](char c) { return !std::isprint(c); }, '?');
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2016-02-02 15:35:27 +00:00
|
|
|
void AddTypedMessage(MessageType type, const std::string& message, u32 ms, u32 rgba)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
2016-02-02 15:35:27 +00:00
|
|
|
std::lock_guard<std::mutex> lock(s_messages_mutex);
|
|
|
|
s_messages.erase(type);
|
2016-10-04 02:37:18 +00:00
|
|
|
s_messages.emplace(type, Message(CleanMessage(message), Common::Timer::GetTimeMs() + ms, rgba));
|
2016-02-02 15:35:27 +00:00
|
|
|
}
|
2013-04-24 13:21:54 +00:00
|
|
|
|
2016-02-02 15:35:27 +00:00
|
|
|
void AddMessage(const std::string& message, u32 ms, u32 rgba)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(s_messages_mutex);
|
|
|
|
s_messages.emplace(MessageType::Typeless,
|
2016-10-04 02:37:18 +00:00
|
|
|
Message(CleanMessage(message), Common::Timer::GetTimeMs() + ms, rgba));
|
2016-02-02 15:35:27 +00:00
|
|
|
}
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2016-02-02 15:35:27 +00:00
|
|
|
void DrawMessage(const Message& msg, int top, int left, int time_left)
|
2013-08-23 23:41:17 +00:00
|
|
|
{
|
2016-02-02 15:35:27 +00:00
|
|
|
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);
|
2013-08-23 23:41:17 +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;
|
|
|
|
|
|
|
|
{
|
2016-02-02 15:35:27 +00:00
|
|
|
std::lock_guard<std::mutex> lock(s_messages_mutex);
|
|
|
|
|
|
|
|
u32 now = Common::Timer::GetTimeMs();
|
2016-10-04 02:37:18 +00:00
|
|
|
int left = 20, top = 20;
|
2016-02-02 15:35:27 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (time_left <= 0)
|
|
|
|
it = s_messages.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
top += 15;
|
|
|
|
}
|
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()
|
|
|
|
{
|
2016-02-02 15:35:27 +00:00
|
|
|
std::lock_guard<std::mutex> lock(s_messages_mutex);
|
|
|
|
s_messages.clear();
|
2011-02-02 04:40:27 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 05:48:53 +00:00
|
|
|
// On-Screen Display Callbacks
|
2013-08-24 00:13:54 +00:00
|
|
|
void AddCallback(CallbackType type, Callback cb)
|
2013-04-13 05:48:53 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
s_callbacks.emplace(type, cb);
|
2013-04-13 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-24 00:13:54 +00:00
|
|
|
void DoCallbacks(CallbackType type)
|
2013-04-13 05:48:53 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
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();
|
2013-04-13 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2009-02-23 06:15:48 +00:00
|
|
|
} // namespace
|