godot/core/input_map.cpp

325 lines
9.7 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* input_map.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
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_map.h"
#include "global_config.h"
#include "os/keyboard.h"
2014-02-10 01:10:30 +00:00
InputMap *InputMap::singleton = NULL;
2014-02-10 01:10:30 +00:00
2014-04-24 01:48:16 +00:00
void InputMap::_bind_methods() {
ClassDB::bind_method(D_METHOD("has_action", "action"), &InputMap::has_action);
ClassDB::bind_method(D_METHOD("get_action_id", "action"), &InputMap::get_action_id);
ClassDB::bind_method(D_METHOD("get_action_from_id", "id"), &InputMap::get_action_from_id);
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);
ClassDB::bind_method(D_METHOD("action_add_event", "action", "event"), &InputMap::action_add_event);
ClassDB::bind_method(D_METHOD("action_has_event", "action", "event"), &InputMap::action_has_event);
ClassDB::bind_method(D_METHOD("action_erase_event", "action", "event"), &InputMap::action_erase_event);
ClassDB::bind_method(D_METHOD("get_action_list", "action"), &InputMap::_get_action_list);
ClassDB::bind_method(D_METHOD("event_is_action", "event", "action"), &InputMap::event_is_action);
ClassDB::bind_method(D_METHOD("load_from_globals"), &InputMap::load_from_globals);
2014-04-24 01:48:16 +00:00
}
void InputMap::add_action(const StringName &p_action) {
2014-04-24 01:48:16 +00: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;
input_id_map[last_id] = p_action;
2014-02-10 01:10:30 +00:00
last_id++;
}
void InputMap::erase_action(const StringName &p_action) {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND(!input_map.has(p_action));
2014-02-10 01:10:30 +00:00
input_id_map.erase(input_map[p_action].id);
input_map.erase(p_action);
}
StringName InputMap::get_action_from_id(int p_id) const {
ERR_FAIL_COND_V(!input_id_map.has(p_id), StringName());
2014-02-10 01:10:30 +00:00
return input_id_map[p_id];
}
Array InputMap::_get_actions() {
Array ret;
List<StringName> actions = get_actions();
if (actions.empty())
return ret;
for (const List<StringName>::Element *E = actions.front(); E; E = E->next()) {
ret.push_back(E->get());
}
return ret;
}
List<StringName> InputMap::get_actions() const {
List<StringName> actions = List<StringName>();
if (input_map.empty()) {
return actions;
}
for (Map<StringName, Action>::Element *E = input_map.front(); E; E = E->next()) {
actions.push_back(E->key());
}
return actions;
}
List<InputEvent>::Element *InputMap::_find_event(List<InputEvent> &p_list, const InputEvent &p_event, bool p_action_test) const {
2014-02-10 01:10:30 +00:00
for (List<InputEvent>::Element *E = p_list.front(); E; E = E->next()) {
2014-02-10 01:10:30 +00:00
const InputEvent &e = E->get();
if (e.type != p_event.type)
2014-02-10 01:10:30 +00:00
continue;
if (e.type != InputEvent::KEY && e.device != p_event.device)
2014-02-10 01:10:30 +00:00
continue;
bool same = false;
2014-02-10 01:10:30 +00:00
switch (p_event.type) {
2014-02-10 01:10:30 +00:00
case InputEvent::KEY: {
if (p_action_test) {
uint32_t code = e.key.get_scancode_with_modifiers();
uint32_t event_code = p_event.key.get_scancode_with_modifiers();
same = (e.key.scancode == p_event.key.scancode && (!p_event.key.pressed || ((code & event_code) == code)));
} else {
same = (e.key.scancode == p_event.key.scancode && e.key.mod == p_event.key.mod);
}
2014-02-10 01:10:30 +00:00
} break;
case InputEvent::JOYPAD_BUTTON: {
2014-02-10 01:10:30 +00:00
same = (e.joy_button.button_index == p_event.joy_button.button_index);
2014-02-10 01:10:30 +00:00
} break;
case InputEvent::MOUSE_BUTTON: {
same = (e.mouse_button.button_index == p_event.mouse_button.button_index);
2014-02-10 01:10:30 +00:00
} break;
case InputEvent::JOYPAD_MOTION: {
2014-02-10 01:10:30 +00:00
same = (e.joy_motion.axis == p_event.joy_motion.axis && (e.joy_motion.axis_value < 0) == (p_event.joy_motion.axis_value < 0));
2014-02-10 01:10:30 +00:00
} break;
}
if (same)
return E;
}
return NULL;
}
bool InputMap::has_action(const StringName &p_action) const {
2014-02-10 01:10:30 +00:00
return input_map.has(p_action);
}
void InputMap::action_add_event(const StringName &p_action, const InputEvent &p_event) {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND(p_event.type == InputEvent::ACTION);
ERR_FAIL_COND(!input_map.has(p_action));
if (_find_event(input_map[p_action].inputs, p_event))
2014-02-10 01:10:30 +00:00
return; //already gots
input_map[p_action].inputs.push_back(p_event);
}
int InputMap::get_action_id(const StringName &p_action) const {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(!input_map.has(p_action), -1);
2014-02-10 01:10:30 +00:00
return input_map[p_action].id;
}
bool InputMap::action_has_event(const StringName &p_action, const InputEvent &p_event) {
2014-02-10 01:10:30 +00: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 01:10:30 +00:00
}
void InputMap::action_erase_event(const StringName &p_action, const InputEvent &p_event) {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND(!input_map.has(p_action));
2014-02-10 01:10:30 +00:00
List<InputEvent>::Element *E = _find_event(input_map[p_action].inputs, p_event);
2014-02-10 01:10:30 +00:00
if (E)
input_map[p_action].inputs.erase(E);
2014-02-10 01:10:30 +00:00
}
Array InputMap::_get_action_list(const StringName &p_action) {
Array ret;
const List<InputEvent> *al = get_action_list(p_action);
if (al) {
for (const List<InputEvent>::Element *E = al->front(); E; E = E->next()) {
2017-01-14 17:03:38 +00:00
ret.push_back(E->get());
}
}
return ret;
}
const List<InputEvent> *InputMap::get_action_list(const StringName &p_action) {
2014-02-10 01:10:30 +00:00
const Map<StringName, Action>::Element *E = input_map.find(p_action);
2014-02-10 01:10:30 +00:00
if (!E)
return NULL;
return &E->get().inputs;
}
bool InputMap::event_is_action(const InputEvent &p_event, const StringName &p_action) const {
2014-02-10 01:10:30 +00: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 01:10:30 +00:00
}
if (p_event.type == InputEvent::ACTION) {
2014-02-10 01:10:30 +00:00
return p_event.action.action == E->get().id;
2014-02-10 01:10:30 +00:00
}
return _find_event(E->get().inputs, p_event, true) != NULL;
2014-02-10 01:10:30 +00:00
}
const Map<StringName, InputMap::Action> &InputMap::get_action_map() const {
return input_map;
}
2014-02-10 01:10:30 +00:00
void InputMap::load_from_globals() {
2017-01-14 17:03:38 +00:00
input_map.clear();
2014-02-10 01:10:30 +00:00
List<PropertyInfo> pinfo;
GlobalConfig::get_singleton()->get_property_list(&pinfo);
2014-02-10 01:10:30 +00:00
for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
const PropertyInfo &pi = E->get();
2014-02-10 01:10:30 +00:00
if (!pi.name.begins_with("input/"))
continue;
String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
2014-02-10 01:10:30 +00:00
add_action(name);
2017-01-14 17:03:38 +00:00
Array va = GlobalConfig::get_singleton()->get(pi.name);
2014-02-10 01:10:30 +00:00
for (int i = 0; i < va.size(); i++) {
2014-02-10 01:10:30 +00:00
InputEvent ie = va[i];
if (ie.type == InputEvent::NONE)
2014-02-10 01:10:30 +00:00
continue;
action_add_event(name, ie);
2014-02-10 01:10:30 +00:00
}
}
}
void InputMap::load_default() {
InputEvent key;
key.type = InputEvent::KEY;
add_action("ui_accept");
key.key.scancode = KEY_RETURN;
action_add_event("ui_accept", key);
key.key.scancode = KEY_ENTER;
action_add_event("ui_accept", key);
key.key.scancode = KEY_SPACE;
action_add_event("ui_accept", key);
add_action("ui_select");
key.key.scancode = KEY_SPACE;
action_add_event("ui_select", key);
add_action("ui_cancel");
key.key.scancode = KEY_ESCAPE;
action_add_event("ui_cancel", key);
add_action("ui_focus_next");
key.key.scancode = KEY_TAB;
action_add_event("ui_focus_next", key);
add_action("ui_focus_prev");
key.key.scancode = KEY_TAB;
key.key.mod.shift = true;
action_add_event("ui_focus_prev", key);
key.key.mod.shift = false;
add_action("ui_left");
key.key.scancode = KEY_LEFT;
action_add_event("ui_left", key);
add_action("ui_right");
key.key.scancode = KEY_RIGHT;
action_add_event("ui_right", key);
add_action("ui_up");
key.key.scancode = KEY_UP;
action_add_event("ui_up", key);
add_action("ui_down");
key.key.scancode = KEY_DOWN;
action_add_event("ui_down", key);
add_action("ui_page_up");
key.key.scancode = KEY_PAGEUP;
action_add_event("ui_page_up", key);
add_action("ui_page_down");
key.key.scancode = KEY_PAGEDOWN;
action_add_event("ui_page_down", key);
//set("display/handheld/orientation", "landscape");
2014-02-10 01:10:30 +00:00
}
InputMap::InputMap() {
ERR_FAIL_COND(singleton);
singleton = this;
2014-02-10 01:10:30 +00:00
}