2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* input_event.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "input_event.h"
|
|
|
|
#include "input_map.h"
|
|
|
|
#include "os/keyboard.h"
|
|
|
|
/**
|
2016-03-08 23:00:52 +00:00
|
|
|
*
|
2014-02-10 01:10:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool InputEvent::operator==(const InputEvent &p_event) const {
|
2017-03-18 23:36:26 +00:00
|
|
|
if (type != p_event.type) {
|
2016-08-22 23:14:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (type) {
|
2017-01-15 21:15:47 +00:00
|
|
|
/** Current clang-format style doesn't play well with the aligned return values of that switch. */
|
|
|
|
/* clang-format off */
|
2016-09-02 18:31:06 +00:00
|
|
|
case NONE:
|
|
|
|
return true;
|
2016-08-22 23:14:04 +00:00
|
|
|
case KEY:
|
|
|
|
return key.unicode == p_event.key.unicode
|
|
|
|
&& key.scancode == p_event.key.scancode
|
|
|
|
&& key.echo == p_event.key.echo
|
|
|
|
&& key.pressed == p_event.key.pressed
|
|
|
|
&& key.mod == p_event.key.mod;
|
|
|
|
case MOUSE_MOTION:
|
|
|
|
return mouse_motion.x == p_event.mouse_motion.x
|
|
|
|
&& mouse_motion.y == p_event.mouse_motion.y
|
2016-10-22 20:47:36 +00:00
|
|
|
&& mouse_motion.relative_x == p_event.mouse_motion.relative_x
|
|
|
|
&& mouse_motion.relative_y == p_event.mouse_motion.relative_y
|
2016-08-22 23:14:04 +00:00
|
|
|
&& mouse_motion.button_mask == p_event.mouse_motion.button_mask
|
|
|
|
&& key.mod == p_event.key.mod;
|
|
|
|
case MOUSE_BUTTON:
|
|
|
|
return mouse_button.pressed == p_event.mouse_button.pressed
|
|
|
|
&& mouse_button.x == p_event.mouse_button.x
|
|
|
|
&& mouse_button.y == p_event.mouse_button.y
|
|
|
|
&& mouse_button.button_index == p_event.mouse_button.button_index
|
|
|
|
&& mouse_button.button_mask == p_event.mouse_button.button_mask
|
|
|
|
&& key.mod == p_event.key.mod;
|
|
|
|
case JOYSTICK_MOTION:
|
|
|
|
return joy_motion.axis == p_event.joy_motion.axis
|
|
|
|
&& joy_motion.axis_value == p_event.joy_motion.axis_value;
|
|
|
|
case JOYSTICK_BUTTON:
|
|
|
|
return joy_button.pressed == p_event.joy_button.pressed
|
|
|
|
&& joy_button.button_index == p_event.joy_button.button_index
|
|
|
|
&& joy_button.pressure == p_event.joy_button.pressure;
|
|
|
|
case SCREEN_TOUCH:
|
|
|
|
return screen_touch.pressed == p_event.screen_touch.pressed
|
|
|
|
&& screen_touch.index == p_event.screen_touch.index
|
|
|
|
&& screen_touch.x == p_event.screen_touch.x
|
|
|
|
&& screen_touch.y == p_event.screen_touch.y;
|
|
|
|
case SCREEN_DRAG:
|
|
|
|
return screen_drag.index == p_event.screen_drag.index
|
|
|
|
&& screen_drag.x == p_event.screen_drag.x
|
|
|
|
&& screen_drag.y == p_event.screen_drag.y;
|
|
|
|
case ACTION:
|
|
|
|
return action.action == p_event.action.action
|
|
|
|
&& action.pressed == p_event.action.pressed;
|
2017-01-15 21:15:47 +00:00
|
|
|
/* clang-format on */
|
2016-09-02 18:31:06 +00:00
|
|
|
default:
|
|
|
|
ERR_PRINT("No logic to compare InputEvents of this type, this shouldn't happen.");
|
2016-08-22 23:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
InputEvent::operator String() const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String str = "Device " + itos(device) + " ID " + itos(ID) + " ";
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (type) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
case NONE: {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return "Event: None";
|
|
|
|
} break;
|
|
|
|
case KEY: {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "Event: Key ";
|
|
|
|
str = str + "Unicode: " + String::chr(key.unicode) + " Scan: " + itos(key.scancode) + " Echo: " + String(key.echo ? "True" : "False") + " Pressed" + String(key.pressed ? "True" : "False") + " Mod: ";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.shift)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "S";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.control)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "C";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.alt)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "A";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.meta)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "M";
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return str;
|
|
|
|
} break;
|
|
|
|
case MOUSE_MOTION: {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "Event: Motion ";
|
|
|
|
str = str + " Pos: " + itos(mouse_motion.x) + "," + itos(mouse_motion.y) + " Rel: " + itos(mouse_motion.relative_x) + "," + itos(mouse_motion.relative_y) + " Mask: ";
|
|
|
|
for (int i = 0; i < 8; i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if ((1 << i) & mouse_motion.button_mask)
|
|
|
|
str += itos(i + 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
str += " Mod: ";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.shift)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "S";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.control)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "C";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.alt)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "A";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.meta)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "M";
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
} break;
|
|
|
|
case MOUSE_BUTTON: {
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "Event: Button ";
|
|
|
|
str = str + "Pressed: " + itos(mouse_button.pressed) + " Pos: " + itos(mouse_button.x) + "," + itos(mouse_button.y) + " Button: " + itos(mouse_button.button_index) + " Mask: ";
|
|
|
|
for (int i = 0; i < 8; i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if ((1 << i) & mouse_button.button_mask)
|
|
|
|
str += itos(i + 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
str += " Mod: ";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.shift)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "S";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.control)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "C";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.alt)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "A";
|
2014-02-10 01:10:30 +00:00
|
|
|
if (key.mod.meta)
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "M";
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
str += String(" DoubleClick: ") + (mouse_button.doubleclick ? "Yes" : "No");
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return str;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case JOYSTICK_MOTION: {
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "Event: JoystickMotion ";
|
|
|
|
str = str + "Axis: " + itos(joy_motion.axis) + " Value: " + rtos(joy_motion.axis_value);
|
2014-02-10 01:10:30 +00:00
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case JOYSTICK_BUTTON: {
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "Event: JoystickButton ";
|
|
|
|
str = str + "Pressed: " + itos(joy_button.pressed) + " Index: " + itos(joy_button.button_index) + " pressure " + rtos(joy_button.pressure);
|
2014-02-10 01:10:30 +00:00
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case SCREEN_TOUCH: {
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "Event: ScreenTouch ";
|
|
|
|
str = str + "Pressed: " + itos(screen_touch.pressed) + " Index: " + itos(screen_touch.index) + " pos " + rtos(screen_touch.x) + "," + rtos(screen_touch.y);
|
2014-02-10 01:10:30 +00:00
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case SCREEN_DRAG: {
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "Event: ScreenDrag ";
|
|
|
|
str = str + " Index: " + itos(screen_drag.index) + " pos " + rtos(screen_drag.x) + "," + rtos(screen_drag.y);
|
2014-02-10 01:10:30 +00:00
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ACTION: {
|
2017-03-18 23:36:26 +00:00
|
|
|
str += "Event: Action: " + InputMap::get_singleton()->get_action_from_id(action.action) + " Pressed: " + itos(action.pressed);
|
2014-02-10 01:10:30 +00:00
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void InputEvent::set_as_action(const String &p_action, bool p_pressed) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
type = ACTION;
|
|
|
|
action.action = InputMap::get_singleton()->get_action_id(p_action);
|
|
|
|
action.pressed = p_pressed;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InputEvent::is_pressed() const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (type) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
case KEY: return key.pressed;
|
|
|
|
case MOUSE_BUTTON: return mouse_button.pressed;
|
|
|
|
case JOYSTICK_BUTTON: return joy_button.pressed;
|
|
|
|
case SCREEN_TOUCH: return screen_touch.pressed;
|
2016-09-15 17:30:35 +00:00
|
|
|
case JOYSTICK_MOTION: return ABS(joy_motion.axis_value) > 0.5;
|
2014-02-10 01:10:30 +00:00
|
|
|
case ACTION: return action.pressed;
|
|
|
|
default: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputEvent::is_echo() const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return (type == KEY && key.echo);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool InputEvent::is_action(const String &p_action) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return InputMap::get_singleton()->event_is_action(*this, p_action);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool InputEvent::is_action_pressed(const String &p_action) const {
|
2015-06-23 20:24:48 +00:00
|
|
|
|
|
|
|
return is_action(p_action) && is_pressed() && !is_echo();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool InputEvent::is_action_released(const String &p_action) const {
|
2015-06-23 20:24:48 +00:00
|
|
|
|
|
|
|
return is_action(p_action) && !is_pressed();
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
uint32_t InputEventKey::get_scancode_with_modifiers() const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint32_t sc = scancode;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (mod.control)
|
2017-03-18 23:36:26 +00:00
|
|
|
sc |= KEY_MASK_CTRL;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (mod.alt)
|
2017-03-18 23:36:26 +00:00
|
|
|
sc |= KEY_MASK_ALT;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (mod.shift)
|
2017-03-18 23:36:26 +00:00
|
|
|
sc |= KEY_MASK_SHIFT;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (mod.meta)
|
2017-03-18 23:36:26 +00:00
|
|
|
sc |= KEY_MASK_META;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return sc;
|
|
|
|
}
|
2016-06-27 12:59:43 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
InputEvent InputEvent::xform_by(const Matrix32 &p_xform) const {
|
2016-06-27 12:59:43 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
InputEvent ev = *this;
|
2016-06-27 12:59:43 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (ev.type) {
|
2016-06-27 12:59:43 +00:00
|
|
|
|
|
|
|
case InputEvent::MOUSE_BUTTON: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 g = p_xform.xform(Vector2(ev.mouse_button.global_x, ev.mouse_button.global_y));
|
|
|
|
Vector2 l = p_xform.xform(Vector2(ev.mouse_button.x, ev.mouse_button.y));
|
|
|
|
ev.mouse_button.x = l.x;
|
|
|
|
ev.mouse_button.y = l.y;
|
|
|
|
ev.mouse_button.global_x = g.x;
|
|
|
|
ev.mouse_button.global_y = g.y;
|
2016-06-27 12:59:43 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case InputEvent::MOUSE_MOTION: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 g = p_xform.xform(Vector2(ev.mouse_motion.global_x, ev.mouse_motion.global_y));
|
|
|
|
Vector2 l = p_xform.xform(Vector2(ev.mouse_motion.x, ev.mouse_motion.y));
|
|
|
|
Vector2 r = p_xform.basis_xform(Vector2(ev.mouse_motion.relative_x, ev.mouse_motion.relative_y));
|
|
|
|
Vector2 s = p_xform.basis_xform(Vector2(ev.mouse_motion.speed_x, ev.mouse_motion.speed_y));
|
|
|
|
ev.mouse_motion.x = l.x;
|
|
|
|
ev.mouse_motion.y = l.y;
|
|
|
|
ev.mouse_motion.global_x = g.x;
|
|
|
|
ev.mouse_motion.global_y = g.y;
|
|
|
|
ev.mouse_motion.relative_x = r.x;
|
|
|
|
ev.mouse_motion.relative_y = r.y;
|
|
|
|
ev.mouse_motion.speed_x = s.x;
|
|
|
|
ev.mouse_motion.speed_y = s.y;
|
2016-06-27 12:59:43 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case InputEvent::SCREEN_TOUCH: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 t = p_xform.xform(Vector2(ev.screen_touch.x, ev.screen_touch.y));
|
|
|
|
ev.screen_touch.x = t.x;
|
|
|
|
ev.screen_touch.y = t.y;
|
2016-06-27 12:59:43 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case InputEvent::SCREEN_DRAG: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 t = p_xform.xform(Vector2(ev.screen_drag.x, ev.screen_drag.y));
|
|
|
|
Vector2 r = p_xform.basis_xform(Vector2(ev.screen_drag.relative_x, ev.screen_drag.relative_y));
|
|
|
|
Vector2 s = p_xform.basis_xform(Vector2(ev.screen_drag.speed_x, ev.screen_drag.speed_y));
|
|
|
|
ev.screen_drag.x = t.x;
|
|
|
|
ev.screen_drag.y = t.y;
|
|
|
|
ev.screen_drag.relative_x = r.x;
|
|
|
|
ev.screen_drag.relative_y = r.y;
|
|
|
|
ev.screen_drag.speed_x = s.x;
|
|
|
|
ev.screen_drag.speed_y = s.y;
|
2016-06-27 12:59:43 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ev;
|
|
|
|
}
|