1
0
Fork 0
forked from suyu/suyu
suyu/src/common/key_map.cpp

153 lines
4.4 KiB
C++
Raw Normal View History

// Copyright 2014 Citra Emulator Project
2014-12-17 06:38:14 +01:00
// Licensed under GPLv2 or any later version
2014-09-04 03:12:58 +02:00
// Refer to the license.txt file included.
#include <map>
2016-05-12 12:09:36 +02:00
#include "common/emu_window.h"
#include "common/key_map.h"
2016-05-12 12:09:36 +02:00
2014-09-04 03:12:58 +02:00
namespace KeyMap {
2016-05-12 12:09:36 +02:00
// TODO (wwylele): currently we treat c-stick as four direction buttons
// and map it directly to EmuWindow::ButtonPressed.
// It should go the analog input way like circle pad does.
const std::array<KeyTarget, Settings::NativeInput::NUM_INPUTS> mapping_targets = {{
Service::HID::PAD_A,
Service::HID::PAD_B,
Service::HID::PAD_X,
Service::HID::PAD_Y,
Service::HID::PAD_L,
Service::HID::PAD_R,
Service::HID::PAD_ZL,
Service::HID::PAD_ZR,
Service::HID::PAD_START,
Service::HID::PAD_SELECT,
Service::HID::PAD_NONE,
Service::HID::PAD_UP,
Service::HID::PAD_DOWN,
Service::HID::PAD_LEFT,
Service::HID::PAD_RIGHT,
Service::HID::PAD_C_UP,
Service::HID::PAD_C_DOWN,
Service::HID::PAD_C_LEFT,
Service::HID::PAD_C_RIGHT,
2016-05-12 12:09:36 +02:00
2016-05-15 12:35:45 +02:00
IndirectTarget::CirclePadUp,
IndirectTarget::CirclePadDown,
IndirectTarget::CirclePadLeft,
IndirectTarget::CirclePadRight,
IndirectTarget::CirclePadModifier,
2016-05-12 12:09:36 +02:00
}};
static std::map<HostDeviceKey, KeyTarget> key_map;
static int next_device_id = 0;
2016-05-13 17:32:43 +02:00
static bool circle_pad_up = false;
static bool circle_pad_down = false;
static bool circle_pad_left = false;
static bool circle_pad_right = false;
static bool circle_pad_modifier = false;
2016-05-12 12:09:36 +02:00
static void UpdateCirclePad(EmuWindow& emu_window) {
2016-11-29 22:51:53 +01:00
constexpr float SQRT_HALF = 0.707106781f;
2016-05-12 12:09:36 +02:00
int x = 0, y = 0;
if (circle_pad_right)
++x;
if (circle_pad_left)
--x;
if (circle_pad_up)
++y;
if (circle_pad_down)
--y;
2016-05-13 17:32:43 +02:00
2016-11-29 22:51:53 +01:00
float modifier = circle_pad_modifier ? Settings::values.pad_circle_modifier_scale : 1.0f;
emu_window.CirclePadUpdated(x * modifier * (y == 0 ? 1.0f : SQRT_HALF),
y * modifier * (x == 0 ? 1.0f : SQRT_HALF));
2016-05-12 12:09:36 +02:00
}
int NewDeviceId() {
return next_device_id++;
}
2014-09-04 03:12:58 +02:00
2016-05-12 12:09:36 +02:00
void SetKeyMapping(HostDeviceKey key, KeyTarget target) {
key_map[key] = target;
2014-09-04 03:12:58 +02:00
}
2016-05-12 12:09:36 +02:00
void ClearKeyMapping(int device_id) {
auto iter = key_map.begin();
while (iter != key_map.end()) {
if (iter->first.device_id == device_id)
key_map.erase(iter++);
else
++iter;
}
}
void PressKey(EmuWindow& emu_window, HostDeviceKey key) {
auto target = key_map.find(key);
if (target == key_map.end())
return;
if (target->second.direct) {
emu_window.ButtonPressed({{target->second.target.direct_target_hex}});
} else {
switch (target->second.target.indirect_target) {
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadUp:
2016-05-12 12:09:36 +02:00
circle_pad_up = true;
UpdateCirclePad(emu_window);
break;
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadDown:
2016-05-12 12:09:36 +02:00
circle_pad_down = true;
UpdateCirclePad(emu_window);
break;
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadLeft:
2016-05-12 12:09:36 +02:00
circle_pad_left = true;
UpdateCirclePad(emu_window);
break;
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadRight:
2016-05-12 12:09:36 +02:00
circle_pad_right = true;
UpdateCirclePad(emu_window);
break;
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadModifier:
2016-05-13 17:32:43 +02:00
circle_pad_modifier = true;
UpdateCirclePad(emu_window);
break;
2016-05-12 12:09:36 +02:00
}
}
}
void ReleaseKey(EmuWindow& emu_window, HostDeviceKey key) {
2016-05-12 12:09:36 +02:00
auto target = key_map.find(key);
if (target == key_map.end())
return;
if (target->second.direct) {
emu_window.ButtonReleased({{target->second.target.direct_target_hex}});
} else {
switch (target->second.target.indirect_target) {
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadUp:
2016-05-12 12:09:36 +02:00
circle_pad_up = false;
UpdateCirclePad(emu_window);
break;
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadDown:
2016-05-12 12:09:36 +02:00
circle_pad_down = false;
UpdateCirclePad(emu_window);
break;
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadLeft:
2016-05-12 12:09:36 +02:00
circle_pad_left = false;
UpdateCirclePad(emu_window);
break;
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadRight:
2016-05-12 12:09:36 +02:00
circle_pad_right = false;
UpdateCirclePad(emu_window);
break;
2016-05-15 12:35:45 +02:00
case IndirectTarget::CirclePadModifier:
2016-05-13 17:32:43 +02:00
circle_pad_modifier = false;
UpdateCirclePad(emu_window);
break;
2016-05-12 12:09:36 +02:00
}
}
2014-09-04 03:12:58 +02:00
}
}