LUS Cleanup: Make ControlDeck a member of Window

This commit is contained in:
Kenix3 2022-07-31 23:14:54 -04:00
parent d9443d98f0
commit b3c3882b12
7 changed files with 67 additions and 39 deletions

View File

@ -152,3 +152,19 @@ void Ship::ControlDeck::SaveControllerSettings() {
Config->save(); Config->save();
} }
std::shared_ptr<Ship::Controller> Ship::ControlDeck::GetPhysicalDevice(int deviceSlot) {
return physicalDevices[deviceSlot];
}
size_t Ship::ControlDeck::GetNumPhysicalDevices() {
return physicalDevices.size();
}
int Ship::ControlDeck::GetVirtualDevice(int slot) {
return virtualDevices[slot];
}
size_t Ship::ControlDeck::GetNumVirtualDevices() {
return virtualDevices.size();
}

View File

@ -8,13 +8,18 @@ namespace Ship {
class ControlDeck { class ControlDeck {
public: public:
std::vector<int> virtualDevices;
std::vector<std::shared_ptr<Controller>> physicalDevices = {};
void Init(uint8_t* controllerBits); void Init(uint8_t* controllerBits);
void ScanPhysicalDevices(); void ScanPhysicalDevices();
void WriteToPad(OSContPad* pad) const; void WriteToPad(OSContPad* pad) const;
void LoadControllerSettings(); void LoadControllerSettings();
void SaveControllerSettings(); void SaveControllerSettings();
void SetPhysicalDevice(int slot, int deviceSlot); void SetPhysicalDevice(int slot, int deviceSlot);
std::shared_ptr<Ship::Controller> GetPhysicalDevice(int deviceSlot);
size_t GetNumPhysicalDevices();
int GetVirtualDevice(int slot);
size_t GetNumVirtualDevices();
private:
std::vector<int> virtualDevices;
std::vector<std::shared_ptr<Controller>> physicalDevices = {};
}; };
} }

View File

@ -792,7 +792,7 @@ namespace SohImGui {
bool menu_bar = CVar_GetS32("gOpenMenuBar", 0); bool menu_bar = CVar_GetS32("gOpenMenuBar", 0);
CVar_SetS32("gOpenMenuBar", !menu_bar); CVar_SetS32("gOpenMenuBar", !menu_bar);
needs_save = true; needs_save = true;
GlobalCtx2::GetInstance()->GetWindow()->dwMenubar = menu_bar; GlobalCtx2::GetInstance()->GetWindow()->SetMenuBar(menu_bar);
ShowCursor(menu_bar, Dialogues::dMenubar); ShowCursor(menu_bar, Dialogues::dMenubar);
GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck()->SaveControllerSettings(); GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck()->SaveControllerSettings();
if (CVar_GetS32("gControlNav", 0)) { if (CVar_GetS32("gControlNav", 0)) {

View File

@ -16,8 +16,8 @@ namespace Ship {
} }
std::shared_ptr<Controller> GetControllerPerSlot(int slot) { std::shared_ptr<Controller> GetControllerPerSlot(int slot) {
const std::vector<int> vDevices = Window::ControllerApi->virtualDevices; auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
return Window::ControllerApi->physicalDevices[vDevices[slot]]; return controlDeck->GetPhysicalDevice(controlDeck->GetVirtualDevice(slot));
} }
void InputEditor::DrawButton(const char* label, int n64Btn) { void InputEditor::DrawButton(const char* label, int n64Btn) {
@ -82,20 +82,17 @@ namespace Ship {
} }
void InputEditor::DrawControllerSchema() { void InputEditor::DrawControllerSchema() {
auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
const std::vector<int> vDevices = Window::ControllerApi->virtualDevices; auto Backend = controlDeck->GetPhysicalDevice(controlDeck->GetVirtualDevice(CurrentPort));
const std::vector<std::shared_ptr<Controller>> devices = Window::ControllerApi->physicalDevices; DeviceProfile& profile = Backend->profiles[CurrentPort];
std::shared_ptr<Controller> Backend = devices[vDevices[CurrentPort]];
DeviceProfile& profile =Backend->profiles[CurrentPort];
float sensitivity = profile.Thresholds[SENSITIVITY]; float sensitivity = profile.Thresholds[SENSITIVITY];
bool IsKeyboard = Backend->GetGuid() == "Keyboard" || Backend->GetGuid() == "Auto" || !Backend->Connected(); bool IsKeyboard = Backend->GetGuid() == "Keyboard" || Backend->GetGuid() == "Auto" || !Backend->Connected();
const char* ControllerName = Backend->GetControllerName(); const char* ControllerName = Backend->GetControllerName();
if (ControllerName != nullptr && ImGui::BeginCombo("##ControllerEntries", ControllerName)) { if (ControllerName != nullptr && ImGui::BeginCombo("##ControllerEntries", ControllerName)) {
for (uint8_t i = 0; i < devices.size(); i++) { for (uint8_t i = 0; i < controlDeck->GetNumPhysicalDevices(); i++) {
if (ImGui::Selectable(devices[i]->GetControllerName(), i == vDevices[CurrentPort])) { if (ImGui::Selectable(controlDeck->GetPhysicalDevice(i)->GetControllerName(), i == controlDeck->GetVirtualDevice(CurrentPort))) {
Window::ControllerApi->SetPhysicalDevice(CurrentPort, i); controlDeck->SetPhysicalDevice(CurrentPort, i);
} }
} }
ImGui::EndCombo(); ImGui::EndCombo();
@ -104,7 +101,7 @@ namespace Ship {
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("Refresh")) { if(ImGui::Button("Refresh")) {
Window::ControllerApi->ScanPhysicalDevices(); controlDeck->ScanPhysicalDevices();
} }
SohImGui::BeginGroupPanel("Buttons", ImVec2(150, 20)); SohImGui::BeginGroupPanel("Buttons", ImVec2(150, 20));

View File

@ -57,7 +57,7 @@ extern "C" {
} }
#endif #endif
Ship::Window::ControllerApi->Init(controllerBits); Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck()->Init(controllerBits);
return 0; return 0;
} }
@ -77,7 +77,7 @@ extern "C" {
pad->gyro_y = 0; pad->gyro_y = 0;
if (!CVar_GetS32("gOpenMenuBar", 0)) { if (!CVar_GetS32("gOpenMenuBar", 0)) {
Ship::Window::ControllerApi->WriteToPad(pad); Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck()->WriteToPad(pad);
} }
ModInternal::ExecuteHooks<ModInternal::ControllerRead>(pad); ModInternal::ExecuteHooks<ModInternal::ControllerRead>(pad);
@ -221,7 +221,7 @@ namespace Ship {
int32_t Window::lastScancode; int32_t Window::lastScancode;
Window::Window(std::shared_ptr<GlobalCtx2> Context) : Context(Context), APlayer(nullptr) { Window::Window(std::shared_ptr<GlobalCtx2> Context) : Context(Context), APlayer(nullptr), ControllerApi(nullptr) {
WmApi = nullptr; WmApi = nullptr;
RenderingApi = nullptr; RenderingApi = nullptr;
bIsFullscreen = false; bIsFullscreen = false;
@ -259,8 +259,9 @@ namespace Ship {
std::shared_ptr<Mercury> pConf = GlobalCtx2::GetInstance()->GetConfig(); std::shared_ptr<Mercury> pConf = GlobalCtx2::GetInstance()->GetConfig();
CreateDefaults(); CreateDefaults();
InitializeAudioPlayer();
InitializeControlDeck();
SetAudioPlayer();
bIsFullscreen = pConf->getBool("Window.Fullscreen.Enabled", false); bIsFullscreen = pConf->getBool("Window.Fullscreen.Enabled", false);
if (bIsFullscreen) { if (bIsFullscreen) {
@ -279,7 +280,7 @@ namespace Ship {
WmApi->set_fullscreen_changed_callback(OnFullscreenChanged); WmApi->set_fullscreen_changed_callback(OnFullscreenChanged);
WmApi->set_keyboard_callbacks(KeyDown, KeyUp, AllKeysUp); WmApi->set_keyboard_callbacks(KeyDown, KeyUp, AllKeysUp);
ModInternal::RegisterHook<ModInternal::ExitGame>([]() { ModInternal::RegisterHook<ModInternal::ExitGame>([this]() {
ControllerApi->SaveControllerSettings(); ControllerApi->SaveControllerSettings();
}); });
} }
@ -347,8 +348,9 @@ namespace Ship {
lastScancode = -1; lastScancode = -1;
bool bIsProcessed = false; bool bIsProcessed = false;
const auto pad = dynamic_cast<KeyboardController*>(ControllerApi->physicalDevices[ControllerApi->physicalDevices.size() - 2].get()); auto controlDeck = GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
if (pad != nullptr) { const auto pad = dynamic_cast<KeyboardController*>(controlDeck->GetPhysicalDevice(controlDeck->GetNumPhysicalDevices() - 2).get());
if (pad != nullptr) {
if (pad->ReleaseButton(dwScancode)) { if (pad->ReleaseButton(dwScancode)) {
bIsProcessed = true; bIsProcessed = true;
} }
@ -359,9 +361,9 @@ namespace Ship {
bool Window::KeyDown(int32_t dwScancode) { bool Window::KeyDown(int32_t dwScancode) {
bool bIsProcessed = false; bool bIsProcessed = false;
auto controlDeck = GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
const auto pad = dynamic_cast<KeyboardController*>(ControllerApi->physicalDevices[ControllerApi->physicalDevices.size() - 2].get()); const auto pad = dynamic_cast<KeyboardController*>(controlDeck->GetPhysicalDevice(controlDeck->GetNumPhysicalDevices() - 2).get());
if (pad != nullptr) { if (pad != nullptr) {
if (pad->PressButton(dwScancode)) { if (pad->PressButton(dwScancode)) {
bIsProcessed = true; bIsProcessed = true;
} }
@ -374,7 +376,8 @@ namespace Ship {
void Window::AllKeysUp(void) { void Window::AllKeysUp(void) {
const auto pad = dynamic_cast<KeyboardController*>(ControllerApi->physicalDevices[ControllerApi->physicalDevices.size() - 2].get()); auto controlDeck = GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
const auto pad = dynamic_cast<KeyboardController*>(controlDeck->GetPhysicalDevice(controlDeck->GetNumPhysicalDevices() - 2).get());
if (pad != nullptr) { if (pad != nullptr) {
pad->ReleaseAllButtons(); pad->ReleaseAllButtons();
} }
@ -400,7 +403,7 @@ namespace Ship {
return dwHeight; return dwHeight;
} }
void Window::SetAudioPlayer() { void Window::InitializeAudioPlayer() {
#ifdef _WIN32 #ifdef _WIN32
APlayer = std::make_shared<WasapiAudioPlayer>(); APlayer = std::make_shared<WasapiAudioPlayer>();
#elif defined(__linux) #elif defined(__linux)
@ -409,4 +412,8 @@ namespace Ship {
APlayer = std::make_shared<SDLAudioPlayer>(); APlayer = std::make_shared<SDLAudioPlayer>();
#endif #endif
} }
void Window::InitializeControlDeck() {
ControllerApi = std::make_shared<ControlDeck>();
}
} }

View File

@ -16,7 +16,6 @@ namespace Ship {
class Window { class Window {
public: public:
static int32_t lastScancode; static int32_t lastScancode;
inline static ControlDeck* ControllerApi = new ControlDeck;
Window(std::shared_ptr<GlobalCtx2> Context); Window(std::shared_ptr<GlobalCtx2> Context);
~Window(); ~Window();
@ -32,12 +31,12 @@ namespace Ship {
void ToggleFullscreen(); void ToggleFullscreen();
void SetFullscreen(bool bIsFullscreen); void SetFullscreen(bool bIsFullscreen);
void ShowCursor(bool hide); void ShowCursor(bool hide);
bool IsFullscreen() { return bIsFullscreen; } bool IsFullscreen() { return bIsFullscreen; }
uint32_t GetCurrentWidth(); uint32_t GetCurrentWidth();
uint32_t GetCurrentHeight(); uint32_t GetCurrentHeight();
ControlDeck* GetControlDeck() { return ControllerApi; }; uint32_t GetMenuBar() { return dwMenubar; }
uint32_t dwMenubar; void SetMenuBar(uint32_t dwMenuBar) { this->dwMenubar = dwMenuBar; }
std::shared_ptr<ControlDeck> GetControlDeck() { return ControllerApi; };
std::shared_ptr<GlobalCtx2> GetContext() { return Context.lock(); } std::shared_ptr<GlobalCtx2> GetContext() { return Context.lock(); }
std::shared_ptr<AudioPlayer> GetAudioPlayer() { return APlayer; } std::shared_ptr<AudioPlayer> GetAudioPlayer() { return APlayer; }
const char* GetKeyName(int scancode) { return WmApi->get_key_name(scancode); } const char* GetKeyName(int scancode) { return WmApi->get_key_name(scancode); }
@ -48,15 +47,18 @@ namespace Ship {
static bool KeyUp(int32_t dwScancode); static bool KeyUp(int32_t dwScancode);
static void AllKeysUp(void); static void AllKeysUp(void);
static void OnFullscreenChanged(bool bIsNowFullscreen); static void OnFullscreenChanged(bool bIsNowFullscreen);
void SetAudioPlayer(); void InitializeControlDeck();
void InitializeAudioPlayer();
std::weak_ptr<GlobalCtx2> Context; std::weak_ptr<GlobalCtx2> Context;
std::shared_ptr<AudioPlayer> APlayer; std::shared_ptr<AudioPlayer> APlayer;
std::shared_ptr<ControlDeck> ControllerApi;
GfxRenderingAPI* RenderingApi; GfxRenderingAPI* RenderingApi;
GfxWindowManagerAPI* WmApi; GfxWindowManagerAPI* WmApi;
bool bIsFullscreen; bool bIsFullscreen;
uint32_t dwWidth; uint32_t dwWidth;
uint32_t dwHeight; uint32_t dwHeight;
uint32_t dwMenubar;
}; };
} }

View File

@ -1306,10 +1306,11 @@ extern "C" uint32_t OTRGetCurrentHeight() {
} }
extern "C" void OTRControllerCallback(ControllerCallback* controller) { extern "C" void OTRControllerCallback(ControllerCallback* controller) {
const auto controllers = Ship::Window::ControllerApi->virtualDevices; auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
for (int i = 0; i < controllers.size(); ++i) { for (int i = 0; i < controlDeck->GetNumVirtualDevices(); ++i) {
Ship::Window::ControllerApi->physicalDevices[controllers[i]]->WriteToSource(i, controller); auto physicalDevice = controlDeck->GetPhysicalDevice(controlDeck->GetVirtualDevice(i));
physicalDevice->WriteToSource(i, controller);
} }
} }
@ -1363,11 +1364,11 @@ extern "C" void AudioPlayer_Play(const uint8_t* buf, uint32_t len) {
} }
extern "C" int Controller_ShouldRumble(size_t i) { extern "C" int Controller_ShouldRumble(size_t i) {
auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
const auto controllers = Ship::Window::ControllerApi->virtualDevices; for (int i = 0; i < controlDeck->GetNumVirtualDevices(); ++i) {
auto physicalDevice = controlDeck->GetPhysicalDevice(controlDeck->GetVirtualDevice(i));
for (const auto virtual_entry : controllers) { if (physicalDevice->CanRumble()) {
if (Ship::Window::ControllerApi->physicalDevices[virtual_entry]->CanRumble()) {
return 1; return 1;
} }
} }