Skip to content

Commit

Permalink
Add imgui-based Netplay Chat
Browse files Browse the repository at this point in the history
  • Loading branch information
spycrab committed Mar 23, 2019
1 parent 8cfbbbe commit f3bdb6b
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Source/Core/DolphinQt/Host.cpp
Expand Up @@ -8,6 +8,8 @@
#include <QApplication>
#include <QProgressDialog>

#include <imgui.h>

#include "Common/Common.h"

#include "Core/ConfigManager.h"
Expand Down Expand Up @@ -149,7 +151,8 @@ void Host_RequestRenderWindowSize(int w, int h)

bool Host_UINeedsControllerState()
{
return Settings::Instance().IsControllerStateNeeded();
return Settings::Instance().IsControllerStateNeeded() ||
(ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureKeyboard);
}

bool Host_UIBlocksControllerState()
Expand Down
44 changes: 23 additions & 21 deletions Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp
Expand Up @@ -54,6 +54,8 @@
#include "UICommon/GameFile.h"
#include "UICommon/UICommon.h"

#include "VideoCommon/NetPlayChatUI.h"
#include "VideoCommon/RenderBase.h"
#include "VideoCommon/VideoConfig.h"

NetPlayDialog::NetPlayDialog(QWidget* parent)
Expand Down Expand Up @@ -355,6 +357,15 @@ void NetPlayDialog::ConnectWidgets()
connect(m_sync_all_wii_saves_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings);
}

void NetPlayDialog::SendMessage(const std::string& msg)
{
Settings::Instance().GetNetPlayClient()->SendChatMessage(msg);

DisplayMessage(QStringLiteral("%1: %2").arg(QString::fromStdString(m_nickname).toHtmlEscaped(),
QString::fromStdString(msg).toHtmlEscaped()),
"#1d6ed8");
}

void NetPlayDialog::OnChat()
{
QueueOnObject(this, [this] {
Expand All @@ -363,12 +374,9 @@ void NetPlayDialog::OnChat()
if (msg.empty())
return;

Settings::Instance().GetNetPlayClient()->SendChatMessage(msg);
m_chat_type_edit->clear();

DisplayMessage(QStringLiteral("%1: %2").arg(QString::fromStdString(m_nickname).toHtmlEscaped(),
QString::fromStdString(msg).toHtmlEscaped()),
"#1d6ed8");
SendMessage(msg);
});
}

Expand Down Expand Up @@ -762,23 +770,12 @@ void NetPlayDialog::DisplayMessage(const QString& msg, const std::string& color,
QStringLiteral("<font color='%1'>%2</font>").arg(QString::fromStdString(color), msg));
});

if (g_ActiveConfig.bShowNetPlayMessages && Core::IsRunning())
{
u32 osd_color;

// Convert the color string to a OSD color
if (color == "red")
osd_color = OSD::Color::RED;
else if (color == "cyan")
osd_color = OSD::Color::CYAN;
else if (color == "green")
osd_color = OSD::Color::GREEN;
else
osd_color = OSD::Color::YELLOW;
QColor c(QString::fromStdString(color));

OSD::AddTypedMessage(OSD::MessageType::NetPlayBuffer, msg.toStdString(), OSD::Duration::NORMAL,
osd_color);
}
if (g_ActiveConfig.bShowNetPlayMessages && Core::IsRunning())
g_renderer->GetNetPlayChatUI()->AppendChat(msg.toStdString(), {static_cast<float>(c.redF()),
static_cast<float>(c.greenF()),
static_cast<float>(c.blueF())});
}

void NetPlayDialog::AppendChat(const std::string& msg)
Expand Down Expand Up @@ -827,8 +824,12 @@ void NetPlayDialog::OnMsgStartGame()
{
DisplayMessage(tr("Started game"), "green");

g_renderer->GetNetPlayChatUI() =
std::make_unique<NetPlayChatUI>([this](const std::string& message) { SendMessage(message); });

QueueOnObject(this, [this] {
auto client = Settings::Instance().GetNetPlayClient();

if (client)
client->StartGame(FindGame(m_current_game));
UpdateDiscordPresence();
Expand All @@ -837,6 +838,7 @@ void NetPlayDialog::OnMsgStartGame()

void NetPlayDialog::OnMsgStopGame()
{
g_renderer->GetNetPlayChatUI().reset();
QueueOnObject(this, [this] { UpdateDiscordPresence(); });
}

Expand All @@ -856,7 +858,7 @@ void NetPlayDialog::OnPadBufferChanged(u32 buffer)
DisplayMessage(m_host_input_authority && !IsHosting() ?
tr("Max buffer size changed to %1").arg(buffer) :
tr("Buffer size changed to %1").arg(buffer),
"");
"yellow");

m_buffer_size = static_cast<int>(buffer);
}
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/DolphinQt/NetPlay/NetPlayDialog.h
Expand Up @@ -95,6 +95,8 @@ class NetPlayDialog : public QDialog, public NetPlay::NetPlayUI

void SetGame(const QString& game_path);

void SendMessage(const std::string& message);

// Chat
QGroupBox* m_chat_box;
QTextEdit* m_chat_edit;
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/DolphinQt/Settings.cpp
Expand Up @@ -27,6 +27,9 @@
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/InputConfig.h"

#include "VideoCommon/NetPlayChatUI.h"
#include "VideoCommon/RenderBase.h"

Settings::Settings()
{
qRegisterMetaType<Core::State>();
Expand Down Expand Up @@ -293,6 +296,7 @@ std::shared_ptr<NetPlay::NetPlayClient> Settings::GetNetPlayClient()
void Settings::ResetNetPlayClient(NetPlay::NetPlayClient* client)
{
m_client.reset(client);
g_renderer->GetNetPlayChatUI().reset();
}

std::shared_ptr<NetPlay::NetPlayServer> Settings::GetNetPlayServer()
Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoCommon/CMakeLists.txt
Expand Up @@ -22,6 +22,7 @@ add_library(videocommon
ImageWrite.cpp
IndexGenerator.cpp
LightingShaderGen.cpp
NetPlayChatUI.cpp
OnScreenDisplay.cpp
OpcodeDecoding.cpp
PerfQueryBase.cpp
Expand Down
97 changes: 97 additions & 0 deletions Source/Core/VideoCommon/NetPlayChatUI.cpp
@@ -0,0 +1,97 @@
// 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;

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';
}
}
33 changes: 33 additions & 0 deletions Source/Core/VideoCommon/NetPlayChatUI.h
@@ -0,0 +1,33 @@
// Copyright 2019 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <array>
#include <deque>
#include <functional>
#include <memory>
#include <string>
#include <utility>

class NetPlayChatUI
{
public:
explicit NetPlayChatUI(std::function<void(const std::string&)> callback);
~NetPlayChatUI() = default;

using Color = std::array<float, 3>;

void Display();
void AppendChat(const std::string& message, Color color);
void SendMessage();

private:
char m_message_buf[256] = {};
bool m_scroll_to_bottom = false;
bool m_is_scrolled_to_bottom = true;

std::deque<std::pair<std::string, Color>> m_messages;
std::function<void(const std::string&)> m_message_callback;
};
9 changes: 9 additions & 0 deletions Source/Core/VideoCommon/RenderBase.cpp
Expand Up @@ -57,6 +57,7 @@
#include "VideoCommon/FPSCounter.h"
#include "VideoCommon/FramebufferManager.h"
#include "VideoCommon/ImageWrite.h"
#include "VideoCommon/NetPlayChatUI.h"
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/PixelEngine.h"
#include "VideoCommon/PixelShaderManager.h"
Expand Down Expand Up @@ -523,6 +524,9 @@ void Renderer::DrawDebugText()
if (g_ActiveConfig.bOverlayStats)
Statistics::Display();

if (g_ActiveConfig.bShowNetPlayMessages && m_netplay_chat_ui)
m_netplay_chat_ui->Display();

if (g_ActiveConfig.bOverlayProjStats)
Statistics::DisplayProj();
}
Expand Down Expand Up @@ -1642,3 +1646,8 @@ std::unique_ptr<VideoCommon::AsyncShaderCompiler> Renderer::CreateAsyncShaderCom
{
return std::make_unique<VideoCommon::AsyncShaderCompiler>();
}

std::unique_ptr<NetPlayChatUI>& Renderer::GetNetPlayChatUI()
{
return m_netplay_chat_ui;
}
5 changes: 5 additions & 0 deletions Source/Core/VideoCommon/RenderBase.h
Expand Up @@ -41,6 +41,7 @@ class AbstractShader;
class AbstractTexture;
class AbstractStagingTexture;
class NativeVertexFormat;
class NetPlayChatUI;
struct TextureConfig;
struct ComputePipelineConfig;
struct AbstractPipelineConfig;
Expand Down Expand Up @@ -242,6 +243,8 @@ class Renderer
void BeginUIFrame();
void EndUIFrame();

std::unique_ptr<NetPlayChatUI>& GetNetPlayChatUI();

protected:
// Bitmask containing information about which configuration has changed for the backend.
enum ConfigChangeBits : u32
Expand Down Expand Up @@ -384,6 +387,8 @@ class Renderer

// Ensures all encoded frames have been written to the output file.
void FinishFrameData();

std::unique_ptr<NetPlayChatUI> m_netplay_chat_ui;
};

extern std::unique_ptr<Renderer> g_renderer;
2 changes: 2 additions & 0 deletions Source/Core/VideoCommon/VideoCommon.vcxproj
Expand Up @@ -57,6 +57,7 @@
<ClCompile Include="HiresTextures_DDSLoader.cpp" />
<ClCompile Include="ImageWrite.cpp" />
<ClCompile Include="IndexGenerator.cpp" />
<ClCompile Include="NetPlayChatUI.cpp" />
<ClCompile Include="OnScreenDisplay.cpp" />
<ClCompile Include="OpcodeDecoding.cpp" />
<ClCompile Include="PerfQueryBase.cpp" />
Expand Down Expand Up @@ -120,6 +121,7 @@
<ClInclude Include="FramebufferManager.h" />
<ClInclude Include="FramebufferShaderGen.h" />
<ClInclude Include="GXPipelineTypes.h" />
<ClInclude Include="NetPlayChatUI.h" />
<ClInclude Include="ShaderCache.h" />
<ClInclude Include="UberShaderCommon.h" />
<ClInclude Include="UberShaderPixel.h" />
Expand Down
6 changes: 6 additions & 0 deletions Source/Core/VideoCommon/VideoCommon.vcxproj.filters
Expand Up @@ -197,6 +197,9 @@
<ClCompile Include="FramebufferManager.cpp">
<Filter>Base</Filter>
</ClCompile>
<ClCompile Include="NetPlayChatUI.cpp">
<Filter>Util</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CommandProcessor.h" />
Expand Down Expand Up @@ -386,6 +389,9 @@
<ClInclude Include="TextureConverterShaderGen.h">
<Filter>Shader Generators</Filter>
</ClInclude>
<ClInclude Include="NetPlayChatUI.h">
<Filter>Util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
Expand Down

0 comments on commit f3bdb6b

Please sign in to comment.