listener.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**************************************************************************/
  2. /* listener.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.h"
  31. #include "scene/resources/mesh.h"
  32. void Listener::_update_audio_listener_state() {
  33. }
  34. void Listener::_request_listener_update() {
  35. _update_listener();
  36. }
  37. bool Listener::_set(const StringName &p_name, const Variant &p_value) {
  38. if (p_name == "current") {
  39. if (p_value.operator bool()) {
  40. make_current();
  41. } else {
  42. clear_current();
  43. }
  44. } else {
  45. return false;
  46. }
  47. return true;
  48. }
  49. bool Listener::_get(const StringName &p_name, Variant &r_ret) const {
  50. if (p_name == "current") {
  51. if (is_inside_tree() && get_tree()->is_node_being_edited(this)) {
  52. r_ret = current;
  53. } else {
  54. r_ret = is_current();
  55. }
  56. } else {
  57. return false;
  58. }
  59. return true;
  60. }
  61. void Listener::_get_property_list(List<PropertyInfo> *p_list) const {
  62. p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("current")));
  63. }
  64. void Listener::_update_listener() {
  65. if (is_inside_tree() && is_current()) {
  66. get_viewport()->_listener_transform_changed_notify();
  67. }
  68. }
  69. void Listener::_notification(int p_what) {
  70. switch (p_what) {
  71. case NOTIFICATION_ENTER_WORLD: {
  72. bool first_listener = get_viewport()->_listener_add(this);
  73. if (!get_tree()->is_node_being_edited(this) && (current || first_listener)) {
  74. make_current();
  75. }
  76. } break;
  77. case NOTIFICATION_TRANSFORM_CHANGED: {
  78. _request_listener_update();
  79. } break;
  80. case NOTIFICATION_EXIT_WORLD: {
  81. if (!get_tree()->is_node_being_edited(this)) {
  82. if (is_current()) {
  83. clear_current();
  84. current = true; //keep it true
  85. } else {
  86. current = false;
  87. }
  88. }
  89. get_viewport()->_listener_remove(this);
  90. } break;
  91. }
  92. }
  93. Transform Listener::get_listener_transform() const {
  94. return get_global_transform().orthonormalized();
  95. }
  96. void Listener::make_current() {
  97. current = true;
  98. if (!is_inside_tree()) {
  99. return;
  100. }
  101. get_viewport()->_listener_set(this);
  102. }
  103. void Listener::clear_current() {
  104. current = false;
  105. if (!is_inside_tree()) {
  106. return;
  107. }
  108. if (get_viewport()->get_listener() == this) {
  109. get_viewport()->_listener_set(nullptr);
  110. get_viewport()->_listener_make_next_current(this);
  111. }
  112. }
  113. bool Listener::is_current() const {
  114. if (is_inside_tree() && !get_tree()->is_node_being_edited(this)) {
  115. return get_viewport()->get_listener() == this;
  116. } else {
  117. return current;
  118. }
  119. return false;
  120. }
  121. void Listener::_bind_methods() {
  122. ClassDB::bind_method(D_METHOD("make_current"), &Listener::make_current);
  123. ClassDB::bind_method(D_METHOD("clear_current"), &Listener::clear_current);
  124. ClassDB::bind_method(D_METHOD("is_current"), &Listener::is_current);
  125. ClassDB::bind_method(D_METHOD("get_listener_transform"), &Listener::get_listener_transform);
  126. }
  127. Listener::Listener() {
  128. current = false;
  129. force_change = false;
  130. set_notify_transform(true);
  131. }
  132. Listener::~Listener() {
  133. }