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