diff options
| author | spycrab <spycrab@users.noreply.github.com> | 2019-03-17 01:09:06 +0100 |
|---|---|---|
| committer | spycrab <spycrab@users.noreply.github.com> | 2019-03-23 20:13:24 +0100 |
| commit | 7cfb626a834cdc065e56e529bbed81df755a7c09 (patch) | |
| tree | e547c145eed01129174d122852960f9f29b51c6f /Source/Core/VideoCommon/NetPlayChatUI.cpp | |
| parent | 8cfbbbe9dc49186b40d5c2a3edc0e7e6d7059191 (diff) | |
Add imgui-based Netplay Chat
Diffstat (limited to 'Source/Core/VideoCommon/NetPlayChatUI.cpp')
| -rw-r--r-- | Source/Core/VideoCommon/NetPlayChatUI.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/Source/Core/VideoCommon/NetPlayChatUI.cpp b/Source/Core/VideoCommon/NetPlayChatUI.cpp new file mode 100644 index 0000000000..2388d94b91 --- /dev/null +++ b/Source/Core/VideoCommon/NetPlayChatUI.cpp @@ -0,0 +1,99 @@ +// Copyright 2019 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#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) +{ + m_message_callback = std::move(callback); +} + +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) + { + ImGui::SetScrollHere(1.0f); + m_scroll_to_bottom = false; + } + + m_is_scrolled_to_bottom = ImGui::GetScrollY() == ImGui::GetScrollMaxY(); + + ImGui::EndChild(); + + ImGui::Spacing(); + + ImGui::PushItemWidth(-50.0f * scale); + + if (ImGui::InputText("", m_message_buf, IM_ARRAYSIZE(m_message_buf), + ImGuiInputTextFlags_EnterReturnsTrue)) + { + SendMessage(); + ImGui::SetKeyboardFocusHere(-1); + } + + ImGui::PopItemWidth(); + + ImGui::SameLine(); + + if (ImGui::Button("Send")) + SendMessage(); + + ImGui::End(); +} + +void NetPlayChatUI::AppendChat(const std::string& message, NetPlayChatUI::Color color) +{ + if (m_messages.size() > MAX_BACKLOG_SIZE) + m_messages.pop_front(); + + m_messages.push_back({message, color}); + + // 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'; + } +} |
