mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-24 06:39:46 -06:00
Introduce the usage of unique_ptr into the InputCommon ControlEmu.h class. Allows for the automatic handling of resource deallocation.
This commit is contained in:
@ -8,55 +8,30 @@
|
||||
#include <X11/Xlib.h>
|
||||
#endif
|
||||
|
||||
ControllerEmu::~ControllerEmu()
|
||||
{
|
||||
for (ControlGroup* cg : groups)
|
||||
delete cg;
|
||||
}
|
||||
|
||||
ControllerEmu::ControlGroup::~ControlGroup()
|
||||
{
|
||||
for (Control* c : controls)
|
||||
delete c;
|
||||
|
||||
for (Setting* s : settings)
|
||||
delete s;
|
||||
}
|
||||
|
||||
ControllerEmu::Extension::~Extension()
|
||||
{
|
||||
for (ControllerEmu* ai : attachments)
|
||||
delete ai;
|
||||
}
|
||||
ControllerEmu::ControlGroup::Control::~Control()
|
||||
{
|
||||
delete control_ref;
|
||||
}
|
||||
|
||||
void ControllerEmu::UpdateReferences(ControllerInterface& devi)
|
||||
{
|
||||
for (ControlGroup* cg : groups)
|
||||
for (auto& ctrlGroup : groups)
|
||||
{
|
||||
for (ControlGroup::Control* control : cg->controls)
|
||||
devi.UpdateReference(control->control_ref, default_device);
|
||||
for (auto& control : ctrlGroup->controls)
|
||||
devi.UpdateReference(control->control_ref.get(), default_device);
|
||||
|
||||
// extension
|
||||
if (GROUP_TYPE_EXTENSION == cg->type)
|
||||
if (ctrlGroup->type == GROUP_TYPE_EXTENSION)
|
||||
{
|
||||
for (ControllerEmu* ai : ((Extension*)cg)->attachments)
|
||||
ai->UpdateReferences(devi);
|
||||
for (auto& attachment : ((Extension*)ctrlGroup.get())->attachments)
|
||||
attachment->UpdateReferences(devi);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ControllerEmu::UpdateDefaultDevice()
|
||||
{
|
||||
for (ControlGroup* cg : groups)
|
||||
for (auto& ctrlGroup : groups)
|
||||
{
|
||||
// extension
|
||||
if (GROUP_TYPE_EXTENSION == cg->type)
|
||||
if (ctrlGroup->type == GROUP_TYPE_EXTENSION)
|
||||
{
|
||||
for (ControllerEmu* ai : ((Extension*)cg)->attachments)
|
||||
for (auto& ai : ((Extension*)ctrlGroup.get())->attachments)
|
||||
{
|
||||
ai->default_device = default_device;
|
||||
ai->UpdateDefaultDevice();
|
||||
@ -70,13 +45,13 @@ void ControllerEmu::ControlGroup::LoadConfig(IniFile::Section *sec, const std::s
|
||||
std::string group(base + name); group += "/";
|
||||
|
||||
// settings
|
||||
for (Setting* s : settings)
|
||||
for (auto& s : settings)
|
||||
{
|
||||
sec->Get((group + s->name).c_str(), &s->value, s->default_value * 100);
|
||||
s->value /= 100;
|
||||
}
|
||||
|
||||
for (Control* c : controls)
|
||||
for (auto& c : controls)
|
||||
{
|
||||
// control expression
|
||||
sec->Get((group + c->name).c_str(), &c->control_ref->expression, "");
|
||||
@ -88,7 +63,7 @@ void ControllerEmu::ControlGroup::LoadConfig(IniFile::Section *sec, const std::s
|
||||
}
|
||||
|
||||
// extensions
|
||||
if (GROUP_TYPE_EXTENSION == type)
|
||||
if (type == GROUP_TYPE_EXTENSION)
|
||||
{
|
||||
Extension* const ext = ((Extension*)this);
|
||||
|
||||
@ -97,7 +72,7 @@ void ControllerEmu::ControlGroup::LoadConfig(IniFile::Section *sec, const std::s
|
||||
std::string extname;
|
||||
sec->Get((base + name).c_str(), &extname, "");
|
||||
|
||||
for (ControllerEmu* ai : ext->attachments)
|
||||
for (auto& ai : ext->attachments)
|
||||
{
|
||||
ai->default_device.FromString(defdev);
|
||||
ai->LoadConfig(sec, base + ai->GetName() + "/");
|
||||
@ -119,7 +94,7 @@ void ControllerEmu::LoadConfig(IniFile::Section *sec, const std::string& base)
|
||||
default_device.FromString(defdev);
|
||||
}
|
||||
|
||||
for (ControlGroup* cg : groups)
|
||||
for (auto& cg : groups)
|
||||
cg->LoadConfig(sec, defdev, base);
|
||||
}
|
||||
|
||||
@ -127,10 +102,10 @@ void ControllerEmu::ControlGroup::SaveConfig(IniFile::Section *sec, const std::s
|
||||
{
|
||||
std::string group(base + name); group += "/";
|
||||
|
||||
for (Setting* s : settings)
|
||||
for (auto& s : settings)
|
||||
sec->Set((group + s->name).c_str(), s->value*100.0f, s->default_value*100.0f);
|
||||
|
||||
for (Control* c : controls)
|
||||
for (auto& c : controls)
|
||||
{
|
||||
// control expression
|
||||
sec->Set((group + c->name).c_str(), c->control_ref->expression, "");
|
||||
@ -140,12 +115,12 @@ void ControllerEmu::ControlGroup::SaveConfig(IniFile::Section *sec, const std::s
|
||||
}
|
||||
|
||||
// extensions
|
||||
if (GROUP_TYPE_EXTENSION == type)
|
||||
if (type == GROUP_TYPE_EXTENSION)
|
||||
{
|
||||
Extension* const ext = ((Extension*)this);
|
||||
sec->Set((base + name).c_str(), ext->attachments[ext->switch_extension]->GetName(), "None");
|
||||
|
||||
for (ControllerEmu* ai : ext->attachments)
|
||||
for (auto& ai : ext->attachments)
|
||||
ai->SaveConfig(sec, base + ai->GetName() + "/");
|
||||
}
|
||||
}
|
||||
@ -156,58 +131,58 @@ void ControllerEmu::SaveConfig(IniFile::Section *sec, const std::string& base)
|
||||
if (base.empty())
|
||||
sec->Set((/*std::string(" ") +*/ base + "Device").c_str(), defdev, "");
|
||||
|
||||
for (ControlGroup* cg : groups)
|
||||
cg->SaveConfig(sec, defdev, base);
|
||||
for (auto& ctrlGroup : groups)
|
||||
ctrlGroup->SaveConfig(sec, defdev, base);
|
||||
}
|
||||
|
||||
ControllerEmu::AnalogStick::AnalogStick(const char* const _name) : ControlGroup(_name, GROUP_TYPE_STICK)
|
||||
{
|
||||
for (auto& named_direction : named_directions)
|
||||
controls.push_back(new Input(named_direction));
|
||||
controls.emplace_back(new Input(named_direction));
|
||||
|
||||
controls.push_back(new Input(_trans("Modifier")));
|
||||
controls.emplace_back(new Input(_trans("Modifier")));
|
||||
|
||||
settings.push_back(new Setting(_trans("Radius"), 0.7f, 0, 100));
|
||||
settings.push_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
settings.push_back(new Setting(_trans("Square Stick"), 0));
|
||||
settings.emplace_back(new Setting(_trans("Radius"), 0.7f, 0, 100));
|
||||
settings.emplace_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
settings.emplace_back(new Setting(_trans("Square Stick"), 0));
|
||||
|
||||
}
|
||||
|
||||
ControllerEmu::Buttons::Buttons(const char* const _name) : ControlGroup(_name, GROUP_TYPE_BUTTONS)
|
||||
{
|
||||
settings.push_back(new Setting(_trans("Threshold"), 0.5f));
|
||||
settings.emplace_back(new Setting(_trans("Threshold"), 0.5f));
|
||||
}
|
||||
|
||||
ControllerEmu::MixedTriggers::MixedTriggers(const char* const _name) : ControlGroup(_name, GROUP_TYPE_MIXED_TRIGGERS)
|
||||
{
|
||||
settings.push_back(new Setting(_trans("Threshold"), 0.9f));
|
||||
settings.emplace_back(new Setting(_trans("Threshold"), 0.9f));
|
||||
}
|
||||
|
||||
ControllerEmu::Triggers::Triggers(const char* const _name) : ControlGroup(_name, GROUP_TYPE_TRIGGERS)
|
||||
{
|
||||
settings.push_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
settings.emplace_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
}
|
||||
|
||||
ControllerEmu::Slider::Slider(const char* const _name) : ControlGroup(_name, GROUP_TYPE_SLIDER)
|
||||
{
|
||||
controls.push_back(new Input("Left"));
|
||||
controls.push_back(new Input("Right"));
|
||||
controls.emplace_back(new Input("Left"));
|
||||
controls.emplace_back(new Input("Right"));
|
||||
|
||||
settings.push_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
settings.emplace_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
}
|
||||
|
||||
ControllerEmu::Force::Force(const char* const _name) : ControlGroup(_name, GROUP_TYPE_FORCE)
|
||||
{
|
||||
memset(m_swing, 0, sizeof(m_swing));
|
||||
|
||||
controls.push_back(new Input(_trans("Up")));
|
||||
controls.push_back(new Input(_trans("Down")));
|
||||
controls.push_back(new Input(_trans("Left")));
|
||||
controls.push_back(new Input(_trans("Right")));
|
||||
controls.push_back(new Input(_trans("Forward")));
|
||||
controls.push_back(new Input(_trans("Backward")));
|
||||
controls.emplace_back(new Input(_trans("Up")));
|
||||
controls.emplace_back(new Input(_trans("Down")));
|
||||
controls.emplace_back(new Input(_trans("Left")));
|
||||
controls.emplace_back(new Input(_trans("Right")));
|
||||
controls.emplace_back(new Input(_trans("Forward")));
|
||||
controls.emplace_back(new Input(_trans("Backward")));
|
||||
|
||||
settings.push_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
settings.emplace_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
}
|
||||
|
||||
ControllerEmu::Tilt::Tilt(const char* const _name)
|
||||
@ -215,16 +190,16 @@ ControllerEmu::Tilt::Tilt(const char* const _name)
|
||||
{
|
||||
memset(m_tilt, 0, sizeof(m_tilt));
|
||||
|
||||
controls.push_back(new Input("Forward"));
|
||||
controls.push_back(new Input("Backward"));
|
||||
controls.push_back(new Input("Left"));
|
||||
controls.push_back(new Input("Right"));
|
||||
controls.emplace_back(new Input("Forward"));
|
||||
controls.emplace_back(new Input("Backward"));
|
||||
controls.emplace_back(new Input("Left"));
|
||||
controls.emplace_back(new Input("Right"));
|
||||
|
||||
controls.push_back(new Input(_trans("Modifier")));
|
||||
controls.emplace_back(new Input(_trans("Modifier")));
|
||||
|
||||
settings.push_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
settings.push_back(new Setting(_trans("Circle Stick"), 0));
|
||||
settings.push_back(new Setting(_trans("Angle"), 0.9f, 0, 180));
|
||||
settings.emplace_back(new Setting(_trans("Dead Zone"), 0, 0, 50));
|
||||
settings.emplace_back(new Setting(_trans("Circle Stick"), 0));
|
||||
settings.emplace_back(new Setting(_trans("Angle"), 0.9f, 0, 180));
|
||||
}
|
||||
|
||||
ControllerEmu::Cursor::Cursor(const char* const _name)
|
||||
@ -232,14 +207,14 @@ ControllerEmu::Cursor::Cursor(const char* const _name)
|
||||
, m_z(0)
|
||||
{
|
||||
for (auto& named_direction : named_directions)
|
||||
controls.push_back(new Input(named_direction));
|
||||
controls.push_back(new Input("Forward"));
|
||||
controls.push_back(new Input("Backward"));
|
||||
controls.push_back(new Input(_trans("Hide")));
|
||||
controls.emplace_back(new Input(named_direction));
|
||||
controls.emplace_back(new Input("Forward"));
|
||||
controls.emplace_back(new Input("Backward"));
|
||||
controls.emplace_back(new Input(_trans("Hide")));
|
||||
|
||||
settings.push_back(new Setting(_trans("Center"), 0.5f));
|
||||
settings.push_back(new Setting(_trans("Width"), 0.5f));
|
||||
settings.push_back(new Setting(_trans("Height"), 0.5f));
|
||||
settings.emplace_back(new Setting(_trans("Center"), 0.5f));
|
||||
settings.emplace_back(new Setting(_trans("Width"), 0.5f));
|
||||
settings.emplace_back(new Setting(_trans("Height"), 0.5f));
|
||||
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
#define NOMINMAX
|
||||
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
@ -62,11 +63,11 @@ public:
|
||||
{
|
||||
protected:
|
||||
Control(ControllerInterface::ControlReference* const _ref, const char* const _name)
|
||||
: control_ref(_ref), name(_name){}
|
||||
public:
|
||||
: control_ref(_ref), name(_name) {}
|
||||
|
||||
virtual ~Control();
|
||||
ControllerInterface::ControlReference* const control_ref;
|
||||
public:
|
||||
virtual ~Control() {}
|
||||
std::unique_ptr<ControllerInterface::ControlReference> const control_ref;
|
||||
const char* const name;
|
||||
|
||||
};
|
||||
@ -77,7 +78,6 @@ public:
|
||||
|
||||
Input(const char* const _name)
|
||||
: Control(new ControllerInterface::InputReference, _name) {}
|
||||
|
||||
};
|
||||
|
||||
class Output : public Control
|
||||
@ -86,7 +86,6 @@ public:
|
||||
|
||||
Output(const char* const _name)
|
||||
: Control(new ControllerInterface::OutputReference, _name) {}
|
||||
|
||||
};
|
||||
|
||||
class Setting
|
||||
@ -108,7 +107,7 @@ public:
|
||||
};
|
||||
|
||||
ControlGroup(const char* const _name, const unsigned int _type = GROUP_TYPE_OTHER) : name(_name), type(_type) {}
|
||||
virtual ~ControlGroup();
|
||||
virtual ~ControlGroup() {}
|
||||
|
||||
virtual void LoadConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" );
|
||||
virtual void SaveConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" );
|
||||
@ -116,8 +115,8 @@ public:
|
||||
const char* const name;
|
||||
const unsigned int type;
|
||||
|
||||
std::vector<Control*> controls;
|
||||
std::vector<Setting*> settings;
|
||||
std::vector<std::unique_ptr<Control>> controls;
|
||||
std::vector<std::unique_ptr<Setting>> settings;
|
||||
|
||||
};
|
||||
|
||||
@ -194,7 +193,7 @@ public:
|
||||
template <typename C>
|
||||
void GetState(C* const buttons, const C* bitmasks)
|
||||
{
|
||||
for (Control* control : controls)
|
||||
for (auto& control : controls)
|
||||
{
|
||||
if (control->control_ref->State() > settings[0]->value) // threshold
|
||||
*buttons |= *bitmasks;
|
||||
@ -425,17 +424,18 @@ public:
|
||||
: ControlGroup(_name, GROUP_TYPE_EXTENSION)
|
||||
, switch_extension(0)
|
||||
, active_extension(0) {}
|
||||
~Extension();
|
||||
|
||||
~Extension() {}
|
||||
|
||||
void GetState(u8* const data, const bool focus = true);
|
||||
|
||||
std::vector<ControllerEmu*> attachments;
|
||||
std::vector<std::unique_ptr<ControllerEmu>> attachments;
|
||||
|
||||
int switch_extension;
|
||||
int active_extension;
|
||||
};
|
||||
|
||||
virtual ~ControllerEmu();
|
||||
virtual ~ControllerEmu() {}
|
||||
|
||||
virtual std::string GetName() const = 0;
|
||||
|
||||
@ -447,7 +447,7 @@ public:
|
||||
|
||||
void UpdateReferences(ControllerInterface& devi);
|
||||
|
||||
std::vector<ControlGroup*> groups;
|
||||
std::vector<std::unique_ptr<ControlGroup>> groups;
|
||||
|
||||
DeviceQualifier default_device;
|
||||
};
|
||||
|
Reference in New Issue
Block a user