Debian Codegraph @

xbmc/games/controllers/types/ControllerGrid.h

raw
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 #pragma once
10
11 #include "ControllerTree.h"
12 #include "games/controllers/ControllerTypes.h"
13
14 #include <string>
15 #include <vector>
16
17 namespace KODI
18 {
19 namespace GAME
20 {
21 /*!
22 * \ingroup games
23 *
24 * \brief Vertex in the grid of controllers
25 */
26 struct ControllerVertex
27 {
28 bool bVisible = true;
29 bool bConnected = false;
30 ControllerPtr controller; // Mandatory if connected
31 PORT_TYPE portType = PORT_TYPE::UNKNOWN; // Optional
32 std::string address; // Optional
33 ControllerVector compatible; // Compatible controllers
34 };
35
36 /*!
37 * \ingroup games
38 *
39 * \brief Column of controllers in the grid
40 */
41 struct ControllerColumn
42 {
43 std::vector<ControllerVertex> vertices;
44 };
45
46 /*!
47 * \ingroup games
48 *
49 * \brief Collection of controllers in a grid layout
50 */
51 using ControllerGrid = std::vector<ControllerColumn>;
52
53 /*!
54 * \ingroup games
55 *
56 * \brief Class to encapsulate grid operations
57 */
58 class CControllerGrid
59 {
60 public:
61 CControllerGrid() = default;
62 CControllerGrid(const CControllerGrid& other) = default;
63 ~CControllerGrid();
64
65 /*!
66 * \brief Create a grid from a controller tree
67 */
68 void SetControllerTree(const CControllerTree& controllerTree);
69
70 /*!
71 * \brief Get the width of the controller grid
72 */
73 unsigned int GetWidth() const { return static_cast<unsigned int>(m_grid.size()); }
74
75 /*!
76 * \brief Get the height (deepest controller) of the controller grid
77 *
78 * The height is cached when the controller grid is created to avoid
79 * iterating the grid
80 */
81 unsigned int GetHeight() const { return m_height; }
82
83 /*!
84 * \brief Access the controller grid
85 */
86 const ControllerGrid& GetGrid() const { return m_grid; }
87
88 /*!
89 * \brief Get the controllers in use by the specified player
90 *
91 * \param playerIndex The column in the grid to get controllers from
92 */
93 ControllerVector GetControllers(unsigned int playerIndex) const;
94
95 private:
96 /*!
97 * \brief Directions of vertex traversal
98 */
99 enum class GRID_DIRECTION
100 {
101 RIGHT,
102 DOWN,
103 };
104
105 /*!
106 * \brief Add ports to the grid
107 *
108 * \param ports The ports on a console or controller
109 * \param[out] grid The controller grid being created
110 *
111 * \return The height of the grid determined by the maximum column height
112 */
113 static unsigned int AddPorts(const PortVec& ports, ControllerGrid& grid);
114
115 /*!
116 * \brief Draw a controller to the column at the specified height
117 *
118 * \param port The controller's port node
119 * \param height The height to draw the controller at
120 * \param column[in/out] The column to draw to
121 * \param grid[in/out] The grid to add additional columns to
122 *
123 * \return The height of the grid
124 */
125 static unsigned int AddController(const CPortNode& port,
126 unsigned int height,
127 std::vector<ControllerVertex>& column,
128 ControllerGrid& grid);
129
130 /*!
131 * \brief Draw a series of controllers to the grid at the specified height
132 *
133 * \param ports The ports of the controllers to draw
134 * \param height The height to start drawing the controllers at
135 * \param bSkipFirst True if the first controller has already been drawn to
136 * a column, false to start drawing at the first controller
137 * \param grid[in/out] The grid to add columns to
138 *
139 * \return The height of the grid
140 */
141 static unsigned int AddHub(const PortVec& ports,
142 unsigned int height,
143 bool bSkipFirst,
144 ControllerGrid& grid);
145
146 /*!
147 * \brief Draw an invisible vertex to the column
148 *
149 * \param[in/out] column The column in a controller grid
150 */
151 static void AddInvisible(std::vector<ControllerVertex>& column);
152
153 /*!
154 * \brief Fill all columns with invisible vertices until the specified height
155 *
156 * \param height The height to make all columns
157 * \param[in/out] grid The grid to update
158 */
159 static void SetHeight(unsigned int height, ControllerGrid& grid);
160
161 /*!
162 * \brief Get the direction of traversal for the next vertex
163 *
164 * \param node The node in the controller tree being visited
165 *
166 * \return The direction of the next vertex, or GRID_DIRECTION::UNKNOWN if
167 * unknown
168 */
169 static GRID_DIRECTION GetDirection(const CControllerNode& node);
170
171 ControllerGrid m_grid;
172 unsigned int m_height = 0;
173 };
174 } // namespace GAME
175 } // namespace KODI