listener_2d.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**************************************************************************/
  2. /* listener_2d.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 "listener_2d.h"
  31. bool Listener2D::_set(const StringName &p_name, const Variant &p_value) {
  32. if (p_name == "current") {
  33. if (p_value.operator bool()) {
  34. make_current();
  35. } else {
  36. clear_current();
  37. }
  38. } else {
  39. return false;
  40. }
  41. return true;
  42. }
  43. bool Listener2D::_get(const StringName &p_name, Variant &r_ret) const {
  44. if (p_name == "current") {
  45. if (is_inside_tree() && get_tree()->is_node_being_edited(this)) {
  46. r_ret = current;
  47. } else {
  48. r_ret = is_current();
  49. }
  50. } else {
  51. return false;
  52. }
  53. return true;
  54. }
  55. void Listener2D::_get_property_list(List<PropertyInfo> *p_list) const {
  56. p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("current")));
  57. }
  58. void Listener2D::_notification(int p_what) {
  59. switch (p_what) {
  60. case NOTIFICATION_ENTER_TREE: {
  61. if (!get_tree()->is_node_being_edited(this) && current) {
  62. make_current();
  63. }
  64. } break;
  65. case NOTIFICATION_EXIT_TREE: {
  66. if (!get_tree()->is_node_being_edited(this)) {
  67. if (is_current()) {
  68. clear_current();
  69. current = true; // Keep it true.
  70. } else {
  71. current = false;
  72. }
  73. }
  74. } break;
  75. }
  76. }
  77. void Listener2D::make_current() {
  78. current = true;
  79. if (!is_inside_tree()) {
  80. return;
  81. }
  82. get_viewport()->_listener_2d_set(this);
  83. }
  84. void Listener2D::clear_current() {
  85. current = false;
  86. if (!is_inside_tree()) {
  87. return;
  88. }
  89. get_viewport()->_listener_2d_remove(this);
  90. }
  91. bool Listener2D::is_current() const {
  92. if (is_inside_tree() && !get_tree()->is_node_being_edited(this)) {
  93. return get_viewport()->get_listener_2d() == this;
  94. } else {
  95. return current;
  96. }
  97. return false;
  98. }
  99. void Listener2D::_bind_methods() {
  100. ClassDB::bind_method(D_METHOD("make_current"), &Listener2D::make_current);
  101. ClassDB::bind_method(D_METHOD("clear_current"), &Listener2D::clear_current);
  102. ClassDB::bind_method(D_METHOD("is_current"), &Listener2D::is_current);
  103. }