2023-01-05 20:25:55 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* shortcut.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-05 07:50:27 +08:00
|
|
|
|
2017-08-28 03:07:15 +08:00
|
|
|
#include "shortcut.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/keyboard.h"
|
2016-06-05 08:31:29 +08:00
|
|
|
|
2021-06-21 09:34:50 +08: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 08:31:29 +08:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2021-06-21 09:34:50 +08: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 08:31:29 +08:00
|
|
|
}
|
|
|
|
|
2021-08-03 23:27:45 +08:00
|
|
|
bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const {
|
2021-05-29 21:28:16 +08:00
|
|
|
Ref<InputEventShortcut> ies = p_event;
|
2021-08-11 18:21:31 +08:00
|
|
|
if (ies.is_valid()) {
|
2021-05-29 21:28:16 +08:00
|
|
|
if (ies->get_shortcut().ptr() == this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-06-21 09:34:50 +08: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 08:31:29 +08:00
|
|
|
}
|
|
|
|
|
2020-09-10 03:53:24 +08:00
|
|
|
String Shortcut::get_as_text() const {
|
2021-06-21 09:34:50 +08: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 22:41:43 +08:00
|
|
|
}
|
2021-06-21 09:34:50 +08:00
|
|
|
|
|
|
|
return "None";
|
2016-06-05 08:31:29 +08:00
|
|
|
}
|
|
|
|
|
2021-08-03 23:27:45 +08:00
|
|
|
bool Shortcut::has_valid_event() const {
|
2021-06-21 09:34:50 +08: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 08:31:29 +08:00
|
|
|
}
|
|
|
|
|
2020-09-10 03:53:24 +08:00
|
|
|
void Shortcut::_bind_methods() {
|
2021-06-21 09:34:50 +08: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 08:31:29 +08:00
|
|
|
|
2021-08-03 23:27:45 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event);
|
2016-06-05 08:31:29 +08:00
|
|
|
|
2021-08-03 23:27:45 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event);
|
2020-09-10 03:53:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text);
|
2016-06-05 08:31:29 +08:00
|
|
|
|
2022-11-22 23:10:41 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("InputEvent")), "set_events", "get_events");
|
2021-06-21 09:34:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 08:31:29 +08:00
|
|
|
}
|