System: Create controllers dynamically based on config
This commit is contained in:
parent
ea0845d5ad
commit
c65279f944
|
@ -23,18 +23,30 @@ bool Controller::Transfer(const u8 data_in, u8* data_out)
|
|||
|
||||
void Controller::SetButtonState(s32 button_code, bool pressed) {}
|
||||
|
||||
std::unique_ptr<Controller> Controller::Create(std::string_view type_name)
|
||||
std::unique_ptr<Controller> Controller::Create(ControllerType type)
|
||||
{
|
||||
if (type_name == "DigitalController")
|
||||
return DigitalController::Create();
|
||||
switch (type)
|
||||
{
|
||||
return {};
|
||||
|
||||
return {};
|
||||
case ControllerType::DigitalController:
|
||||
return DigitalController::Create();
|
||||
|
||||
case ControllerType::None:
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<s32> Controller::GetButtonCodeByName(std::string_view type_name, std::string_view button_name)
|
||||
std::optional<s32> Controller::GetButtonCodeByName(ControllerType type, std::string_view button_name)
|
||||
{
|
||||
if (type_name == "DigitalController")
|
||||
return DigitalController::GetButtonCodeByName(button_name);
|
||||
switch (type)
|
||||
{
|
||||
case ControllerType::DigitalController:
|
||||
return DigitalController::GetButtonCodeByName(button_name);
|
||||
|
||||
return {};
|
||||
case ControllerType::None:
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,8 +25,8 @@ public:
|
|||
virtual void SetButtonState(s32 button_code, bool pressed);
|
||||
|
||||
/// Creates a new controller of the specified type.
|
||||
static std::unique_ptr<Controller> Create(std::string_view type_name);
|
||||
static std::unique_ptr<Controller> Create(ControllerType type);
|
||||
|
||||
/// Gets the integer code for a button in the specified controller type.
|
||||
static std::optional<s32> GetButtonCodeByName(std::string_view type_name, std::string_view button_name);
|
||||
static std::optional<s32> GetButtonCodeByName(ControllerType type, std::string_view button_name);
|
||||
};
|
||||
|
|
|
@ -33,6 +33,9 @@ void Settings::SetDefaults()
|
|||
bios_patch_tty_enable = false;
|
||||
bios_patch_fast_boot = false;
|
||||
|
||||
controller_a_type = ControllerType::DigitalController;
|
||||
controller_b_type = ControllerType::None;
|
||||
|
||||
memory_card_a_path = "memory_card_a.mcd";
|
||||
memory_card_b_path.clear();
|
||||
}
|
||||
|
@ -69,6 +72,11 @@ void Settings::Load(const char* filename)
|
|||
bios_patch_tty_enable = ini.GetBoolValue("BIOS", "PatchTTYEnable", true);
|
||||
bios_patch_fast_boot = ini.GetBoolValue("BIOS", "PatchFastBoot", false);
|
||||
|
||||
controller_a_type = ParseControllerTypeName(ini.GetValue("Controller", "PortAType", "DigitalController"))
|
||||
.value_or(ControllerType::DigitalController);
|
||||
controller_b_type =
|
||||
ParseControllerTypeName(ini.GetValue("Controller", "PortBType", "None")).value_or(ControllerType::None);
|
||||
|
||||
memory_card_a_path = ini.GetValue("MemoryCard", "CardAPath", "memory_card_a.mcd");
|
||||
memory_card_b_path = ini.GetValue("MemoryCard", "CardBPath", "");
|
||||
}
|
||||
|
@ -102,6 +110,9 @@ bool Settings::Save(const char* filename) const
|
|||
ini.SetBoolValue("BIOS", "PatchTTYEnable", bios_patch_tty_enable);
|
||||
ini.SetBoolValue("BIOS", "PatchFastBoot", bios_patch_fast_boot);
|
||||
|
||||
ini.SetValue("Controller", "PortAType", GetControllerTypeName(controller_a_type));
|
||||
ini.SetValue("Controller", "PortBType", GetControllerTypeName(controller_b_type));
|
||||
|
||||
if (!memory_card_a_path.empty())
|
||||
ini.SetValue("MemoryCard", "CardAPath", memory_card_a_path.c_str());
|
||||
else
|
||||
|
@ -205,3 +216,30 @@ const char* Settings::GetRendererDisplayName(GPURenderer renderer)
|
|||
{
|
||||
return s_gpu_renderer_display_names[static_cast<int>(renderer)];
|
||||
}
|
||||
|
||||
static std::array<const char*, 2> s_controller_type_names = {{"None", "DigitalController"}};
|
||||
static std::array<const char*, 2> s_controller_display_names = {{"None", "Digital Controller"}};
|
||||
|
||||
std::optional<ControllerType> Settings::ParseControllerTypeName(const char* str)
|
||||
{
|
||||
int index = 0;
|
||||
for (const char* name : s_controller_type_names)
|
||||
{
|
||||
if (strcasecmp(name, str) == 0)
|
||||
return static_cast<ControllerType>(index);
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const char* Settings::GetControllerTypeName(ControllerType type)
|
||||
{
|
||||
return s_controller_type_names[static_cast<int>(type)];
|
||||
}
|
||||
|
||||
const char* Settings::GetControllerTypeDisplayName(ControllerType type)
|
||||
{
|
||||
return s_controller_display_names[static_cast<int>(type)];
|
||||
}
|
||||
|
|
|
@ -45,6 +45,9 @@ struct Settings
|
|||
bool bios_patch_tty_enable = false;
|
||||
bool bios_patch_fast_boot = false;
|
||||
|
||||
ControllerType controller_a_type = ControllerType::None;
|
||||
ControllerType controller_b_type = ControllerType::None;
|
||||
|
||||
std::string memory_card_a_path;
|
||||
std::string memory_card_b_path;
|
||||
|
||||
|
@ -63,4 +66,8 @@ struct Settings
|
|||
static std::optional<GPURenderer> ParseRendererName(const char* str);
|
||||
static const char* GetRendererName(GPURenderer renderer);
|
||||
static const char* GetRendererDisplayName(GPURenderer renderer);
|
||||
|
||||
static std::optional<ControllerType> ParseControllerTypeName(const char* str);
|
||||
static const char* GetControllerTypeName(ControllerType type);
|
||||
static const char* GetControllerTypeDisplayName(ControllerType type);
|
||||
};
|
||||
|
|
|
@ -462,11 +462,20 @@ void System::UpdateControllers()
|
|||
m_pad->SetController(0, nullptr);
|
||||
m_pad->SetController(1, nullptr);
|
||||
|
||||
const Settings& settings = m_host_interface->GetSettings();
|
||||
if (settings.controller_a_type != ControllerType::None)
|
||||
{
|
||||
std::unique_ptr<Controller> controller = Controller::Create("DigitalController");
|
||||
std::unique_ptr<Controller> controller = Controller::Create(settings.controller_a_type);
|
||||
if (controller)
|
||||
m_pad->SetController(0, std::move(controller));
|
||||
}
|
||||
|
||||
if (settings.controller_b_type != ControllerType::None)
|
||||
{
|
||||
std::unique_ptr<Controller> controller = Controller::Create(settings.controller_b_type);
|
||||
if (controller)
|
||||
m_pad->SetController(1, std::move(controller));
|
||||
}
|
||||
}
|
||||
|
||||
void System::UpdateMemoryCards()
|
||||
|
|
|
@ -47,6 +47,12 @@ enum class GPURenderer : u8
|
|||
Count
|
||||
};
|
||||
|
||||
enum class ControllerType
|
||||
{
|
||||
None,
|
||||
DigitalController,
|
||||
};
|
||||
|
||||
enum : u32
|
||||
{
|
||||
CPU_CODE_CACHE_PAGE_SIZE = 1024,
|
||||
|
|
Loading…
Reference in New Issue