1 /*************************************************************************/
2 /*  visibility_notifier.cpp                                              */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #include "visibility_notifier.h"
32 
33 #include "core/engine.h"
34 #include "scene/3d/camera.h"
35 #include "scene/3d/physics_body.h"
36 #include "scene/animation/animation_player.h"
37 #include "scene/scene_string_names.h"
38 
_enter_camera(Camera * p_camera)39 void VisibilityNotifier::_enter_camera(Camera *p_camera) {
40 
41 	ERR_FAIL_COND(cameras.has(p_camera));
42 	cameras.insert(p_camera);
43 	if (cameras.size() == 1) {
44 		emit_signal(SceneStringNames::get_singleton()->screen_entered);
45 		_screen_enter();
46 	}
47 
48 	emit_signal(SceneStringNames::get_singleton()->camera_entered, p_camera);
49 }
50 
_exit_camera(Camera * p_camera)51 void VisibilityNotifier::_exit_camera(Camera *p_camera) {
52 
53 	ERR_FAIL_COND(!cameras.has(p_camera));
54 	cameras.erase(p_camera);
55 
56 	emit_signal(SceneStringNames::get_singleton()->camera_exited, p_camera);
57 	if (cameras.size() == 0) {
58 		emit_signal(SceneStringNames::get_singleton()->screen_exited);
59 
60 		_screen_exit();
61 	}
62 }
63 
set_aabb(const AABB & p_aabb)64 void VisibilityNotifier::set_aabb(const AABB &p_aabb) {
65 
66 	if (aabb == p_aabb)
67 		return;
68 	aabb = p_aabb;
69 
70 	if (is_inside_world()) {
71 		get_world()->_update_notifier(this, get_global_transform().xform(aabb));
72 	}
73 
74 	_change_notify("aabb");
75 	update_gizmo();
76 }
77 
get_aabb() const78 AABB VisibilityNotifier::get_aabb() const {
79 
80 	return aabb;
81 }
82 
_notification(int p_what)83 void VisibilityNotifier::_notification(int p_what) {
84 
85 	switch (p_what) {
86 		case NOTIFICATION_ENTER_WORLD: {
87 
88 			get_world()->_register_notifier(this, get_global_transform().xform(aabb));
89 		} break;
90 		case NOTIFICATION_TRANSFORM_CHANGED: {
91 
92 			get_world()->_update_notifier(this, get_global_transform().xform(aabb));
93 		} break;
94 		case NOTIFICATION_EXIT_WORLD: {
95 
96 			get_world()->_remove_notifier(this);
97 		} break;
98 	}
99 }
100 
is_on_screen() const101 bool VisibilityNotifier::is_on_screen() const {
102 
103 	return cameras.size() != 0;
104 }
105 
_bind_methods()106 void VisibilityNotifier::_bind_methods() {
107 
108 	ClassDB::bind_method(D_METHOD("set_aabb", "rect"), &VisibilityNotifier::set_aabb);
109 	ClassDB::bind_method(D_METHOD("get_aabb"), &VisibilityNotifier::get_aabb);
110 	ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibilityNotifier::is_on_screen);
111 
112 	ADD_PROPERTY(PropertyInfo(Variant::AABB, "aabb"), "set_aabb", "get_aabb");
113 
114 	ADD_SIGNAL(MethodInfo("camera_entered", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera")));
115 	ADD_SIGNAL(MethodInfo("camera_exited", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera")));
116 	ADD_SIGNAL(MethodInfo("screen_entered"));
117 	ADD_SIGNAL(MethodInfo("screen_exited"));
118 }
119 
VisibilityNotifier()120 VisibilityNotifier::VisibilityNotifier() {
121 
122 	aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
123 	set_notify_transform(true);
124 }
125 
126 //////////////////////////////////////
127 
_screen_enter()128 void VisibilityEnabler::_screen_enter() {
129 
130 	for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
131 
132 		_change_node_state(E->key(), true);
133 	}
134 
135 	visible = true;
136 }
137 
_screen_exit()138 void VisibilityEnabler::_screen_exit() {
139 
140 	for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
141 
142 		_change_node_state(E->key(), false);
143 	}
144 
145 	visible = false;
146 }
147 
_find_nodes(Node * p_node)148 void VisibilityEnabler::_find_nodes(Node *p_node) {
149 
150 	bool add = false;
151 	Variant meta;
152 
153 	{
154 		RigidBody *rb = Object::cast_to<RigidBody>(p_node);
155 		if (rb && ((rb->get_mode() == RigidBody::MODE_CHARACTER || rb->get_mode() == RigidBody::MODE_RIGID))) {
156 
157 			add = true;
158 			meta = rb->get_mode();
159 		}
160 	}
161 
162 	{
163 		AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
164 		if (ap) {
165 			add = true;
166 		}
167 	}
168 
169 	if (add) {
170 
171 		p_node->connect(SceneStringNames::get_singleton()->tree_exiting, this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
172 		nodes[p_node] = meta;
173 		_change_node_state(p_node, false);
174 	}
175 
176 	for (int i = 0; i < p_node->get_child_count(); i++) {
177 		Node *c = p_node->get_child(i);
178 		if (c->get_filename() != String())
179 			continue; //skip, instance
180 
181 		_find_nodes(c);
182 	}
183 }
184 
_notification(int p_what)185 void VisibilityEnabler::_notification(int p_what) {
186 
187 	if (p_what == NOTIFICATION_ENTER_TREE) {
188 
189 		if (Engine::get_singleton()->is_editor_hint())
190 			return;
191 
192 		Node *from = this;
193 		//find where current scene starts
194 		while (from->get_parent() && from->get_filename() == String())
195 			from = from->get_parent();
196 
197 		_find_nodes(from);
198 	}
199 
200 	if (p_what == NOTIFICATION_EXIT_TREE) {
201 
202 		if (Engine::get_singleton()->is_editor_hint())
203 			return;
204 
205 		for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
206 
207 			if (!visible)
208 				_change_node_state(E->key(), true);
209 			E->key()->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, "_node_removed");
210 		}
211 
212 		nodes.clear();
213 	}
214 }
215 
_change_node_state(Node * p_node,bool p_enabled)216 void VisibilityEnabler::_change_node_state(Node *p_node, bool p_enabled) {
217 
218 	ERR_FAIL_COND(!nodes.has(p_node));
219 
220 	if (enabler[ENABLER_FREEZE_BODIES]) {
221 		RigidBody *rb = Object::cast_to<RigidBody>(p_node);
222 		if (rb)
223 
224 			rb->set_sleeping(!p_enabled);
225 	}
226 
227 	if (enabler[ENABLER_PAUSE_ANIMATIONS]) {
228 		AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
229 
230 		if (ap) {
231 
232 			ap->set_active(p_enabled);
233 		}
234 	}
235 }
236 
_node_removed(Node * p_node)237 void VisibilityEnabler::_node_removed(Node *p_node) {
238 
239 	if (!visible)
240 		_change_node_state(p_node, true);
241 	nodes.erase(p_node);
242 }
243 
_bind_methods()244 void VisibilityEnabler::_bind_methods() {
245 
246 	ClassDB::bind_method(D_METHOD("set_enabler", "enabler", "enabled"), &VisibilityEnabler::set_enabler);
247 	ClassDB::bind_method(D_METHOD("is_enabler_enabled", "enabler"), &VisibilityEnabler::is_enabler_enabled);
248 	ClassDB::bind_method(D_METHOD("_node_removed"), &VisibilityEnabler::_node_removed);
249 
250 	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animations"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATIONS);
251 	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "freeze_bodies"), "set_enabler", "is_enabler_enabled", ENABLER_FREEZE_BODIES);
252 
253 	BIND_ENUM_CONSTANT(ENABLER_PAUSE_ANIMATIONS);
254 	BIND_ENUM_CONSTANT(ENABLER_FREEZE_BODIES);
255 	BIND_ENUM_CONSTANT(ENABLER_MAX);
256 }
257 
set_enabler(Enabler p_enabler,bool p_enable)258 void VisibilityEnabler::set_enabler(Enabler p_enabler, bool p_enable) {
259 
260 	ERR_FAIL_INDEX(p_enabler, ENABLER_MAX);
261 	enabler[p_enabler] = p_enable;
262 }
is_enabler_enabled(Enabler p_enabler) const263 bool VisibilityEnabler::is_enabler_enabled(Enabler p_enabler) const {
264 
265 	ERR_FAIL_INDEX_V(p_enabler, ENABLER_MAX, false);
266 	return enabler[p_enabler];
267 }
268 
VisibilityEnabler()269 VisibilityEnabler::VisibilityEnabler() {
270 
271 	for (int i = 0; i < ENABLER_MAX; i++)
272 		enabler[i] = true;
273 
274 	visible = false;
275 }
276