2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* input_event.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2016-01-01 14:50:53 +01:00
|
|
|
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01: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-09 00:00:52 +01:00
|
|
|
*
|
2014-02-10 02:10:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool InputEvent::operator==(const InputEvent &p_event) const {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
InputEvent::operator String() const {
|
|
|
|
|
|
|
|
String str="Device "+itos(device)+" ID "+itos(ID)+" ";
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
switch(type) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
case NONE: {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return "Event: None";
|
|
|
|
} break;
|
|
|
|
case KEY: {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01: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: ";
|
|
|
|
if (key.mod.shift)
|
|
|
|
str+="S";
|
|
|
|
if (key.mod.control)
|
|
|
|
str+="C";
|
|
|
|
if (key.mod.alt)
|
|
|
|
str+="A";
|
|
|
|
if (key.mod.meta)
|
|
|
|
str+="M";
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return str;
|
|
|
|
} break;
|
|
|
|
case MOUSE_MOTION: {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01: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-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if ((1<<i)&mouse_motion.button_mask)
|
|
|
|
str+=itos(i+1);
|
|
|
|
}
|
|
|
|
str+=" Mod: ";
|
|
|
|
if (key.mod.shift)
|
|
|
|
str+="S";
|
|
|
|
if (key.mod.control)
|
|
|
|
str+="C";
|
|
|
|
if (key.mod.alt)
|
|
|
|
str+="A";
|
|
|
|
if (key.mod.meta)
|
|
|
|
str+="M";
|
|
|
|
|
|
|
|
return str;
|
|
|
|
} break;
|
|
|
|
case MOUSE_BUTTON: {
|
|
|
|
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-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if ((1<<i)&mouse_button.button_mask)
|
|
|
|
str+=itos(i+1);
|
|
|
|
}
|
|
|
|
str+=" Mod: ";
|
|
|
|
if (key.mod.shift)
|
|
|
|
str+="S";
|
|
|
|
if (key.mod.control)
|
|
|
|
str+="C";
|
|
|
|
if (key.mod.alt)
|
|
|
|
str+="A";
|
|
|
|
if (key.mod.meta)
|
|
|
|
str+="M";
|
|
|
|
|
|
|
|
str+=String(" DoubleClick: ")+(mouse_button.doubleclick?"Yes":"No");
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return str;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case JOYSTICK_MOTION: {
|
2016-02-10 22:58:32 +01:00
|
|
|
str+= "Event: JoystickMotion ";
|
2014-02-10 02:10:30 +01:00
|
|
|
str=str+"Axis: "+itos(joy_motion.axis)+" Value: " +rtos(joy_motion.axis_value);
|
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case JOYSTICK_BUTTON: {
|
2016-02-10 22:58:32 +01:00
|
|
|
str+= "Event: JoystickButton ";
|
2014-02-10 02:10:30 +01:00
|
|
|
str=str+"Pressed: "+itos(joy_button.pressed)+" Index: " +itos(joy_button.button_index)+" pressure "+rtos(joy_button.pressure);
|
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case SCREEN_TOUCH: {
|
|
|
|
str+= "Event: ScreenTouch ";
|
|
|
|
str=str+"Pressed: "+itos(screen_touch.pressed)+" Index: " +itos(screen_touch.index)+" pos "+rtos(screen_touch.x)+","+rtos(screen_touch.y);
|
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case SCREEN_DRAG: {
|
|
|
|
str+= "Event: ScreenDrag ";
|
|
|
|
str=str+" Index: " +itos(screen_drag.index)+" pos "+rtos(screen_drag.x)+","+rtos(screen_drag.y);
|
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ACTION: {
|
|
|
|
str+= "Event: Action: "+InputMap::get_singleton()->get_action_from_id(action.action)+" Pressed: "+itos(action.pressed);
|
|
|
|
return str;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
void InputEvent::set_as_action(const String& p_action, bool p_pressed) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
type=ACTION;
|
|
|
|
action.action=InputMap::get_singleton()->get_action_id(p_action);
|
2014-11-06 01:20:42 +01:00
|
|
|
action.pressed=p_pressed;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InputEvent::is_pressed() const {
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
|
|
|
|
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-01-02 13:37:16 +01:00
|
|
|
case JOYSTICK_MOTION: return InputMap::get_singleton()->event_is_joy_motion_action_pressed(*this);
|
2014-02-10 02:10:30 +01:00
|
|
|
case ACTION: return action.pressed;
|
|
|
|
default: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputEvent::is_echo() const {
|
|
|
|
|
|
|
|
return (type==KEY && key.echo);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputEvent::is_action(const String& p_action) const {
|
|
|
|
|
|
|
|
return InputMap::get_singleton()->event_is_action(*this,p_action);
|
|
|
|
}
|
|
|
|
|
2015-06-23 22:24:48 +02:00
|
|
|
bool InputEvent::is_action_pressed(const String& p_action) const {
|
|
|
|
|
|
|
|
return is_action(p_action) && is_pressed() && !is_echo();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputEvent::is_action_released(const String& p_action) const {
|
|
|
|
|
|
|
|
return is_action(p_action) && !is_pressed();
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t InputEventKey::get_scancode_with_modifiers() const {
|
|
|
|
|
|
|
|
uint32_t sc=scancode;
|
|
|
|
if (mod.control)
|
|
|
|
sc|=KEY_MASK_CTRL;
|
|
|
|
if (mod.alt)
|
|
|
|
sc|=KEY_MASK_ALT;
|
|
|
|
if (mod.shift)
|
|
|
|
sc|=KEY_MASK_SHIFT;
|
|
|
|
if (mod.meta)
|
|
|
|
sc|=KEY_MASK_META;
|
|
|
|
|
|
|
|
return sc;
|
|
|
|
|
|
|
|
}
|