Skip to content

Commit

Permalink
Controller dialog: Filter controllers by current game add-on
Browse files Browse the repository at this point in the history
  • Loading branch information
garbear committed Jul 2, 2018
1 parent 1249e45 commit f952eaa
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 2 deletions.
51 changes: 51 additions & 0 deletions xbmc/games/controllers/types/ControllerTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ void CControllerNode::SetHub(CControllerHub hub)
m_hub.reset(new CControllerHub(std::move(hub)));
}

bool CControllerNode::IsControllerAccepted(const std::string &controllerId) const
{
bool bAccepted = false;

for (const auto &port : m_hub->Ports())
{
if (port.IsControllerAccepted(controllerId))
{
bAccepted = true;
break;
}
}

return bAccepted;
}

bool CControllerNode::IsControllerAccepted(const std::string &portAddress,
const std::string &controllerId) const
{
Expand Down Expand Up @@ -128,6 +144,24 @@ void CControllerPortNode::SetCompatibleControllers(ControllerNodeVec controllers
m_controllers = std::move(controllers);
}

bool CControllerPortNode::IsControllerAccepted(const std::string &controllerId) const
{
// Base case
CControllerPort port;
GetControllerPort(port);
if (port.IsCompatible(controllerId))
return true;

// Visit nodes
for (const auto &node : m_controllers)
{
if (node.IsControllerAccepted(controllerId))
return true;
}

return false;
}

bool CControllerPortNode::IsControllerAccepted(const std::string &portAddress,
const std::string &controllerId) const
{
Expand All @@ -143,6 +177,7 @@ bool CControllerPortNode::IsControllerAccepted(const std::string &portAddress,
}
else
{
// Visit nodes
for (const auto &node : m_controllers)
{
if (node.IsControllerAccepted(portAddress, controllerId))
Expand Down Expand Up @@ -184,6 +219,22 @@ void CControllerHub::SetPorts(ControllerPortVec ports)
m_ports = std::move(ports);
}

bool CControllerHub::IsControllerAccepted(const std::string &controllerId) const
{
bool bAccepted = false;

for (const CControllerPortNode &port : m_ports)
{
if (port.IsControllerAccepted(controllerId))
{
bAccepted = true;
break;
}
}

return bAccepted;
}

bool CControllerHub::IsControllerAccepted(const std::string &portAddress,
const std::string &controllerId) const
{
Expand Down
19 changes: 19 additions & 0 deletions xbmc/games/controllers/types/ControllerTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ namespace GAME
*/
bool IsValid() const { return m_controller.get() != nullptr; }

/*!
* \brief Check to see if a controller is compatible with a controller port
*
* \param controllerId The ID of the controller
*
* \return True if the controller is compatible with a port, false otherwise
*/
bool IsControllerAccepted(const std::string &controllerId) const;

/*!
* \brief Check to see if a controller is compatible with a controller port
*
Expand Down Expand Up @@ -163,6 +172,15 @@ namespace GAME
const ControllerNodeVec &CompatibleControllers() const { return m_controllers; }
void SetCompatibleControllers(ControllerNodeVec controllers);

/*!
* \brief Check to see if a controller is compatible with this tree
*
* \param controllerId The ID of the controller
*
* \return True if the controller is compatible with the tree, false otherwise
*/
bool IsControllerAccepted(const std::string &controllerId) const;

/*!
* \brief Check to see if a controller is compatible with this tree
*
Expand Down Expand Up @@ -206,6 +224,7 @@ namespace GAME
const ControllerPortVec &Ports() const { return m_ports; }
void SetPorts(ControllerPortVec ports);

bool IsControllerAccepted(const std::string &controllerId) const;
bool IsControllerAccepted(const std::string &portAddress,
const std::string &controllerId) const;

Expand Down
36 changes: 35 additions & 1 deletion xbmc/games/controllers/windows/GUIControllerList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
#include "GUIControllerWindow.h"
#include "GUIFeatureList.h"
#include "addons/AddonManager.h"
#include "cores/RetroPlayer/guibridge/GUIGameRenderManager.h"
#include "cores/RetroPlayer/guibridge/GUIGameSettingsHandle.h"
#include "dialogs/GUIDialogYesNo.h"
#include "games/addons/input/GameClientInput.h"
#include "games/addons/GameClient.h"
#include "games/controllers/types/ControllerTree.h"
#include "games/controllers/Controller.h"
#include "games/controllers/ControllerIDs.h"
#include "games/controllers/ControllerFeature.h"
Expand Down Expand Up @@ -68,6 +73,19 @@ bool CGUIControllerList::Initialize(void)
if (m_controllerButton)
m_controllerButton->SetVisible(false);

// Get active game add-on
GameClientPtr gameClient;
{
auto gameSettingsHandle = CServiceBroker::GetGameRenderManager().RegisterGameSettingsDialog();
if (gameSettingsHandle)
{
ADDON::AddonPtr addon;
if (CServiceBroker::GetAddonMgr().GetAddon(gameSettingsHandle->GameClientID(), addon, ADDON::ADDON_GAMEDLL))
gameClient = std::static_pointer_cast<CGameClient>(addon);
}
}
m_gameClient = std::move(gameClient);

CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CGUIControllerList::OnEvent);
Refresh();

Expand All @@ -79,6 +97,8 @@ void CGUIControllerList::Deinitialize(void)
{
CServiceBroker::GetAddonMgr().Events().Unsubscribe(this);

m_gameClient.reset();

CleanupButtons();

m_controllerList = nullptr;
Expand Down Expand Up @@ -166,7 +186,7 @@ bool CGUIControllerList::RefreshControllers(void)
CGameServices& gameServices = CServiceBroker::GetGameServices();
ControllerVector newControllers = gameServices.GetControllers();

// Don't show an empty list in the GUI
// Don't show an empty feature list in the GUI
auto HasButtonForFeature = [this](const CControllerFeature &feature)
{
return m_featureList->HasButton(feature.Type());
Expand All @@ -181,6 +201,20 @@ bool CGUIControllerList::RefreshControllers(void)

newControllers.erase(std::remove_if(newControllers.begin(), newControllers.end(), HasButtonForController), newControllers.end());

// Filter by current game add-on
if (m_gameClient)
{
const CControllerTree &controllers = m_gameClient->Input().GetControllerTree();

auto ControllerNotAccepted = [&controllers](const ControllerPtr &controller)
{
return !controllers.IsControllerAccepted(controller->ID());
};

if (!std::all_of(newControllers.begin(), newControllers.end(), ControllerNotAccepted))
newControllers.erase(std::remove_if(newControllers.begin(), newControllers.end(), ControllerNotAccepted), newControllers.end());
}

// Check for changes
std::set<std::string> oldControllerIds;
std::set<std::string> newControllerIds;
Expand Down
2 changes: 2 additions & 0 deletions xbmc/games/controllers/windows/GUIControllerList.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "IConfigurationWindow.h"
#include "addons/AddonEvents.h"
#include "addons/Addon.h"
#include "games/GameTypes.h"
#include "games/controllers/ControllerTypes.h"

#include <set>
Expand Down Expand Up @@ -68,6 +69,7 @@ namespace GAME
// Game stuff
ControllerVector m_controllers;
int m_focusedController;
GameClientPtr m_gameClient;
};
}
}
23 changes: 22 additions & 1 deletion xbmc/games/controllers/windows/GUIControllerWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include "addons/GUIWindowAddonBrowser.h"
#include "addons/IAddon.h"
#include "addons/AddonManager.h"
#include "cores/RetroPlayer/guibridge/GUIGameRenderManager.h"
#include "cores/RetroPlayer/guibridge/GUIGameSettingsHandle.h"
#include "games/addons/GameClient.h"
#include "games/controllers/dialogs/GUIDialogIgnoreInput.h"
#include "guilib/GUIButtonControl.h"
#include "guilib/GUIControl.h"
Expand Down Expand Up @@ -200,6 +203,19 @@ void CGUIControllerWindow::OnEvent(const ADDON::CRepositoryUpdater::RepositoryUp

void CGUIControllerWindow::OnInitWindow(void)
{
// Get active game add-on
GameClientPtr gameClient;
{
auto gameSettingsHandle = CServiceBroker::GetGameRenderManager().RegisterGameSettingsDialog();
if (gameSettingsHandle)
{
ADDON::AddonPtr addon;
if (CServiceBroker::GetAddonMgr().GetAddon(gameSettingsHandle->GameClientID(), addon, ADDON::ADDON_GAMEDLL))
gameClient = std::static_pointer_cast<CGameClient>(addon);
}
}
m_gameClient = std::move(gameClient);

CGUIDialog::OnInitWindow();

if (!m_featureList)
Expand Down Expand Up @@ -249,6 +265,8 @@ void CGUIControllerWindow::OnDeinitWindow(int nextWindowID)
}

CGUIDialog::OnDeinitWindow(nextWindowID);

m_gameClient.reset();
}

void CGUIControllerWindow::OnControllerFocused(unsigned int controllerIndex)
Expand Down Expand Up @@ -280,7 +298,10 @@ void CGUIControllerWindow::UpdateButtons(void)
using namespace ADDON;

VECADDONS addons;
CONTROL_ENABLE_ON_CONDITION(CONTROL_GET_MORE, CServiceBroker::GetAddonMgr().GetInstallableAddons(addons, ADDON::ADDON_GAME_CONTROLLER) && !addons.empty());
if (m_gameClient)
SET_CONTROL_HIDDEN(CONTROL_GET_MORE);
else
CONTROL_ENABLE_ON_CONDITION(CONTROL_GET_MORE, CServiceBroker::GetAddonMgr().GetInstallableAddons(addons, ADDON::ADDON_GAME_CONTROLLER) && !addons.empty());
}

void CGUIControllerWindow::GetMoreControllers(void)
Expand Down
4 changes: 4 additions & 0 deletions xbmc/games/controllers/windows/GUIControllerWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once

#include "addons/RepositoryUpdater.h"
#include "games/GameTypes.h"
#include "guilib/GUIDialog.h"

namespace KODI
Expand Down Expand Up @@ -61,6 +62,9 @@ namespace GAME

IControllerList* m_controllerList;
IFeatureList* m_featureList;

// Game paremeters
GameClientPtr m_gameClient;
};
}
}

0 comments on commit f952eaa

Please sign in to comment.