shortcut.cpp 4.7 KB

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