2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* input_default.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00: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-04 23:50:27 +00:00
|
|
|
|
2015-09-24 21:06:15 +00:00
|
|
|
#include "input_default.h"
|
2017-08-27 19:07:15 +00:00
|
|
|
|
2018-03-06 03:10:17 +00:00
|
|
|
#include "default_controller_mappings.h"
|
2015-09-24 21:06:15 +00:00
|
|
|
#include "input_map.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "os/os.h"
|
2016-07-03 17:36:12 +00:00
|
|
|
#include "scene/resources/texture.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "servers/visual_server.h"
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void InputDefault::SpeedTrack::update(const Vector2 &p_delta_p) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
uint64_t tick = OS::get_singleton()->get_ticks_usec();
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t tdiff = tick - last_tick;
|
2015-09-24 21:06:15 +00:00
|
|
|
float delta_t = tdiff / 1000000.0;
|
2017-03-05 15:44:50 +00:00
|
|
|
last_tick = tick;
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
accum += p_delta_p;
|
|
|
|
accum_t += delta_t;
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (accum_t > max_ref_frame * 10)
|
|
|
|
accum_t = max_ref_frame * 10;
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (accum_t >= min_ref_frame) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
float slice_t = min_ref_frame / accum_t;
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 slice = accum * slice_t;
|
|
|
|
accum = accum - slice;
|
|
|
|
accum_t -= min_ref_frame;
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
speed = (slice / min_ref_frame).linear_interpolate(speed, min_ref_frame / max_ref_frame);
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDefault::SpeedTrack::reset() {
|
|
|
|
last_tick = OS::get_singleton()->get_ticks_usec();
|
2017-03-05 15:44:50 +00:00
|
|
|
speed = Vector2();
|
|
|
|
accum_t = 0;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InputDefault::SpeedTrack::SpeedTrack() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
min_ref_frame = 0.1;
|
|
|
|
max_ref_frame = 0.3;
|
|
|
|
reset();
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 21:58:52 +00:00
|
|
|
bool InputDefault::is_key_pressed(int p_scancode) const {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
return keys_pressed.has(p_scancode);
|
|
|
|
}
|
|
|
|
|
2016-09-01 21:58:52 +00:00
|
|
|
bool InputDefault::is_mouse_button_pressed(int p_button) const {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
2017-09-16 15:12:11 +00:00
|
|
|
return (mouse_button_mask & (1 << (p_button - 1))) != 0;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static int _combine_device(int p_value, int p_device) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return p_value | (p_device << 20);
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool InputDefault::is_joy_button_pressed(int p_device, int p_button) const {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
2017-03-05 15:44:50 +00:00
|
|
|
return joy_buttons_pressed.has(_combine_device(p_button, p_device));
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool InputDefault::is_action_pressed(const StringName &p_action) const {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2016-09-01 21:58:52 +00:00
|
|
|
return action_state.has(p_action) && action_state[p_action].pressed;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool InputDefault::is_action_just_pressed(const StringName &p_action) const {
|
2016-09-01 21:58:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const Map<StringName, Action>::Element *E = action_state.find(p_action);
|
2016-09-01 21:58:52 +00:00
|
|
|
if (!E)
|
|
|
|
return false;
|
|
|
|
|
2017-09-30 14:19:07 +00:00
|
|
|
if (Engine::get_singleton()->is_in_physics_frame()) {
|
|
|
|
return E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames();
|
2016-09-01 21:58:52 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames();
|
2016-09-01 21:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool InputDefault::is_action_just_released(const StringName &p_action) const {
|
2016-09-01 21:58:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const Map<StringName, Action>::Element *E = action_state.find(p_action);
|
2016-09-01 21:58:52 +00:00
|
|
|
if (!E)
|
|
|
|
return false;
|
|
|
|
|
2017-09-30 14:19:07 +00:00
|
|
|
if (Engine::get_singleton()->is_in_physics_frame()) {
|
|
|
|
return !E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames();
|
2016-09-01 21:58:52 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return !E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames();
|
2016-09-01 21:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float InputDefault::get_joy_axis(int p_device, int p_axis) const {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
2017-03-05 15:44:50 +00:00
|
|
|
int c = _combine_device(p_axis, p_device);
|
2015-12-18 05:12:53 +00:00
|
|
|
if (_joy_axis.has(c)) {
|
|
|
|
return _joy_axis[c];
|
2015-09-24 21:06:15 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String InputDefault::get_joy_name(int p_idx) {
|
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
2015-12-18 05:12:53 +00:00
|
|
|
return joy_names[p_idx].name;
|
2015-09-24 21:06:15 +00:00
|
|
|
};
|
|
|
|
|
2016-06-15 05:25:35 +00:00
|
|
|
Vector2 InputDefault::get_joy_vibration_strength(int p_device) {
|
|
|
|
if (joy_vibration.has(p_device)) {
|
|
|
|
return Vector2(joy_vibration[p_device].weak_magnitude, joy_vibration[p_device].strong_magnitude);
|
|
|
|
} else {
|
|
|
|
return Vector2(0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t InputDefault::get_joy_vibration_timestamp(int p_device) {
|
|
|
|
if (joy_vibration.has(p_device)) {
|
|
|
|
return joy_vibration[p_device].timestamp;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float InputDefault::get_joy_vibration_duration(int p_device) {
|
|
|
|
if (joy_vibration.has(p_device)) {
|
|
|
|
return joy_vibration[p_device].duration;
|
|
|
|
} else {
|
|
|
|
return 0.f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 05:12:53 +00:00
|
|
|
static String _hex_str(uint8_t p_byte) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const char *dict = "0123456789abcdef";
|
2015-12-18 18:15:32 +00:00
|
|
|
char ret[3];
|
|
|
|
ret[2] = 0;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ret[0] = dict[p_byte >> 4];
|
2015-12-18 18:15:32 +00:00
|
|
|
ret[1] = dict[p_byte & 0xf];
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
return ret;
|
2015-12-18 05:12:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void InputDefault::joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2015-12-18 05:12:53 +00:00
|
|
|
_THREAD_SAFE_METHOD_
|
2017-01-08 20:05:51 +00:00
|
|
|
Joypad js;
|
2015-12-18 18:15:32 +00:00
|
|
|
js.name = p_connected ? p_name : "";
|
|
|
|
js.uid = p_connected ? p_guid : "";
|
|
|
|
js.mapping = -1;
|
|
|
|
js.hat_current = 0;
|
|
|
|
|
|
|
|
if (p_connected) {
|
|
|
|
|
|
|
|
String uidname = p_guid;
|
|
|
|
if (p_guid == "") {
|
|
|
|
int uidlen = MIN(p_name.length(), 16);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < uidlen; i++) {
|
2015-12-18 18:15:32 +00:00
|
|
|
uidname = uidname + _hex_str(p_name[i]);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
js.uid = uidname;
|
2016-07-05 12:07:46 +00:00
|
|
|
js.connected = true;
|
2016-01-24 17:01:37 +00:00
|
|
|
int mapping = fallback_mapping;
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < map_db.size(); i++) {
|
2015-12-18 18:15:32 +00:00
|
|
|
if (js.uid == map_db[i].uid) {
|
|
|
|
mapping = i;
|
2016-02-29 15:48:19 +00:00
|
|
|
js.name = map_db[i].name;
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
js.mapping = mapping;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2016-07-05 12:07:46 +00:00
|
|
|
js.connected = false;
|
2016-01-26 03:40:04 +00:00
|
|
|
for (int i = 0; i < JOY_BUTTON_MAX; i++) {
|
|
|
|
|
|
|
|
if (i < JOY_AXIS_MAX)
|
|
|
|
set_joy_axis(p_idx, i, 0.0f);
|
|
|
|
|
|
|
|
int c = _combine_device(i, p_idx);
|
|
|
|
joy_buttons_pressed.erase(c);
|
|
|
|
};
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
|
|
|
joy_names[p_idx] = js;
|
|
|
|
|
|
|
|
emit_signal("joy_connection_changed", p_idx, p_connected);
|
2015-09-24 21:06:15 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 InputDefault::get_gravity() const {
|
2016-11-23 12:32:44 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
return gravity;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 InputDefault::get_accelerometer() const {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
return accelerometer;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 InputDefault::get_magnetometer() const {
|
2016-05-27 17:29:37 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
return magnetometer;
|
|
|
|
}
|
|
|
|
|
2016-09-01 21:58:52 +00:00
|
|
|
Vector3 InputDefault::get_gyroscope() const {
|
2016-07-15 07:31:34 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
return gyroscope;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
void InputDefault::parse_input_event(const Ref<InputEvent> &p_event) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
2017-03-24 20:55:58 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventKey> k = p_event;
|
2017-05-25 20:58:37 +00:00
|
|
|
if (k.is_valid() && !k->is_echo() && k->get_scancode() != 0) {
|
2017-05-20 15:38:03 +00:00
|
|
|
|
|
|
|
//print_line(p_event);
|
|
|
|
|
|
|
|
if (k->is_pressed())
|
|
|
|
keys_pressed.insert(k->get_scancode());
|
|
|
|
else
|
|
|
|
keys_pressed.erase(k->get_scancode());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
|
|
|
|
|
|
|
if (mb.is_valid() && !mb->is_doubleclick()) {
|
|
|
|
|
2017-09-16 15:12:11 +00:00
|
|
|
if (mb->is_pressed()) {
|
|
|
|
mouse_button_mask |= (1 << (mb->get_button_index() - 1));
|
|
|
|
} else {
|
|
|
|
mouse_button_mask &= ~(1 << (mb->get_button_index() - 1));
|
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
|
|
|
|
if (main_loop && emulate_touch && mb->get_button_index() == 1) {
|
|
|
|
Ref<InputEventScreenTouch> touch_event;
|
|
|
|
touch_event.instance();
|
|
|
|
touch_event->set_pressed(mb->is_pressed());
|
2017-06-03 08:54:24 +00:00
|
|
|
touch_event->set_position(mb->get_position());
|
2017-05-20 15:38:03 +00:00
|
|
|
main_loop->input_event(touch_event);
|
|
|
|
}
|
|
|
|
|
2017-06-03 08:54:24 +00:00
|
|
|
Point2 pos = mb->get_global_position();
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mouse_pos != pos) {
|
|
|
|
set_mouse_position(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
|
|
|
|
if (mm.is_valid()) {
|
|
|
|
|
|
|
|
if (main_loop && emulate_touch && mm->get_button_mask() & 1) {
|
|
|
|
Ref<InputEventScreenDrag> drag_event;
|
|
|
|
drag_event.instance();
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-06-03 08:54:24 +00:00
|
|
|
drag_event->set_position(mm->get_position());
|
2017-05-20 15:38:03 +00:00
|
|
|
drag_event->set_relative(mm->get_relative());
|
|
|
|
drag_event->set_speed(mm->get_speed());
|
|
|
|
|
|
|
|
main_loop->input_event(drag_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventJoypadButton> jb = p_event;
|
|
|
|
|
|
|
|
if (jb.is_valid()) {
|
|
|
|
|
|
|
|
int c = _combine_device(jb->get_button_index(), jb->get_device());
|
|
|
|
|
|
|
|
if (jb->is_pressed())
|
|
|
|
joy_buttons_pressed.insert(c);
|
|
|
|
else
|
|
|
|
joy_buttons_pressed.erase(c);
|
|
|
|
}
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventJoypadMotion> jm = p_event;
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (jm.is_valid()) {
|
|
|
|
set_joy_axis(jm->get_device(), jm->get_axis(), jm->get_axis_value());
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 20:49:39 +00:00
|
|
|
Ref<InputEventGesture> ge = p_event;
|
|
|
|
|
|
|
|
if (ge.is_valid()) {
|
|
|
|
|
|
|
|
if (main_loop) {
|
|
|
|
main_loop->input_event(ge);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 19:14:59 +00:00
|
|
|
if (!p_event->is_echo()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (const Map<StringName, InputMap::Action>::Element *E = InputMap::get_singleton()->get_action_map().front(); E; E = E->next()) {
|
2016-09-01 21:58:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (InputMap::get_singleton()->event_is_action(p_event, E->key()) && is_action_pressed(E->key()) != p_event->is_pressed()) {
|
2017-01-25 19:37:39 +00:00
|
|
|
Action action;
|
2017-09-30 14:19:07 +00:00
|
|
|
action.physics_frame = Engine::get_singleton()->get_physics_frames();
|
2017-03-05 15:44:50 +00:00
|
|
|
action.idle_frame = Engine::get_singleton()->get_idle_frames();
|
2017-05-20 15:38:03 +00:00
|
|
|
action.pressed = p_event->is_pressed();
|
2017-03-05 15:44:50 +00:00
|
|
|
action_state[E->key()] = action;
|
2016-09-01 21:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 21:06:15 +00:00
|
|
|
if (main_loop)
|
|
|
|
main_loop->input_event(p_event);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void InputDefault::set_joy_axis(int p_device, int p_axis, float p_value) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
2017-03-05 15:44:50 +00:00
|
|
|
int c = _combine_device(p_axis, p_device);
|
|
|
|
_joy_axis[c] = p_value;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 05:25:35 +00:00
|
|
|
void InputDefault::start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration) {
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
if (p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VibrationInfo vibration;
|
|
|
|
vibration.weak_magnitude = p_weak_magnitude;
|
|
|
|
vibration.strong_magnitude = p_strong_magnitude;
|
|
|
|
vibration.duration = p_duration;
|
2016-06-15 12:40:57 +00:00
|
|
|
vibration.timestamp = OS::get_singleton()->get_ticks_usec();
|
2016-06-15 05:25:35 +00:00
|
|
|
joy_vibration[p_device] = vibration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDefault::stop_joy_vibration(int p_device) {
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
VibrationInfo vibration;
|
|
|
|
vibration.weak_magnitude = 0;
|
|
|
|
vibration.strong_magnitude = 0;
|
|
|
|
vibration.duration = 0;
|
2016-06-20 07:56:41 +00:00
|
|
|
vibration.timestamp = OS::get_singleton()->get_ticks_usec();
|
2016-06-15 05:25:35 +00:00
|
|
|
joy_vibration[p_device] = vibration;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void InputDefault::set_gravity(const Vector3 &p_gravity) {
|
2016-11-23 12:32:44 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
gravity = p_gravity;
|
2016-11-23 12:32:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void InputDefault::set_accelerometer(const Vector3 &p_accel) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
accelerometer = p_accel;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void InputDefault::set_magnetometer(const Vector3 &p_magnetometer) {
|
2016-05-27 17:29:37 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
magnetometer = p_magnetometer;
|
2016-05-27 17:29:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void InputDefault::set_gyroscope(const Vector3 &p_gyroscope) {
|
2016-07-15 07:31:34 +00:00
|
|
|
|
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
gyroscope = p_gyroscope;
|
2016-07-15 07:31:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 21:06:15 +00:00
|
|
|
void InputDefault::set_main_loop(MainLoop *p_main_loop) {
|
2017-03-05 15:44:50 +00:00
|
|
|
main_loop = p_main_loop;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 15:29:38 +00:00
|
|
|
void InputDefault::set_mouse_position(const Point2 &p_posf) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mouse_speed_track.update(p_posf - mouse_pos);
|
|
|
|
mouse_pos = p_posf;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 15:29:38 +00:00
|
|
|
Point2 InputDefault::get_mouse_position() const {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
return mouse_pos;
|
|
|
|
}
|
2017-01-13 22:23:42 +00:00
|
|
|
Point2 InputDefault::get_last_mouse_speed() const {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
return mouse_speed_track.speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
int InputDefault::get_mouse_button_mask() const {
|
|
|
|
|
2018-02-21 16:30:55 +00:00
|
|
|
return mouse_button_mask; // do not trust OS implementation, should remove it - OS::get_singleton()->get_mouse_button_state();
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 13:37:49 +00:00
|
|
|
void InputDefault::warp_mouse_position(const Vector2 &p_to) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-09-10 13:37:49 +00:00
|
|
|
OS::get_singleton()->warp_mouse_position(p_to);
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Point2i InputDefault::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect) {
|
2017-03-22 20:18:47 +00:00
|
|
|
|
2017-04-03 00:47:23 +00:00
|
|
|
// The relative distance reported for the next event after a warp is in the boundaries of the
|
|
|
|
// size of the rect on that axis, but it may be greater, in which case there's not problem as fmod()
|
|
|
|
// will warp it, but if the pointer has moved in the opposite direction between the pointer relocation
|
|
|
|
// and the subsequent event, the reported relative distance will be less than the size of the rect
|
|
|
|
// and thus fmod() will be disabled for handling the situation.
|
|
|
|
// And due to this mouse warping mechanism being stateless, we need to apply some heuristics to
|
|
|
|
// detect the warp: if the relative distance is greater than the half of the size of the relevant rect
|
|
|
|
// (checked per each axis), it will be considered as the consequence of a former pointer warp.
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
const Point2i rel_sgn(p_motion->get_relative().x >= 0.0f ? 1 : -1, p_motion->get_relative().y >= 0.0 ? 1 : -1);
|
2017-04-03 00:47:23 +00:00
|
|
|
const Size2i warp_margin = p_rect.size * 0.5f;
|
|
|
|
const Point2i rel_warped(
|
2017-05-20 15:38:03 +00:00
|
|
|
Math::fmod(p_motion->get_relative().x + rel_sgn.x * warp_margin.x, p_rect.size.x) - rel_sgn.x * warp_margin.x,
|
|
|
|
Math::fmod(p_motion->get_relative().y + rel_sgn.y * warp_margin.y, p_rect.size.y) - rel_sgn.y * warp_margin.y);
|
2017-04-03 00:47:23 +00:00
|
|
|
|
2017-06-03 22:25:13 +00:00
|
|
|
const Point2i pos_local = p_motion->get_global_position() - p_rect.position;
|
2017-03-22 20:18:47 +00:00
|
|
|
const Point2i pos_warped(Math::fposmod(pos_local.x, p_rect.size.x), Math::fposmod(pos_local.y, p_rect.size.y));
|
|
|
|
if (pos_warped != pos_local) {
|
2017-09-10 13:37:49 +00:00
|
|
|
OS::get_singleton()->warp_mouse_position(pos_warped + p_rect.position);
|
2017-03-22 20:18:47 +00:00
|
|
|
}
|
2017-04-03 00:47:23 +00:00
|
|
|
|
2017-03-22 20:18:47 +00:00
|
|
|
return rel_warped;
|
|
|
|
}
|
|
|
|
|
2015-09-24 21:06:15 +00:00
|
|
|
void InputDefault::iteration(float p_step) {
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void InputDefault::action_press(const StringName &p_action) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2016-09-01 21:58:52 +00:00
|
|
|
Action action;
|
|
|
|
|
2017-09-30 14:19:07 +00:00
|
|
|
action.physics_frame = Engine::get_singleton()->get_physics_frames();
|
2017-03-05 15:44:50 +00:00
|
|
|
action.idle_frame = Engine::get_singleton()->get_idle_frames();
|
|
|
|
action.pressed = true;
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
action_state[p_action] = action;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void InputDefault::action_release(const StringName &p_action) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2016-09-01 21:58:52 +00:00
|
|
|
Action action;
|
|
|
|
|
2017-09-30 14:19:07 +00:00
|
|
|
action.physics_frame = Engine::get_singleton()->get_physics_frames();
|
2017-03-05 15:44:50 +00:00
|
|
|
action.idle_frame = Engine::get_singleton()->get_idle_frames();
|
|
|
|
action.pressed = false;
|
2016-09-01 21:58:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
action_state[p_action] = action;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputDefault::set_emulate_touch(bool p_emulate) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
emulate_touch = p_emulate;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InputDefault::is_emulating_touchscreen() const {
|
|
|
|
|
|
|
|
return emulate_touch;
|
|
|
|
}
|
|
|
|
|
2017-11-10 10:50:11 +00:00
|
|
|
void InputDefault::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
|
|
|
|
if (Engine::get_singleton()->is_editor_hint())
|
|
|
|
return;
|
|
|
|
|
|
|
|
OS::get_singleton()->set_custom_mouse_cursor(p_cursor, (OS::CursorShape)p_shape, p_hotspot);
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputDefault::set_mouse_in_window(bool p_in_window) {
|
2017-08-07 21:06:57 +00:00
|
|
|
/* no longer supported, leaving this for reference to anyone who might want to implement hardware cursors
|
2015-09-24 21:06:15 +00:00
|
|
|
if (custom_cursor.is_valid()) {
|
|
|
|
|
|
|
|
if (p_in_window) {
|
|
|
|
set_mouse_mode(MOUSE_MODE_HIDDEN);
|
|
|
|
VisualServer::get_singleton()->cursor_set_visible(true);
|
|
|
|
} else {
|
|
|
|
set_mouse_mode(MOUSE_MODE_VISIBLE);
|
|
|
|
VisualServer::get_singleton()->cursor_set_visible(false);
|
|
|
|
}
|
|
|
|
}
|
2017-08-07 21:06:57 +00:00
|
|
|
*/
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InputDefault::InputDefault() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mouse_button_mask = 0;
|
|
|
|
emulate_touch = false;
|
|
|
|
main_loop = NULL;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
|
|
|
hat_map_default[HAT_UP].type = TYPE_BUTTON;
|
|
|
|
hat_map_default[HAT_UP].index = JOY_DPAD_UP;
|
|
|
|
hat_map_default[HAT_UP].value = 0;
|
|
|
|
|
|
|
|
hat_map_default[HAT_RIGHT].type = TYPE_BUTTON;
|
|
|
|
hat_map_default[HAT_RIGHT].index = JOY_DPAD_RIGHT;
|
|
|
|
hat_map_default[HAT_RIGHT].value = 0;
|
|
|
|
|
|
|
|
hat_map_default[HAT_DOWN].type = TYPE_BUTTON;
|
|
|
|
hat_map_default[HAT_DOWN].index = JOY_DPAD_DOWN;
|
|
|
|
hat_map_default[HAT_DOWN].value = 0;
|
|
|
|
|
|
|
|
hat_map_default[HAT_LEFT].type = TYPE_BUTTON;
|
|
|
|
hat_map_default[HAT_LEFT].index = JOY_DPAD_LEFT;
|
|
|
|
hat_map_default[HAT_LEFT].value = 0;
|
|
|
|
|
2016-01-24 17:01:37 +00:00
|
|
|
fallback_mapping = -1;
|
|
|
|
|
2015-12-18 05:12:53 +00:00
|
|
|
String env_mapping = OS::get_singleton()->get_environment("SDL_GAMECONTROLLERCONFIG");
|
|
|
|
if (env_mapping != "") {
|
|
|
|
|
|
|
|
Vector<String> entries = env_mapping.split("\n");
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < entries.size(); i++) {
|
2015-12-18 05:12:53 +00:00
|
|
|
if (entries[i] == "")
|
|
|
|
continue;
|
|
|
|
parse_mapping(entries[i]);
|
|
|
|
};
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
2015-12-18 05:12:53 +00:00
|
|
|
|
|
|
|
int i = 0;
|
2018-03-06 03:10:17 +00:00
|
|
|
while (DefaultControllerMappings::mappings[i]) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2018-03-06 03:10:17 +00:00
|
|
|
parse_mapping(DefaultControllerMappings::mappings[i++]);
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-26 13:59:13 +00:00
|
|
|
void InputDefault::joy_button(int p_device, int p_button, bool p_pressed) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
_THREAD_SAFE_METHOD_;
|
2017-03-05 15:44:50 +00:00
|
|
|
Joypad &joy = joy_names[p_device];
|
2015-12-18 18:15:32 +00:00
|
|
|
//printf("got button %i, mapping is %i\n", p_button, joy.mapping);
|
|
|
|
if (joy.last_buttons[p_button] == p_pressed) {
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
}
|
|
|
|
joy.last_buttons[p_button] = p_pressed;
|
|
|
|
if (joy.mapping == -1) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, p_button, p_pressed);
|
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<int, JoyEvent>::Element *el = map_db[joy.mapping].buttons.find(p_button);
|
2015-12-18 18:15:32 +00:00
|
|
|
if (!el) {
|
|
|
|
//don't process un-mapped events for now, it could mess things up badly for devices with additional buttons/axis
|
|
|
|
//return _button_event(p_last_id, p_device, p_button, p_pressed);
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
JoyEvent map = el->get();
|
|
|
|
if (map.type == TYPE_BUTTON) {
|
2016-01-02 02:34:32 +00:00
|
|
|
//fake additional axis event for triggers
|
|
|
|
if (map.index == JOY_L2 || map.index == JOY_R2) {
|
|
|
|
float value = p_pressed ? 1.0f : 0.0f;
|
|
|
|
int axis = map.index == JOY_L2 ? JOY_ANALOG_L2 : JOY_ANALOG_R2;
|
2017-03-26 13:59:13 +00:00
|
|
|
_axis_event(p_device, axis, value);
|
2016-01-02 02:34:32 +00:00
|
|
|
}
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, map.index, p_pressed);
|
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (map.type == TYPE_AXIS) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_axis_event(p_device, map.index, p_pressed ? 1.0 : 0.0);
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
|
|
|
|
2017-03-26 13:59:13 +00:00
|
|
|
return; // no event?
|
2015-12-18 05:12:53 +00:00
|
|
|
};
|
|
|
|
|
2017-03-26 13:59:13 +00:00
|
|
|
void InputDefault::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
_THREAD_SAFE_METHOD_;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-06-22 14:52:11 +00:00
|
|
|
ERR_FAIL_INDEX(p_axis, JOY_AXIS_MAX);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Joypad &joy = joy_names[p_device];
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
if (joy.last_axis[p_axis] == p_value.value) {
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
}
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
if (p_value.value > joy.last_axis[p_axis]) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_value.value < joy.last_axis[p_axis] + joy.filter) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (p_value.value > joy.last_axis[p_axis] - joy.filter) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
}
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-08-12 18:45:56 +00:00
|
|
|
//when changing direction quickly, insert fake event to release pending inputmap actions
|
|
|
|
float last = joy.last_axis[p_axis];
|
|
|
|
if (p_value.min == 0 && (last < 0.25 || last > 0.75) && (last - 0.5) * (p_value.value - 0.5) < 0) {
|
|
|
|
JoyAxis jx;
|
|
|
|
jx.min = p_value.min;
|
|
|
|
jx.value = p_value.value < 0.5 ? 0.6 : 0.4;
|
|
|
|
joy_axis(p_device, p_axis, jx);
|
|
|
|
} else if (ABS(last) > 0.5 && last * p_value.value < 0) {
|
2016-09-15 17:30:35 +00:00
|
|
|
JoyAxis jx;
|
|
|
|
jx.min = p_value.min;
|
|
|
|
jx.value = p_value.value < 0 ? 0.1 : -0.1;
|
2017-03-26 13:59:13 +00:00
|
|
|
joy_axis(p_device, p_axis, jx);
|
2016-09-15 17:30:35 +00:00
|
|
|
}
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
joy.last_axis[p_axis] = p_value.value;
|
2016-01-02 02:34:32 +00:00
|
|
|
float val = p_value.min == 0 ? -1.0f + 2.0f * p_value.value : p_value.value;
|
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
if (joy.mapping == -1) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_axis_event(p_device, p_axis, val);
|
2017-04-10 21:41:47 +00:00
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<int, JoyEvent>::Element *el = map_db[joy.mapping].axis.find(p_axis);
|
2015-12-18 18:15:32 +00:00
|
|
|
if (!el) {
|
|
|
|
//return _axis_event(p_last_id, p_device, p_axis, p_value);
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
JoyEvent map = el->get();
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
if (map.type == TYPE_BUTTON) {
|
2016-01-02 02:34:32 +00:00
|
|
|
//send axis event for triggers
|
|
|
|
if (map.index == JOY_L2 || map.index == JOY_R2) {
|
|
|
|
float value = p_value.min == 0 ? p_value.value : 0.5f + p_value.value / 2.0f;
|
|
|
|
int axis = map.index == JOY_L2 ? JOY_ANALOG_L2 : JOY_ANALOG_R2;
|
2017-03-26 13:59:13 +00:00
|
|
|
_axis_event(p_device, axis, value);
|
2016-01-02 02:34:32 +00:00
|
|
|
}
|
2016-01-21 01:23:15 +00:00
|
|
|
|
|
|
|
if (map.index == JOY_DPAD_UP || map.index == JOY_DPAD_DOWN) {
|
|
|
|
bool pressed = p_value.value != 0.0f;
|
|
|
|
int button = p_value.value < 0 ? JOY_DPAD_UP : JOY_DPAD_DOWN;
|
|
|
|
|
|
|
|
if (!pressed) {
|
|
|
|
if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_UP, p_device))) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, JOY_DPAD_UP, false);
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_DOWN, p_device))) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, JOY_DPAD_DOWN, false);
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) {
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, button, true);
|
|
|
|
return;
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
if (map.index == JOY_DPAD_LEFT || map.index == JOY_DPAD_RIGHT) {
|
|
|
|
bool pressed = p_value.value != 0.0f;
|
|
|
|
int button = p_value.value < 0 ? JOY_DPAD_LEFT : JOY_DPAD_RIGHT;
|
|
|
|
|
|
|
|
if (!pressed) {
|
|
|
|
if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_LEFT, p_device))) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, JOY_DPAD_LEFT, false);
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
if (joy_buttons_pressed.has(_combine_device(JOY_DPAD_RIGHT, p_device))) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, JOY_DPAD_RIGHT, false);
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) {
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, button, true);
|
|
|
|
return;
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
2015-12-18 18:15:32 +00:00
|
|
|
float deadzone = p_value.min == 0 ? 0.5f : 0.0f;
|
2015-12-20 14:33:29 +00:00
|
|
|
bool pressed = p_value.value > deadzone ? true : false;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pressed == joy_buttons_pressed.has(_combine_device(map.index, p_device))) {
|
2015-12-20 14:33:29 +00:00
|
|
|
// button already pressed or released, this is an axis bounce value
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2015-12-20 14:33:29 +00:00
|
|
|
};
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, map.index, pressed);
|
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
if (map.type == TYPE_AXIS) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-26 13:59:13 +00:00
|
|
|
_axis_event(p_device, map.index, val);
|
|
|
|
return;
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
|
|
|
//printf("invalid mapping\n");
|
2017-03-26 13:59:13 +00:00
|
|
|
return;
|
2015-12-18 05:12:53 +00:00
|
|
|
};
|
|
|
|
|
2017-03-26 13:59:13 +00:00
|
|
|
void InputDefault::joy_hat(int p_device, int p_val) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
_THREAD_SAFE_METHOD_;
|
2017-03-05 15:44:50 +00:00
|
|
|
const Joypad &joy = joy_names[p_device];
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
JoyEvent *map;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
if (joy.mapping == -1) {
|
|
|
|
map = hat_map_default;
|
|
|
|
} else {
|
|
|
|
map = map_db[joy.mapping].hat;
|
|
|
|
};
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
int cur_val = joy_names[p_device].hat_current;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if ((p_val & HAT_MASK_UP) != (cur_val & HAT_MASK_UP)) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, map[HAT_UP].index, p_val & HAT_MASK_UP);
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if ((p_val & HAT_MASK_RIGHT) != (cur_val & HAT_MASK_RIGHT)) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, map[HAT_RIGHT].index, p_val & HAT_MASK_RIGHT);
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
2017-03-05 15:44:50 +00:00
|
|
|
if ((p_val & HAT_MASK_DOWN) != (cur_val & HAT_MASK_DOWN)) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, map[HAT_DOWN].index, p_val & HAT_MASK_DOWN);
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
2017-03-05 15:44:50 +00:00
|
|
|
if ((p_val & HAT_MASK_LEFT) != (cur_val & HAT_MASK_LEFT)) {
|
2017-03-26 13:59:13 +00:00
|
|
|
_button_event(p_device, map[HAT_LEFT].index, p_val & HAT_MASK_LEFT);
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
joy_names[p_device].hat_current = p_val;
|
2015-12-18 05:12:53 +00:00
|
|
|
};
|
|
|
|
|
2017-03-26 13:59:13 +00:00
|
|
|
void InputDefault::_button_event(int p_device, int p_index, bool p_pressed) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventJoypadButton> ievent;
|
|
|
|
ievent.instance();
|
|
|
|
ievent->set_device(p_device);
|
|
|
|
ievent->set_button_index(p_index);
|
|
|
|
ievent->set_pressed(p_pressed);
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
parse_input_event(ievent);
|
2015-12-18 05:12:53 +00:00
|
|
|
};
|
|
|
|
|
2017-03-26 13:59:13 +00:00
|
|
|
void InputDefault::_axis_event(int p_device, int p_axis, float p_value) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventJoypadMotion> ievent;
|
|
|
|
ievent.instance();
|
|
|
|
ievent->set_device(p_device);
|
|
|
|
ievent->set_axis(p_axis);
|
|
|
|
ievent->set_axis_value(p_value);
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
parse_input_event(ievent);
|
2015-12-18 05:12:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
InputDefault::JoyEvent InputDefault::_find_to_event(String p_to) {
|
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
// string names of the SDL buttons in the same order as input_event.h godot buttons
|
2017-03-05 15:44:50 +00:00
|
|
|
static const char *buttons[] = { "a", "b", "x", "y", "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger", "leftstick", "rightstick", "back", "start", "dpup", "dpdown", "dpleft", "dpright", "guide", NULL };
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const char *axis[] = { "leftx", "lefty", "rightx", "righty", NULL };
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
JoyEvent ret;
|
|
|
|
ret.type = -1;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int i = 0;
|
2015-12-18 18:15:32 +00:00
|
|
|
while (buttons[i]) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
if (p_to == buttons[i]) {
|
|
|
|
//printf("mapping button %s\n", buttons[i]);
|
|
|
|
ret.type = TYPE_BUTTON;
|
|
|
|
ret.index = i;
|
|
|
|
ret.value = 0;
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
++i;
|
|
|
|
};
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
i = 0;
|
|
|
|
while (axis[i]) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
if (p_to == axis[i]) {
|
|
|
|
ret.type = TYPE_AXIS;
|
|
|
|
ret.index = i;
|
|
|
|
ret.value = 0;
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
++i;
|
|
|
|
};
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
return ret;
|
2015-12-18 05:12:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void InputDefault::parse_mapping(String p_mapping) {
|
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
_THREAD_SAFE_METHOD_;
|
|
|
|
JoyDeviceMapping mapping;
|
2017-04-02 07:43:37 +00:00
|
|
|
for (int i = 0; i < HAT_MAX; ++i)
|
|
|
|
mapping.hat[i].index = 1024 + i;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
Vector<String> entry = p_mapping.split(",");
|
2018-03-06 03:10:17 +00:00
|
|
|
if (entry.size() < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
CharString uid;
|
|
|
|
uid.resize(17);
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
mapping.uid = entry[0];
|
2016-02-29 15:48:19 +00:00
|
|
|
mapping.name = entry[1];
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
int idx = 1;
|
|
|
|
while (++idx < entry.size()) {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
if (entry[idx] == "")
|
|
|
|
continue;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2016-10-15 15:55:15 +00:00
|
|
|
String from = entry[idx].get_slice(":", 1).replace(" ", "");
|
|
|
|
String to = entry[idx].get_slice(":", 0).replace(" ", "");
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
JoyEvent to_event = _find_to_event(to);
|
|
|
|
if (to_event.type == -1)
|
|
|
|
continue;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
String etype = from.substr(0, 1);
|
|
|
|
if (etype == "a") {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int aid = from.substr(1, from.length() - 1).to_int();
|
2015-12-18 18:15:32 +00:00
|
|
|
mapping.axis[aid] = to_event;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
} else if (etype == "b") {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int bid = from.substr(1, from.length() - 1).to_int();
|
2015-12-18 18:15:32 +00:00
|
|
|
mapping.buttons[bid] = to_event;
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
} else if (etype == "h") {
|
2015-12-18 05:12:53 +00:00
|
|
|
|
2015-12-18 18:15:32 +00:00
|
|
|
int hat_value = from.get_slice(".", 1).to_int();
|
|
|
|
switch (hat_value) {
|
2017-03-05 15:44:50 +00:00
|
|
|
case 1:
|
|
|
|
mapping.hat[HAT_UP] = to_event;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mapping.hat[HAT_RIGHT] = to_event;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
mapping.hat[HAT_DOWN] = to_event;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
mapping.hat[HAT_LEFT] = to_event;
|
|
|
|
break;
|
2015-12-18 18:15:32 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
map_db.push_back(mapping);
|
|
|
|
//printf("added mapping with uuid %ls\n", mapping.uid.c_str());
|
2015-12-18 05:12:53 +00:00
|
|
|
};
|
2016-01-07 23:40:41 +00:00
|
|
|
|
|
|
|
void InputDefault::add_joy_mapping(String p_mapping, bool p_update_existing) {
|
|
|
|
parse_mapping(p_mapping);
|
|
|
|
if (p_update_existing) {
|
|
|
|
Vector<String> entry = p_mapping.split(",");
|
|
|
|
String uid = entry[0];
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < joy_names.size(); i++) {
|
2016-01-07 23:40:41 +00:00
|
|
|
if (uid == joy_names[i].uid) {
|
2017-03-05 15:44:50 +00:00
|
|
|
joy_names[i].mapping = map_db.size() - 1;
|
2016-01-07 23:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDefault::remove_joy_mapping(String p_guid) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = map_db.size() - 1; i >= 0; i--) {
|
2016-01-07 23:40:41 +00:00
|
|
|
if (p_guid == map_db[i].uid) {
|
|
|
|
map_db.remove(i);
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < joy_names.size(); i++) {
|
2016-01-07 23:40:41 +00:00
|
|
|
if (joy_names[i].uid == p_guid) {
|
|
|
|
joy_names[i].mapping = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 11:18:34 +00:00
|
|
|
void InputDefault::set_fallback_mapping(String p_guid) {
|
|
|
|
|
|
|
|
for (int i = 0; i < map_db.size(); i++) {
|
|
|
|
if (map_db[i].uid == p_guid) {
|
|
|
|
fallback_mapping = i;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 23:40:41 +00:00
|
|
|
//Defaults to simple implementation for platforms with a fixed gamepad layout, like consoles.
|
|
|
|
bool InputDefault::is_joy_known(int p_device) {
|
|
|
|
|
|
|
|
return OS::get_singleton()->is_joy_known(p_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
String InputDefault::get_joy_guid(int p_device) const {
|
|
|
|
return OS::get_singleton()->get_joy_guid(p_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
//platforms that use the remapping system can override and call to these ones
|
|
|
|
bool InputDefault::is_joy_mapped(int p_device) {
|
2016-01-27 11:18:34 +00:00
|
|
|
int mapping = joy_names[p_device].mapping;
|
2016-02-16 22:23:06 +00:00
|
|
|
return mapping != -1 ? (mapping != fallback_mapping) : false;
|
2016-01-07 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String InputDefault::get_joy_guid_remapped(int p_device) const {
|
2016-08-28 15:14:21 +00:00
|
|
|
ERR_FAIL_COND_V(!joy_names.has(p_device), "");
|
2016-01-07 23:40:41 +00:00
|
|
|
return joy_names[p_device].uid;
|
|
|
|
}
|
2016-07-05 12:07:46 +00:00
|
|
|
|
2017-01-08 20:05:51 +00:00
|
|
|
Array InputDefault::get_connected_joypads() {
|
2016-07-05 12:07:46 +00:00
|
|
|
Array ret;
|
2017-01-08 20:05:51 +00:00
|
|
|
Map<int, Joypad>::Element *elem = joy_names.front();
|
2016-07-05 12:07:46 +00:00
|
|
|
while (elem) {
|
|
|
|
if (elem->get().connected) {
|
|
|
|
ret.push_back(elem->key());
|
|
|
|
}
|
|
|
|
elem = elem->next();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2016-09-20 20:12:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const char *_buttons[] = {
|
2016-09-20 20:12:52 +00:00
|
|
|
"Face Button Bottom",
|
|
|
|
"Face Button Right",
|
|
|
|
"Face Button Left",
|
|
|
|
"Face Button Top",
|
|
|
|
"L",
|
|
|
|
"R",
|
|
|
|
"L2",
|
|
|
|
"R2",
|
|
|
|
"L3",
|
|
|
|
"R3",
|
|
|
|
"Select",
|
|
|
|
"Start",
|
|
|
|
"DPAD Up",
|
|
|
|
"DPAD Down",
|
|
|
|
"DPAD Left",
|
|
|
|
"DPAD Right"
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const char *_axes[] = {
|
2016-09-20 20:12:52 +00:00
|
|
|
"Left Stick X",
|
|
|
|
"Left Stick Y",
|
|
|
|
"Right Stick X",
|
|
|
|
"Right Stick Y",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"L2",
|
|
|
|
"R2"
|
|
|
|
};
|
|
|
|
|
|
|
|
String InputDefault::get_joy_button_string(int p_button) {
|
|
|
|
ERR_FAIL_INDEX_V(p_button, JOY_BUTTON_MAX, "");
|
|
|
|
return _buttons[p_button];
|
|
|
|
}
|
|
|
|
|
|
|
|
int InputDefault::get_joy_button_index_from_string(String p_button) {
|
|
|
|
for (int i = 0; i < JOY_BUTTON_MAX; i++) {
|
|
|
|
if (p_button == _buttons[i]) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERR_FAIL_V(-1);
|
|
|
|
}
|
|
|
|
|
2017-02-21 16:02:49 +00:00
|
|
|
int InputDefault::get_unused_joy_id() {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < JOYPADS_MAX; i++) {
|
2017-02-21 16:02:49 +00:00
|
|
|
if (!joy_names.has(i) || !joy_names[i].connected) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-20 20:12:52 +00:00
|
|
|
String InputDefault::get_joy_axis_string(int p_axis) {
|
|
|
|
ERR_FAIL_INDEX_V(p_axis, JOY_AXIS_MAX, "");
|
|
|
|
return _axes[p_axis];
|
|
|
|
}
|
|
|
|
|
|
|
|
int InputDefault::get_joy_axis_index_from_string(String p_axis) {
|
|
|
|
for (int i = 0; i < JOY_AXIS_MAX; i++) {
|
|
|
|
if (p_axis == _axes[i]) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERR_FAIL_V(-1);
|
|
|
|
}
|