diff options
| author | Léo Lam <leo@innovatetechnologi.es> | 2016-07-11 19:35:32 +0200 |
|---|---|---|
| committer | Léo Lam <leo@innovatetechnologi.es> | 2016-07-12 11:42:18 +0200 |
| commit | 5e829f45271bcd6a13ff541308c1599fb8eb2cfd (patch) | |
| tree | 5bd788d0478c1f042cac2e9c4de63b1b0c22acb1 /Source/Core/InputCommon | |
| parent | 7530a2d2069a76aa65640ac97755d75871359a46 (diff) | |
ControllerEmu: Split the Setting class
The Setting class was used for both numeric values and booleans, and
other parts of the code had hacks to make it work with booleans.
By splitting Setting into NumericSetting and BooleanSetting, it is
clear which settings are numeric, and which are boolean, so there is
no need to guess by checking the default values or anything like that.
Also, booleans are stored as booleans in config files, instead of 1.0.
Diffstat (limited to 'Source/Core/InputCommon')
| -rw-r--r-- | Source/Core/InputCommon/ControllerEmu.cpp | 54 | ||||
| -rw-r--r-- | Source/Core/InputCommon/ControllerEmu.h | 95 |
2 files changed, 86 insertions, 63 deletions
diff --git a/Source/Core/InputCommon/ControllerEmu.cpp b/Source/Core/InputCommon/ControllerEmu.cpp index 54d25748ad..117184c8db 100644 --- a/Source/Core/InputCommon/ControllerEmu.cpp +++ b/Source/Core/InputCommon/ControllerEmu.cpp @@ -44,14 +44,18 @@ void ControllerEmu::ControlGroup::LoadConfig(IniFile::Section* sec, const std::s std::string group(base + name + "/"); // settings - for (auto& s : settings) + for (auto& s : numeric_settings) { - if (s->is_virtual) + if (s->m_type == SettingType::VIRTUAL) continue; - if (s->is_iterate) + sec->Get(group + s->m_name, &s->m_value, s->m_default_value * 100); + s->m_value /= 100; + } + for (auto& s : boolean_settings) + { + if (s->m_type == SettingType::VIRTUAL) continue; - sec->Get(group + s->name, &s->value, s->default_value * 100); - s->value /= 100; + sec->Get(group + s->m_name, &s->m_value, s->m_default_value); } for (auto& c : controls) @@ -105,14 +109,17 @@ void ControllerEmu::ControlGroup::SaveConfig(IniFile::Section* sec, const std::s { std::string group(base + name + "/"); - for (auto& s : settings) + for (auto& s : numeric_settings) { - if (s->is_virtual) + if (s->m_type == SettingType::VIRTUAL) continue; - if (s->is_iterate) + sec->Set(group + s->m_name, s->m_value * 100.0, s->m_default_value * 100.0); + } + for (auto& s : boolean_settings) + { + if (s->m_type == SettingType::VIRTUAL) continue; - - sec->Set(group + s->name, s->value * 100.0, s->default_value * 100.0); + sec->Set(group + s->m_name, s->m_value, s->m_default_value); } for (auto& c : controls) @@ -163,25 +170,26 @@ ControllerEmu::AnalogStick::AnalogStick(const char* const _name, const char* con controls.emplace_back(std::make_unique<Input>(named_direction)); controls.emplace_back(std::make_unique<Input>(_trans("Modifier"))); - settings.emplace_back(std::make_unique<Setting>(_trans("Radius"), default_radius, 0, 100)); - settings.emplace_back(std::make_unique<Setting>(_trans("Dead Zone"), 0, 0, 50)); + numeric_settings.emplace_back( + std::make_unique<NumericSetting>(_trans("Radius"), default_radius, 0, 100)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50)); } ControllerEmu::Buttons::Buttons(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_BUTTONS) { - settings.emplace_back(std::make_unique<Setting>(_trans("Threshold"), 0.5)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Threshold"), 0.5)); } ControllerEmu::MixedTriggers::MixedTriggers(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_MIXED_TRIGGERS) { - settings.emplace_back(std::make_unique<Setting>(_trans("Threshold"), 0.9)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Threshold"), 0.9)); } ControllerEmu::Triggers::Triggers(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_TRIGGERS) { - settings.emplace_back(std::make_unique<Setting>(_trans("Dead Zone"), 0, 0, 50)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50)); } ControllerEmu::Slider::Slider(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_SLIDER) @@ -189,7 +197,7 @@ ControllerEmu::Slider::Slider(const std::string& _name) : ControlGroup(_name, GR controls.emplace_back(std::make_unique<Input>("Left")); controls.emplace_back(std::make_unique<Input>("Right")); - settings.emplace_back(std::make_unique<Setting>(_trans("Dead Zone"), 0, 0, 50)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50)); } ControllerEmu::Force::Force(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_FORCE) @@ -203,7 +211,7 @@ ControllerEmu::Force::Force(const std::string& _name) : ControlGroup(_name, GROU controls.emplace_back(std::make_unique<Input>(_trans("Forward"))); controls.emplace_back(std::make_unique<Input>(_trans("Backward"))); - settings.emplace_back(std::make_unique<Setting>(_trans("Dead Zone"), 0, 0, 50)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50)); } ControllerEmu::Tilt::Tilt(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_TILT) @@ -217,9 +225,9 @@ ControllerEmu::Tilt::Tilt(const std::string& _name) : ControlGroup(_name, GROUP_ controls.emplace_back(std::make_unique<Input>(_trans("Modifier"))); - settings.emplace_back(std::make_unique<Setting>(_trans("Dead Zone"), 0, 0, 50)); - settings.emplace_back(std::make_unique<Setting>(_trans("Circle Stick"), 0)); - settings.emplace_back(std::make_unique<Setting>(_trans("Angle"), 0.9, 0, 180)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Circle Stick"), 0)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Angle"), 0.9, 0, 180)); } ControllerEmu::Cursor::Cursor(const std::string& _name) @@ -231,9 +239,9 @@ ControllerEmu::Cursor::Cursor(const std::string& _name) controls.emplace_back(std::make_unique<Input>("Backward")); controls.emplace_back(std::make_unique<Input>(_trans("Hide"))); - settings.emplace_back(std::make_unique<Setting>(_trans("Center"), 0.5)); - settings.emplace_back(std::make_unique<Setting>(_trans("Width"), 0.5)); - settings.emplace_back(std::make_unique<Setting>(_trans("Height"), 0.5)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Center"), 0.5)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Width"), 0.5)); + numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Height"), 0.5)); } void ControllerEmu::LoadDefaults(const ControllerInterface& ciface) diff --git a/Source/Core/InputCommon/ControllerEmu.h b/Source/Core/InputCommon/ControllerEmu.h index 56e469ee8e..fd5f499159 100644 --- a/Source/Core/InputCommon/ControllerEmu.h +++ b/Source/Core/InputCommon/ControllerEmu.h @@ -71,48 +71,62 @@ public: Output(const std::string& _name) : Control(new ControllerInterface::OutputReference, _name) {} }; - class Setting + enum class SettingType + { + NORMAL, // normal settings are saved to configuration files + VIRTUAL, // virtual settings are not saved at all + }; + + class NumericSetting { public: - Setting(const std::string& _name, const ControlState def_value, const unsigned int _low = 0, - const unsigned int _high = 100) - : name(_name), value(def_value), default_value(def_value), low(_low), high(_high), - is_virtual(false), is_iterate(false) + NumericSetting(const std::string& name, const ControlState default_value, + const unsigned int low = 0, const unsigned int high = 100, + const SettingType type = SettingType::NORMAL) + : m_type(type), m_name(name), m_default_value(default_value), m_low(low), m_high(high) { } - virtual ~Setting() {} - const std::string name; - ControlState value; - const ControlState default_value; - const unsigned int low, high; - bool is_virtual; - bool is_iterate; - - virtual void SetValue(ControlState new_value) { value = new_value; } - virtual ControlState GetValue() { return value; } + ControlState GetValue() const { return m_value; } + void SetValue(ControlState value) { m_value = value; } + const SettingType m_type; + const std::string m_name; + const ControlState m_default_value; + const unsigned int m_low, m_high; + ControlState m_value; }; - class BackgroundInputSetting : public Setting + class BooleanSetting { public: - BackgroundInputSetting(const std::string& _name) : Setting(_name, false) - { - is_virtual = true; - } - - void SetValue(ControlState new_value) override + BooleanSetting(const std::string& name, const bool default_value, + const SettingType type = SettingType::NORMAL) + : m_type(type), m_name(name), m_default_value(default_value) { - SConfig::GetInstance().m_BackgroundInput = !!new_value; } - ControlState GetValue() override { return SConfig::GetInstance().m_BackgroundInput; } + virtual bool GetValue() const { return m_value; } + virtual void SetValue(bool value) { m_value = value; } + const SettingType m_type; + const std::string m_name; + const bool m_default_value; + bool m_value; }; - class IterateUI : public Setting + class BackgroundInputSetting : public BooleanSetting { public: - IterateUI(const std::string& _name) : Setting(_name, false) { is_iterate = true; } + BackgroundInputSetting(const std::string& name) + : BooleanSetting(name, false, SettingType::VIRTUAL) + { + } + + bool GetValue() const override { return SConfig::GetInstance().m_BackgroundInput; } + void SetValue(bool value) override + { + m_value = value; + SConfig::GetInstance().m_BackgroundInput = value; + } }; ControlGroup(const std::string& _name, const unsigned int _type = GROUP_TYPE_OTHER) @@ -137,7 +151,8 @@ public: const unsigned int type; std::vector<std::unique_ptr<Control>> controls; - std::vector<std::unique_ptr<Setting>> settings; + std::vector<std::unique_ptr<NumericSetting>> numeric_settings; + std::vector<std::unique_ptr<BooleanSetting>> boolean_settings; }; class AnalogStick : public ControlGroup @@ -152,8 +167,8 @@ public: ControlState yy = controls[0]->control_ref->State() - controls[1]->control_ref->State(); ControlState xx = controls[3]->control_ref->State() - controls[2]->control_ref->State(); - ControlState radius = settings[SETTING_RADIUS]->value; - ControlState deadzone = settings[SETTING_DEADZONE]->value; + ControlState radius = numeric_settings[SETTING_RADIUS]->GetValue(); + ControlState deadzone = numeric_settings[SETTING_DEADZONE]->GetValue(); ControlState m = controls[4]->control_ref->State(); ControlState ang = atan2(yy, xx); @@ -192,7 +207,7 @@ public: { for (auto& control : controls) { - if (control->control_ref->State() > settings[0]->value) // threshold + if (control->control_ref->State() > numeric_settings[0]->GetValue()) // threshold *buttons |= *bitmasks; bitmasks++; @@ -210,7 +225,7 @@ public: const unsigned int trig_count = ((unsigned int)(controls.size() / 2)); for (unsigned int i = 0; i < trig_count; ++i, ++bitmasks, ++analog) { - if (controls[i]->control_ref->State() > settings[0]->value) // threshold + if (controls[i]->control_ref->State() > numeric_settings[0]->GetValue()) // threshold { *analog = 1.0; *digital |= *bitmasks; @@ -231,7 +246,7 @@ public: void GetState(ControlState* analog) { const unsigned int trig_count = ((unsigned int)(controls.size())); - const ControlState deadzone = settings[0]->value; + const ControlState deadzone = numeric_settings[0]->GetValue(); for (unsigned int i = 0; i < trig_count; ++i, ++analog) *analog = std::max(controls[i]->control_ref->State() - deadzone, 0.0) / (1 - deadzone); } @@ -244,7 +259,7 @@ public: void GetState(ControlState* const slider) { - const ControlState deadzone = settings[0]->value; + const ControlState deadzone = numeric_settings[0]->GetValue(); const ControlState state = controls[1]->control_ref->State() - controls[0]->control_ref->State(); @@ -262,7 +277,7 @@ public: void GetState(ControlState* axis) { - const ControlState deadzone = settings[0]->value; + const ControlState deadzone = numeric_settings[0]->GetValue(); for (unsigned int i = 0; i < 6; i += 2) { ControlState tmpf = 0; @@ -293,9 +308,9 @@ public: ControlState yy = controls[0]->control_ref->State() - controls[1]->control_ref->State(); ControlState xx = controls[3]->control_ref->State() - controls[2]->control_ref->State(); - ControlState deadzone = settings[0]->value; - ControlState circle = settings[1]->value; - auto const angle = settings[2]->value / 1.8; + ControlState deadzone = numeric_settings[0]->GetValue(); + ControlState circle = numeric_settings[1]->GetValue(); + auto const angle = numeric_settings[2]->GetValue() / 1.8; ControlState m = controls[4]->control_ref->State(); // deadzone / circle stick code @@ -386,9 +401,9 @@ public: // adjust cursor according to settings if (adjusted) { - xx *= (settings[1]->value * 2); - yy *= (settings[2]->value * 2); - yy += (settings[0]->value - 0.5); + xx *= (numeric_settings[1]->GetValue() * 2); + yy *= (numeric_settings[2]->GetValue() * 2); + yy += (numeric_settings[0]->GetValue() - 0.5); } *x = xx; |
