Fix more lioncash code style issues

This commit is contained in:
Anon 2016-08-06 15:41:09 -05:00
parent 555556bb0f
commit 743c4647b8
12 changed files with 129 additions and 90 deletions

View File

@ -42,7 +42,7 @@ void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
}
void EmuWindow_SDL2::OnKeyEvent(SDL_Keysym key, u8 state) {
auto& keyboard = InputCore::main_keyboard;
auto& keyboard = InputCore::GetKeyboard();
KeyboardKey param = KeyboardKey(key.sym, key.scancode, SDL_GetKeyName(key.scancode));
if (state == SDL_PRESSED) {

View File

@ -237,14 +237,14 @@ void GRenderWindow::closeEvent(QCloseEvent* event) {
void GRenderWindow::keyPressEvent(QKeyEvent* event)
{
auto& keyboard = InputCore::main_keyboard;
auto& keyboard = InputCore::GetKeyboard();
KeyboardKey param = KeyboardKey(event->key(), event->nativeScanCode(), QKeySequence(event->key()).toString().toStdString());
keyboard->KeyPressed(param);
}
void GRenderWindow::keyReleaseEvent(QKeyEvent* event)
{
auto& keyboard = InputCore::main_keyboard;
auto& keyboard = InputCore::GetKeyboard();
KeyboardKey param = KeyboardKey(event->key(), event->nativeScanCode(), QKeySequence(event->key()).toString().toStdString());
keyboard->KeyReleased(param);
}

View File

@ -76,10 +76,10 @@ void Update() {
return;
}
PadState state = InputCore::pad_state;
PadState state = InputCore::GetPadState();
// Get current circle pad position and update circle pad direction
s16 circle_pad_x, circle_pad_y;
std::tie(circle_pad_x, circle_pad_y) = InputCore::circle_pad;
std::tie(circle_pad_x, circle_pad_y) = InputCore::GetCirclePad();
state.hex |= GetCirclePadDirectionState(circle_pad_x, circle_pad_y).hex;
mem->pad.current_state.hex = state.hex;

View File

@ -56,37 +56,32 @@ namespace Settings {
int number = 0;
Device device = Device::Keyboard;
std::string key;
InputDeviceMapping() { }
InputDeviceMapping(std::string input) {
InputDeviceMapping() = default;
InputDeviceMapping(const std::string& input) {
std::vector<std::string> parts;
Common::SplitString(input, '/', parts);
if (parts.size() == 4) {
if (parts[0] == "Qt")
framework = DeviceFramework::Qt;
else if (parts[0] == "SDL")
framework = DeviceFramework::SDL;
if (parts.size() != 4)
return;
number = std::stoi(parts[1]);
if (parts[2] == "Keyboard")
device = Device::Keyboard;
else if (parts[2] == "Gamepad")
device = Device::Gamepad;
key = parts[3];
}
else {
//default if can't read properly
if (parts[0] == "Qt")
framework = DeviceFramework::Qt;
number = 0;
else if (parts[0] == "SDL")
framework = DeviceFramework::SDL;
number = std::stoi(parts[1]);
if (parts[2] == "Keyboard")
device = Device::Keyboard;
key = "";
}
else if (parts[2] == "Gamepad")
device = Device::Gamepad;
key = parts[3];
}
bool operator==(const InputDeviceMapping& rhs) const {
return (this->device == rhs.device) && (this->framework == rhs.framework) && (this->number == rhs.number);
}
std::string ToString() const{
std::string ToString() const {
std::string result;
if (framework == DeviceFramework::Qt)
result = "Qt";
@ -101,6 +96,7 @@ namespace Settings {
result += "Keyboard";
else if (device == Device::Gamepad)
result += "Gamepad";
result += "/";
result += key;
return result;

View File

@ -12,9 +12,11 @@
class IDevice {
public:
virtual ~IDevice() = default;
std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMapping; /// Maps the string in the settings file to the HID Padstate object
virtual bool InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) = 0;
virtual bool InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) = 0;
virtual void ProcessInput() = 0;
virtual bool CloseDevice() = 0;
protected:
std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMapping; ///< Maps the string in the settings file to the HID Padstate object
};

View File

@ -12,7 +12,7 @@ Keyboard::Keyboard() {
Keyboard::~Keyboard() {
}
bool Keyboard::InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) {
bool Keyboard::InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) {
keyMapping = keyMap;
return true;
}
@ -21,17 +21,17 @@ void Keyboard::ProcessInput() {
std::lock_guard<std::mutex> lock(m);
auto keysPressedCopy = keysPressed;
lock.~lock_guard();
for (auto const &ent1 : keyMapping) {
for (const auto &ent1 : keyMapping) {
int scancode = std::stoul(ent1.first, nullptr, 16);
KeyboardKey proxy = KeyboardKey(0, scancode, "");
if (keysPressedCopy[proxy] == true && keysPressedLast[scancode] == false) {
for (auto& key : ent1.second) {
for (const auto& key : ent1.second) {
KeyMap::PressKey(key, 1.0);
}
keysPressedLast[scancode] = true;
}
else if (keysPressedCopy[proxy] == false && keysPressedLast[scancode] == true) {
for (auto& key : ent1.second) {
for (const auto& key : ent1.second) {
KeyMap::ReleaseKey(key);
}
keysPressedLast[scancode] = false;

View File

@ -20,7 +20,7 @@ private:
public:
Keyboard();
~Keyboard();
bool InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) override;
bool InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) override;
void ProcessInput() override;
bool CloseDevice() override;
void KeyPressed(KeyboardKey key);
@ -31,18 +31,16 @@ struct KeyboardKey {
uint32_t key;
uint32_t scancode;
std::string character;
KeyboardKey(uint32_t Key, uint32_t Scancode, std::string Character) {
key = Key;
scancode = Scancode;
character = Character;
KeyboardKey(uint32_t key_, uint32_t scancode_, std::string character_) : key(key_), scancode(scancode_), character(std::move(character_)) {
}
bool operator==(const KeyboardKey& other) const {
return (this->scancode == other.scancode);
return scancode == other.scancode;
}
bool operator==(uint32_t other) const {
return (this->scancode == other);
return scancode == other;
}
bool operator<(const KeyboardKey &o) const {
return (this->scancode < o.scancode);
bool operator<(const KeyboardKey& other) const {
return scancode < other.scancode;
}
};

View File

@ -20,7 +20,7 @@ SDLGamepad::~SDLGamepad() {
CloseDevice();
}
bool SDLGamepad::InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) {
bool SDLGamepad::InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) {
if (!SDLGamepad::SDLInitialized && SDL_Init(SDL_INIT_GAMECONTROLLER) < 0) {
LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_GAMECONTROLLER) failed");
return false;
@ -36,7 +36,7 @@ bool SDLGamepad::InitDevice(int number, std::map<std::string, std::vector<KeyMap
}
}
keyMapping = keyMap;
for (auto& entry : keyMapping) {
for (const auto& entry : keyMapping) {
keysPressed[entry.first] = false;
}
@ -47,30 +47,31 @@ void SDLGamepad::ProcessInput() {
if (gamepad == nullptr)
return;
SDL_GameControllerUpdate();
for (auto const &ent1 : keyMapping) {
for (const auto &ent1 : keyMapping) {
SDL_GameControllerButton button = SDL_GameControllerGetButtonFromString(friendlyNameMapping[ent1.first].c_str());
if (button != SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_INVALID) {
Uint8 pressed = SDL_GameControllerGetButton(gamepad, button);
if (pressed == 1 && keysPressed[ent1.first] == false) {
for (auto& padstate : ent1.second) {
for (const auto& padstate : ent1.second) {
KeyMap::PressKey(padstate, 1.0);
keysPressed[ent1.first] = true;
}
}
else if (pressed == 0 && keysPressed[ent1.first] == true) {
for (auto& padstate : ent1.second) {
for (const auto& padstate : ent1.second) {
KeyMap::ReleaseKey(padstate);
keysPressed[ent1.first] = false;
}
}
}
else {
//Try axis if button isn't valid
// Try axis if button isn't valid
SDL_GameControllerAxis axis = SDL_GameControllerGetAxisFromString(friendlyNameMapping[ent1.first].c_str());
if (axis != SDL_GameControllerAxis::SDL_CONTROLLER_AXIS_INVALID) {
Sint16 value = SDL_GameControllerGetAxis(gamepad, axis);
for (auto& padstate : ent1.second) {
if (abs(value) < 0.2 * 32767.0) // dont process if in deadzone. Replace later with settings for deadzone.
for (const auto& padstate : ent1.second) {
// dont process if in deadzone. Replace later with settings for deadzone.
if (abs(value) < 0.2 * 32767.0)
KeyMap::ReleaseKey(padstate);
else
KeyMap::PressKey(padstate, (float)value / 32767.0);

View File

@ -13,7 +13,7 @@ public:
SDLGamepad();
~SDLGamepad();
bool InitDevice(int number, std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMap) override;
bool InitDevice(int number, const std::map<std::string, std::vector<KeyMap::KeyTarget>>& keyMap) override;
void ProcessInput() override;
bool CloseDevice() override;
private:
@ -45,5 +45,5 @@ private:
};
static bool SDLInitialized;
std::map<std::string, bool> keysPressed;
SDL_GameController* gamepad;
SDL_GameController* gamepad = nullptr;
};

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
#include <map>
#include "core/core_timing.h"
@ -11,7 +12,6 @@
#include "input_core/devices/SDLGamepad.h"
namespace InputCore {
constexpr u64 frame_ticks = 268123480ull / 60;
static int tick_event;
Service::HID::PadState pad_state;
@ -30,7 +30,7 @@ static void InputTickCallback(u64, int cycles_late) {
}
void Init() {
devices = ParseSettings();
ParseSettings();
tick_event = CoreTiming::RegisterEvent("InputCore::tick_event", InputTickCallback);
CoreTiming::ScheduleEvent(frame_ticks, tick_event);
}
@ -39,18 +39,52 @@ void Shutdown() {
devices.clear();
}
std::vector<std::shared_ptr<IDevice>> ParseSettings() {
std::vector<std::shared_ptr<IDevice>> devices;
std::vector<Settings::InputDeviceMapping> uniqueMappings; //unique mappings from settings file, used to init devices.
Service::HID::PadState GetPadState() {
return pad_state;
}
void SetPadState(Service::HID::PadState& state) {
pad_state.hex = state.hex;
}
std::tuple<s16, s16> GetCirclePad() {
return circle_pad;
}
void SetCirclePad(std::tuple<s16, s16>& pad) {
circle_pad = pad;
}
std::shared_ptr<Keyboard> GetKeyboard() {
return main_keyboard;
}
///Get Unique input mappings from settings
std::vector<Settings::InputDeviceMapping> GatherUniqueMappings() {
std::vector<Settings::InputDeviceMapping> uniqueMappings;
//Get Unique input mappings from settings
for (auto& mapping : Settings::values.input_mappings) {
if (!CheckIfMappingExists(uniqueMappings, mapping)) {
uniqueMappings.push_back(mapping);
}
}
return uniqueMappings;
}
//Generate a device for each unique mapping
std::map<std::string, std::vector<KeyMap::KeyTarget>> BuildKeyMapping(Settings::InputDeviceMapping mapping) {
std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMapping;
for (int i = 0; i < Settings::values.input_mappings.size(); i++) {
KeyMap::KeyTarget val = KeyMap::mapping_targets[i];
std::string key = Settings::values.input_mappings[i].key;
if (Settings::values.input_mappings[i] == mapping) {
keyMapping[key].push_back(val);
}
}
return keyMapping;
}
///Generate a device for each unique mapping
void GenerateUniqueDevices(std::vector<Settings::InputDeviceMapping> uniqueMappings) {
std::shared_ptr<IDevice> input;
for (auto& mapping : uniqueMappings) {
switch (mapping.framework) {
@ -76,25 +110,23 @@ std::vector<std::shared_ptr<IDevice>> ParseSettings() {
devices.push_back(input);
//Build map of inputs to listen for, for this device
std::map<std::string, std::vector<KeyMap::KeyTarget>> keyMapping;
for (int i = 0; i < Settings::values.input_mappings.size(); i++) {
KeyMap::KeyTarget val = KeyMap::mapping_targets[i];
std::string key = Settings::values.input_mappings[i].key;
if (Settings::values.input_mappings[i] == mapping) {
keyMapping[key].push_back(val);
}
}
auto keyMapping = BuildKeyMapping(mapping);
input->InitDevice(mapping.number, keyMapping);
}
}
void ParseSettings() {
std::vector<std::shared_ptr<IDevice>> devices;
auto uniqueMappings = GatherUniqueMappings();
GenerateUniqueDevices(uniqueMappings);
//init keyboard, if it hasn't already
if (main_keyboard == nullptr)
main_keyboard = std::make_shared<Keyboard>();
return devices;
}
bool CheckIfMappingExists(const std::vector<Settings::InputDeviceMapping> uniqueMapping, Settings::InputDeviceMapping mappingToCheck) {
bool CheckIfMappingExists(const std::vector<Settings::InputDeviceMapping>& uniqueMapping, Settings::InputDeviceMapping mappingToCheck) {
return std::any_of(uniqueMapping.begin(), uniqueMapping.end(), [mappingToCheck](const auto& mapping) {
return mapping == mappingToCheck;
});

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#pragma once
#include <memory>
#include <tuple>
@ -13,17 +14,16 @@
class Keyboard;
namespace InputCore {
extern Service::HID::PadState pad_state;
extern std::tuple<s16, s16> circle_pad;
extern std::shared_ptr<Keyboard> main_keyboard; ///< Instance of main keyboard device. Always initialized regardless of settings.
void Init();
void Shutdown();
Service::HID::PadState GetPadState();
void SetPadState(Service::HID::PadState& state);
std::tuple<s16, s16> GetCirclePad();
void SetCirclePad(std::tuple<s16, s16>& circle );
std::shared_ptr<Keyboard> GetKeyboard();
/// Read settings to initialize devices
void ParseSettings();
///Read settings to initialize devices
std::vector<std::shared_ptr<IDevice>> ParseSettings();
///Helper method to check if device was already initialized
bool CheckIfMappingExists(std::vector<Settings::InputDeviceMapping> uniqueMapping, Settings::InputDeviceMapping mappingToCheck);
/// Helper method to check if device was already initialized
bool CheckIfMappingExists(const std::vector<Settings::InputDeviceMapping>& uniqueMapping, Settings::InputDeviceMapping mappingToCheck);
}

View File

@ -34,30 +34,40 @@ namespace KeyMap {
};
void PressKey(KeyTarget target, const float strength) {
if (std::find(std::begin(analog_inputs), std::end(analog_inputs), target) == std::end(analog_inputs)) { // If is digital keytarget
InputCore::pad_state.hex |= target.target.direct_target_hex;
auto pad_state = InputCore::GetPadState();
// If is digital keytarget
if (std::find(std::begin(analog_inputs), std::end(analog_inputs), target) == std::end(analog_inputs)) {
pad_state.hex |= target.target.direct_target_hex;
InputCore::SetPadState(pad_state);
}
else { // it is analog input
auto circle_pad = InputCore::GetCirclePad();
if (target == Service::HID::PAD_CIRCLE_UP || target == Service::HID::PAD_CIRCLE_DOWN) {
std::get<1>(InputCore::circle_pad) = MAX_CIRCLEPAD_POS * strength * -1;
std::get<1>(circle_pad) = MAX_CIRCLEPAD_POS * strength * -1;
}
else if (target == Service::HID::PAD_CIRCLE_LEFT || target == Service::HID::PAD_CIRCLE_RIGHT) {
std::get<0>(InputCore::circle_pad) = MAX_CIRCLEPAD_POS * strength;
std::get<0>(circle_pad) = MAX_CIRCLEPAD_POS * strength;
}
InputCore::SetCirclePad(circle_pad);
}
}
void ReleaseKey(KeyTarget target) {
if (std::find(std::begin(analog_inputs), std::end(analog_inputs), target) == std::end(analog_inputs)) { // If is digital keytarget
InputCore::pad_state.hex &= ~target.target.direct_target_hex;
auto pad_state = InputCore::GetPadState();
// If is digital keytarget
if (std::find(std::begin(analog_inputs), std::end(analog_inputs), target) == std::end(analog_inputs)) {
pad_state.hex &= ~target.target.direct_target_hex;
InputCore::SetPadState(pad_state);
}
else { // it is analog input
auto circle_pad = InputCore::GetCirclePad();
if (target == Service::HID::PAD_CIRCLE_UP || target == Service::HID::PAD_CIRCLE_DOWN) {
std::get<1>(InputCore::circle_pad) = 0;
std::get<1>(circle_pad) = 0;
}
else if (target == Service::HID::PAD_CIRCLE_LEFT || target == Service::HID::PAD_CIRCLE_RIGHT) {
std::get<0>(InputCore::circle_pad) = 0;
std::get<0>(circle_pad) = 0;
}
InputCore::SetCirclePad(circle_pad);
}
}
}