2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2017-08-27 19:07:15 +00:00
|
|
|
/* shortcut.cpp */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-03-05 14:47:28 +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
|
|
|
|
2017-08-27 19:07:15 +00:00
|
|
|
#include "shortcut.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/keyboard.h"
|
2016-06-05 00:31:29 +00:00
|
|
|
|
2021-06-21 01:34:50 +00:00
|
|
|
void Shortcut::set_events(const Array &p_events) {
|
|
|
|
for (int i = 0; i < p_events.size(); i++) {
|
|
|
|
Ref<InputEventShortcut> ies = p_events[i];
|
|
|
|
ERR_FAIL_COND_MSG(ies.is_valid(), "Cannot set a shortcut event to an instance of InputEventShortcut.");
|
|
|
|
}
|
|
|
|
|
|
|
|
events = p_events;
|
2016-06-05 00:31:29 +00:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2021-06-21 01:34:50 +00:00
|
|
|
void Shortcut::set_events_list(const List<Ref<InputEvent>> *p_events) {
|
|
|
|
events.clear();
|
|
|
|
|
|
|
|
for (const Ref<InputEvent> &ie : *p_events) {
|
|
|
|
events.push_back(ie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Array Shortcut::get_events() const {
|
|
|
|
return events;
|
2016-06-05 00:31:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 15:27:45 +00:00
|
|
|
bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const {
|
2021-05-29 13:28:16 +00:00
|
|
|
Ref<InputEventShortcut> ies = p_event;
|
2021-08-11 10:21:31 +00:00
|
|
|
if (ies.is_valid()) {
|
2021-05-29 13:28:16 +00:00
|
|
|
if (ies->get_shortcut().ptr() == this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-06-21 01:34:50 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < events.size(); i++) {
|
|
|
|
Ref<InputEvent> ie = events[i];
|
|
|
|
bool valid = ie.is_valid() && ie->is_match(p_event);
|
|
|
|
|
|
|
|
// Stop on first valid event - don't need to check further.
|
|
|
|
if (valid) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-06-05 00:31:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 19:53:24 +00:00
|
|
|
String Shortcut::get_as_text() const {
|
2021-06-21 01:34:50 +00:00
|
|
|
for (int i = 0; i < events.size(); i++) {
|
|
|
|
Ref<InputEvent> ie = events[i];
|
|
|
|
// Return first shortcut which is valid
|
|
|
|
if (ie.is_valid()) {
|
|
|
|
return ie->as_text();
|
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-06-21 01:34:50 +00:00
|
|
|
|
|
|
|
return "None";
|
2016-06-05 00:31:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 15:27:45 +00:00
|
|
|
bool Shortcut::has_valid_event() const {
|
2021-06-21 01:34:50 +00:00
|
|
|
// Tests if there is ANY input event which is valid.
|
|
|
|
for (int i = 0; i < events.size(); i++) {
|
|
|
|
Ref<InputEvent> ie = events[i];
|
|
|
|
if (ie.is_valid()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-06-05 00:31:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 19:53:24 +00:00
|
|
|
void Shortcut::_bind_methods() {
|
2021-06-21 01:34:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_events", "events"), &Shortcut::set_events);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_events"), &Shortcut::get_events);
|
2016-06-05 00:31:29 +00:00
|
|
|
|
2021-08-03 15:27:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event);
|
2016-06-05 00:31:29 +00:00
|
|
|
|
2021-08-03 15:27:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event);
|
2020-09-09 19:53:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text);
|
2016-06-05 00:31:29 +00:00
|
|
|
|
2021-06-21 01:34:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")), "set_events", "get_events");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Shortcut::is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2) {
|
|
|
|
if (p_event_array1.size() != p_event_array2.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_same = true;
|
|
|
|
for (int i = 0; i < p_event_array1.size(); i++) {
|
|
|
|
Ref<InputEvent> ie_1 = p_event_array1[i];
|
|
|
|
Ref<InputEvent> ie_2 = p_event_array2[i];
|
|
|
|
|
|
|
|
is_same = ie_1->is_match(ie_2);
|
|
|
|
|
|
|
|
// Break on the first that doesn't match - don't need to check further.
|
|
|
|
if (!is_same) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_same;
|
2016-06-05 00:31:29 +00:00
|
|
|
}
|