summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControllerEmu/ControlGroup/MixedTriggers.cpp
blob: 2807603f6f9d3a042decb6171c57d0e028e8b25d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "InputCommon/ControllerEmu/ControlGroup/MixedTriggers.h"

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <string>

#include "Common/Common.h"
#include "Common/CommonTypes.h"

#include "InputCommon/ControllerEmu/Control/Control.h"

namespace ControllerEmu
{
MixedTriggers::MixedTriggers(const std::string& name_)
    : ControlGroup(name_, GroupType::MixedTriggers)
{
  AddDeadzoneSetting(&m_deadzone_setting, 25);

  AddSetting(&m_threshold_setting,
             {_trans("Threshold"),
              // i18n: The percent symbol.
              _trans("%"),
              // i18n: Refers to the "threshold" setting for pressure sensitive gamepad inputs.
              _trans("Input strength required for activation.")},
             90, 1, 100);
}

void MixedTriggers::GetState(u16* const digital, const u16* bitmasks, ControlState* analog,
                             bool adjusted) const
{
  const ControlState threshold = GetThreshold();
  ControlState deadzone = GetDeadzone();

  // Return raw values. (used in UI)
  if (!adjusted)
  {
    deadzone = 0.0;
  }

  const int trigger_count = int(controls.size() / 2);
  for (int i = 0; i != trigger_count; ++i)
  {
    const ControlState button_value = ApplyDeadzone(controls[i]->GetState(), deadzone);
    ControlState analog_value =
        std::min(ApplyDeadzone(controls[trigger_count + i]->GetState(), deadzone), 1.0);

    // Apply threshold:
    if (button_value >= threshold)
    {
      // Fully activate analog:
      analog_value = 1.0;

      // Activate button:
      *digital |= bitmasks[i];
    }

    analog[i] = analog_value;
  }
}

void MixedTriggers::GetState(u16* digital, const u16* bitmasks, ControlState* analog,
                             const InputOverrideFunction& override_func, bool adjusted) const
{
  if (!override_func)
    return GetState(digital, bitmasks, analog, adjusted);

  const ControlState threshold = GetThreshold();
  ControlState deadzone = GetDeadzone();

  // Return raw values. (used in UI)
  if (!adjusted)
  {
    deadzone = 0.0;
  }

  const int trigger_count = int(controls.size() / 2);
  for (int i = 0; i != trigger_count; ++i)
  {
    bool button_bool = false;
    const ControlState button_value = ApplyDeadzone(controls[i]->GetState(), deadzone);
    ControlState analog_value = ApplyDeadzone(controls[trigger_count + i]->GetState(), deadzone);

    // Apply threshold:
    if (button_value >= threshold)
    {
      analog_value = 1.0;
      button_bool = true;
    }

    if (const std::optional<ControlState> button_override =
            override_func(name, controls[i]->name, static_cast<ControlState>(button_bool)))
    {
      button_bool = std::lround(*button_override) > 0;
    }

    if (const std::optional<ControlState> analog_override =
            override_func(name, controls[trigger_count + i]->name, analog_value))
    {
      analog_value = *analog_override;
    }

    if (button_bool)
      *digital |= bitmasks[i];
    analog[i] = std::min(analog_value, 1.0);
  }
}

ControlState MixedTriggers::GetDeadzone() const
{
  return m_deadzone_setting.GetValue() / 100;
}

ControlState MixedTriggers::GetThreshold() const
{
  return m_threshold_setting.GetValue() / 100;
}

size_t MixedTriggers::GetTriggerCount() const
{
  return controls.size() / 2;
}

}  // namespace ControllerEmu