Debian Codegraph @

xbmc/games/controllers/dialogs/GUIDialogAxisDetection.cpp

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 #include "GUIDialogAxisDetection.h"
10
11 #include "guilib/LocalizeStrings.h"
12 #include "input/joysticks/DriverPrimitive.h"
13 #include "input/joysticks/JoystickTranslator.h"
14 #include "input/joysticks/interfaces/IButtonMap.h"
15 #include "utils/StringUtils.h"
16
17 #include <algorithm>
18
19 using namespace KODI;
20 using namespace GAME;
21
22 std::string CGUIDialogAxisDetection::GetDialogText()
23 {
24 // "Press all analog buttons now to detect them:[CR][CR]%s"
25 const std::string& dialogText = g_localizeStrings.Get(35020);
26
27 std::vector<std::string> primitives;
28
29 for (const auto& axisEntry : m_detectedAxes)
30 {
31 JOYSTICK::CDriverPrimitive axis(axisEntry.second, 0, JOYSTICK::SEMIAXIS_DIRECTION::POSITIVE, 1);
32 primitives.emplace_back(JOYSTICK::CJoystickTranslator::GetPrimitiveName(axis));
33 }
34
35 return StringUtils::Format(dialogText, StringUtils::Join(primitives, " | "));
36 }
37
38 std::string CGUIDialogAxisDetection::GetDialogHeader()
39 {
40 return g_localizeStrings.Get(35058); // "Controller Configuration"
41 }
42
43 bool CGUIDialogAxisDetection::MapPrimitiveInternal(JOYSTICK::IButtonMap* buttonMap,
44 KEYMAP::IKeymap* keymap,
45 const JOYSTICK::CDriverPrimitive& primitive)
46 {
47 if (primitive.Type() == JOYSTICK::PRIMITIVE_TYPE::SEMIAXIS)
48 AddAxis(buttonMap->Location(), primitive.Index());
49
50 return true;
51 }
52
53 bool CGUIDialogAxisDetection::AcceptsPrimitive(JOYSTICK::PRIMITIVE_TYPE type) const
54 {
55 switch (type)
56 {
57 case JOYSTICK::PRIMITIVE_TYPE::SEMIAXIS:
58 return true;
59 default:
60 break;
61 }
62
63 return false;
64 }
65
66 void CGUIDialogAxisDetection::OnLateAxis(const JOYSTICK::IButtonMap* buttonMap,
67 unsigned int axisIndex)
68 {
69 AddAxis(buttonMap->Location(), axisIndex);
70 }
71
72 void CGUIDialogAxisDetection::AddAxis(const std::string& deviceLocation, unsigned int axisIndex)
73 {
74 auto it = std::find_if(m_detectedAxes.begin(), m_detectedAxes.end(),
75 [&deviceLocation, axisIndex](const AxisEntry& axis)
76 { return axis.first == deviceLocation && axis.second == axisIndex; });
77
78 if (it == m_detectedAxes.end())
79 {
80 m_detectedAxes.emplace_back(std::make_pair(deviceLocation, axisIndex));
81 m_captureEvent.Set();
82 }
83 }