2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* input_map.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
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_map.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2016-06-05 06:19:42 +02:00
|
|
|
#include "os/keyboard.h"
|
2017-07-30 22:53:40 +02:00
|
|
|
#include "project_settings.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
InputMap *InputMap::singleton = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-04-24 03:48:16 +02:00
|
|
|
void InputMap::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("has_action", "action"), &InputMap::has_action);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_actions"), &InputMap::_get_actions);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_action", "action"), &InputMap::add_action);
|
|
|
|
ClassDB::bind_method(D_METHOD("erase_action", "action"), &InputMap::erase_action);
|
|
|
|
|
2017-07-18 21:03:34 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("action_add_event", "action", "event:InputEvent"), &InputMap::action_add_event);
|
|
|
|
ClassDB::bind_method(D_METHOD("action_has_event", "action", "event:InputEvent"), &InputMap::action_has_event);
|
|
|
|
ClassDB::bind_method(D_METHOD("action_erase_event", "action", "event:InputEvent"), &InputMap::action_erase_event);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_action_list", "action"), &InputMap::_get_action_list);
|
2017-07-18 21:03:34 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("event_is_action", "event:InputEvent", "action"), &InputMap::event_is_action);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("load_from_globals"), &InputMap::load_from_globals);
|
2014-04-24 03:48:16 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void InputMap::add_action(const StringName &p_action) {
|
2014-04-24 03:48:16 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(input_map.has(p_action));
|
|
|
|
input_map[p_action] = Action();
|
|
|
|
static int last_id = 1;
|
|
|
|
input_map[p_action].id = last_id;
|
2014-02-10 02:10:30 +01:00
|
|
|
last_id++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void InputMap::erase_action(const StringName &p_action) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(!input_map.has(p_action));
|
2014-02-10 02:10:30 +01:00
|
|
|
input_map.erase(p_action);
|
|
|
|
}
|
|
|
|
|
2016-06-04 17:55:09 +02:00
|
|
|
Array InputMap::_get_actions() {
|
|
|
|
|
|
|
|
Array ret;
|
|
|
|
List<StringName> actions = get_actions();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (actions.empty())
|
2016-06-04 17:55:09 +02:00
|
|
|
return ret;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (const List<StringName>::Element *E = actions.front(); E; E = E->next()) {
|
2016-06-04 17:55:09 +02:00
|
|
|
|
|
|
|
ret.push_back(E->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<StringName> InputMap::get_actions() const {
|
|
|
|
|
|
|
|
List<StringName> actions = List<StringName>();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (input_map.empty()) {
|
2016-06-04 17:55:09 +02:00
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<StringName, Action>::Element *E = input_map.front(); E; E = E->next()) {
|
2016-06-04 17:55:09 +02:00
|
|
|
actions.push_back(E->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
List<Ref<InputEvent> >::Element *InputMap::_find_event(List<Ref<InputEvent> > &p_list, const Ref<InputEvent> &p_event, bool p_action_test) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
for (List<Ref<InputEvent> >::Element *E = p_list.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
const Ref<InputEvent> e = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
//if (e.type != Ref<InputEvent>::KEY && e.device != p_event.device) -- unsure about the KEY comparison, why is this here?
|
|
|
|
// continue;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (e->get_device() != p_event->get_device())
|
|
|
|
continue;
|
|
|
|
if (e->action_match(p_event))
|
2014-02-10 02:10:30 +01:00
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool InputMap::has_action(const StringName &p_action) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return input_map.has(p_action);
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(!input_map.has(p_action));
|
|
|
|
if (_find_event(input_map[p_action].inputs, p_event))
|
2014-02-10 02:10:30 +01:00
|
|
|
return; //already gots
|
|
|
|
|
|
|
|
input_map[p_action].inputs.push_back(p_event);
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
bool InputMap::action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!input_map.has(p_action), false);
|
|
|
|
return (_find_event(input_map[p_action].inputs, p_event) != NULL);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(!input_map.has(p_action));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
List<Ref<InputEvent> >::Element *E = _find_event(input_map[p_action].inputs, p_event);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (E)
|
2016-03-07 18:56:44 +01:00
|
|
|
input_map[p_action].inputs.erase(E);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Array InputMap::_get_action_list(const StringName &p_action) {
|
2015-05-18 15:20:54 +02:00
|
|
|
|
|
|
|
Array ret;
|
2017-05-20 17:38:03 +02:00
|
|
|
const List<Ref<InputEvent> > *al = get_action_list(p_action);
|
2015-05-18 15:20:54 +02:00
|
|
|
if (al) {
|
2017-05-20 17:38:03 +02:00
|
|
|
for (const List<Ref<InputEvent> >::Element *E = al->front(); E; E = E->next()) {
|
2015-05-18 15:20:54 +02:00
|
|
|
|
2017-01-14 18:03:38 +01:00
|
|
|
ret.push_back(E->get());
|
2015-05-18 15:20:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
const List<Ref<InputEvent> > *InputMap::get_action_list(const StringName &p_action) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Map<StringName, Action>::Element *E = input_map.find(p_action);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!E)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return &E->get().inputs;
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<StringName, Action>::Element *E = input_map.find(p_action);
|
|
|
|
if (!E) {
|
|
|
|
ERR_EXPLAIN("Request for nonexistent InputMap action: " + String(p_action));
|
|
|
|
ERR_FAIL_COND_V(!E, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
Ref<InputEventAction> iea = p_event;
|
|
|
|
if (iea.is_valid()) {
|
|
|
|
return iea->get_action() == p_action;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return _find_event(E->get().inputs, p_event, true) != NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Map<StringName, InputMap::Action> &InputMap::get_action_map() const {
|
2016-09-01 23:58:52 +02:00
|
|
|
return input_map;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void InputMap::load_from_globals() {
|
|
|
|
|
2017-01-14 18:03:38 +01:00
|
|
|
input_map.clear();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
List<PropertyInfo> pinfo;
|
2017-07-19 22:00:46 +02:00
|
|
|
ProjectSettings::get_singleton()->get_property_list(&pinfo);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
|
|
|
|
const PropertyInfo &pi = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (!pi.name.begins_with("input/"))
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
add_action(name);
|
|
|
|
|
2017-07-19 22:00:46 +02:00
|
|
|
Array va = ProjectSettings::get_singleton()->get(pi.name);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < va.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
Ref<InputEvent> ie = va[i];
|
|
|
|
if (ie.is_null())
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event(name, ie);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 06:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputMap::load_default() {
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
Ref<InputEventKey> key;
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_accept");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
2017-08-06 15:26:07 +02:00
|
|
|
key->set_scancode(KEY_ENTER);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_accept", key);
|
2017-05-20 17:38:03 +02:00
|
|
|
|
|
|
|
key.instance();
|
2017-08-06 15:26:07 +02:00
|
|
|
key->set_scancode(KEY_KP_ENTER);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_accept", key);
|
2017-05-20 17:38:03 +02:00
|
|
|
|
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_SPACE);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_accept", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_select");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_SPACE);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_select", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_cancel");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_ESCAPE);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_cancel", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_focus_next");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_TAB);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_focus_next", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_focus_prev");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_TAB);
|
|
|
|
key->set_shift(true);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_focus_prev", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_left");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_LEFT);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_left", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_right");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_RIGHT);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_right", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_up");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_UP);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_up", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_down");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_DOWN);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_down", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_page_up");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_PAGEUP);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_page_up", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2016-06-05 19:06:57 +02:00
|
|
|
add_action("ui_page_down");
|
2017-05-20 17:38:03 +02:00
|
|
|
key.instance();
|
|
|
|
key->set_scancode(KEY_PAGEDOWN);
|
2017-03-05 16:44:50 +01:00
|
|
|
action_add_event("ui_page_down", key);
|
2016-06-05 06:19:42 +02:00
|
|
|
|
2017-07-19 03:25:35 +02:00
|
|
|
//set("display/window/handheld/orientation", "landscape");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
InputMap::InputMap() {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(singleton);
|
2017-03-05 16:44:50 +01:00
|
|
|
singleton = this;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|