Filoppi a19a0096db InputCommon: improve code that returns a controller attachment index
casting a value to a u32 when it's originally an int, and it's exposed as int to users,
could end up in cases where a negative number would result as a positive one.
This doesn't really affect the value range of the attachment enum,
still I think the code was wrong.

Heavily tested.
2021-05-19 20:51:34 +03:00

45 lines
1023 B
C++

// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "InputCommon/ControllerEmu/ControlGroup/Attachments.h"
namespace ControllerEmu
{
Attachments::Attachments(const std::string& name_) : ControlGroup(name_, GroupType::Attachments)
{
}
void Attachments::AddAttachment(std::unique_ptr<EmulatedController> att)
{
m_attachments.emplace_back(std::move(att));
}
u32 Attachments::GetSelectedAttachment() const
{
// This is originally an int, treat it as such
const int value = m_selection_value.GetValue();
if (value > 0 && static_cast<size_t>(value) < m_attachments.size())
return u32(value);
return 0;
}
void Attachments::SetSelectedAttachment(u32 val)
{
m_selection_setting.SetValue(val);
}
NumericSetting<int>& Attachments::GetSelectionSetting()
{
return m_selection_setting;
}
const std::vector<std::unique_ptr<EmulatedController>>& Attachments::GetAttachmentList() const
{
return m_attachments;
}
} // namespace ControllerEmu