2017-02-26 12:00:24 -08:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-02-26 12:00:24 -08:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-04-08 16:58:32 -07:00
|
|
|
#include <algorithm>
|
2019-03-26 19:31:03 -05:00
|
|
|
#include <atomic>
|
2024-04-08 16:58:32 -07:00
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
2017-02-26 12:00:24 -08:00
|
|
|
#include <string>
|
2024-04-08 16:58:32 -07:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2017-02-26 12:00:24 -08:00
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2019-03-26 19:31:03 -05:00
|
|
|
#include "Common/IniFile.h"
|
2019-10-18 14:54:02 -05:00
|
|
|
#include "InputCommon/ControlReference/ControlReference.h"
|
2020-09-15 04:34:41 -07:00
|
|
|
#include "InputCommon/ControllerInterface/CoreDevice.h"
|
2017-02-26 12:00:24 -08:00
|
|
|
|
|
|
|
namespace ControllerEmu
|
|
|
|
{
|
2019-03-26 19:31:03 -05:00
|
|
|
enum class SettingType
|
|
|
|
{
|
2020-01-18 11:19:32 -06:00
|
|
|
Int,
|
2019-03-26 19:31:03 -05:00
|
|
|
Double,
|
|
|
|
Bool,
|
|
|
|
};
|
|
|
|
|
2022-05-19 22:33:15 -05:00
|
|
|
enum class SettingVisibility
|
|
|
|
{
|
|
|
|
Normal,
|
|
|
|
Advanced,
|
|
|
|
};
|
|
|
|
|
2019-03-26 19:31:03 -05:00
|
|
|
struct NumericSettingDetails
|
|
|
|
{
|
|
|
|
NumericSettingDetails(const char* const _ini_name, const char* const _ui_suffix = nullptr,
|
|
|
|
const char* const _ui_description = nullptr,
|
2022-05-19 22:33:15 -05:00
|
|
|
const char* const _ui_name = nullptr,
|
|
|
|
SettingVisibility _visibility = SettingVisibility::Normal)
|
2019-03-26 19:31:03 -05:00
|
|
|
: ini_name(_ini_name), ui_suffix(_ui_suffix), ui_description(_ui_description),
|
2022-05-19 22:33:15 -05:00
|
|
|
ui_name(_ui_name ? _ui_name : _ini_name), visibility(_visibility)
|
2019-03-26 19:31:03 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// The name used in ini files.
|
|
|
|
const char* const ini_name;
|
|
|
|
|
|
|
|
// A string applied to the number in the UI (unit of measure).
|
|
|
|
const char* const ui_suffix;
|
|
|
|
|
|
|
|
// Detailed description of the setting.
|
|
|
|
const char* const ui_description;
|
|
|
|
|
|
|
|
// The name used in the UI (if different from ini file).
|
|
|
|
const char* const ui_name;
|
2022-05-19 22:33:15 -05:00
|
|
|
|
|
|
|
// Advanced settings should be harder to change in the UI. They might confuse users.
|
|
|
|
const SettingVisibility visibility;
|
2019-03-26 19:31:03 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class NumericSettingBase
|
2017-02-26 12:00:24 -08:00
|
|
|
{
|
|
|
|
public:
|
2019-03-26 19:31:03 -05:00
|
|
|
NumericSettingBase(const NumericSettingDetails& details);
|
|
|
|
|
|
|
|
virtual ~NumericSettingBase() = default;
|
|
|
|
|
2023-04-13 09:38:09 -04:00
|
|
|
virtual void LoadFromIni(const Common::IniFile::Section& section,
|
|
|
|
const std::string& group_name) = 0;
|
|
|
|
virtual void SaveToIni(Common::IniFile::Section& section,
|
|
|
|
const std::string& group_name) const = 0;
|
2019-03-26 19:31:03 -05:00
|
|
|
|
2019-10-18 14:54:02 -05:00
|
|
|
virtual InputReference& GetInputReference() = 0;
|
|
|
|
virtual const InputReference& GetInputReference() const = 0;
|
|
|
|
|
2020-01-18 11:19:32 -06:00
|
|
|
virtual bool IsSimpleValue() const = 0;
|
|
|
|
|
2019-10-18 14:54:02 -05:00
|
|
|
// Convert a literal expression e.g. "7.0" to a regular value. (disables expression parsing)
|
|
|
|
virtual void SimplifyIfPossible() = 0;
|
|
|
|
|
|
|
|
// Convert a regular value to an expression. (used before expression editing)
|
|
|
|
virtual void SetExpressionFromValue() = 0;
|
|
|
|
|
2019-03-26 19:31:03 -05:00
|
|
|
virtual SettingType GetType() const = 0;
|
|
|
|
|
2022-05-19 22:47:30 -05:00
|
|
|
virtual void SetToDefault() = 0;
|
|
|
|
|
2022-06-26 20:27:18 +02:00
|
|
|
const char* GetININame() const;
|
2019-03-26 19:31:03 -05:00
|
|
|
const char* GetUIName() const;
|
|
|
|
const char* GetUISuffix() const;
|
|
|
|
const char* GetUIDescription() const;
|
2022-05-19 22:47:30 -05:00
|
|
|
SettingVisibility GetVisibility() const;
|
2019-03-26 19:31:03 -05:00
|
|
|
|
|
|
|
protected:
|
|
|
|
NumericSettingDetails m_details;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class SettingValue;
|
|
|
|
|
|
|
|
template <typename T>
|
2020-01-18 11:19:32 -06:00
|
|
|
class NumericSetting final : public NumericSettingBase
|
2019-03-26 19:31:03 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using ValueType = T;
|
|
|
|
|
2020-01-18 11:19:32 -06:00
|
|
|
static_assert(std::is_same<ValueType, int>() || std::is_same<ValueType, double>() ||
|
|
|
|
std::is_same<ValueType, bool>(),
|
|
|
|
"NumericSetting is only implemented for int, double, and bool.");
|
2019-03-26 19:31:03 -05:00
|
|
|
|
|
|
|
NumericSetting(SettingValue<ValueType>* value, const NumericSettingDetails& details,
|
|
|
|
ValueType default_value, ValueType min_value, ValueType max_value)
|
|
|
|
: NumericSettingBase(details), m_value(*value), m_default_value(default_value),
|
|
|
|
m_min_value(min_value), m_max_value(max_value)
|
|
|
|
{
|
2022-05-19 22:47:30 -05:00
|
|
|
SetToDefault();
|
2019-03-26 19:31:03 -05:00
|
|
|
}
|
|
|
|
|
2022-05-19 22:47:30 -05:00
|
|
|
void SetToDefault() override { m_value.SetValue(m_default_value); }
|
|
|
|
|
2023-04-13 09:38:09 -04:00
|
|
|
void LoadFromIni(const Common::IniFile::Section& section, const std::string& group_name) override
|
2019-03-26 19:31:03 -05:00
|
|
|
{
|
2019-10-18 14:54:02 -05:00
|
|
|
std::string str_value;
|
|
|
|
if (section.Get(group_name + m_details.ini_name, &str_value))
|
|
|
|
{
|
|
|
|
m_value.m_input.SetExpression(std::move(str_value));
|
|
|
|
SimplifyIfPossible();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetValue(m_default_value);
|
|
|
|
}
|
2019-03-26 19:31:03 -05:00
|
|
|
}
|
|
|
|
|
2023-04-13 09:38:09 -04:00
|
|
|
void SaveToIni(Common::IniFile::Section& section, const std::string& group_name) const override
|
2019-03-26 19:31:03 -05:00
|
|
|
{
|
2019-10-18 14:54:02 -05:00
|
|
|
if (IsSimpleValue())
|
2021-05-11 16:30:29 +03:00
|
|
|
{
|
2019-10-18 14:54:02 -05:00
|
|
|
section.Set(group_name + m_details.ini_name, GetValue(), m_default_value);
|
2021-05-11 16:30:29 +03:00
|
|
|
}
|
2019-10-18 14:54:02 -05:00
|
|
|
else
|
2021-05-11 16:30:29 +03:00
|
|
|
{
|
|
|
|
// We can't save line breaks in a single line config. Restoring them is too complicated.
|
|
|
|
std::string expression = m_value.m_input.GetExpression();
|
|
|
|
ReplaceBreaksWithSpaces(expression);
|
|
|
|
section.Set(group_name + m_details.ini_name, expression, "");
|
|
|
|
}
|
2019-03-26 19:31:03 -05:00
|
|
|
}
|
|
|
|
|
2020-01-18 11:19:32 -06:00
|
|
|
bool IsSimpleValue() const override { return m_value.IsSimpleValue(); }
|
2024-03-20 22:40:01 -05:00
|
|
|
void SimplifyIfPossible() override;
|
2020-01-18 11:19:32 -06:00
|
|
|
void SetExpressionFromValue() override;
|
2019-10-18 14:54:02 -05:00
|
|
|
InputReference& GetInputReference() override { return m_value.m_input; }
|
|
|
|
const InputReference& GetInputReference() const override { return m_value.m_input; }
|
|
|
|
|
2019-03-26 19:31:03 -05:00
|
|
|
ValueType GetValue() const { return m_value.GetValue(); }
|
|
|
|
void SetValue(ValueType value) { m_value.SetValue(value); }
|
|
|
|
|
|
|
|
ValueType GetDefaultValue() const { return m_default_value; }
|
|
|
|
ValueType GetMinValue() const { return m_min_value; }
|
|
|
|
ValueType GetMaxValue() const { return m_max_value; }
|
|
|
|
|
|
|
|
SettingType GetType() const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
SettingValue<ValueType>& m_value;
|
|
|
|
|
|
|
|
const ValueType m_default_value;
|
|
|
|
const ValueType m_min_value;
|
|
|
|
const ValueType m_max_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class SettingValue
|
|
|
|
{
|
|
|
|
using ValueType = T;
|
|
|
|
|
|
|
|
friend class NumericSetting<T>;
|
|
|
|
|
|
|
|
public:
|
2024-04-08 16:58:32 -07:00
|
|
|
virtual ~SettingValue() = default;
|
|
|
|
|
|
|
|
virtual ValueType GetValue() const
|
2019-10-18 14:54:02 -05:00
|
|
|
{
|
2020-02-09 18:25:35 -06:00
|
|
|
// Only update dynamic values when the input gate is enabled.
|
|
|
|
// Otherwise settings will all change to 0 when window focus is lost.
|
|
|
|
// This is very undesirable for things like battery level or attached extension.
|
|
|
|
if (!IsSimpleValue() && ControlReference::GetInputGate())
|
|
|
|
m_value = m_input.GetState<ValueType>();
|
|
|
|
|
|
|
|
return m_value;
|
2019-10-18 14:54:02 -05:00
|
|
|
}
|
|
|
|
|
2024-04-08 16:58:32 -07:00
|
|
|
ValueType GetCachedValue() const { return m_value; }
|
|
|
|
|
2019-10-18 14:54:02 -05:00
|
|
|
bool IsSimpleValue() const { return m_input.GetExpression().empty(); }
|
2019-03-26 19:31:03 -05:00
|
|
|
|
2024-04-08 16:58:32 -07:00
|
|
|
virtual void SetValue(const ValueType value)
|
2019-10-18 14:54:02 -05:00
|
|
|
{
|
|
|
|
m_value = value;
|
|
|
|
|
|
|
|
// Clear the expression to use our new "simple" value.
|
|
|
|
m_input.SetExpression("");
|
|
|
|
}
|
2019-03-26 19:31:03 -05:00
|
|
|
|
2024-04-15 14:51:06 -05:00
|
|
|
private:
|
2019-03-26 19:31:03 -05:00
|
|
|
// Values are R/W by both UI and CPU threads.
|
2020-02-09 18:25:35 -06:00
|
|
|
mutable std::atomic<ValueType> m_value = {};
|
2019-10-18 14:54:02 -05:00
|
|
|
|
|
|
|
// Unfortunately InputReference's state grabbing is non-const requiring mutable here.
|
|
|
|
mutable InputReference m_input;
|
2017-02-26 12:00:24 -08:00
|
|
|
};
|
|
|
|
|
2024-04-08 16:58:32 -07:00
|
|
|
template <typename ValueType>
|
|
|
|
class SubscribableSettingValue final : public SettingValue<ValueType>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Base = SettingValue<ValueType>;
|
|
|
|
|
|
|
|
ValueType GetValue() const override
|
|
|
|
{
|
|
|
|
const ValueType cached_value = GetCachedValue();
|
|
|
|
if (IsSimpleValue())
|
|
|
|
return cached_value;
|
|
|
|
|
|
|
|
const ValueType updated_value = Base::GetValue();
|
|
|
|
if (updated_value != cached_value)
|
|
|
|
TriggerCallbacks();
|
|
|
|
|
|
|
|
return updated_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetValue(const ValueType value) override
|
|
|
|
{
|
|
|
|
if (value != GetCachedValue())
|
|
|
|
{
|
|
|
|
Base::SetValue(value);
|
|
|
|
TriggerCallbacks();
|
|
|
|
}
|
|
|
|
else if (!IsSimpleValue())
|
|
|
|
{
|
|
|
|
// The setting has an expression with a cached value equal to the one currently being set.
|
|
|
|
// Don't trigger the callbacks (since the value didn't change), but clear the expression and
|
|
|
|
// make the setting a simple value instead.
|
|
|
|
Base::SetValue(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueType GetCachedValue() const { return Base::GetCachedValue(); }
|
|
|
|
bool IsSimpleValue() const { return Base::IsSimpleValue(); }
|
|
|
|
|
|
|
|
using SettingChangedCallback = std::function<void(ValueType)>;
|
|
|
|
|
|
|
|
int AddCallback(const SettingChangedCallback& callback)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(m_mutex);
|
|
|
|
const int callback_id = m_next_callback_id;
|
|
|
|
++m_next_callback_id;
|
|
|
|
m_callback_pairs.emplace_back(callback_id, callback);
|
|
|
|
|
|
|
|
return callback_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveCallback(const int id)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(m_mutex);
|
|
|
|
const auto iter = std::ranges::find(m_callback_pairs, id, &IDCallbackPair::first);
|
|
|
|
if (iter != m_callback_pairs.end())
|
|
|
|
m_callback_pairs.erase(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void TriggerCallbacks() const
|
|
|
|
{
|
|
|
|
std::lock_guard lock(m_mutex);
|
|
|
|
const ValueType value = Base::GetValue();
|
|
|
|
for (const auto& pair : m_callback_pairs)
|
|
|
|
pair.second(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
using IDCallbackPair = std::pair<int, SettingChangedCallback>;
|
|
|
|
std::vector<IDCallbackPair> m_callback_pairs;
|
|
|
|
int m_next_callback_id = 0;
|
|
|
|
|
|
|
|
mutable std::mutex m_mutex;
|
|
|
|
};
|
|
|
|
|
2017-02-26 12:00:24 -08:00
|
|
|
} // namespace ControllerEmu
|