summaryrefslogtreecommitdiff
path: root/scene/2d/visibility_notifier_2d.cpp
blob: 8138f96aa692e48ba5dcb015bd1f80344056dd13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*************************************************************************/
/*  visibility_notifier_2d.cpp                                           */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "visibility_notifier_2d.h"

#include "core/engine.h"
#include "particles_2d.h"
#include "scene/2d/animated_sprite.h"
#include "scene/2d/physics_body_2d.h"
#include "scene/animation/animation_player.h"
#include "scene/main/viewport.h"
#include "scene/scene_string_names.h"

void VisibilityNotifier2D::_enter_viewport(Viewport *p_viewport) {

	ERR_FAIL_COND(viewports.has(p_viewport));
	viewports.insert(p_viewport);

	if (is_inside_tree() && Engine::get_singleton()->is_editor_hint())
		return;

	if (viewports.size() == 1) {
		emit_signal(SceneStringNames::get_singleton()->screen_entered);

		_screen_enter();
	}
	emit_signal(SceneStringNames::get_singleton()->viewport_entered, p_viewport);
}

void VisibilityNotifier2D::_exit_viewport(Viewport *p_viewport) {

	ERR_FAIL_COND(!viewports.has(p_viewport));
	viewports.erase(p_viewport);

	if (is_inside_tree() && Engine::get_singleton()->is_editor_hint())
		return;

	emit_signal(SceneStringNames::get_singleton()->viewport_exited, p_viewport);
	if (viewports.size() == 0) {
		emit_signal(SceneStringNames::get_singleton()->screen_exited);

		_screen_exit();
	}
}

void VisibilityNotifier2D::set_rect(const Rect2 &p_rect) {

	rect = p_rect;
	if (is_inside_tree()) {
		get_world_2d()->_update_notifier(this, get_global_transform().xform(rect));
		if (Engine::get_singleton()->is_editor_hint()) {
			update();
			item_rect_changed();
		}
	}

	_change_notify("rect");
}

Rect2 VisibilityNotifier2D::_edit_get_rect() const {

	return rect;
}

bool VisibilityNotifier2D::_edit_use_rect() const {
	return true;
}

Rect2 VisibilityNotifier2D::get_rect() const {

	return rect;
}

void VisibilityNotifier2D::_notification(int p_what) {

	switch (p_what) {
		case NOTIFICATION_ENTER_TREE: {

			//get_world_2d()->
			get_world_2d()->_register_notifier(this, get_global_transform().xform(rect));
		} break;
		case NOTIFICATION_TRANSFORM_CHANGED: {

			//get_world_2d()->
			get_world_2d()->_update_notifier(this, get_global_transform().xform(rect));
		} break;
		case NOTIFICATION_DRAW: {

			if (Engine::get_singleton()->is_editor_hint()) {

				draw_rect(rect, Color(1, 0.5, 1, 0.2));
			}
		} break;
		case NOTIFICATION_EXIT_TREE: {

			get_world_2d()->_remove_notifier(this);
		} break;
	}
}

bool VisibilityNotifier2D::is_on_screen() const {

	return viewports.size() > 0;
}

void VisibilityNotifier2D::_bind_methods() {

	ClassDB::bind_method(D_METHOD("set_rect", "rect"), &VisibilityNotifier2D::set_rect);
	ClassDB::bind_method(D_METHOD("get_rect"), &VisibilityNotifier2D::get_rect);
	ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibilityNotifier2D::is_on_screen);

	ADD_PROPERTY(PropertyInfo(Variant::RECT2, "rect"), "set_rect", "get_rect");

	ADD_SIGNAL(MethodInfo("viewport_entered", PropertyInfo(Variant::OBJECT, "viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport")));
	ADD_SIGNAL(MethodInfo("viewport_exited", PropertyInfo(Variant::OBJECT, "viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport")));
	ADD_SIGNAL(MethodInfo("screen_entered"));
	ADD_SIGNAL(MethodInfo("screen_exited"));
}

VisibilityNotifier2D::VisibilityNotifier2D() {

	rect = Rect2(-10, -10, 20, 20);
	set_notify_transform(true);
}

//////////////////////////////////////

void VisibilityEnabler2D::_screen_enter() {

	for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {

		_change_node_state(E->key(), true);
	}

	if (enabler[ENABLER_PARENT_PHYSICS_PROCESS] && get_parent())
		get_parent()->set_physics_process(true);
	if (enabler[ENABLER_PARENT_PROCESS] && get_parent())
		get_parent()->set_process(true);

	visible = true;
}

void VisibilityEnabler2D::_screen_exit() {

	for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {

		_change_node_state(E->key(), false);
	}

	if (enabler[ENABLER_PARENT_PHYSICS_PROCESS] && get_parent())
		get_parent()->set_physics_process(false);
	if (enabler[ENABLER_PARENT_PROCESS] && get_parent())
		get_parent()->set_process(false);

	visible = false;
}

void VisibilityEnabler2D::_find_nodes(Node *p_node) {

	bool add = false;
	Variant meta;

	if (enabler[ENABLER_FREEZE_BODIES]) {

		RigidBody2D *rb2d = Object::cast_to<RigidBody2D>(p_node);
		if (rb2d && ((rb2d->get_mode() == RigidBody2D::MODE_CHARACTER || rb2d->get_mode() == RigidBody2D::MODE_RIGID))) {

			add = true;
			meta = rb2d->get_mode();
		}
	}

	if (enabler[ENABLER_PAUSE_ANIMATIONS]) {

		AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
		if (ap) {
			add = true;
		}
	}

	if (enabler[ENABLER_PAUSE_ANIMATED_SPRITES]) {

		AnimatedSprite *as = Object::cast_to<AnimatedSprite>(p_node);
		if (as) {
			add = true;
		}
	}

	if (enabler[ENABLER_PAUSE_PARTICLES]) {

		Particles2D *ps = Object::cast_to<Particles2D>(p_node);
		if (ps) {
			add = true;
		}
	}

	if (add) {

		p_node->connect(SceneStringNames::get_singleton()->tree_exiting, this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
		nodes[p_node] = meta;
		_change_node_state(p_node, false);
	}

	for (int i = 0; i < p_node->get_child_count(); i++) {
		Node *c = p_node->get_child(i);
		if (c->get_filename() != String())
			continue; //skip, instance

		_find_nodes(c);
	}
}

void VisibilityEnabler2D::_notification(int p_what) {

	if (p_what == NOTIFICATION_ENTER_TREE) {

		if (Engine::get_singleton()->is_editor_hint())
			return;

		Node *from = this;
		//find where current scene starts
		while (from->get_parent() && from->get_filename() == String())
			from = from->get_parent();

		_find_nodes(from);

		if (enabler[ENABLER_PARENT_PHYSICS_PROCESS] && get_parent())
			get_parent()->set_physics_process(false);
		if (enabler[ENABLER_PARENT_PROCESS] && get_parent())
			get_parent()->set_process(false);
	}

	if (p_what == NOTIFICATION_EXIT_TREE) {

		if (Engine::get_singleton()->is_editor_hint())
			return;

		for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {

			if (!visible)
				_change_node_state(E->key(), true);
			E->key()->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, "_node_removed");
		}

		nodes.clear();
	}
}

void VisibilityEnabler2D::_change_node_state(Node *p_node, bool p_enabled) {

	ERR_FAIL_COND(!nodes.has(p_node));

	{
		RigidBody2D *rb = Object::cast_to<RigidBody2D>(p_node);
		if (rb) {

			rb->set_sleeping(!p_enabled);
		}
	}

	{
		AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);

		if (ap) {

			ap->set_active(p_enabled);
		}
	}
	{
		AnimatedSprite *as = Object::cast_to<AnimatedSprite>(p_node);

		if (as) {

			if (p_enabled)
				as->play();
			else
				as->stop();
		}
	}

	{
		Particles2D *ps = Object::cast_to<Particles2D>(p_node);

		if (ps) {

			ps->set_emitting(p_enabled);
		}
	}
}

void VisibilityEnabler2D::_node_removed(Node *p_node) {

	if (!visible)
		_change_node_state(p_node, true);
	//changed to one shot, not needed
	//p_node->disconnect(SceneStringNames::get_singleton()->exit_scene,this,"_node_removed");
	nodes.erase(p_node);
}

String VisibilityEnabler2D::get_configuration_warning() const {
#ifdef TOOLS_ENABLED
	if (is_inside_tree() && get_parent() && (get_parent()->get_filename() == String() && get_parent() != get_tree()->get_edited_scene_root())) {
		return TTR("VisibilityEnabler2D works best when used with the edited scene root directly as parent.");
	}
#endif
	return String();
}

void VisibilityEnabler2D::_bind_methods() {

	ClassDB::bind_method(D_METHOD("set_enabler", "enabler", "enabled"), &VisibilityEnabler2D::set_enabler);
	ClassDB::bind_method(D_METHOD("is_enabler_enabled", "enabler"), &VisibilityEnabler2D::is_enabler_enabled);
	ClassDB::bind_method(D_METHOD("_node_removed"), &VisibilityEnabler2D::_node_removed);

	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animations"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATIONS);
	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "freeze_bodies"), "set_enabler", "is_enabler_enabled", ENABLER_FREEZE_BODIES);
	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_particles"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_PARTICLES);
	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animated_sprites"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATED_SPRITES);
	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "process_parent"), "set_enabler", "is_enabler_enabled", ENABLER_PARENT_PROCESS);
	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "physics_process_parent"), "set_enabler", "is_enabler_enabled", ENABLER_PARENT_PHYSICS_PROCESS);

	BIND_ENUM_CONSTANT(ENABLER_PAUSE_ANIMATIONS);
	BIND_ENUM_CONSTANT(ENABLER_FREEZE_BODIES);
	BIND_ENUM_CONSTANT(ENABLER_PAUSE_PARTICLES);
	BIND_ENUM_CONSTANT(ENABLER_PARENT_PROCESS);
	BIND_ENUM_CONSTANT(ENABLER_PARENT_PHYSICS_PROCESS);
	BIND_ENUM_CONSTANT(ENABLER_PAUSE_ANIMATED_SPRITES);
	BIND_ENUM_CONSTANT(ENABLER_MAX);
}

void VisibilityEnabler2D::set_enabler(Enabler p_enabler, bool p_enable) {

	ERR_FAIL_INDEX(p_enabler, ENABLER_MAX);
	enabler[p_enabler] = p_enable;
}
bool VisibilityEnabler2D::is_enabler_enabled(Enabler p_enabler) const {

	ERR_FAIL_INDEX_V(p_enabler, ENABLER_MAX, false);
	return enabler[p_enabler];
}

VisibilityEnabler2D::VisibilityEnabler2D() {

	for (int i = 0; i < ENABLER_MAX; i++)
		enabler[i] = true;
	enabler[ENABLER_PARENT_PROCESS] = false;
	enabler[ENABLER_PARENT_PHYSICS_PROCESS] = false;

	visible = false;
}