shortcut.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "core/os/keyboard.h"
  32. void Shortcut::set_events(const Array &p_events) {
  33. for (int i = 0; i < p_events.size(); i++) {
  34. Ref<InputEventShortcut> ies = p_events[i];
  35. ERR_FAIL_COND_MSG(ies.is_valid(), "Cannot set a shortcut event to an instance of InputEventShortcut.");
  36. }
  37. events = p_events;
  38. emit_changed();
  39. }
  40. void Shortcut::set_events_list(const List<Ref<InputEvent>> *p_events) {
  41. events.clear();
  42. for (const Ref<InputEvent> &ie : *p_events) {
  43. events.push_back(ie);
  44. }
  45. }
  46. Array Shortcut::get_events() const {
  47. return events;
  48. }
  49. bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const {
  50. Ref<InputEventShortcut> ies = p_event;
  51. if (ies.is_valid()) {
  52. if (ies->get_shortcut().ptr() == this) {
  53. return true;
  54. }
  55. }
  56. for (int i = 0; i < events.size(); i++) {
  57. Ref<InputEvent> ie = events[i];
  58. bool valid = ie.is_valid() && ie->is_match(p_event);
  59. // Stop on first valid event - don't need to check further.
  60. if (valid) {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. String Shortcut::get_as_text() const {
  67. for (int i = 0; i < events.size(); i++) {
  68. Ref<InputEvent> ie = events[i];
  69. // Return first shortcut which is valid
  70. if (ie.is_valid()) {
  71. return ie->as_text();
  72. }
  73. }
  74. return "None";
  75. }
  76. bool Shortcut::has_valid_event() const {
  77. // Tests if there is ANY input event which is valid.
  78. for (int i = 0; i < events.size(); i++) {
  79. Ref<InputEvent> ie = events[i];
  80. if (ie.is_valid()) {
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. void Shortcut::_bind_methods() {
  87. ClassDB::bind_method(D_METHOD("set_events", "events"), &Shortcut::set_events);
  88. ClassDB::bind_method(D_METHOD("get_events"), &Shortcut::get_events);
  89. ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event);
  90. ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event);
  91. ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text);
  92. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("InputEvent")), "set_events", "get_events");
  93. }
  94. bool Shortcut::is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2) {
  95. if (p_event_array1.size() != p_event_array2.size()) {
  96. return false;
  97. }
  98. bool is_same = true;
  99. for (int i = 0; i < p_event_array1.size(); i++) {
  100. Ref<InputEvent> ie_1 = p_event_array1[i];
  101. Ref<InputEvent> ie_2 = p_event_array2[i];
  102. is_same = ie_1->is_match(ie_2);
  103. // Break on the first that doesn't match - don't need to check further.
  104. if (!is_same) {
  105. break;
  106. }
  107. }
  108. return is_same;
  109. }