2019-03-17 00:09:06 +00:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-03-17 00:09:06 +00:00
|
|
|
|
|
|
|
#include "VideoCommon/NetPlayChatUI.h"
|
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
constexpr float DEFAULT_WINDOW_WIDTH = 220.0f;
|
|
|
|
constexpr float DEFAULT_WINDOW_HEIGHT = 400.0f;
|
|
|
|
|
|
|
|
constexpr size_t MAX_BACKLOG_SIZE = 100;
|
|
|
|
|
|
|
|
std::unique_ptr<NetPlayChatUI> g_netplay_chat_ui;
|
|
|
|
|
|
|
|
NetPlayChatUI::NetPlayChatUI(std::function<void(const std::string&)> callback)
|
2019-05-29 10:09:47 +00:00
|
|
|
: m_message_callback{std::move(callback)}
|
2019-03-17 00:09:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-29 10:18:55 +00:00
|
|
|
NetPlayChatUI::~NetPlayChatUI() = default;
|
|
|
|
|
2019-03-17 00:09:06 +00:00
|
|
|
void NetPlayChatUI::Display()
|
|
|
|
{
|
|
|
|
const float scale = ImGui::GetIO().DisplayFramebufferScale.x;
|
|
|
|
|
|
|
|
ImGui::SetNextWindowPos(ImVec2(10.0f * scale, 10.0f * scale), ImGuiCond_FirstUseEver);
|
|
|
|
ImGui::SetNextWindowSizeConstraints(
|
|
|
|
ImVec2(DEFAULT_WINDOW_WIDTH * scale, DEFAULT_WINDOW_HEIGHT * scale),
|
|
|
|
ImGui::GetIO().DisplaySize);
|
|
|
|
|
|
|
|
if (!ImGui::Begin("Chat", nullptr, ImGuiWindowFlags_None))
|
|
|
|
{
|
|
|
|
ImGui::End();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::BeginChild("Scrolling", ImVec2(0, -30 * scale), true, ImGuiWindowFlags_None);
|
|
|
|
for (const auto& msg : m_messages)
|
|
|
|
{
|
|
|
|
auto c = msg.second;
|
|
|
|
ImGui::PushTextWrapPos(0.0f);
|
|
|
|
ImGui::TextColored(ImVec4(c[0], c[1], c[2], 1.0f), "%s", msg.first.c_str());
|
|
|
|
ImGui::PopTextWrapPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_scroll_to_bottom)
|
|
|
|
{
|
2021-10-26 21:34:39 +00:00
|
|
|
ImGui::SetScrollHereY(1.0f);
|
2019-03-17 00:09:06 +00:00
|
|
|
m_scroll_to_bottom = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_is_scrolled_to_bottom = ImGui::GetScrollY() == ImGui::GetScrollMaxY();
|
|
|
|
|
|
|
|
ImGui::EndChild();
|
|
|
|
|
|
|
|
ImGui::Spacing();
|
|
|
|
|
|
|
|
ImGui::PushItemWidth(-50.0f * scale);
|
|
|
|
|
2023-07-29 05:27:07 +00:00
|
|
|
if (ImGui::InputText("##NetplayMessageBuffer", m_message_buf, IM_ARRAYSIZE(m_message_buf),
|
2019-03-17 00:09:06 +00:00
|
|
|
ImGuiInputTextFlags_EnterReturnsTrue))
|
|
|
|
{
|
|
|
|
SendMessage();
|
2019-03-24 14:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_activate)
|
|
|
|
{
|
2019-03-17 00:09:06 +00:00
|
|
|
ImGui::SetKeyboardFocusHere(-1);
|
2019-03-24 14:57:36 +00:00
|
|
|
m_activate = false;
|
2019-03-17 00:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
if (ImGui::Button("Send"))
|
|
|
|
SendMessage();
|
|
|
|
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
2019-05-29 10:12:27 +00:00
|
|
|
void NetPlayChatUI::AppendChat(std::string message, Color color)
|
2019-03-17 00:09:06 +00:00
|
|
|
{
|
|
|
|
if (m_messages.size() > MAX_BACKLOG_SIZE)
|
|
|
|
m_messages.pop_front();
|
|
|
|
|
2019-05-29 10:12:27 +00:00
|
|
|
m_messages.emplace_back(std::move(message), color);
|
2019-03-17 00:09:06 +00:00
|
|
|
|
|
|
|
// Only scroll to bottom, if we were at the bottom previously
|
|
|
|
if (m_is_scrolled_to_bottom)
|
|
|
|
m_scroll_to_bottom = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetPlayChatUI::SendMessage()
|
|
|
|
{
|
|
|
|
// Check whether the input field is empty
|
|
|
|
if (m_message_buf[0] != '\0')
|
|
|
|
{
|
|
|
|
if (m_message_callback)
|
|
|
|
m_message_callback(m_message_buf);
|
|
|
|
|
|
|
|
// 'Empty' the buffer
|
|
|
|
m_message_buf[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 14:57:36 +00:00
|
|
|
|
|
|
|
void NetPlayChatUI::Activate()
|
|
|
|
{
|
|
|
|
if (ImGui::IsItemFocused())
|
2019-05-29 10:14:40 +00:00
|
|
|
ImGui::SetWindowFocus(nullptr);
|
2019-03-24 14:57:36 +00:00
|
|
|
else
|
|
|
|
m_activate = true;
|
|
|
|
}
|