Skip to content

Commit

Permalink
Add imgui golf mode overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
Techjar committed Apr 5, 2019
1 parent 1a12876 commit 6c393f9
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Source/Core/Core/Config/NetplaySettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ const ConfigInfo<bool> NETPLAY_HOST_INPUT_AUTHORITY{{System::Main, "NetPlay", "H
const ConfigInfo<bool> NETPLAY_SYNC_ALL_WII_SAVES{{System::Main, "NetPlay", "SyncAllWiiSaves"},
false};
const ConfigInfo<bool> NETPLAY_GOLF_MODE{{System::Main, "NetPlay", "GolfMode"}, false};
const ConfigInfo<bool> NETPLAY_GOLF_MODE_OVERLAY{{System::Main, "NetPlay", "GolfModeOverlay"},
true};

} // namespace Config
1 change: 1 addition & 0 deletions Source/Core/Core/Config/NetplaySettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ extern const ConfigInfo<bool> NETPLAY_STRICT_SETTINGS_SYNC;
extern const ConfigInfo<bool> NETPLAY_HOST_INPUT_AUTHORITY;
extern const ConfigInfo<bool> NETPLAY_SYNC_ALL_WII_SAVES;
extern const ConfigInfo<bool> NETPLAY_GOLF_MODE;
extern const ConfigInfo<bool> NETPLAY_GOLF_MODE_OVERLAY;

} // namespace Config
29 changes: 23 additions & 6 deletions Source/Core/Core/NetPlayClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2109,14 +2109,18 @@ void NetPlayClient::RequestGolfControl()
}

// called from ---GUI--- thread
bool NetPlayClient::LocalPlayerHasControllerMapped() const
std::string NetPlayClient::GetCurrentGolfer()
{
const auto mapping_matches_player_id = [this](const PlayerId& mapping) {
return mapping == m_local_player->pid;
};
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
if (m_players.count(m_current_golfer))
return m_players[m_current_golfer].name;
return "";
}

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);
// called from ---GUI--- thread
bool NetPlayClient::LocalPlayerHasControllerMapped() const
{
return PlayerHasControllerMapped(m_local_player->pid);
}

bool NetPlayClient::IsFirstInGamePad(int ingame_pad) const
Expand Down Expand Up @@ -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<std::mutex> lk(crit_netplay_client);
Expand Down
7 changes: 5 additions & 2 deletions Source/Core/Core/NetPlayClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class NetPlayClient : public TraversalClientClient
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);
Expand All @@ -131,6 +132,10 @@ class NetPlayClient : public TraversalClientClient
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();

Expand Down Expand Up @@ -209,8 +214,6 @@ class NetPlayClient : public TraversalClientClient
Failure
};

bool LocalPlayerHasControllerMapped() const;

void SendStartGamePacket();
void SendStopGamePacket();

Expand Down
13 changes: 13 additions & 0 deletions Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "UICommon/UICommon.h"

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

Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -836,6 +842,11 @@ void NetPlayDialog::OnMsgStartGame()
g_netplay_chat_ui =
std::make_unique<NetPlayChatUI>([this](const std::string& message) { SendMessage(message); });

if (Settings::Instance().GetNetPlayClient()->GetNetSettings().m_GolfMode)
{
g_netplay_golf_ui = std::make_unique<NetPlayGolfUI>(Settings::Instance().GetNetPlayClient());
}

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

Expand All @@ -848,6 +859,7 @@ void NetPlayDialog::OnMsgStartGame()
void NetPlayDialog::OnMsgStopGame()
{
g_netplay_chat_ui.reset();
g_netplay_golf_ui.reset();
QueueOnObject(this, [this] { UpdateDiscordPresence(); });
}

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DolphinQt/NetPlay/NetPlayDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class NetPlayDialog : public QDialog, public NetPlay::NetPlayUI
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;

Expand Down
2 changes: 2 additions & 0 deletions Source/Core/DolphinQt/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "InputCommon/InputConfig.h"

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

Settings::Settings()
Expand Down Expand Up @@ -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<NetPlay::NetPlayServer> Settings::GetNetPlayServer()
Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoCommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_library(videocommon
IndexGenerator.cpp
LightingShaderGen.cpp
NetPlayChatUI.cpp
NetPlayGolfUI.cpp
OnScreenDisplay.cpp
OpcodeDecoding.cpp
PerfQueryBase.cpp
Expand Down
66 changes: 66 additions & 0 deletions Source/Core/VideoCommon/NetPlayGolfUI.cpp
Original file line number Diff line number Diff line change
@@ -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 <imgui.h>

constexpr float DEFAULT_WINDOW_WIDTH = 220.0f;
constexpr float DEFAULT_WINDOW_HEIGHT = 45.0f;

std::unique_ptr<NetPlayGolfUI> g_netplay_golf_ui;

NetPlayGolfUI::NetPlayGolfUI(std::shared_ptr<NetPlay::NetPlayClient> 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();
}
27 changes: 27 additions & 0 deletions Source/Core/VideoCommon/NetPlayGolfUI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2019 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <memory>
#include <string>

namespace NetPlay
{
class NetPlayClient;
}

class NetPlayGolfUI
{
public:
explicit NetPlayGolfUI(std::shared_ptr<NetPlay::NetPlayClient> netplay_client);
~NetPlayGolfUI() = default;

void Display();

private:
std::weak_ptr<NetPlay::NetPlayClient> m_netplay_client;
};

extern std::unique_ptr<NetPlayGolfUI> g_netplay_golf_ui;
5 changes: 5 additions & 0 deletions Source/Core/VideoCommon/RenderBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/VideoCommon/VideoCommon.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<ClCompile Include="ImageWrite.cpp" />
<ClCompile Include="IndexGenerator.cpp" />
<ClCompile Include="NetPlayChatUI.cpp" />
<ClCompile Include="NetPlayGolfUI.cpp" />
<ClCompile Include="OnScreenDisplay.cpp" />
<ClCompile Include="OpcodeDecoding.cpp" />
<ClCompile Include="PerfQueryBase.cpp" />
Expand Down Expand Up @@ -122,6 +123,7 @@
<ClInclude Include="FramebufferShaderGen.h" />
<ClInclude Include="GXPipelineTypes.h" />
<ClInclude Include="NetPlayChatUI.h" />
<ClInclude Include="NetPlayGolfUI.h" />
<ClInclude Include="ShaderCache.h" />
<ClInclude Include="UberShaderCommon.h" />
<ClInclude Include="UberShaderPixel.h" />
Expand Down Expand Up @@ -192,4 +194,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
8 changes: 7 additions & 1 deletion Source/Core/VideoCommon/VideoCommon.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@
<ClCompile Include="NetPlayChatUI.cpp">
<Filter>Util</Filter>
</ClCompile>
<ClCompile Include="NetPlayGolfUI.cpp">
<Filter>Util</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CommandProcessor.h" />
Expand Down Expand Up @@ -392,8 +395,11 @@
<ClInclude Include="NetPlayChatUI.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="NetPlayGolfUI.h">
<Filter>Util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
</ItemGroup>
</Project>
</Project>

0 comments on commit 6c393f9

Please sign in to comment.