| 1 |
/* |
| 2 |
* Copyright (C) 2017-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "ControllerGrid.h" |
| 10 |
|
| 11 |
#include "games/controllers/Controller.h" |
| 12 |
#include "utils/log.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <utility> |
| 16 |
|
| 17 |
using namespace KODI; |
| 18 |
using namespace GAME; |
| 19 |
|
| 20 |
CControllerGrid::~CControllerGrid() = default; |
| 21 |
|
| 22 |
void CControllerGrid::SetControllerTree(const CControllerTree& controllerTree) |
| 23 |
{ |
| 24 |
// Clear the result |
| 25 |
m_grid.clear(); |
| 26 |
|
| 27 |
m_height = AddPorts(controllerTree.GetPorts(), m_grid); |
| 28 |
SetHeight(m_height, m_grid); |
| 29 |
} |
| 30 |
|
| 31 |
ControllerVector CControllerGrid::GetControllers(unsigned int playerIndex) const |
| 32 |
{ |
| 33 |
ControllerVector controllers; |
| 34 |
|
| 35 |
if (playerIndex < m_grid.size()) |
| 36 |
{ |
| 37 |
for (const auto& controllerVertex : m_grid[playerIndex].vertices) |
| 38 |
{ |
| 39 |
if (controllerVertex.controller) |
| 40 |
controllers.emplace_back(controllerVertex.controller); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return controllers; |
| 45 |
} |
| 46 |
|
| 47 |
unsigned int CControllerGrid::AddPorts(const PortVec& ports, ControllerGrid& grid) |
| 48 |
{ |
| 49 |
unsigned int height = 0; |
| 50 |
|
| 51 |
auto itKeyboard = |
| 52 |
std::find_if(ports.begin(), ports.end(), |
| 53 |
[](const CPortNode& port) { return port.GetPortType() == PORT_TYPE::KEYBOARD; }); |
| 54 |
|
| 55 |
auto itMouse = |
| 56 |
std::find_if(ports.begin(), ports.end(), |
| 57 |
[](const CPortNode& port) { return port.GetPortType() == PORT_TYPE::MOUSE; }); |
| 58 |
|
| 59 |
auto itController = std::find_if(ports.begin(), ports.end(), |
| 60 |
[](const CPortNode& port) |
| 61 |
{ return port.GetPortType() == PORT_TYPE::CONTROLLER; }); |
| 62 |
|
| 63 |
// Keyboard and mouse are not allowed to have ports because they might |
| 64 |
// overlap with controllers |
| 65 |
if (itKeyboard != ports.end() && itKeyboard->GetActiveController().GetHub().HasPorts()) |
| 66 |
{ |
| 67 |
CLog::Log(LOGERROR, "Found keyboard with controller ports, skipping"); |
| 68 |
itKeyboard = ports.end(); |
| 69 |
} |
| 70 |
if (itMouse != ports.end() && itMouse->GetActiveController().GetHub().HasPorts()) |
| 71 |
{ |
| 72 |
CLog::Log(LOGERROR, "Found mouse with controller ports, skipping"); |
| 73 |
itMouse = ports.end(); |
| 74 |
} |
| 75 |
|
| 76 |
if (itController != ports.end()) |
| 77 |
{ |
| 78 |
// Add controller ports |
| 79 |
bool bFirstPlayer = true; |
| 80 |
for (const CPortNode& port : ports) |
| 81 |
{ |
| 82 |
ControllerColumn column; |
| 83 |
|
| 84 |
if (port.GetPortType() == PORT_TYPE::CONTROLLER) |
| 85 |
{ |
| 86 |
// Add controller |
| 87 |
height = |
| 88 |
std::max(height, AddController(port, static_cast<unsigned int>(column.vertices.size()), |
| 89 |
column.vertices, grid)); |
| 90 |
|
| 91 |
if (bFirstPlayer) |
| 92 |
{ |
| 93 |
bFirstPlayer = false; |
| 94 |
|
| 95 |
// Keyboard and mouse are added below the first controller |
| 96 |
if (itKeyboard != ports.end()) |
| 97 |
height = |
| 98 |
std::max(height, AddController(*itKeyboard, |
| 99 |
static_cast<unsigned int>(column.vertices.size()), |
| 100 |
column.vertices, grid)); |
| 101 |
if (itMouse != ports.end()) |
| 102 |
height = std::max( |
| 103 |
height, AddController(*itMouse, static_cast<unsigned int>(column.vertices.size()), |
| 104 |
column.vertices, grid)); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (!column.vertices.empty()) |
| 109 |
grid.emplace_back(std::move(column)); |
| 110 |
} |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
// No controllers, add keyboard and mouse |
| 115 |
ControllerColumn column; |
| 116 |
|
| 117 |
if (itKeyboard != ports.end()) |
| 118 |
height = std::max(height, AddController(*itKeyboard, |
| 119 |
static_cast<unsigned int>(column.vertices.size()), |
| 120 |
column.vertices, grid)); |
| 121 |
if (itMouse != ports.end()) |
| 122 |
height = std::max(height, |
| 123 |
AddController(*itMouse, static_cast<unsigned int>(column.vertices.size()), |
| 124 |
column.vertices, grid)); |
| 125 |
|
| 126 |
if (!column.vertices.empty()) |
| 127 |
grid.emplace_back(std::move(column)); |
| 128 |
} |
| 129 |
|
| 130 |
return height; |
| 131 |
} |
| 132 |
|
| 133 |
unsigned int CControllerGrid::AddController(const CPortNode& port, |
| 134 |
unsigned int height, |
| 135 |
std::vector<ControllerVertex>& column, |
| 136 |
ControllerGrid& grid) |
| 137 |
{ |
| 138 |
// Add spacers |
| 139 |
while (column.size() < height) |
| 140 |
AddInvisible(column); |
| 141 |
|
| 142 |
const CControllerNode& activeController = port.GetActiveController(); |
| 143 |
|
| 144 |
// Add vertex |
| 145 |
ControllerVertex vertex; |
| 146 |
vertex.bVisible = true; |
| 147 |
vertex.bConnected = port.IsConnected(); |
| 148 |
vertex.portType = port.GetPortType(); |
| 149 |
vertex.controller = activeController.GetController(); |
| 150 |
vertex.address = activeController.GetControllerAddress(); |
| 151 |
for (const CControllerNode& node : port.GetCompatibleControllers()) |
| 152 |
vertex.compatible.emplace_back(node.GetController()); |
| 153 |
column.emplace_back(std::move(vertex)); |
| 154 |
|
| 155 |
height++; |
| 156 |
|
| 157 |
// Process ports |
| 158 |
const PortVec& ports = activeController.GetHub().GetPorts(); |
| 159 |
if (!ports.empty()) |
| 160 |
{ |
| 161 |
switch (GetDirection(activeController)) |
| 162 |
{ |
| 163 |
case GRID_DIRECTION::RIGHT: |
| 164 |
{ |
| 165 |
height = std::max(height, AddHub(ports, height - 1, false, grid)); |
| 166 |
break; |
| 167 |
} |
| 168 |
case GRID_DIRECTION::DOWN: |
| 169 |
{ |
| 170 |
const unsigned int row = height; |
| 171 |
|
| 172 |
// Add the first controller to the column |
| 173 |
const CPortNode firstController = ports.at(0); |
| 174 |
height = std::max(height, AddController(firstController, row, column, grid)); |
| 175 |
|
| 176 |
// Add the remaining controllers on the same row |
| 177 |
height = std::max(height, AddHub(ports, row, true, grid)); |
| 178 |
|
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return height; |
| 185 |
} |
| 186 |
|
| 187 |
unsigned int CControllerGrid::AddHub(const PortVec& ports, |
| 188 |
unsigned int height, |
| 189 |
bool bSkipFirst, |
| 190 |
ControllerGrid& grid) |
| 191 |
{ |
| 192 |
const unsigned int row = height; |
| 193 |
|
| 194 |
unsigned int port = 0; |
| 195 |
for (const auto& controllerPort : ports) |
| 196 |
{ |
| 197 |
// If controller has no player, it has already added the hub's first controller |
| 198 |
if (bSkipFirst && port == 0) |
| 199 |
continue; |
| 200 |
|
| 201 |
// Add a column for this controller |
| 202 |
grid.emplace_back(); |
| 203 |
ControllerColumn& column = grid.back(); |
| 204 |
|
| 205 |
height = std::max(height, AddController(controllerPort, row, column.vertices, grid)); |
| 206 |
|
| 207 |
port++; |
| 208 |
} |
| 209 |
|
| 210 |
return height; |
| 211 |
} |
| 212 |
|
| 213 |
void CControllerGrid::AddInvisible(std::vector<ControllerVertex>& column) |
| 214 |
{ |
| 215 |
ControllerVertex vertex; |
| 216 |
vertex.bVisible = false; |
| 217 |
column.emplace_back(std::move(vertex)); |
| 218 |
} |
| 219 |
|
| 220 |
void CControllerGrid::SetHeight(unsigned int height, ControllerGrid& grid) |
| 221 |
{ |
| 222 |
for (auto& column : grid) |
| 223 |
{ |
| 224 |
while (static_cast<unsigned int>(column.vertices.size()) < height) |
| 225 |
AddInvisible(column.vertices); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
CControllerGrid::GRID_DIRECTION CControllerGrid::GetDirection(const CControllerNode& node) |
| 230 |
{ |
| 231 |
// Hub controllers are added horizontally, one per row. |
| 232 |
// |
| 233 |
// If the current controller offers a player spot, the row starts to the |
| 234 |
// right at the same height as the controller. |
| 235 |
// |
| 236 |
// Otherwise, to row starts below the current controller in the same |
| 237 |
// column. |
| 238 |
if (node.ProvidesInput()) |
| 239 |
return GRID_DIRECTION::RIGHT; |
| 240 |
else |
| 241 |
return GRID_DIRECTION::DOWN; |
| 242 |
} |