From 9ac6090c9aaad185b537bca15f9742918f99d52b Mon Sep 17 00:00:00 2001 From: iwubcode Date: Fri, 12 Jun 2020 00:25:06 -0500 Subject: [PATCH] Core: Add Free Look controllers that are initialized at boot --- Source/Core/Core/CMakeLists.txt | 2 + Source/Core/Core/Core.cpp | 13 +- Source/Core/Core/Core.vcxproj | 2 + Source/Core/Core/FreeLookManager.cpp | 242 +++++++++++++++++++++++++++ Source/Core/Core/FreeLookManager.h | 57 +++++++ 5 files changed, 315 insertions(+), 1 deletion(-) create mode 100644 Source/Core/Core/FreeLookManager.cpp create mode 100644 Source/Core/Core/FreeLookManager.h diff --git a/Source/Core/Core/CMakeLists.txt b/Source/Core/Core/CMakeLists.txt index 10ed707a27..ccdc3268ae 100644 --- a/Source/Core/Core/CMakeLists.txt +++ b/Source/Core/Core/CMakeLists.txt @@ -19,6 +19,8 @@ add_library(core DSPEmulator.h FreeLookConfig.cpp FreeLookConfig.h + FreeLookManager.cpp + FreeLookManager.h GeckoCodeConfig.cpp GeckoCodeConfig.h GeckoCode.cpp diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index b4d0de38d1..e942677efc 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -43,6 +43,7 @@ #include "Core/CoreTiming.h" #include "Core/DSPEmulator.h" #include "Core/FifoPlayer/FifoPlayer.h" +#include "Core/FreeLookManager.h" #include "Core/HLE/HLE.h" #include "Core/HW/CPU.h" #include "Core/HW/DSP.h" @@ -83,7 +84,6 @@ #include "VideoCommon/OnScreenDisplay.h" #include "VideoCommon/RenderBase.h" #include "VideoCommon/VideoBackendBase.h" -#include "VideoCommon/VideoConfig.h" #ifdef ANDROID #include "jni/AndroidCommon/IDCache.h" @@ -485,6 +485,15 @@ static void EmuThread(std::unique_ptr boot, WindowSystemInfo wsi NetPlay::SetupWiimotes(); } + if (init_controllers) + { + FreeLook::Initialize(); + } + else + { + FreeLook::LoadInputConfig(); + } + Common::ScopeGuard controller_guard{[init_controllers, init_wiimotes] { if (!init_controllers) return; @@ -495,6 +504,8 @@ static void EmuThread(std::unique_ptr boot, WindowSystemInfo wsi Wiimote::Shutdown(); } + FreeLook::Shutdown(); + ResetRumble(); Keyboard::Shutdown(); diff --git a/Source/Core/Core/Core.vcxproj b/Source/Core/Core/Core.vcxproj index 1694450783..c772229835 100644 --- a/Source/Core/Core/Core.vcxproj +++ b/Source/Core/Core/Core.vcxproj @@ -85,6 +85,7 @@ + @@ -440,6 +441,7 @@ + diff --git a/Source/Core/Core/FreeLookManager.cpp b/Source/Core/Core/FreeLookManager.cpp new file mode 100644 index 0000000000..a7b6f2a928 --- /dev/null +++ b/Source/Core/Core/FreeLookManager.cpp @@ -0,0 +1,242 @@ +// Copyright 2020 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "Core/FreeLookManager.h" + +#include "Common/Common.h" +#include "Common/CommonTypes.h" +#include "Common/Config/Config.h" + +#include "Core/ConfigManager.h" +#include "Core/FreeLookConfig.h" + +#include "InputCommon/ControllerEmu/ControlGroup/Buttons.h" +#include "InputCommon/InputConfig.h" + +#include "VideoCommon/FreeLookCamera.h" +#include "VideoCommon/OnScreenDisplay.h" + +namespace +{ +namespace MoveButtons +{ +enum MoveButtons +{ + Up, + Down, + Left, + Right, + Forward, + Backward, +}; +} + +namespace SpeedButtons +{ +enum SpeedButtons +{ + Decrease, + Increase, + Reset, +}; +} + +namespace OtherButtons +{ +enum OtherButtons +{ + ResetView, +}; +} + +namespace FieldOfViewButtons +{ +enum FieldOfViewButtons +{ + IncreaseX, + DecreaseX, + IncreaseY, + DecreaseY, +}; +} +} // namespace + +FreeLookController::FreeLookController(const unsigned int index) : m_index(index) +{ + groups.emplace_back(m_move_buttons = new ControllerEmu::Buttons(_trans("Move"))); + + m_move_buttons->AddInput(ControllerEmu::Translate, _trans("Up")); + m_move_buttons->AddInput(ControllerEmu::Translate, _trans("Down")); + m_move_buttons->AddInput(ControllerEmu::Translate, _trans("Left")); + m_move_buttons->AddInput(ControllerEmu::Translate, _trans("Right")); + m_move_buttons->AddInput(ControllerEmu::Translate, _trans("Forward")); + m_move_buttons->AddInput(ControllerEmu::Translate, _trans("Backward")); + + groups.emplace_back(m_speed_buttons = new ControllerEmu::Buttons(_trans("Speed"))); + + m_speed_buttons->AddInput(ControllerEmu::Translate, _trans("Decrease")); + m_speed_buttons->AddInput(ControllerEmu::Translate, _trans("Increase")); + m_speed_buttons->AddInput(ControllerEmu::Translate, _trans("Reset")); + + groups.emplace_back(m_other_buttons = new ControllerEmu::Buttons(_trans("Other"))); + + m_other_buttons->AddInput(ControllerEmu::Translate, _trans("Reset View")); + + groups.emplace_back(m_fov_buttons = new ControllerEmu::Buttons(_trans("Field of View"))); + + m_fov_buttons->AddInput(ControllerEmu::Translate, _trans("Increase X")); + m_fov_buttons->AddInput(ControllerEmu::Translate, _trans("Decrease X")); + m_fov_buttons->AddInput(ControllerEmu::Translate, _trans("Increase Y")); + m_fov_buttons->AddInput(ControllerEmu::Translate, _trans("Decrease Y")); +} + +std::string FreeLookController::GetName() const +{ + return std::string("FreeLook") + char('1' + m_index); +} + +void FreeLookController::LoadDefaults(const ControllerInterface& ciface) +{ + EmulatedController::LoadDefaults(ciface); + + auto hotkey_string = [](std::vector inputs) { + return "@(" + JoinStrings(inputs, "+") + ')'; + }; + + m_move_buttons->SetControlExpression(MoveButtons::Up, hotkey_string({"Shift", "E"})); + m_move_buttons->SetControlExpression(MoveButtons::Down, hotkey_string({"Shift", "Q"})); + m_move_buttons->SetControlExpression(MoveButtons::Left, hotkey_string({"Shift", "A"})); + m_move_buttons->SetControlExpression(MoveButtons::Right, hotkey_string({"Shift", "D"})); + m_move_buttons->SetControlExpression(MoveButtons::Forward, hotkey_string({"Shift", "W"})); + m_move_buttons->SetControlExpression(MoveButtons::Backward, hotkey_string({"Shift", "S"})); + + m_speed_buttons->SetControlExpression(SpeedButtons::Decrease, hotkey_string({"Shift", "`1`"})); + m_speed_buttons->SetControlExpression(SpeedButtons::Increase, hotkey_string({"Shift", "`2`"})); + m_speed_buttons->SetControlExpression(SpeedButtons::Reset, hotkey_string({"Shift", "F"})); + + m_other_buttons->SetControlExpression(OtherButtons::ResetView, hotkey_string({"Shift", "R"})); + + m_fov_buttons->SetControlExpression(FieldOfViewButtons::IncreaseX, + hotkey_string({"Shift", "`Axis Z+`"})); + m_fov_buttons->SetControlExpression(FieldOfViewButtons::DecreaseX, + hotkey_string({"Shift", "`Axis Z-`"})); + m_fov_buttons->SetControlExpression(FieldOfViewButtons::IncreaseY, + hotkey_string({"Shift", "`Axis Z+`"})); + m_fov_buttons->SetControlExpression(FieldOfViewButtons::DecreaseY, + hotkey_string({"Shift", "`Axis Z-`"})); +} + +ControllerEmu::ControlGroup* FreeLookController::GetGroup(FreeLookGroup group) const +{ + switch (group) + { + case FreeLookGroup::Move: + return m_move_buttons; + case FreeLookGroup::Speed: + return m_speed_buttons; + case FreeLookGroup::FieldOfView: + return m_fov_buttons; + case FreeLookGroup::Other: + return m_other_buttons; + default: + return nullptr; + } +} + +void FreeLookController::Update() +{ + if (m_move_buttons->controls[MoveButtons::Up]->GetState()) + g_freelook_camera.MoveVertical(-g_freelook_camera.GetSpeed()); + + if (m_move_buttons->controls[MoveButtons::Down]->GetState()) + g_freelook_camera.MoveVertical(g_freelook_camera.GetSpeed()); + + if (m_move_buttons->controls[MoveButtons::Left]->GetState()) + g_freelook_camera.MoveHorizontal(g_freelook_camera.GetSpeed()); + + if (m_move_buttons->controls[MoveButtons::Right]->GetState()) + g_freelook_camera.MoveHorizontal(-g_freelook_camera.GetSpeed()); + + if (m_move_buttons->controls[MoveButtons::Forward]->GetState()) + g_freelook_camera.MoveForward(g_freelook_camera.GetSpeed()); + + if (m_move_buttons->controls[MoveButtons::Backward]->GetState()) + g_freelook_camera.MoveForward(-g_freelook_camera.GetSpeed()); + + if (m_fov_buttons->controls[FieldOfViewButtons::IncreaseX]->GetState()) + g_freelook_camera.IncreaseFovX(g_freelook_camera.GetFovStepSize()); + + if (m_fov_buttons->controls[FieldOfViewButtons::DecreaseX]->GetState()) + g_freelook_camera.IncreaseFovX(-1.0f * g_freelook_camera.GetFovStepSize()); + + if (m_fov_buttons->controls[FieldOfViewButtons::IncreaseY]->GetState()) + g_freelook_camera.IncreaseFovY(g_freelook_camera.GetFovStepSize()); + + if (m_fov_buttons->controls[FieldOfViewButtons::DecreaseY]->GetState()) + g_freelook_camera.IncreaseFovY(-1.0f * g_freelook_camera.GetFovStepSize()); + + if (m_speed_buttons->controls[SpeedButtons::Decrease]->GetState()) + g_freelook_camera.ModifySpeed(1.0f / 1.1f); + + if (m_speed_buttons->controls[SpeedButtons::Increase]->GetState()) + g_freelook_camera.ModifySpeed(1.1f); + + if (m_speed_buttons->controls[SpeedButtons::Reset]->GetState()) + g_freelook_camera.ResetSpeed(); + + if (m_other_buttons->controls[OtherButtons::ResetView]->GetState()) + g_freelook_camera.Reset(); +} + +namespace FreeLook +{ +static InputConfig s_config("FreeLookController", _trans("FreeLook"), "FreeLookController"); +InputConfig* GetInputConfig() +{ + return &s_config; +} + +void Shutdown() +{ + s_config.UnregisterHotplugCallback(); + + s_config.ClearControllers(); +} + +void Initialize() +{ + if (s_config.ControllersNeedToBeCreated()) + { + s_config.CreateController(0); + } + + s_config.RegisterHotplugCallback(); + + s_config.LoadConfig(true); +} + +void LoadInputConfig() +{ + s_config.LoadConfig(true); +} + +bool IsInitialized() +{ + return !s_config.ControllersNeedToBeCreated(); +} + +ControllerEmu::ControlGroup* GetInputGroup(int pad_num, FreeLookGroup group) +{ + return static_cast(s_config.GetController(pad_num))->GetGroup(group); +} + +void UpdateInput() +{ + for (int i = 0; i < s_config.GetControllerCount(); i++) + { + static_cast(s_config.GetController(i))->Update(); + } +} + +} // namespace FreeLook diff --git a/Source/Core/Core/FreeLookManager.h b/Source/Core/Core/FreeLookManager.h new file mode 100644 index 0000000000..a74d26271f --- /dev/null +++ b/Source/Core/Core/FreeLookManager.h @@ -0,0 +1,57 @@ +// Copyright 2020 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include "Common/CommonTypes.h" +#include "InputCommon/ControllerEmu/ControllerEmu.h" + +class InputConfig; + +namespace ControllerEmu +{ +class ControlGroup; +class Buttons; +} // namespace ControllerEmu + +enum class FreeLookGroup +{ + Move, + Speed, + FieldOfView, + Other +}; + +namespace FreeLook +{ +void Shutdown(); +void Initialize(); +void LoadInputConfig(); +bool IsInitialized(); +void UpdateInput(); + +InputConfig* GetInputConfig(); +ControllerEmu::ControlGroup* GetInputGroup(int pad_num, FreeLookGroup group); + +} // namespace FreeLook + +class FreeLookController final : public ControllerEmu::EmulatedController +{ +public: + explicit FreeLookController(unsigned int index); + + std::string GetName() const override; + void LoadDefaults(const ControllerInterface& ciface) override; + + ControllerEmu::ControlGroup* GetGroup(FreeLookGroup group) const; + void Update(); + +private: + ControllerEmu::Buttons* m_move_buttons; + ControllerEmu::Buttons* m_speed_buttons; + ControllerEmu::Buttons* m_fov_buttons; + ControllerEmu::Buttons* m_other_buttons; + + const unsigned int m_index; +};