Debian Codegraph @

Source/Core/InputCommon/ControllerInterface/MappingCommon.h

raw symbols SCIP
1 // Copyright 2022 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <string>
7
8 #include "Common/Matrix.h"
9 #include "InputCommon/ControllerEmu/StickGate.h"
10 #include "InputCommon/ControllerInterface/CoreDevice.h"
11
12 namespace ControllerEmu
13 {
14 class EmulatedController;
15 } // namespace ControllerEmu
16
17 namespace ciface::MappingCommon
18 {
19 enum class Quote
20 {
21 On,
22 Off
23 };
24
25 std::string GetExpressionForControl(const std::string& control_name,
26 const Core::DeviceQualifier& control_device,
27 const Core::DeviceQualifier& default_device,
28 Quote quote = Quote::On);
29
30 std::string BuildExpression(const Core::InputDetector::Results&,
31 const Core::DeviceQualifier& default_device, Quote quote);
32
33 void RemoveSpuriousTriggerCombinations(Core::InputDetector::Results*);
34 void RemoveDetectionsAfterTimePoint(Core::InputDetector::Results*, Clock::time_point after);
35 bool ContainsCompleteDetection(const Core::InputDetector::Results&);
36
37 // class for detecting four directional input mappings in sequence.
38 class ReshapableInputMapper
39 {
40 public:
41 // Four cardinal directions.
42 static constexpr std::size_t REQUIRED_INPUT_COUNT = 4;
43
44 // Caller should hold the "StateLock".
45 ReshapableInputMapper(const Core::DeviceContainer& container,
46 std::span<const std::string> device_strings);
47
48 // Reads inputs and updates internal state.
49 // Returns true if an input was detected in this call.
50 // (useful for UI animation)
51 // Caller should hold the "StateLock".
52 bool Update();
53
54 // A counter-clockwise angle in radians for the currently desired input direction.
55 // Used for a graphical indicator in the UI.
56 // 0 == East
57 float GetCurrentAngle() const;
58
59 // True if all four directions have been detected or the timer expired.
60 bool IsComplete() const;
61
62 // Returns true if "analog" inputs were detected and calibration should be performed.
63 // Must use *before* ApplyResults.
64 bool IsCalibrationNeeded() const;
65
66 // Use when IsComplete returns true.
67 // Updates the mappings on the provided ReshapableInput.
68 // Caller should hold the "StateLock".
69 bool ApplyResults(ControllerEmu::EmulatedController*, ControllerEmu::ReshapableInput* stick);
70
71 private:
72 Core::InputDetector m_input_detector;
73 };
74
75 class CalibrationBuilder
76 {
77 public:
78 // Provide nullopt if you want to calibrate the center on first Update.
79 explicit CalibrationBuilder(std::optional<Common::DVec2> center = Common::DVec2{});
80
81 // Updates the calibration data using the provided point and the previous point.
82 void Update(Common::DVec2 point);
83
84 // Returns true when the calibration data seems to be reasonably filled in.
85 // Used to update the UI to encourage the user to click the "Finish" button.
86 bool IsCalibrationDataSensible() const;
87
88 // Returns true when the calibration data seems sensible,
89 // and the input then approaches the center position.
90 bool IsComplete() const;
91
92 // Grabs the calibration value at the provided angle.
93 // Used to render the calibration in the UI while it's in progress.
94 ControlState GetCalibrationRadiusAtAngle(double angle) const;
95
96 // Sets the calibration data of the provided ReshapableInput.
97 // Caller should hold the "StateLock".
98 void ApplyResults(ControllerEmu::ReshapableInput* stick);
99
100 Common::DVec2 GetCenter() const;
101
102 private:
103 ControllerEmu::ReshapableInput::CalibrationData m_calibration_data;
104
105 std::optional<Common::DVec2> m_center = std::nullopt;
106
107 Common::DVec2 m_prev_point{};
108 };
109
110 } // namespace ciface::MappingCommon