/**************************************************************************/1/* shortcut.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "shortcut.h"3132void Shortcut::set_events(const Array &p_events) {33for (int i = 0; i < p_events.size(); i++) {34Ref<InputEventShortcut> ies = p_events[i];35ERR_FAIL_COND_MSG(ies.is_valid(), "Cannot set a shortcut event to an instance of InputEventShortcut.");36}3738events = p_events;39emit_changed();40}4142void Shortcut::set_events_list(const List<Ref<InputEvent>> *p_events) {43events.clear();4445for (const Ref<InputEvent> &ie : *p_events) {46events.push_back(ie);47}48}4950Array Shortcut::get_events() const {51return events;52}5354bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const {55Ref<InputEventShortcut> ies = p_event;56if (ies.is_valid()) {57if (ies->get_shortcut().ptr() == this) {58return true;59}60}6162for (int i = 0; i < events.size(); i++) {63Ref<InputEvent> ie = events[i];64bool valid = ie.is_valid() && ie->is_match(p_event);6566// Stop on first valid event - don't need to check further.67if (valid) {68return true;69}70}7172return false;73}7475String Shortcut::get_as_text() const {76for (int i = 0; i < events.size(); i++) {77Ref<InputEvent> ie = events[i];78// Return first shortcut which is valid79if (ie.is_valid()) {80return ie->as_text();81}82}8384return "None";85}8687bool Shortcut::has_valid_event() const {88// Tests if there is ANY input event which is valid.89for (int i = 0; i < events.size(); i++) {90Ref<InputEvent> ie = events[i];91if (ie.is_valid()) {92return true;93}94}9596return false;97}9899void Shortcut::_bind_methods() {100ClassDB::bind_method(D_METHOD("set_events", "events"), &Shortcut::set_events);101ClassDB::bind_method(D_METHOD("get_events"), &Shortcut::get_events);102103ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event);104105ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event);106ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text);107108ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("InputEvent")), "set_events", "get_events");109}110111bool Shortcut::is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2) {112if (p_event_array1.size() != p_event_array2.size()) {113return false;114}115116bool is_same = true;117for (int i = 0; i < p_event_array1.size(); i++) {118Ref<InputEvent> ie_1 = p_event_array1[i];119Ref<InputEvent> ie_2 = p_event_array2[i];120121is_same = ie_1->is_match(ie_2);122123// Break on the first that doesn't match - don't need to check further.124if (!is_same) {125break;126}127}128129return is_same;130}131132133