Debian Codegraph @

Source/Core/InputCommon/ControlReference/ControlReference.h

raw symbols SCIP
1 // Copyright 2016 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <cmath>
7 #include <memory>
8
9 #include "InputCommon/ControlReference/ExpressionParser.h"
10 #include "InputCommon/ControllerInterface/CoreDevice.h"
11
12 namespace ControllerEmu
13 {
14 template <typename T>
15 T ControlStateCast(ControlState value);
16 }
17
18 // ControlReference
19 //
20 // These are what you create to actually use the inputs, InputReference or OutputReference.
21 //
22 // After being bound to devices and controls with UpdateReference,
23 // each one can link to multiple devices and controls
24 // when you change a ControlReference's expression,
25 // you must use UpdateReference on it to rebind controls
26 //
27
28 class ControlReference
29 {
30 public:
31 // Note: this is per thread.
32 static void SetInputGate(bool enable);
33 static bool GetInputGate();
34
35 virtual ~ControlReference();
36 virtual ControlState State(const ControlState state = 0) = 0;
37 virtual bool IsInput() const = 0;
38
39 template <typename T>
40 T GetState()
41 {
42 return ControllerEmu::ControlStateCast<T>(State());
43 }
44
45 int BoundCount() const;
46 ciface::ExpressionParser::ParseStatus GetParseStatus() const;
47 void UpdateReference(ciface::ExpressionParser::ControlEnvironment& env);
48 std::string GetExpression() const;
49
50 // Returns a human-readable error description when the given expression is invalid.
51 std::optional<std::string> SetExpression(std::string expr);
52
53 ControlState range = 1;
54
55 protected:
56 ControlReference();
57 std::string m_expression;
58 std::unique_ptr<ciface::ExpressionParser::Expression> m_parsed_expression;
59 ciface::ExpressionParser::ParseStatus m_parse_status =
60 ciface::ExpressionParser::ParseStatus::EmptyExpression;
61 };
62
63 namespace ControllerEmu
64 {
65 template <>
66 inline bool ControlStateCast<bool>(ControlState value)
67 {
68 // Round to nearest of 0 or 1.
69 return std::lround(value) > 0;
70 }
71
72 template <>
73 inline int ControlStateCast<int>(ControlState value)
74 {
75 return std::lround(value);
76 }
77
78 template <>
79 inline ControlState ControlStateCast<ControlState>(ControlState value)
80 {
81 return value;
82 }
83 } // namespace ControllerEmu
84
85 //
86 // InputReference
87 //
88 // Control reference for inputs
89 //
90 class InputReference : public ControlReference
91 {
92 public:
93 InputReference();
94 bool IsInput() const override;
95 ControlState State(const ControlState state) override;
96 };
97
98 //
99 // OutputReference
100 //
101 // Control reference for outputs
102 //
103 class OutputReference : public ControlReference
104 {
105 public:
106 OutputReference();
107 bool IsInput() const override;
108 ControlState State(const ControlState state) override;
109 };