LumixEngine/src/editor/log_ui.cpp

208 lines
4.2 KiB
C++
Raw Normal View History

2015-09-14 18:03:45 +02:00
#include "log_ui.h"
#include "core/log.h"
2015-12-23 19:52:09 +01:00
#include "imgui/imgui.h"
2015-09-14 18:03:45 +02:00
LogUI::LogUI(Lumix::IAllocator& allocator)
: m_allocator(allocator)
, m_messages(allocator)
, m_current_tab(0)
, m_notifications(allocator)
, m_last_uid(1)
, m_guard(false)
2015-09-14 18:03:45 +02:00
{
2015-09-19 01:18:52 +02:00
m_is_opened = false;
2015-09-14 18:03:45 +02:00
Lumix::g_log_info.getCallback().bind<LogUI, &LogUI::onInfo>(this);
Lumix::g_log_error.getCallback().bind<LogUI, &LogUI::onError>(this);
Lumix::g_log_warning.getCallback().bind<LogUI, &LogUI::onWarning>(this);
for (int i = 0; i < Count; ++i)
{
m_new_message_count[i] = 0;
m_messages.emplace(allocator);
}
}
LogUI::~LogUI()
{
Lumix::g_log_info.getCallback().unbind<LogUI, &LogUI::onInfo>(this);
Lumix::g_log_error.getCallback().unbind<LogUI, &LogUI::onError>(this);
Lumix::g_log_warning.getCallback().unbind<LogUI, &LogUI::onWarning>(this);
}
void LogUI::setNotificationTime(int uid, float time)
{
for (auto& notif : m_notifications)
{
if (notif.uid == uid)
{
notif.time = time;
break;
}
}
}
int LogUI::addNotification(const char* text)
{
m_move_notifications_to_front = true;
auto& notif = m_notifications.emplace(m_allocator);
notif.time = 10.0f;
notif.message = text;
notif.uid = ++m_last_uid;
return notif.uid;
}
2015-09-14 18:03:45 +02:00
void LogUI::push(Type type, const char* message)
{
Lumix::MT::SpinLock lock(m_guard);
2015-09-14 18:03:45 +02:00
++m_new_message_count[type];
m_messages[type].push(Lumix::string(message, m_allocator));
if (type == Error)
2015-09-14 18:03:45 +02:00
{
addNotification(message);
2015-09-14 18:03:45 +02:00
}
}
void LogUI::onInfo(const char* system, const char* message)
{
2016-02-14 16:49:37 +01:00
push(Info, message);
2015-09-14 18:03:45 +02:00
}
void LogUI::onWarning(const char* system, const char* message)
{
2016-02-14 16:49:37 +01:00
push(Warning, message);
2015-09-14 18:03:45 +02:00
}
void LogUI::onError(const char* system, const char* message)
{
2016-02-14 16:49:37 +01:00
push(Error, message);
2015-09-14 18:03:45 +02:00
}
void fillLabel(char* output, int max_size, const char* label, int count)
{
Lumix::copyString(output, max_size, label);
Lumix::catString(output, max_size, "(");
2015-11-12 17:26:49 +01:00
int len = Lumix::stringLength(output);
2015-09-14 18:03:45 +02:00
Lumix::toCString(count, output + len, max_size - len);
Lumix::catString(output, max_size, ")###");
Lumix::catString(output, max_size, label);
2015-09-14 18:03:45 +02:00
}
void LogUI::showNotifications()
{
m_are_notifications_hovered = false;
2015-09-14 18:03:45 +02:00
if (m_notifications.empty()) return;
ImGui::SetNextWindowPos(ImVec2(10, 30));
2015-09-14 18:03:45 +02:00
bool opened;
if (!ImGui::Begin("Notifications",
2016-01-28 14:17:22 +01:00
&opened,
ImVec2(200, 0),
1.0f,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_ShowBorders))
2015-09-14 18:03:45 +02:00
{
ImGui::End();
2015-09-14 18:03:45 +02:00
return;
}
m_are_notifications_hovered = ImGui::IsWindowHovered();
if (ImGui::Button("Close")) m_notifications.clear();
if (m_move_notifications_to_front) ImGui::BringToFront();
m_move_notifications_to_front = false;
2015-09-14 18:03:45 +02:00
for (int i = 0; i < m_notifications.size(); ++i)
{
if (i > 0) ImGui::Separator();
ImGui::Text(m_notifications[i].message.c_str());
2015-09-14 18:03:45 +02:00
}
ImGui::End();
2015-09-14 18:03:45 +02:00
}
void LogUI::update(float time_delta)
{
if (m_are_notifications_hovered) return;
2015-09-14 18:03:45 +02:00
for (int i = 0; i < m_notifications.size(); ++i)
{
m_notifications[i].time -= time_delta;
if (m_notifications[i].time < 0)
{
m_notifications.erase(i);
--i;
}
}
}
2016-02-12 22:31:05 +01:00
int LogUI::getUnreadErrorCount() const
{
return m_new_message_count[Error];
}
2015-10-03 01:14:38 +02:00
void LogUI::onGUI()
2015-09-14 18:03:45 +02:00
{
Lumix::MT::SpinLock lock(m_guard);
2015-09-25 23:03:10 +02:00
showNotifications();
2016-01-01 20:31:34 +01:00
if (ImGui::BeginDock("Log", &m_is_opened))
2015-09-14 18:03:45 +02:00
{
2016-02-14 16:49:37 +01:00
const char* labels[] = { "Info", "Warning", "Error" };
2015-09-14 18:03:45 +02:00
for (int i = 0; i < Lumix::lengthOf(labels); ++i)
{
char label[40];
2015-09-14 18:03:45 +02:00
fillLabel(label, sizeof(label), labels[i], m_new_message_count[i]);
if(i > 0) ImGui::SameLine();
if (ImGui::Button(label))
2015-09-14 18:03:45 +02:00
{
m_current_tab = i;
m_new_message_count[i] = 0;
}
}
2015-09-14 18:03:45 +02:00
auto* messages = &m_messages[m_current_tab];
if (ImGui::Button("Clear"))
2015-09-14 18:03:45 +02:00
{
for (int i = 0; i < m_messages.size(); ++i)
{
m_messages[i].clear();
m_new_message_count[i] = 0;
}
}
ImGui::SameLine();
2015-09-14 18:03:45 +02:00
char filter[128] = "";
ImGui::InputText("Filter", filter, sizeof(filter));
2015-09-14 18:03:45 +02:00
if (ImGui::BeginChild("log_messages"))
2015-09-14 18:03:45 +02:00
{
2015-10-15 20:53:13 +02:00
for (int i = 0; i < messages->size(); ++i)
2015-09-14 18:03:45 +02:00
{
2015-10-15 20:53:13 +02:00
const char* msg = (*messages)[i].c_str();
if (filter[0] == '\0' || strstr(msg, filter) != nullptr)
{
ImGui::Text(msg);
2015-10-15 20:53:13 +02:00
}
2015-09-14 18:03:45 +02:00
}
}
ImGui::EndChild();
2015-09-14 18:03:45 +02:00
}
2016-01-01 20:31:34 +01:00
ImGui::EndDock();
2015-09-14 18:03:45 +02:00
}