| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControlReference/ControlReference.h" |
| 5 | |
| 6 | using namespace ciface::ExpressionParser; |
| 7 | |
| 8 | static thread_local bool tls_input_gate = true; |
| 9 | |
| 10 | void ControlReference::SetInputGate(bool enable) |
| 11 | { |
| 12 | tls_input_gate = enable; |
| 13 | } |
| 14 | |
| 15 | bool ControlReference::GetInputGate() |
| 16 | { |
| 17 | return tls_input_gate; |
| 18 | } |
| 19 | |
| 20 | // |
| 21 | // UpdateReference |
| 22 | // |
| 23 | // Updates a controlreference's binded devices/controls |
| 24 | // need to call this to re-bind a control reference after changing its expression |
| 25 | // |
| 26 | void ControlReference::UpdateReference(ciface::ExpressionParser::ControlEnvironment& env) |
| 27 | { |
| 28 | if (m_parsed_expression) |
| 29 | { |
| 30 | m_parsed_expression->UpdateReferences(env); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | int ControlReference::BoundCount() const |
| 35 | { |
| 36 | if (m_parsed_expression) |
| 37 | return m_parsed_expression->CountNumControls(); |
| 38 | else |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | ParseStatus ControlReference::GetParseStatus() const |
| 43 | { |
| 44 | return m_parse_status; |
| 45 | } |
| 46 | |
| 47 | std::string ControlReference::GetExpression() const |
| 48 | { |
| 49 | return m_expression; |
| 50 | } |
| 51 | |
| 52 | std::optional<std::string> ControlReference::SetExpression(std::string expr) |
| 53 | { |
| 54 | m_expression = std::move(expr); |
| 55 | auto parse_result = ParseExpression(m_expression); |
| 56 | m_parse_status = parse_result.status; |
| 57 | m_parsed_expression = std::move(parse_result.expr); |
| 58 | return parse_result.description; |
| 59 | } |
| 60 | |
| 61 | ControlReference::ControlReference() = default; |
| 62 | |
| 63 | ControlReference::~ControlReference() = default; |
| 64 | |
| 65 | InputReference::InputReference() : ControlReference() |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | OutputReference::OutputReference() : ControlReference() |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | bool InputReference::IsInput() const |
| 74 | { |
| 75 | return true; |
| 76 | } |
| 77 | bool OutputReference::IsInput() const |
| 78 | { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | // |
| 83 | // InputReference :: State |
| 84 | // |
| 85 | // Gets the state of an input reference |
| 86 | // override function for ControlReference::State ... |
| 87 | // |
| 88 | ControlState InputReference::State(const ControlState ignore) |
| 89 | { |
| 90 | if (m_parsed_expression && GetInputGate()) |
| 91 | return m_parsed_expression->GetValue() * range; |
| 92 | return 0.0; |
| 93 | } |
| 94 | |
| 95 | // |
| 96 | // OutputReference :: State |
| 97 | // |
| 98 | // Set the state of all binded outputs |
| 99 | // overrides ControlReference::State .. combined them so I could make the GUI simple / inputs == |
| 100 | // same as outputs one list |
| 101 | // I was lazy and it works so watever |
| 102 | // |
| 103 | ControlState OutputReference::State(const ControlState state) |
| 104 | { |
| 105 | if (m_parsed_expression) |
| 106 | m_parsed_expression->SetValue(state * range); |
| 107 | return 0.0; |
| 108 | } |