listener.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*************************************************************************/
  2. /* listener.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. return true;
  47. }
  48. bool Listener::_get(const StringName &p_name, Variant &r_ret) const {
  49. if (p_name == "current") {
  50. if (is_inside_tree() && get_tree()->is_node_being_edited(this)) {
  51. r_ret = current;
  52. } else {
  53. r_ret = is_current();
  54. }
  55. } else
  56. return false;
  57. return true;
  58. }
  59. void Listener::_get_property_list(List<PropertyInfo> *p_list) const {
  60. p_list->push_back(PropertyInfo(Variant::BOOL, "current"));
  61. }
  62. void Listener::_update_listener() {
  63. if (is_inside_tree() && is_current()) {
  64. get_viewport()->_listener_transform_changed_notify();
  65. }
  66. }
  67. void Listener::_notification(int p_what) {
  68. switch (p_what) {
  69. case NOTIFICATION_ENTER_WORLD: {
  70. bool first_listener = get_viewport()->_listener_add(this);
  71. if (!get_tree()->is_node_being_edited(this) && (current || first_listener))
  72. make_current();
  73. } break;
  74. case NOTIFICATION_TRANSFORM_CHANGED: {
  75. _request_listener_update();
  76. } break;
  77. case NOTIFICATION_EXIT_WORLD: {
  78. if (!get_tree()->is_node_being_edited(this)) {
  79. if (is_current()) {
  80. clear_current();
  81. current = true; //keep it true
  82. } else {
  83. current = false;
  84. }
  85. }
  86. get_viewport()->_listener_remove(this);
  87. } break;
  88. }
  89. }
  90. Transform Listener::get_listener_transform() const {
  91. return get_global_transform().orthonormalized();
  92. }
  93. void Listener::make_current() {
  94. current = true;
  95. if (!is_inside_tree())
  96. return;
  97. get_viewport()->_listener_set(this);
  98. }
  99. void Listener::clear_current() {
  100. current = false;
  101. if (!is_inside_tree())
  102. return;
  103. if (get_viewport()->get_listener() == this) {
  104. get_viewport()->_listener_set(NULL);
  105. get_viewport()->_listener_make_next_current(this);
  106. }
  107. }
  108. bool Listener::is_current() const {
  109. if (is_inside_tree() && !get_tree()->is_node_being_edited(this)) {
  110. return get_viewport()->get_listener() == this;
  111. } else
  112. return current;
  113. return false;
  114. }
  115. bool Listener::_can_gizmo_scale() const {
  116. return false;
  117. }
  118. RES Listener::_get_gizmo_geometry() const {
  119. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  120. return mesh;
  121. }
  122. void Listener::_bind_methods() {
  123. ClassDB::bind_method(D_METHOD("make_current"), &Listener::make_current);
  124. ClassDB::bind_method(D_METHOD("clear_current"), &Listener::clear_current);
  125. ClassDB::bind_method(D_METHOD("is_current"), &Listener::is_current);
  126. ClassDB::bind_method(D_METHOD("get_listener_transform"), &Listener::get_listener_transform);
  127. }
  128. Listener::Listener() {
  129. current = false;
  130. force_change = false;
  131. set_notify_transform(true);
  132. //active=false;
  133. }
  134. Listener::~Listener() {
  135. }