2023-11-24 23:10:21 +01:00
|
|
|
#ifndef __RECOMP_INPUT_H__
|
|
|
|
#define __RECOMP_INPUT_H__
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <variant>
|
|
|
|
#include <vector>
|
|
|
|
#include <type_traits>
|
2023-12-13 08:06:56 +01:00
|
|
|
#include <span>
|
2024-01-16 03:06:52 +01:00
|
|
|
#include <string>
|
2024-01-21 20:04:56 +01:00
|
|
|
#include <string_view>
|
2023-11-24 23:10:21 +01:00
|
|
|
|
2024-06-12 09:40:14 +02:00
|
|
|
#include "ultramodern/input.hpp"
|
|
|
|
|
2024-03-04 08:13:12 +01:00
|
|
|
#include "json/json.hpp"
|
|
|
|
|
2023-11-24 23:10:21 +01:00
|
|
|
namespace recomp {
|
2024-01-21 20:04:56 +01:00
|
|
|
// x-macros to build input enums and arrays.
|
|
|
|
// First parameter is the enum name, second parameter is the bit field for the input (or 0 if there is no associated one), third is the readable name.
|
2024-06-05 01:12:43 +02:00
|
|
|
// TODO refactor this to allow projects to rename these, or get rid of the readable name and leave that up to individual projects to map.
|
2024-01-21 20:04:56 +01:00
|
|
|
#define DEFINE_N64_BUTTON_INPUTS() \
|
2024-03-03 21:06:34 +01:00
|
|
|
DEFINE_INPUT(A, 0x8000, "Action") \
|
|
|
|
DEFINE_INPUT(B, 0x4000, "Attack/Cancel") \
|
|
|
|
DEFINE_INPUT(Z, 0x2000, "Target") \
|
|
|
|
DEFINE_INPUT(START, 0x1000, "Start") \
|
|
|
|
DEFINE_INPUT(L, 0x0020, "Toggle map") \
|
|
|
|
DEFINE_INPUT(R, 0x0010, "Shield") \
|
|
|
|
DEFINE_INPUT(C_UP, 0x0008, "Look/Fairy") \
|
|
|
|
DEFINE_INPUT(C_LEFT, 0x0002, "Item 1") \
|
|
|
|
DEFINE_INPUT(C_DOWN, 0x0004, "Item 2") \
|
|
|
|
DEFINE_INPUT(C_RIGHT, 0x0001, "Item 3") \
|
|
|
|
DEFINE_INPUT(DPAD_UP, 0x0800, "Special Item 1") \
|
|
|
|
DEFINE_INPUT(DPAD_RIGHT, 0x0100, "Special Item 2") \
|
|
|
|
DEFINE_INPUT(DPAD_DOWN, 0x0400, "Special Item 3") \
|
|
|
|
DEFINE_INPUT(DPAD_LEFT, 0x0200, "Special Item 4")
|
2024-01-21 20:04:56 +01:00
|
|
|
|
|
|
|
#define DEFINE_N64_AXIS_INPUTS() \
|
2024-03-03 21:06:34 +01:00
|
|
|
DEFINE_INPUT(Y_AXIS_POS, 0, "Up") \
|
|
|
|
DEFINE_INPUT(Y_AXIS_NEG, 0, "Down") \
|
|
|
|
DEFINE_INPUT(X_AXIS_NEG, 0, "Left") \
|
|
|
|
DEFINE_INPUT(X_AXIS_POS, 0, "Right") \
|
2024-01-21 20:04:56 +01:00
|
|
|
|
2024-05-26 15:58:51 +02:00
|
|
|
#define DEFINE_RECOMP_UI_INPUTS() \
|
2024-07-06 03:19:31 +02:00
|
|
|
DEFINE_INPUT(TOGGLE_MENU, 0, "Toggle Menu") \
|
|
|
|
DEFINE_INPUT(ACCEPT_MENU, 0, "Accept (Menu)") \
|
|
|
|
DEFINE_INPUT(APPLY_MENU, 0, "Apply (Menu)")
|
2024-05-26 15:58:51 +02:00
|
|
|
|
2024-01-21 20:04:56 +01:00
|
|
|
#define DEFINE_ALL_INPUTS() \
|
|
|
|
DEFINE_N64_BUTTON_INPUTS() \
|
2024-05-26 15:58:51 +02:00
|
|
|
DEFINE_N64_AXIS_INPUTS() \
|
|
|
|
DEFINE_RECOMP_UI_INPUTS()
|
2024-01-21 20:04:56 +01:00
|
|
|
|
|
|
|
// Enum containing every recomp input.
|
|
|
|
#define DEFINE_INPUT(name, value, readable) name,
|
|
|
|
enum class GameInput {
|
|
|
|
DEFINE_ALL_INPUTS()
|
|
|
|
|
|
|
|
COUNT,
|
|
|
|
N64_BUTTON_START = A,
|
|
|
|
N64_BUTTON_COUNT = C_RIGHT - N64_BUTTON_START + 1,
|
|
|
|
N64_AXIS_START = X_AXIS_NEG,
|
|
|
|
N64_AXIS_COUNT = Y_AXIS_POS - N64_AXIS_START + 1,
|
|
|
|
};
|
|
|
|
#undef DEFINE_INPUT
|
|
|
|
|
2023-12-13 08:06:56 +01:00
|
|
|
struct InputField {
|
2024-01-16 03:06:52 +01:00
|
|
|
uint32_t input_type;
|
2023-12-13 08:06:56 +01:00
|
|
|
int32_t input_id;
|
2024-01-16 03:06:52 +01:00
|
|
|
std::string to_string() const;
|
|
|
|
auto operator<=>(const InputField& rhs) const = default;
|
2023-11-24 23:10:21 +01:00
|
|
|
};
|
|
|
|
|
2023-12-13 08:06:56 +01:00
|
|
|
void poll_inputs();
|
|
|
|
float get_input_analog(const InputField& field);
|
|
|
|
float get_input_analog(const std::span<const recomp::InputField> fields);
|
|
|
|
bool get_input_digital(const InputField& field);
|
|
|
|
bool get_input_digital(const std::span<const recomp::InputField> fields);
|
2024-01-23 05:08:59 +01:00
|
|
|
void get_gyro_deltas(float* x, float* y);
|
2024-04-26 07:48:26 +02:00
|
|
|
void get_mouse_deltas(float* x, float* y);
|
2024-05-26 15:34:26 +02:00
|
|
|
void get_right_analog(float* x, float* y);
|
2024-01-16 03:06:52 +01:00
|
|
|
|
|
|
|
enum class InputDevice {
|
|
|
|
Controller,
|
|
|
|
Keyboard,
|
|
|
|
COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
void start_scanning_input(InputDevice device);
|
2024-03-12 14:35:58 +01:00
|
|
|
void stop_scanning_input();
|
2024-01-16 03:06:52 +01:00
|
|
|
void finish_scanning_input(InputField scanned_field);
|
2024-03-12 14:35:58 +01:00
|
|
|
void cancel_scanning_input();
|
2024-05-02 07:51:09 +02:00
|
|
|
void config_menu_set_cont_or_kb(bool cont_interacted);
|
2024-01-16 03:06:52 +01:00
|
|
|
InputField get_scanned_input();
|
2024-07-06 03:19:31 +02:00
|
|
|
int get_scanned_input_index();
|
2023-12-13 08:06:56 +01:00
|
|
|
|
|
|
|
struct DefaultN64Mappings {
|
|
|
|
std::vector<InputField> a;
|
|
|
|
std::vector<InputField> b;
|
|
|
|
std::vector<InputField> l;
|
|
|
|
std::vector<InputField> r;
|
|
|
|
std::vector<InputField> z;
|
|
|
|
std::vector<InputField> start;
|
|
|
|
|
|
|
|
std::vector<InputField> c_left;
|
|
|
|
std::vector<InputField> c_right;
|
|
|
|
std::vector<InputField> c_up;
|
|
|
|
std::vector<InputField> c_down;
|
|
|
|
|
|
|
|
std::vector<InputField> dpad_left;
|
|
|
|
std::vector<InputField> dpad_right;
|
|
|
|
std::vector<InputField> dpad_up;
|
|
|
|
std::vector<InputField> dpad_down;
|
|
|
|
|
|
|
|
std::vector<InputField> analog_left;
|
|
|
|
std::vector<InputField> analog_right;
|
|
|
|
std::vector<InputField> analog_up;
|
|
|
|
std::vector<InputField> analog_down;
|
2024-05-26 15:58:51 +02:00
|
|
|
|
|
|
|
std::vector<InputField> toggle_menu;
|
2024-07-06 03:19:31 +02:00
|
|
|
std::vector<InputField> accept_menu;
|
|
|
|
std::vector<InputField> apply_menu;
|
2023-11-24 23:10:21 +01:00
|
|
|
};
|
|
|
|
|
2024-09-09 04:45:37 +02:00
|
|
|
inline const std::vector<InputField>& get_default_mapping_for_input(const DefaultN64Mappings& defaults, const GameInput input) {
|
|
|
|
static const std::vector<InputField> empty_input_field{};
|
2024-05-26 15:58:51 +02:00
|
|
|
switch (input) {
|
|
|
|
case GameInput::A: return defaults.a;
|
|
|
|
case GameInput::B: return defaults.b;
|
|
|
|
case GameInput::L: return defaults.l;
|
|
|
|
case GameInput::R: return defaults.r;
|
|
|
|
case GameInput::Z: return defaults.z;
|
|
|
|
case GameInput::START: return defaults.start;
|
|
|
|
case GameInput::C_LEFT: return defaults.c_left;
|
|
|
|
case GameInput::C_RIGHT: return defaults.c_right;
|
|
|
|
case GameInput::C_UP: return defaults.c_up;
|
|
|
|
case GameInput::C_DOWN: return defaults.c_down;
|
|
|
|
case GameInput::DPAD_LEFT: return defaults.dpad_left;
|
|
|
|
case GameInput::DPAD_RIGHT: return defaults.dpad_right;
|
|
|
|
case GameInput::DPAD_UP: return defaults.dpad_up;
|
|
|
|
case GameInput::DPAD_DOWN: return defaults.dpad_down;
|
|
|
|
case GameInput::X_AXIS_NEG: return defaults.analog_left;
|
|
|
|
case GameInput::X_AXIS_POS: return defaults.analog_right;
|
|
|
|
case GameInput::Y_AXIS_POS: return defaults.analog_up;
|
|
|
|
case GameInput::Y_AXIS_NEG: return defaults.analog_down;
|
|
|
|
case GameInput::TOGGLE_MENU: return defaults.toggle_menu;
|
2024-07-06 03:19:31 +02:00
|
|
|
case GameInput::ACCEPT_MENU: return defaults.accept_menu;
|
|
|
|
case GameInput::APPLY_MENU: return defaults.apply_menu;
|
2024-09-09 04:45:37 +02:00
|
|
|
default: return empty_input_field;
|
2024-05-26 15:58:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 09:38:05 +01:00
|
|
|
extern const DefaultN64Mappings default_n64_keyboard_mappings;
|
|
|
|
extern const DefaultN64Mappings default_n64_controller_mappings;
|
2023-11-24 23:10:21 +01:00
|
|
|
|
2024-01-16 03:06:52 +01:00
|
|
|
constexpr size_t bindings_per_input = 2;
|
|
|
|
|
|
|
|
size_t get_num_inputs();
|
2024-01-21 20:04:56 +01:00
|
|
|
const std::string& get_input_name(GameInput input);
|
|
|
|
const std::string& get_input_enum_name(GameInput input);
|
|
|
|
GameInput get_input_from_enum_name(const std::string_view name);
|
|
|
|
InputField& get_input_binding(GameInput input, size_t binding_index, InputDevice device);
|
|
|
|
void set_input_binding(GameInput input, size_t binding_index, InputDevice device, InputField value);
|
2024-01-15 04:49:04 +01:00
|
|
|
|
2024-06-12 09:40:14 +02:00
|
|
|
bool get_n64_input(int controller_num, uint16_t* buttons_out, float* x_out, float* y_out);
|
|
|
|
void set_rumble(int controller_num, bool);
|
2024-04-15 17:15:45 +02:00
|
|
|
void update_rumble();
|
2023-11-24 23:10:21 +01:00
|
|
|
void handle_events();
|
2024-06-12 09:40:14 +02:00
|
|
|
|
|
|
|
ultramodern::input::connected_device_info_t get_connected_device_info(int controller_num);
|
2024-03-04 08:13:12 +01:00
|
|
|
|
|
|
|
// Rumble strength ranges from 0 to 100.
|
|
|
|
int get_rumble_strength();
|
|
|
|
void set_rumble_strength(int strength);
|
2024-04-21 19:14:41 +02:00
|
|
|
|
2024-04-26 07:48:26 +02:00
|
|
|
// Gyro and mouse sensitivities range from 0 to 100.
|
2024-04-21 19:14:41 +02:00
|
|
|
int get_gyro_sensitivity();
|
2024-04-26 07:48:26 +02:00
|
|
|
int get_mouse_sensitivity();
|
2024-05-24 23:55:16 +02:00
|
|
|
int get_joystick_deadzone();
|
2024-04-21 19:14:41 +02:00
|
|
|
void set_gyro_sensitivity(int strength);
|
2024-04-26 07:48:26 +02:00
|
|
|
void set_mouse_sensitivity(int strength);
|
2024-05-24 23:55:16 +02:00
|
|
|
void set_joystick_deadzone(int strength);
|
2024-05-26 15:34:26 +02:00
|
|
|
void apply_joystick_deadzone(float x_in, float y_in, float* x_out, float* y_out);
|
|
|
|
void set_right_analog_suppressed(bool suppressed);
|
2024-03-04 08:13:12 +01:00
|
|
|
|
2024-04-22 00:33:09 +02:00
|
|
|
enum class BackgroundInputMode {
|
|
|
|
On,
|
|
|
|
Off,
|
|
|
|
OptionCount
|
|
|
|
};
|
|
|
|
|
|
|
|
NLOHMANN_JSON_SERIALIZE_ENUM(recomp::BackgroundInputMode, {
|
|
|
|
{recomp::BackgroundInputMode::On, "On"},
|
|
|
|
{recomp::BackgroundInputMode::Off, "Off"}
|
|
|
|
});
|
|
|
|
|
|
|
|
BackgroundInputMode get_background_input_mode();
|
|
|
|
void set_background_input_mode(BackgroundInputMode mode);
|
|
|
|
|
2024-01-08 09:38:05 +01:00
|
|
|
bool game_input_disabled();
|
2024-01-16 03:06:52 +01:00
|
|
|
bool all_input_disabled();
|
2023-11-24 23:10:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|