Merge pull request #7903 from spycrab/imgui_np_chat

Add imgui-based Netplay Chat
This commit is contained in:
spycrab
2019-03-23 21:11:33 +01:00
committed by GitHub
17 changed files with 209 additions and 27 deletions

View File

@ -22,6 +22,7 @@ add_library(videocommon
ImageWrite.cpp
IndexGenerator.cpp
LightingShaderGen.cpp
NetPlayChatUI.cpp
OnScreenDisplay.cpp
OpcodeDecoding.cpp
PerfQueryBase.cpp

View File

@ -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';
}
}

View File

@ -0,0 +1,35 @@
// 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;
};
extern std::unique_ptr<NetPlayChatUI> g_netplay_chat_ui;

View File

@ -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"
@ -523,6 +524,9 @@ void Renderer::DrawDebugText()
if (g_ActiveConfig.bOverlayStats)
Statistics::Display();
if (g_ActiveConfig.bShowNetPlayMessages && g_netplay_chat_ui)
g_netplay_chat_ui->Display();
if (g_ActiveConfig.bOverlayProjStats)
Statistics::DisplayProj();
}
@ -979,8 +983,7 @@ bool Renderer::InitializeImGui()
pconfig.vertex_format = m_imgui_vertex_format.get();
pconfig.vertex_shader = vertex_shader.get();
pconfig.pixel_shader = pixel_shader.get();
pconfig.rasterization_state =
RenderState::GetCullBackFaceRasterizationState(PrimitiveType::Triangles);
pconfig.rasterization_state = RenderState::GetNoCullRasterizationState(PrimitiveType::Triangles);
pconfig.depth_state = RenderState::GetNoDepthTestingDepthState();
pconfig.blending_state = RenderState::GetNoBlendingBlendState();
pconfig.blending_state.blendenable = true;

View File

@ -41,6 +41,7 @@ class AbstractShader;
class AbstractTexture;
class AbstractStagingTexture;
class NativeVertexFormat;
class NetPlayChatUI;
struct TextureConfig;
struct ComputePipelineConfig;
struct AbstractPipelineConfig;
@ -384,6 +385,8 @@ private:
// 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;

View File

@ -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" />
@ -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" />

View File

@ -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" />
@ -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" />