From 6c393f9ff420c2a4185122cfb1bed0fbb2bcfad9 Mon Sep 17 00:00:00 2001 From: Techjar Date: Tue, 2 Apr 2019 17:13:42 -0400 Subject: [PATCH] Add imgui golf mode overlay --- Source/Core/Core/Config/NetplaySettings.cpp | 2 + Source/Core/Core/Config/NetplaySettings.h | 1 + Source/Core/Core/NetPlayClient.cpp | 29 ++++++-- Source/Core/Core/NetPlayClient.h | 7 +- .../Core/DolphinQt/NetPlay/NetPlayDialog.cpp | 13 ++++ Source/Core/DolphinQt/NetPlay/NetPlayDialog.h | 1 + Source/Core/DolphinQt/Settings.cpp | 2 + Source/Core/VideoCommon/CMakeLists.txt | 1 + Source/Core/VideoCommon/NetPlayGolfUI.cpp | 66 +++++++++++++++++++ Source/Core/VideoCommon/NetPlayGolfUI.h | 27 ++++++++ Source/Core/VideoCommon/RenderBase.cpp | 5 ++ Source/Core/VideoCommon/VideoCommon.vcxproj | 4 +- .../VideoCommon/VideoCommon.vcxproj.filters | 8 ++- 13 files changed, 156 insertions(+), 10 deletions(-) create mode 100644 Source/Core/VideoCommon/NetPlayGolfUI.cpp create mode 100644 Source/Core/VideoCommon/NetPlayGolfUI.h diff --git a/Source/Core/Core/Config/NetplaySettings.cpp b/Source/Core/Core/Config/NetplaySettings.cpp index 66bc826b31..1f01452bcb 100644 --- a/Source/Core/Core/Config/NetplaySettings.cpp +++ b/Source/Core/Core/Config/NetplaySettings.cpp @@ -56,5 +56,7 @@ const ConfigInfo NETPLAY_HOST_INPUT_AUTHORITY{{System::Main, "NetPlay", "H const ConfigInfo NETPLAY_SYNC_ALL_WII_SAVES{{System::Main, "NetPlay", "SyncAllWiiSaves"}, false}; const ConfigInfo NETPLAY_GOLF_MODE{{System::Main, "NetPlay", "GolfMode"}, false}; +const ConfigInfo NETPLAY_GOLF_MODE_OVERLAY{{System::Main, "NetPlay", "GolfModeOverlay"}, + true}; } // namespace Config diff --git a/Source/Core/Core/Config/NetplaySettings.h b/Source/Core/Core/Config/NetplaySettings.h index 672b6c1aff..adbac875e0 100644 --- a/Source/Core/Core/Config/NetplaySettings.h +++ b/Source/Core/Core/Config/NetplaySettings.h @@ -46,5 +46,6 @@ extern const ConfigInfo NETPLAY_STRICT_SETTINGS_SYNC; extern const ConfigInfo NETPLAY_HOST_INPUT_AUTHORITY; extern const ConfigInfo NETPLAY_SYNC_ALL_WII_SAVES; extern const ConfigInfo NETPLAY_GOLF_MODE; +extern const ConfigInfo NETPLAY_GOLF_MODE_OVERLAY; } // namespace Config diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index 2fe9998248..a2e6dcc562 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -2108,15 +2108,19 @@ void NetPlayClient::RequestGolfControl() RequestGolfControl(m_local_player->pid); } +// called from ---GUI--- thread +std::string NetPlayClient::GetCurrentGolfer() +{ + std::lock_guard lkp(m_crit.players); + if (m_players.count(m_current_golfer)) + return m_players[m_current_golfer].name; + return ""; +} + // called from ---GUI--- thread bool NetPlayClient::LocalPlayerHasControllerMapped() const { - const auto mapping_matches_player_id = [this](const PlayerId& mapping) { - return mapping == m_local_player->pid; - }; - - return std::any_of(m_pad_map.begin(), m_pad_map.end(), mapping_matches_player_id) || - std::any_of(m_wiimote_map.begin(), m_wiimote_map.end(), mapping_matches_player_id); + return PlayerHasControllerMapped(m_local_player->pid); } bool NetPlayClient::IsFirstInGamePad(int ingame_pad) const @@ -2169,6 +2173,19 @@ int NetPlayClient::LocalPadToInGamePad(int local_pad) const return ingame_pad; } +bool NetPlayClient::PlayerHasControllerMapped(const PlayerId pid) const +{ + const auto mapping_matches_player_id = [pid](const PlayerId& mapping) { return mapping == pid; }; + + return std::any_of(m_pad_map.begin(), m_pad_map.end(), mapping_matches_player_id) || + std::any_of(m_wiimote_map.begin(), m_wiimote_map.end(), mapping_matches_player_id); +} + +bool NetPlayClient::IsLocalPlayer(const PlayerId pid) const +{ + return pid == m_local_player->pid; +} + void NetPlayClient::SendTimeBase() { std::lock_guard lk(crit_netplay_client); diff --git a/Source/Core/Core/NetPlayClient.h b/Source/Core/Core/NetPlayClient.h index 54206eb408..9afa624dd4 100644 --- a/Source/Core/Core/NetPlayClient.h +++ b/Source/Core/Core/NetPlayClient.h @@ -114,6 +114,7 @@ public: void SendPowerButtonEvent(); void RequestGolfControl(PlayerId pid); void RequestGolfControl(); + std::string GetCurrentGolfer(); // Send and receive pads values bool WiimoteUpdate(int _number, u8* data, const u8 size, u8 reporting_mode); @@ -131,6 +132,10 @@ public: int InGamePadToLocalPad(int ingame_pad) const; int LocalPadToInGamePad(int localPad) const; + bool PlayerHasControllerMapped(PlayerId pid) const; + bool LocalPlayerHasControllerMapped() const; + bool IsLocalPlayer(PlayerId pid) const; + static void SendTimeBase(); bool DoAllPlayersHaveGame(); @@ -209,8 +214,6 @@ private: Failure }; - bool LocalPlayerHasControllerMapped() const; - void SendStartGamePacket(); void SendStopGamePacket(); diff --git a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp index 206015ab82..80b643b648 100644 --- a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp +++ b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp @@ -55,6 +55,7 @@ #include "UICommon/UICommon.h" #include "VideoCommon/NetPlayChatUI.h" +#include "VideoCommon/NetPlayGolfUI.h" #include "VideoCommon/RenderBase.h" #include "VideoCommon/VideoConfig.h" @@ -86,6 +87,7 @@ NetPlayDialog::NetPlayDialog(QWidget* parent) const bool host_input_authority = Config::Get(Config::NETPLAY_HOST_INPUT_AUTHORITY); const bool sync_all_wii_saves = Config::Get(Config::NETPLAY_SYNC_ALL_WII_SAVES); const bool golf_mode = Config::Get(Config::NETPLAY_GOLF_MODE); + const bool golf_mode_overlay = Config::Get(Config::NETPLAY_GOLF_MODE_OVERLAY); m_buffer_size_box->setValue(buffer_size); m_save_sd_action->setChecked(write_save_sdcard_data); @@ -98,6 +100,7 @@ NetPlayDialog::NetPlayDialog(QWidget* parent) m_host_input_authority_action->setChecked(host_input_authority); m_sync_all_wii_saves_action->setChecked(sync_all_wii_saves); m_golf_mode_action->setChecked(golf_mode); + m_golf_mode_overlay_action->setChecked(golf_mode_overlay); ConnectWidgets(); @@ -152,6 +155,8 @@ void NetPlayDialog::CreateMainLayout() m_other_menu = m_menu_bar->addMenu(tr("Other")); m_record_input_action = m_other_menu->addAction(tr("Record Inputs")); m_record_input_action->setCheckable(true); + m_golf_mode_overlay_action = m_other_menu->addAction(tr("Show Golf Mode Overlay")); + m_golf_mode_overlay_action->setCheckable(true); m_game_button->setDefault(false); m_game_button->setAutoDefault(false); @@ -362,6 +367,7 @@ void NetPlayDialog::ConnectWidgets() connect(m_host_input_authority_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); connect(m_sync_all_wii_saves_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); connect(m_golf_mode_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); + connect(m_golf_mode_overlay_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); } void NetPlayDialog::SendMessage(const std::string& msg) @@ -836,6 +842,11 @@ void NetPlayDialog::OnMsgStartGame() g_netplay_chat_ui = std::make_unique([this](const std::string& message) { SendMessage(message); }); + if (Settings::Instance().GetNetPlayClient()->GetNetSettings().m_GolfMode) + { + g_netplay_golf_ui = std::make_unique(Settings::Instance().GetNetPlayClient()); + } + QueueOnObject(this, [this] { auto client = Settings::Instance().GetNetPlayClient(); @@ -848,6 +859,7 @@ void NetPlayDialog::OnMsgStartGame() void NetPlayDialog::OnMsgStopGame() { g_netplay_chat_ui.reset(); + g_netplay_golf_ui.reset(); QueueOnObject(this, [this] { UpdateDiscordPresence(); }); } @@ -1038,6 +1050,7 @@ void NetPlayDialog::SaveSettings() Config::SetBase(Config::NETPLAY_HOST_INPUT_AUTHORITY, m_host_input_authority_action->isChecked()); Config::SetBase(Config::NETPLAY_SYNC_ALL_WII_SAVES, m_sync_all_wii_saves_action->isChecked()); Config::SetBase(Config::NETPLAY_GOLF_MODE, m_golf_mode_action->isChecked()); + Config::SetBase(Config::NETPLAY_GOLF_MODE_OVERLAY, m_golf_mode_overlay_action->isChecked()); } void NetPlayDialog::ShowMD5Dialog(const std::string& file_identifier) diff --git a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.h b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.h index e0aefdb311..558990cb1d 100644 --- a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.h +++ b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.h @@ -133,6 +133,7 @@ private: QAction* m_host_input_authority_action; QAction* m_sync_all_wii_saves_action; QAction* m_golf_mode_action; + QAction* m_golf_mode_overlay_action; QPushButton* m_quit_button; QSplitter* m_splitter; diff --git a/Source/Core/DolphinQt/Settings.cpp b/Source/Core/DolphinQt/Settings.cpp index 61be8ff8c0..95242b8ceb 100644 --- a/Source/Core/DolphinQt/Settings.cpp +++ b/Source/Core/DolphinQt/Settings.cpp @@ -28,6 +28,7 @@ #include "InputCommon/InputConfig.h" #include "VideoCommon/NetPlayChatUI.h" +#include "VideoCommon/NetPlayGolfUI.h" #include "VideoCommon/RenderBase.h" Settings::Settings() @@ -298,6 +299,7 @@ void Settings::ResetNetPlayClient(NetPlay::NetPlayClient* client) m_client.reset(client); g_netplay_chat_ui.reset(); + g_netplay_golf_ui.reset(); } std::shared_ptr Settings::GetNetPlayServer() diff --git a/Source/Core/VideoCommon/CMakeLists.txt b/Source/Core/VideoCommon/CMakeLists.txt index 459fbcb7aa..dfabf8fede 100644 --- a/Source/Core/VideoCommon/CMakeLists.txt +++ b/Source/Core/VideoCommon/CMakeLists.txt @@ -23,6 +23,7 @@ add_library(videocommon IndexGenerator.cpp LightingShaderGen.cpp NetPlayChatUI.cpp + NetPlayGolfUI.cpp OnScreenDisplay.cpp OpcodeDecoding.cpp PerfQueryBase.cpp diff --git a/Source/Core/VideoCommon/NetPlayGolfUI.cpp b/Source/Core/VideoCommon/NetPlayGolfUI.cpp new file mode 100644 index 0000000000..3a831b1cf1 --- /dev/null +++ b/Source/Core/VideoCommon/NetPlayGolfUI.cpp @@ -0,0 +1,66 @@ +// Copyright 2019 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "Common/StringUtil.h" + +#include "Core/NetPlayClient.h" + +#include "VideoCommon/NetPlayGolfUI.h" + +#include + +constexpr float DEFAULT_WINDOW_WIDTH = 220.0f; +constexpr float DEFAULT_WINDOW_HEIGHT = 45.0f; + +std::unique_ptr g_netplay_golf_ui; + +NetPlayGolfUI::NetPlayGolfUI(std::shared_ptr netplay_client) +{ + m_netplay_client = netplay_client; +} + +void NetPlayGolfUI::Display() +{ + auto client = m_netplay_client.lock(); + if (!client) + return; + + const float scale = ImGui::GetIO().DisplayFramebufferScale.x; + + ImGui::SetNextWindowPos(ImVec2((20.0f + DEFAULT_WINDOW_WIDTH) * scale, 10.0f * scale), + ImGuiCond_FirstUseEver); + ImGui::SetNextWindowSizeConstraints( + ImVec2(DEFAULT_WINDOW_WIDTH * scale, DEFAULT_WINDOW_HEIGHT * scale), + ImGui::GetIO().DisplaySize); + + // TODO: Translate these strings once imgui has multilingual fonts + if (!ImGui::Begin("Golf Mode", nullptr, ImGuiWindowFlags_None)) + { + ImGui::End(); + return; + } + + ImGui::Text("Current Golfer: %s", client->GetCurrentGolfer().c_str()); + + if (client->LocalPlayerHasControllerMapped()) + { + if (ImGui::Button("Take Control")) + { + client->RequestGolfControl(); + } + + for (auto player : client->GetPlayers()) + { + if (client->IsLocalPlayer(player->pid) || !client->PlayerHasControllerMapped(player->pid)) + continue; + + if (ImGui::Button(StringFromFormat("Give Control to %s", player->name.c_str()).c_str())) + { + client->RequestGolfControl(player->pid); + } + } + } + + ImGui::End(); +} diff --git a/Source/Core/VideoCommon/NetPlayGolfUI.h b/Source/Core/VideoCommon/NetPlayGolfUI.h new file mode 100644 index 0000000000..097c6cf600 --- /dev/null +++ b/Source/Core/VideoCommon/NetPlayGolfUI.h @@ -0,0 +1,27 @@ +// Copyright 2019 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +namespace NetPlay +{ +class NetPlayClient; +} + +class NetPlayGolfUI +{ +public: + explicit NetPlayGolfUI(std::shared_ptr netplay_client); + ~NetPlayGolfUI() = default; + + void Display(); + +private: + std::weak_ptr m_netplay_client; +}; + +extern std::unique_ptr g_netplay_golf_ui; diff --git a/Source/Core/VideoCommon/RenderBase.cpp b/Source/Core/VideoCommon/RenderBase.cpp index 688c7fc43b..09d59e4877 100644 --- a/Source/Core/VideoCommon/RenderBase.cpp +++ b/Source/Core/VideoCommon/RenderBase.cpp @@ -37,6 +37,7 @@ #include "Common/Timer.h" #include "Core/Analytics.h" +#include "Core/Config/NetplaySettings.h" #include "Core/Config/SYSCONFSettings.h" #include "Core/ConfigManager.h" #include "Core/Core.h" @@ -58,6 +59,7 @@ #include "VideoCommon/FramebufferManager.h" #include "VideoCommon/ImageWrite.h" #include "VideoCommon/NetPlayChatUI.h" +#include "VideoCommon/NetPlayGolfUI.h" #include "VideoCommon/OnScreenDisplay.h" #include "VideoCommon/PixelEngine.h" #include "VideoCommon/PixelShaderManager.h" @@ -532,6 +534,9 @@ void Renderer::DrawDebugText() if (g_ActiveConfig.bShowNetPlayMessages && g_netplay_chat_ui) g_netplay_chat_ui->Display(); + if (Config::Get(Config::NETPLAY_GOLF_MODE_OVERLAY) && g_netplay_golf_ui) + g_netplay_golf_ui->Display(); + if (g_ActiveConfig.bOverlayProjStats) Statistics::DisplayProj(); } diff --git a/Source/Core/VideoCommon/VideoCommon.vcxproj b/Source/Core/VideoCommon/VideoCommon.vcxproj index d18f7f444e..51f93d0457 100644 --- a/Source/Core/VideoCommon/VideoCommon.vcxproj +++ b/Source/Core/VideoCommon/VideoCommon.vcxproj @@ -58,6 +58,7 @@ + @@ -122,6 +123,7 @@ + @@ -192,4 +194,4 @@ - \ No newline at end of file + diff --git a/Source/Core/VideoCommon/VideoCommon.vcxproj.filters b/Source/Core/VideoCommon/VideoCommon.vcxproj.filters index c1ecc02dbd..ea9e2d3346 100644 --- a/Source/Core/VideoCommon/VideoCommon.vcxproj.filters +++ b/Source/Core/VideoCommon/VideoCommon.vcxproj.filters @@ -200,6 +200,9 @@ Util + + Util + @@ -392,8 +395,11 @@ Util + + Util + - \ No newline at end of file +