| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerEmu/Setting/NumericSetting.h" |
| 5 | |
| 6 | #include <fmt/format.h> |
| 7 | |
| 8 | namespace ControllerEmu |
| 9 | { |
| 10 | NumericSettingBase::NumericSettingBase(const NumericSettingDetails& details) : m_details(details) |
| 11 | { |
| 12 | } |
| 13 | |
| 14 | // Explicit instantiations so generic definitions can exist outside of the header. |
| 15 | template class NumericSetting<int>; |
| 16 | template class NumericSetting<double>; |
| 17 | template class NumericSetting<bool>; |
| 18 | |
| 19 | const char* NumericSettingBase::GetININame() const |
| 20 | { |
| 21 | return m_details.ini_name; |
| 22 | } |
| 23 | |
| 24 | const char* NumericSettingBase::GetUIName() const |
| 25 | { |
| 26 | return m_details.ui_name; |
| 27 | } |
| 28 | |
| 29 | const char* NumericSettingBase::GetUISuffix() const |
| 30 | { |
| 31 | return m_details.ui_suffix; |
| 32 | } |
| 33 | |
| 34 | const char* NumericSettingBase::GetUIDescription() const |
| 35 | { |
| 36 | return m_details.ui_description; |
| 37 | } |
| 38 | |
| 39 | SettingVisibility NumericSettingBase::GetVisibility() const |
| 40 | { |
| 41 | return m_details.visibility; |
| 42 | } |
| 43 | |
| 44 | template <typename T> |
| 45 | void NumericSetting<T>::SetExpressionFromValue() |
| 46 | { |
| 47 | // Always include -/+ sign to prevent CoalesceExpression binding. |
| 48 | // e.g. 1 is a valid input name for keyboard devices, +1 is not. |
| 49 | m_value.m_input.SetExpression(fmt::format("{:+g}", ControlState(GetValue()))); |
| 50 | } |
| 51 | |
| 52 | template <typename T> |
| 53 | void NumericSetting<T>::SimplifyIfPossible() |
| 54 | { |
| 55 | ValueType value; |
| 56 | if (TryParse(std::string(StripWhitespace(m_value.m_input.GetExpression())), &value)) |
| 57 | m_value.SetValue(value); |
| 58 | } |
| 59 | |
| 60 | template <> |
| 61 | SettingType NumericSetting<int>::GetType() const |
| 62 | { |
| 63 | return SettingType::Int; |
| 64 | } |
| 65 | |
| 66 | template <> |
| 67 | SettingType NumericSetting<double>::GetType() const |
| 68 | { |
| 69 | return SettingType::Double; |
| 70 | } |
| 71 | |
| 72 | template <> |
| 73 | SettingType NumericSetting<bool>::GetType() const |
| 74 | { |
| 75 | return SettingType::Bool; |
| 76 | } |
| 77 | |
| 78 | } // namespace ControllerEmu |