2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* input_default.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2022-01-13 09:45:09 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2015-09-24 23:06:15 +02:00
|
|
|
#ifndef INPUT_DEFAULT_H
|
|
|
|
#define INPUT_DEFAULT_H
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/input.h"
|
2015-09-24 23:06:15 +02:00
|
|
|
|
|
|
|
class InputDefault : public Input {
|
2017-03-05 16:44:50 +01:00
|
|
|
GDCLASS(InputDefault, Input);
|
2015-09-24 23:06:15 +02:00
|
|
|
_THREAD_SAFE_CLASS_
|
|
|
|
|
|
|
|
int mouse_button_mask;
|
2016-09-01 23:58:52 +02:00
|
|
|
|
2021-11-23 10:14:19 +01:00
|
|
|
Set<int> physical_keys_pressed;
|
2015-09-24 23:06:15 +02:00
|
|
|
Set<int> keys_pressed;
|
|
|
|
Set<int> joy_buttons_pressed;
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<int, float> _joy_axis;
|
2016-09-01 23:58:52 +02:00
|
|
|
//Map<StringName,int> custom_action_press;
|
2016-11-23 13:32:44 +01:00
|
|
|
Vector3 gravity;
|
2015-09-24 23:06:15 +02:00
|
|
|
Vector3 accelerometer;
|
2016-05-27 19:29:37 +02:00
|
|
|
Vector3 magnetometer;
|
2016-07-15 09:31:34 +02:00
|
|
|
Vector3 gyroscope;
|
2015-09-24 23:06:15 +02:00
|
|
|
Vector2 mouse_pos;
|
|
|
|
MainLoop *main_loop;
|
|
|
|
|
2016-09-01 23:58:52 +02:00
|
|
|
struct Action {
|
2017-09-30 16:19:07 +02:00
|
|
|
uint64_t physics_frame;
|
2016-09-01 23:58:52 +02:00
|
|
|
uint64_t idle_frame;
|
|
|
|
bool pressed;
|
Allow checking for exact matches with Action events.
Added additional param to action related methods to test for exactness.
If "p_exact_match" is true, then the action will only be "matched" if the provided input event *exactly* matches with the action event.
Before:
* Action Event = KEY_S
* Input Event = KEY_CONTROL + KEY_S
* Is Action Pressed = True
Now:
You can still do the above, however you can optionally check that the input is exactly what the action event is:
* Action Event = KEY_S
* Input Event = KEY_CONTROL + KEY_S
* p_exact_match = True
* Is Action Pressed = False
* If the Input Event was only KEY_S, then the result would be true.
Usage:
```gdscript
Input.is_action_pressed(action_name: String, exact_match: bool)
Input.is_action_pressed("my_action", true)
InputMap.event_is_action(p_event, "my_action", true)
func _input(event: InputEvent):
event.is_action_pressed("my_action", false, true) # false = "allow_echo", true = "exact_match"
event.is_action("my_action", true)
```
Co-authored-by: Eric M <itsjusteza@gmail.com>
2020-12-13 15:22:42 +01:00
|
|
|
bool exact;
|
2018-02-21 22:06:34 +01:00
|
|
|
float strength;
|
2021-07-23 02:28:08 +02:00
|
|
|
float raw_strength;
|
2016-09-01 23:58:52 +02:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<StringName, Action> action_state;
|
2016-09-01 23:58:52 +02:00
|
|
|
|
2018-02-24 03:04:30 +01:00
|
|
|
bool emulate_touch_from_mouse;
|
|
|
|
bool emulate_mouse_from_touch;
|
|
|
|
|
|
|
|
int mouse_from_touch_index;
|
2016-06-15 07:25:35 +02:00
|
|
|
|
2015-09-24 23:06:15 +02:00
|
|
|
struct SpeedTrack {
|
|
|
|
uint64_t last_tick;
|
|
|
|
Vector2 speed;
|
|
|
|
Vector2 accum;
|
|
|
|
float accum_t;
|
|
|
|
float min_ref_frame;
|
|
|
|
float max_ref_frame;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void update(const Vector2 &p_delta_p);
|
2015-09-24 23:06:15 +02:00
|
|
|
void reset();
|
|
|
|
SpeedTrack();
|
|
|
|
};
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
struct Joypad {
|
2015-12-18 19:15:32 +01:00
|
|
|
StringName name;
|
|
|
|
StringName uid;
|
2016-07-05 14:07:46 +02:00
|
|
|
bool connected;
|
2021-05-21 18:17:42 +02:00
|
|
|
bool last_buttons[JOY_BUTTON_MAX + 12]; //apparently SDL specifies 35 possible buttons on android
|
2015-12-18 19:15:32 +01:00
|
|
|
float last_axis[JOY_AXIS_MAX];
|
|
|
|
int last_hat;
|
|
|
|
int mapping;
|
|
|
|
int hat_current;
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
Joypad() {
|
2015-12-18 19:15:32 +01:00
|
|
|
for (int i = 0; i < JOY_AXIS_MAX; i++) {
|
|
|
|
last_axis[i] = 0.0f;
|
|
|
|
}
|
2021-05-21 18:17:42 +02:00
|
|
|
for (int i = 0; i < JOY_BUTTON_MAX + 12; i++) {
|
2015-12-18 19:15:32 +01:00
|
|
|
last_buttons[i] = false;
|
|
|
|
}
|
2016-07-05 14:07:46 +02:00
|
|
|
connected = false;
|
2015-12-18 19:15:32 +01:00
|
|
|
last_hat = HAT_MASK_CENTER;
|
2016-01-08 00:40:41 +01:00
|
|
|
mapping = -1;
|
2018-04-19 13:04:41 +02:00
|
|
|
hat_current = 0;
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
|
|
|
};
|
2015-12-18 06:12:53 +01:00
|
|
|
|
|
|
|
SpeedTrack mouse_speed_track;
|
2018-10-14 17:55:03 +02:00
|
|
|
Map<int, SpeedTrack> touch_speed_track;
|
2017-01-08 21:05:51 +01:00
|
|
|
Map<int, Joypad> joy_names;
|
2016-01-24 18:01:37 +01:00
|
|
|
int fallback_mapping;
|
2018-09-26 11:22:59 +02:00
|
|
|
|
|
|
|
CursorShape default_shape;
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2015-12-18 06:12:53 +01:00
|
|
|
public:
|
|
|
|
enum HatMask {
|
|
|
|
HAT_MASK_CENTER = 0,
|
|
|
|
HAT_MASK_UP = 1,
|
|
|
|
HAT_MASK_RIGHT = 2,
|
|
|
|
HAT_MASK_DOWN = 4,
|
|
|
|
HAT_MASK_LEFT = 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum HatDir {
|
|
|
|
HAT_UP,
|
|
|
|
HAT_RIGHT,
|
|
|
|
HAT_DOWN,
|
|
|
|
HAT_LEFT,
|
|
|
|
HAT_MAX,
|
|
|
|
};
|
2017-02-21 17:02:49 +01:00
|
|
|
|
|
|
|
enum {
|
|
|
|
JOYPADS_MAX = 16,
|
|
|
|
};
|
|
|
|
|
2015-12-18 06:12:53 +01:00
|
|
|
struct JoyAxis {
|
|
|
|
int min;
|
|
|
|
float value;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum JoyType {
|
|
|
|
TYPE_BUTTON,
|
|
|
|
TYPE_AXIS,
|
|
|
|
TYPE_HAT,
|
|
|
|
TYPE_MAX,
|
|
|
|
};
|
|
|
|
|
2020-05-13 17:37:59 +02:00
|
|
|
enum JoyAxisRange {
|
|
|
|
NEGATIVE_HALF_AXIS = -1,
|
|
|
|
FULL_AXIS = 0,
|
|
|
|
POSITIVE_HALF_AXIS = 1
|
|
|
|
};
|
|
|
|
|
2015-12-18 06:12:53 +01:00
|
|
|
struct JoyEvent {
|
|
|
|
int type;
|
|
|
|
int index;
|
2020-05-13 17:37:59 +02:00
|
|
|
float value;
|
2015-12-18 06:12:53 +01:00
|
|
|
};
|
|
|
|
|
2020-05-13 17:37:59 +02:00
|
|
|
struct JoyBinding {
|
|
|
|
JoyType inputType;
|
|
|
|
union {
|
|
|
|
int button;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int axis;
|
|
|
|
JoyAxisRange range;
|
|
|
|
bool invert;
|
|
|
|
} axis;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int hat;
|
|
|
|
HatMask hat_mask;
|
|
|
|
} hat;
|
|
|
|
|
|
|
|
} input;
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2020-05-13 17:37:59 +02:00
|
|
|
JoyType outputType;
|
|
|
|
union {
|
|
|
|
JoystickList button;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
JoystickList axis;
|
|
|
|
JoyAxisRange range;
|
|
|
|
} axis;
|
|
|
|
|
|
|
|
} output;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JoyDeviceMapping {
|
2015-12-18 19:15:32 +01:00
|
|
|
String uid;
|
2016-02-29 16:48:19 +01:00
|
|
|
String name;
|
2020-05-13 17:37:59 +02:00
|
|
|
Vector<JoyBinding> bindings;
|
2015-12-18 19:15:32 +01:00
|
|
|
};
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
Vector<JoyDeviceMapping> map_db;
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2020-05-13 17:37:59 +02:00
|
|
|
JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, int p_button);
|
2020-10-14 18:07:50 +02:00
|
|
|
JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, int p_axis, float p_value);
|
2020-05-13 17:37:59 +02:00
|
|
|
void _get_mapped_hat_events(const JoyDeviceMapping &mapping, int p_hat, JoyEvent r_events[HAT_MAX]);
|
|
|
|
JoystickList _get_output_button(String output);
|
|
|
|
JoystickList _get_output_axis(String output);
|
2017-03-26 15:59:13 +02:00
|
|
|
void _button_event(int p_device, int p_index, bool p_pressed);
|
|
|
|
void _axis_event(int p_device, int p_axis, float p_value);
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2018-02-24 03:04:30 +01:00
|
|
|
void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
|
|
|
|
|
2021-08-03 18:59:20 +02:00
|
|
|
List<Ref<InputEvent>> buffered_events;
|
|
|
|
bool use_input_buffering;
|
2019-03-03 23:52:18 +01:00
|
|
|
bool use_accumulated_input;
|
|
|
|
|
2020-04-20 08:06:03 +02:00
|
|
|
protected:
|
|
|
|
struct VibrationInfo {
|
|
|
|
float weak_magnitude;
|
|
|
|
float strong_magnitude;
|
|
|
|
float duration; // Duration in seconds
|
|
|
|
uint64_t timestamp;
|
|
|
|
};
|
|
|
|
|
|
|
|
Map<int, VibrationInfo> joy_vibration;
|
|
|
|
|
2015-09-24 23:06:15 +02:00
|
|
|
public:
|
2016-09-01 23:58:52 +02:00
|
|
|
virtual bool is_key_pressed(int p_scancode) const;
|
2021-11-23 10:14:19 +01:00
|
|
|
virtual bool is_physical_key_pressed(int p_scancode) const;
|
2016-09-01 23:58:52 +02:00
|
|
|
virtual bool is_mouse_button_pressed(int p_button) const;
|
|
|
|
virtual bool is_joy_button_pressed(int p_device, int p_button) const;
|
Allow checking for exact matches with Action events.
Added additional param to action related methods to test for exactness.
If "p_exact_match" is true, then the action will only be "matched" if the provided input event *exactly* matches with the action event.
Before:
* Action Event = KEY_S
* Input Event = KEY_CONTROL + KEY_S
* Is Action Pressed = True
Now:
You can still do the above, however you can optionally check that the input is exactly what the action event is:
* Action Event = KEY_S
* Input Event = KEY_CONTROL + KEY_S
* p_exact_match = True
* Is Action Pressed = False
* If the Input Event was only KEY_S, then the result would be true.
Usage:
```gdscript
Input.is_action_pressed(action_name: String, exact_match: bool)
Input.is_action_pressed("my_action", true)
InputMap.event_is_action(p_event, "my_action", true)
func _input(event: InputEvent):
event.is_action_pressed("my_action", false, true) # false = "allow_echo", true = "exact_match"
event.is_action("my_action", true)
```
Co-authored-by: Eric M <itsjusteza@gmail.com>
2020-12-13 15:22:42 +01:00
|
|
|
virtual bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;
|
|
|
|
virtual bool is_action_just_pressed(const StringName &p_action, bool p_exact = false) const;
|
|
|
|
virtual bool is_action_just_released(const StringName &p_action, bool p_exact = false) const;
|
|
|
|
virtual float get_action_strength(const StringName &p_action, bool p_exact = false) const;
|
|
|
|
virtual float get_action_raw_strength(const StringName &p_action, bool p_exact = false) const;
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual float get_joy_axis(int p_device, int p_axis) const;
|
2015-09-24 23:06:15 +02:00
|
|
|
String get_joy_name(int p_idx);
|
2017-01-08 21:05:51 +01:00
|
|
|
virtual Array get_connected_joypads();
|
2016-06-15 07:25:35 +02:00
|
|
|
virtual Vector2 get_joy_vibration_strength(int p_device);
|
|
|
|
virtual float get_joy_vibration_duration(int p_device);
|
|
|
|
virtual uint64_t get_joy_vibration_timestamp(int p_device);
|
2015-12-18 06:12:53 +01:00
|
|
|
void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = "");
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2016-11-23 13:32:44 +01:00
|
|
|
virtual Vector3 get_gravity() const;
|
2016-09-01 23:58:52 +02:00
|
|
|
virtual Vector3 get_accelerometer() const;
|
|
|
|
virtual Vector3 get_magnetometer() const;
|
|
|
|
virtual Vector3 get_gyroscope() const;
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2017-03-29 17:29:38 +02:00
|
|
|
virtual Point2 get_mouse_position() const;
|
2017-01-13 23:23:42 +01:00
|
|
|
virtual Point2 get_last_mouse_speed() const;
|
2015-09-24 23:06:15 +02:00
|
|
|
virtual int get_mouse_button_mask() const;
|
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
virtual void warp_mouse_position(const Vector2 &p_to);
|
2017-05-20 17:38:03 +02:00
|
|
|
virtual Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
virtual void parse_input_event(const Ref<InputEvent> &p_event);
|
2017-03-19 09:20:20 +01:00
|
|
|
|
2021-10-12 22:09:30 +02:00
|
|
|
virtual void set_gravity(const Vector3 &p_gravity);
|
|
|
|
virtual void set_accelerometer(const Vector3 &p_accel);
|
|
|
|
virtual void set_magnetometer(const Vector3 &p_magnetometer);
|
|
|
|
virtual void set_gyroscope(const Vector3 &p_gyroscope);
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_joy_axis(int p_device, int p_axis, float p_value);
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
|
2016-06-15 07:25:35 +02:00
|
|
|
virtual void stop_joy_vibration(int p_device);
|
2019-08-17 17:27:29 +02:00
|
|
|
virtual void vibrate_handheld(int p_duration_ms = 500);
|
2016-06-15 07:25:35 +02:00
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
void set_main_loop(MainLoop *p_main_loop);
|
2017-03-29 17:29:38 +02:00
|
|
|
void set_mouse_position(const Point2 &p_posf);
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2018-11-09 22:32:32 +01:00
|
|
|
void action_press(const StringName &p_action, float p_strength = 1.f);
|
2017-03-05 16:44:50 +01:00
|
|
|
void action_release(const StringName &p_action);
|
2015-09-24 23:06:15 +02:00
|
|
|
|
|
|
|
void iteration(float p_step);
|
|
|
|
|
2018-02-24 03:04:30 +01:00
|
|
|
void set_emulate_touch_from_mouse(bool p_emulate);
|
|
|
|
virtual bool is_emulating_touch_from_mouse() const;
|
|
|
|
void ensure_touch_mouse_raised();
|
|
|
|
|
|
|
|
void set_emulate_mouse_from_touch(bool p_emulate);
|
|
|
|
virtual bool is_emulating_mouse_from_touch() const;
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2019-04-15 17:30:20 +02:00
|
|
|
virtual CursorShape get_default_cursor_shape() const;
|
2018-04-09 22:48:24 +02:00
|
|
|
virtual void set_default_cursor_shape(CursorShape p_shape);
|
2019-04-15 17:30:20 +02:00
|
|
|
virtual CursorShape get_current_cursor_shape() const;
|
2017-11-10 11:50:11 +01:00
|
|
|
virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2015-12-18 06:12:53 +01:00
|
|
|
void parse_mapping(String p_mapping);
|
2017-03-26 15:59:13 +02:00
|
|
|
void joy_button(int p_device, int p_button, bool p_pressed);
|
|
|
|
void joy_axis(int p_device, int p_axis, const JoyAxis &p_value);
|
|
|
|
void joy_hat(int p_device, int p_val);
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void add_joy_mapping(String p_mapping, bool p_update_existing = false);
|
2016-01-08 00:40:41 +01:00
|
|
|
virtual void remove_joy_mapping(String p_guid);
|
|
|
|
virtual bool is_joy_known(int p_device);
|
|
|
|
virtual String get_joy_guid(int p_device) const;
|
|
|
|
|
2016-09-20 22:12:52 +02:00
|
|
|
virtual String get_joy_button_string(int p_button);
|
|
|
|
virtual String get_joy_axis_string(int p_axis);
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int get_joy_axis_index_from_string(String p_axis);
|
|
|
|
virtual int get_joy_button_index_from_string(String p_button);
|
2016-09-20 22:12:52 +02:00
|
|
|
|
2017-02-21 17:02:49 +01:00
|
|
|
int get_unused_joy_id();
|
|
|
|
|
2016-01-08 00:40:41 +01:00
|
|
|
bool is_joy_mapped(int p_device);
|
|
|
|
String get_joy_guid_remapped(int p_device) const;
|
2016-01-27 12:18:34 +01:00
|
|
|
void set_fallback_mapping(String p_guid);
|
2019-03-03 23:52:18 +01:00
|
|
|
|
2021-08-03 18:59:20 +02:00
|
|
|
virtual void flush_buffered_events();
|
|
|
|
virtual bool is_using_input_buffering();
|
|
|
|
virtual void set_use_input_buffering(bool p_enable);
|
2019-03-03 23:52:18 +01:00
|
|
|
virtual void set_use_accumulated_input(bool p_enable);
|
|
|
|
|
2019-04-15 19:41:44 +02:00
|
|
|
virtual void release_pressed_events();
|
2015-12-18 06:12:53 +01:00
|
|
|
InputDefault();
|
2015-09-24 23:06:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // INPUT_DEFAULT_H
|