Debian Codegraph @

xbmc/games/controllers/windows/GUIFeatureList.cpp

raw
1 /*
2 * Copyright (C) 2014-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 "GUIFeatureList.h"
10
11 #include "GUIConfigurationWizard.h"
12 #include "GUIControllerDefines.h"
13 #include "games/addons/GameClient.h"
14 #include "games/addons/input/GameClientInput.h"
15 #include "games/controllers/Controller.h"
16 #include "games/controllers/guicontrols/GUIFeatureButton.h"
17 #include "games/controllers/guicontrols/GUIFeatureControls.h"
18 #include "games/controllers/guicontrols/GUIFeatureFactory.h"
19 #include "games/controllers/guicontrols/GUIFeatureTranslator.h"
20 #include "games/controllers/input/PhysicalFeature.h"
21 #include "guilib/GUIButtonControl.h"
22 #include "guilib/GUIControlGroupList.h"
23 #include "guilib/GUIImage.h"
24 #include "guilib/GUILabelControl.h"
25 #include "guilib/GUIWindow.h"
26 #include "guilib/LocalizeStrings.h"
27
28 using namespace KODI;
29 using namespace GAME;
30
31 CGUIFeatureList::CGUIFeatureList(CGUIWindow* window, GameClientPtr gameClient)
32 : m_window(window), m_gameClient(std::move(gameClient)), m_wizard(new CGUIConfigurationWizard)
33 {
34 }
35
36 CGUIFeatureList::~CGUIFeatureList(void)
37 {
38 Deinitialize();
39 delete m_wizard;
40 }
41
42 bool CGUIFeatureList::Initialize(void)
43 {
44 m_guiList = dynamic_cast<CGUIControlGroupList*>(m_window->GetControl(CONTROL_FEATURE_LIST));
45 m_guiButtonTemplate =
46 dynamic_cast<CGUIButtonControl*>(m_window->GetControl(CONTROL_FEATURE_BUTTON_TEMPLATE));
47 m_guiGroupTitle =
48 dynamic_cast<CGUILabelControl*>(m_window->GetControl(CONTROL_FEATURE_GROUP_TITLE));
49 m_guiFeatureSeparator = dynamic_cast<CGUIImage*>(m_window->GetControl(CONTROL_FEATURE_SEPARATOR));
50
51 if (m_guiButtonTemplate)
52 m_guiButtonTemplate->SetVisible(false);
53
54 if (m_guiGroupTitle)
55 m_guiGroupTitle->SetVisible(false);
56
57 if (m_guiFeatureSeparator)
58 m_guiFeatureSeparator->SetVisible(false);
59
60 return m_guiList != nullptr && m_guiButtonTemplate != nullptr;
61 }
62
63 void CGUIFeatureList::Deinitialize(void)
64 {
65 CleanupButtons();
66
67 m_guiList = nullptr;
68 m_guiButtonTemplate = nullptr;
69 m_guiGroupTitle = nullptr;
70 m_guiFeatureSeparator = nullptr;
71 }
72
73 void CGUIFeatureList::Load(const ControllerPtr& controller)
74 {
75 if (m_controller && m_controller->ID() == controller->ID())
76 return; // Already loaded
77
78 CleanupButtons();
79
80 // Set new controller
81 m_controller = controller;
82
83 // Get features
84 const std::vector<CPhysicalFeature>& features = controller->Features();
85
86 // Split into groups
87 auto featureGroups = GetFeatureGroups(features);
88
89 // Create controls
90 m_buttonCount = 0;
91 for (auto itGroup = featureGroups.begin(); itGroup != featureGroups.end(); ++itGroup)
92 {
93 const std::string& groupName = itGroup->groupName;
94 const bool bIsVirtualKey = itGroup->bIsVirtualKey;
95
96 std::vector<CGUIButtonControl*> buttons;
97
98 // Create buttons
99 if (bIsVirtualKey)
100 {
101 CGUIButtonControl* button = GetSelectKeyButton(itGroup->features, m_buttonCount);
102 if (button != nullptr)
103 buttons.push_back(button);
104 }
105 else
106 {
107 buttons = GetButtons(itGroup->features, m_buttonCount);
108 }
109
110 // Just in case
111 if (m_buttonCount + buttons.size() >= MAX_FEATURE_COUNT)
112 break;
113
114 // Add a separator if the group list isn't empty
115 if (m_guiFeatureSeparator && m_guiList->GetTotalSize() > 0)
116 {
117 CGUIFeatureSeparator* pSeparator =
118 new CGUIFeatureSeparator(*m_guiFeatureSeparator, m_buttonCount);
119 m_guiList->AddControl(pSeparator);
120 }
121
122 // Add the group title
123 if (m_guiGroupTitle && !groupName.empty())
124 {
125 CGUIFeatureGroupTitle* pGroupTitle =
126 new CGUIFeatureGroupTitle(*m_guiGroupTitle, groupName, m_buttonCount);
127 m_guiList->AddControl(pGroupTitle);
128 }
129
130 // Add the buttons
131 for (CGUIButtonControl* pButton : buttons)
132 m_guiList->AddControl(pButton);
133
134 m_buttonCount += static_cast<unsigned int>(buttons.size());
135 }
136 }
137
138 void CGUIFeatureList::OnSelect(unsigned int buttonIndex)
139 {
140 // Generate list of buttons for the wizard
141 std::vector<IFeatureButton*> buttons;
142 for (; buttonIndex < m_buttonCount; buttonIndex++)
143 {
144 IFeatureButton* control = GetButtonControl(buttonIndex);
145 if (control == nullptr)
146 continue;
147
148 if (control->AllowWizard())
149 buttons.push_back(control);
150 else
151 {
152 // Only map this button if it's the only one
153 if (buttons.empty())
154 buttons.push_back(control);
155 break;
156 }
157 }
158
159 m_wizard->Run(m_controller->ID(), buttons);
160 }
161
162 IFeatureButton* CGUIFeatureList::GetButtonControl(unsigned int buttonIndex)
163 {
164 CGUIControl* control = m_guiList->GetControl(CONTROL_FEATURE_BUTTONS_START + buttonIndex);
165
166 return static_cast<IFeatureButton*>(dynamic_cast<CGUIFeatureButton*>(control));
167 }
168
169 void CGUIFeatureList::CleanupButtons(void)
170 {
171 m_buttonCount = 0;
172
173 m_wizard->Abort(true);
174 m_wizard->UnregisterKeys();
175
176 if (m_guiList)
177 m_guiList->ClearAll();
178 }
179
180 std::vector<CGUIFeatureList::FeatureGroup> CGUIFeatureList::GetFeatureGroups(
181 const std::vector<CPhysicalFeature>& features) const
182 {
183 std::vector<FeatureGroup> groups;
184
185 // Get group names
186 std::vector<std::string> groupNames;
187 for (const CPhysicalFeature& feature : features)
188 {
189 // Skip features not supported by the game client
190 if (m_gameClient)
191 {
192 if (!m_gameClient->Input().HasFeature(m_controller->ID(), feature.Name()))
193 continue;
194 }
195
196 bool bAdded = false;
197
198 if (!groups.empty())
199 {
200 FeatureGroup& previousGroup = *groups.rbegin();
201 if (feature.CategoryLabel() == previousGroup.groupName)
202 {
203 // Add feature to previous group
204 previousGroup.features.emplace_back(feature);
205 bAdded = true;
206
207 // If feature is a key, add it to the preceding virtual group as well
208 if (feature.Category() == JOYSTICK::FEATURE_CATEGORY::KEY && groups.size() >= 2)
209 {
210 FeatureGroup& virtualGroup = *(groups.rbegin() + 1);
211 if (virtualGroup.bIsVirtualKey)
212 virtualGroup.features.emplace_back(feature);
213 }
214 }
215 }
216
217 if (!bAdded)
218 {
219 // If feature is a key, create a virtual group that allows the user to
220 // select which key to map
221 if (feature.Category() == JOYSTICK::FEATURE_CATEGORY::KEY)
222 {
223 FeatureGroup virtualGroup;
224 virtualGroup.groupName = g_localizeStrings.Get(35166); // "All keys"
225 virtualGroup.bIsVirtualKey = true;
226 virtualGroup.features.emplace_back(feature);
227 groups.emplace_back(std::move(virtualGroup));
228 }
229
230 // Create new group and add feature
231 FeatureGroup group;
232 group.groupName = feature.CategoryLabel();
233 group.features.emplace_back(feature);
234 groups.emplace_back(std::move(group));
235 }
236 }
237
238 // If there are no features, add an empty group
239 if (groups.empty())
240 {
241 FeatureGroup group;
242 group.groupName = g_localizeStrings.Get(35022); // "Nothing to map"
243 groups.emplace_back(std::move(group));
244 }
245
246 return groups;
247 }
248
249 bool CGUIFeatureList::HasButton(JOYSTICK::FEATURE_TYPE type) const
250 {
251 return CGUIFeatureTranslator::GetButtonType(type) != BUTTON_TYPE::UNKNOWN;
252 }
253
254 std::vector<CGUIButtonControl*> CGUIFeatureList::GetButtons(
255 const std::vector<CPhysicalFeature>& features, unsigned int startIndex)
256 {
257 std::vector<CGUIButtonControl*> buttons;
258
259 // Create buttons
260 unsigned int buttonIndex = startIndex;
261 for (const CPhysicalFeature& feature : features)
262 {
263 BUTTON_TYPE buttonType = CGUIFeatureTranslator::GetButtonType(feature.Type());
264
265 CGUIButtonControl* pButton = CGUIFeatureFactory::CreateButton(buttonType, *m_guiButtonTemplate,
266 m_wizard, feature, buttonIndex);
267
268 // If successful, add button to result
269 if (pButton != nullptr)
270 {
271 buttons.push_back(pButton);
272 buttonIndex++;
273 }
274 }
275
276 return buttons;
277 }
278
279 CGUIButtonControl* CGUIFeatureList::GetSelectKeyButton(
280 const std::vector<CPhysicalFeature>& features, unsigned int buttonIndex)
281 {
282 // Expose keycodes to the wizard
283 for (const CPhysicalFeature& feature : features)
284 {
285 if (feature.Type() == JOYSTICK::FEATURE_TYPE::KEY)
286 m_wizard->RegisterKey(feature);
287 }
288
289 return CGUIFeatureFactory::CreateButton(BUTTON_TYPE::SELECT_KEY, *m_guiButtonTemplate, m_wizard,
290 CPhysicalFeature(), buttonIndex);
291 }