Cleanup old code.

This commit is contained in:
Anon 2016-12-18 12:30:44 -06:00
parent 6d9a4eccf5
commit 134cd66ae3
3 changed files with 23 additions and 101 deletions

View File

@ -12,26 +12,7 @@ Keyboard::Keyboard() {}
Keyboard::~Keyboard() {}
bool Keyboard::InitDevice(int number, Settings::InputDeviceMapping device_mapping) {
// Check if keyboard is mapped for circle up or left. if so, set modifier to -1
/*for (const auto& entry : key_mapping) {
if (entry.first == "")
continue;
for (const auto& padstate : entry.second) {
if (padstate == Service::HID::PAD_CIRCLE_UP ||
padstate == Service::HID::PAD_CIRCLE_LEFT) {
circle_pad_directions[stoi(entry.first)] = -1.0;
} else if (padstate == Service::HID::PAD_CIRCLE_DOWN ||
padstate == Service::HID::PAD_CIRCLE_RIGHT) {
circle_pad_directions[stoi(entry.first)] = 1.0;
}
}
}*/
// Check if responsible for circle pad modifier
input_device_mapping = device_mapping;
auto mapping = Settings::values.pad_circle_modifier;
if (mapping.device == Settings::Device::Keyboard && mapping.key != -1)
circle_pad_modifier = KeyboardKey(mapping.key, "");
return true;
}
@ -70,16 +51,10 @@ void Keyboard::Clear() {
}
Settings::InputDeviceMapping Keyboard::GetInput() {
std::map<KeyboardKey, bool> keysPressedCopy;
{
std::lock_guard<std::mutex> lock(m);
keysPressedCopy = keys_pressed;
}
for (const auto& entry : keysPressedCopy) {
int keycode = entry.first.key;
if (keysPressedCopy[entry.first] == true && keys_pressed_last[keycode] == false) {
return Settings::InputDeviceMapping("SDL/0/Keyboard/" + std::to_string(keycode));
}
auto result = ProcessInput();
for (const auto& entry : result) {
if (entry.second > 0.5)
return entry.first;
}
return Settings::InputDeviceMapping("");
}

View File

@ -35,15 +35,10 @@ bool SDLGamepad::InitDevice(int number, Settings::InputDeviceMapping device_mapp
if (SDL_IsGameController(number)) {
gamepad = SDL_GameControllerOpen(number);
if (gamepad == nullptr) {
LOG_INFO(Input, "Controller found but unable to open connection.");
return false;
}
}
input_device_mapping = device_mapping;
/*for (const auto& entry : key_mapping) {
keys_pressed[entry.first] = false;
}*/
return true;
}
@ -68,10 +63,12 @@ std::map<Settings::InputDeviceMapping, float> SDLGamepad::ProcessInput() {
button_status.emplace(input_device_mapping, 0);
input_device_mapping.key += 1; // minus axis value is always one greater
button_status.emplace(input_device_mapping, abs(strength));
} else {
} else if (strength >= 0 && i < 4) {
button_status.emplace(input_device_mapping, abs(strength));
input_device_mapping.key += 1; // minus axis value is always one greater
input_device_mapping.key += 1;
button_status.emplace(input_device_mapping, 0);
} else { // Only trigger buttons
button_status.emplace(input_device_mapping, abs(strength));
}
}
return button_status;
@ -86,20 +83,12 @@ bool SDLGamepad::CloseDevice() {
std::vector<std::shared_ptr<IDevice>> SDLGamepad::GetAllDevices() {
std::vector<std::shared_ptr<IDevice>> devices;
if (!SDLGamepad::SDLInitialized && SDL_Init(SDL_INIT_GAMECONTROLLER) < 0) {
LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_GAMECONTROLLER) failed");
return devices;
}
LoadGameControllerDB();
SDL_GameControllerEventState(SDL_IGNORE);
for (int i = 0; i < 8; i++) {
SDL_GameController* gamecontroller;
if (SDL_IsGameController(i)) {
gamecontroller = SDL_GameControllerOpen(i);
if (gamecontroller != nullptr) {
devices.push_back(std::make_shared<SDLGamepad>(i, gamecontroller));
}
}
auto gamepad = std::make_shared<SDLGamepad>();
bool success = gamepad->InitDevice(
i, Settings::InputDeviceMapping("SDL/" + std::to_string(i) + "/Gamepad/-1"));
if (success)
devices.push_back(gamepad);
}
return devices;
}
@ -121,54 +110,11 @@ void SDLGamepad::LoadGameControllerDB() {
Settings::InputDeviceMapping SDLGamepad::GetInput() {
if (gamepad == nullptr)
return Settings::InputDeviceMapping("");
SDL_GameControllerUpdate();
for (int i = 0; i < SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_MAX; i++) {
Uint8 pressed = SDL_GameControllerGetButton(gamepad, SDL_GameControllerButton(i));
if (pressed == 0)
continue;
auto buttonName = SDL_GameControllerGetStringForButton(SDL_GameControllerButton(i));
for (const auto& mapping : gamepadinput_to_sdlname_mapping) {
if (mapping.second == buttonName) {
return Settings::InputDeviceMapping(
"SDL/" + std::to_string(number) + "/" + "Gamepad/" +
std::to_string(static_cast<int>(mapping.first)));
}
}
}
for (int i = 0; i < SDL_GameControllerAxis::SDL_CONTROLLER_AXIS_MAX; i++) {
Sint16 value = SDL_GameControllerGetAxis(gamepad, SDL_GameControllerAxis(i));
// TODO: calculate deadzone by radial field rather than axial field. (sqrt(x^2 + y^2) >
// deadzone)
// dont process if in deadzone. Replace later with settings for deadzone.
if (abs(value) < 0.2 * 32767.0)
continue;
std::string modifier;
if (value > 0)
modifier = "+";
else
modifier = "-";
std::string axisName = SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis(i));
for (const auto& mapping : gamepadinput_to_sdlname_mapping) {
if (mapping.second == axisName) {
if ((mapping.first == GamepadInputs::LeftXMinus ||
mapping.first == GamepadInputs::LeftYMinus ||
mapping.first == GamepadInputs::RightXMinus ||
mapping.first == GamepadInputs::RightYMinus) &&
modifier == "+") {
continue;
} else if ((mapping.first == GamepadInputs::LeftXPlus ||
mapping.first == GamepadInputs::LeftYPlus ||
mapping.first == GamepadInputs::RightXPlus ||
mapping.first == GamepadInputs::RightYPlus) &&
modifier == "-") {
continue;
}
return Settings::InputDeviceMapping(
"SDL/" + std::to_string(this->number) + "/" + "Gamepad/" +
std::to_string(static_cast<int>(mapping.first)));
}
}
auto results = ProcessInput();
for (auto& input : results) {
if (input.second > 0.5)
return input.first;
}
return Settings::InputDeviceMapping("");
}

View File

@ -55,6 +55,7 @@ void InputCore::InputTickCallback(u64, int cycles_late) {
void InputCore::UpdateEmulatorInputs(
std::vector<std::map<Settings::InputDeviceMapping, float>> inputs) {
std::lock_guard<std::mutex> lock(pad_state_mutex);
// Apply deadzone
float leftx = 0, lefty = 0;
float circle_pad_modifier = 1.0;
@ -202,14 +203,11 @@ void InputCore::GenerateUniqueDevices() {
}
}
devices.push_back(input);
// Build map of inputs to listen for, for this device
BuildKeyMapping();
input->InitDevice(mapping.number, mapping);
}
if (main_keyboard == nullptr) {
main_keyboard = std::make_shared<Keyboard>();
main_keyboard->InitDevice(0, Settings::InputDeviceMapping("SDL/0/Keyboard/-1"));
devices.push_back(main_keyboard);
}
}
@ -217,6 +215,7 @@ void InputCore::GenerateUniqueDevices() {
/// Read settings to initialize devices
void InputCore::ParseSettings() {
GenerateUniqueDevices();
BuildKeyMapping();
}
std::tuple<float, float> InputCore::ApplyDeadzone(float x, float y, float dead_zone) {
@ -244,7 +243,9 @@ void InputCore::ReloadSettings() {
/// Returns all available input devices. Used for key binding in GUI
std::vector<std::shared_ptr<IDevice>> InputCore::GetAllDevices() {
auto all_devices = SDLGamepad::GetAllDevices();
all_devices.push_back(InputCore::GetKeyboard());
auto keyboard = InputCore::GetKeyboard();
keyboard->InitDevice(0, Settings::InputDeviceMapping("SDL/0/Keyboard/-1"));
all_devices.push_back(keyboard);
return all_devices;
}