2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* visibility_notifier_2d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "visibility_notifier_2d.h"
|
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/engine.h"
|
2020-03-27 07:44:44 +00:00
|
|
|
#include "gpu_particles_2d.h"
|
2020-03-26 21:49:16 +00:00
|
|
|
#include "scene/2d/animated_sprite_2d.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "scene/2d/physics_body_2d.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "scene/animation/animation_player.h"
|
2020-03-04 01:51:12 +00:00
|
|
|
#include "scene/main/window.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "scene/scene_string_names.h"
|
|
|
|
|
2019-10-21 21:37:07 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
Rect2 VisibilityNotifier2D::_edit_get_rect() const {
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisibilityNotifier2D::_edit_use_rect() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-15 20:33:24 +00:00
|
|
|
void VisibilityNotifier2D::_visibility_enter() {
|
|
|
|
if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
|
2016-08-08 22:04:12 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-08-08 22:04:12 +00:00
|
|
|
|
2021-06-15 20:33:24 +00:00
|
|
|
on_screen = true;
|
|
|
|
emit_signal(SceneStringNames::get_singleton()->screen_entered);
|
|
|
|
_screen_enter();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2021-06-15 20:33:24 +00:00
|
|
|
void VisibilityNotifier2D::_visibility_exit() {
|
|
|
|
if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
|
2016-08-08 22:04:12 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-08-08 22:04:12 +00:00
|
|
|
|
2021-06-15 20:33:24 +00:00
|
|
|
on_screen = false;
|
|
|
|
emit_signal(SceneStringNames::get_singleton()->screen_exited);
|
|
|
|
_screen_exit();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void VisibilityNotifier2D::set_rect(const Rect2 &p_rect) {
|
|
|
|
rect = p_rect;
|
2015-04-02 10:22:17 +00:00
|
|
|
if (is_inside_tree()) {
|
2021-06-15 20:33:24 +00:00
|
|
|
RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), true, rect, callable_mp(this, &VisibilityNotifier2D::_visibility_enter), callable_mp(this, &VisibilityNotifier2D::_visibility_exit));
|
2015-04-02 10:22:17 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Rect2 VisibilityNotifier2D::get_rect() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityNotifier2D::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
//get_world_2d()->
|
2021-06-15 20:33:24 +00:00
|
|
|
on_screen = false;
|
|
|
|
RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), true, rect, callable_mp(this, &VisibilityNotifier2D::_visibility_enter), callable_mp(this, &VisibilityNotifier2D::_visibility_exit));
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_DRAW: {
|
2017-08-18 23:02:56 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_rect(rect, Color(1, 0.5, 1, 0.2));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} break;
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2021-06-15 20:33:24 +00:00
|
|
|
on_screen = false;
|
|
|
|
RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), false, Rect2(), Callable(), Callable());
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisibilityNotifier2D::is_on_screen() const {
|
2021-06-15 20:33:24 +00:00
|
|
|
return on_screen;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "rect"), "set_rect", "get_rect");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("screen_entered"));
|
|
|
|
ADD_SIGNAL(MethodInfo("screen_exited"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VisibilityNotifier2D::VisibilityNotifier2D() {
|
2017-03-05 15:44:50 +00:00
|
|
|
rect = Rect2(-10, -10, 20, 20);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////
|
|
|
|
|
|
|
|
void VisibilityEnabler2D::_screen_enter() {
|
2021-06-15 20:33:24 +00:00
|
|
|
_update_enable_mode(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void VisibilityEnabler2D::_screen_exit() {
|
2021-06-15 20:33:24 +00:00
|
|
|
_update_enable_mode(false);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 20:33:24 +00:00
|
|
|
void VisibilityEnabler2D::set_enable_mode(EnableMode p_mode) {
|
|
|
|
enable_mode = p_mode;
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
_update_enable_mode(is_on_screen());
|
2015-03-09 05:34:56 +00:00
|
|
|
}
|
2021-06-15 20:33:24 +00:00
|
|
|
}
|
|
|
|
VisibilityEnabler2D::EnableMode VisibilityEnabler2D::get_enable_mode() {
|
|
|
|
return enable_mode;
|
|
|
|
}
|
2015-03-09 05:34:56 +00:00
|
|
|
|
2021-06-15 20:33:24 +00:00
|
|
|
void VisibilityEnabler2D::set_enable_node_path(NodePath p_path) {
|
|
|
|
if (enable_node_path == p_path) {
|
|
|
|
return;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2021-06-15 20:33:24 +00:00
|
|
|
enable_node_path = p_path;
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
node_id = ObjectID();
|
|
|
|
Node *node = get_node(enable_node_path);
|
|
|
|
if (node) {
|
|
|
|
node_id = node->get_instance_id();
|
|
|
|
_update_enable_mode(is_on_screen());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NodePath VisibilityEnabler2D::get_enable_node_path() {
|
|
|
|
return enable_node_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityEnabler2D::_update_enable_mode(bool p_enable) {
|
|
|
|
Node *node = static_cast<Node *>(ObjectDB::get_instance(node_id));
|
|
|
|
if (node) {
|
|
|
|
if (p_enable) {
|
|
|
|
switch (enable_mode) {
|
|
|
|
case ENABLE_MODE_INHERIT: {
|
|
|
|
node->set_process_mode(PROCESS_MODE_INHERIT);
|
|
|
|
} break;
|
|
|
|
case ENABLE_MODE_ALWAYS: {
|
|
|
|
node->set_process_mode(PROCESS_MODE_ALWAYS);
|
|
|
|
} break;
|
|
|
|
case ENABLE_MODE_WHEN_PAUSED: {
|
|
|
|
node->set_process_mode(PROCESS_MODE_WHEN_PAUSED);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
node->set_process_mode(PROCESS_MODE_DISABLED);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void VisibilityEnabler2D::_notification(int p_what) {
|
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-06-15 20:33:24 +00:00
|
|
|
node_id = ObjectID();
|
|
|
|
Node *node = get_node(enable_node_path);
|
|
|
|
if (node) {
|
|
|
|
node_id = node->get_instance_id();
|
|
|
|
node->set_process_mode(PROCESS_MODE_DISABLED);
|
2019-09-03 18:22:22 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_EXIT_TREE) {
|
2021-06-15 20:33:24 +00:00
|
|
|
node_id = ObjectID();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-05-17 21:27:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void VisibilityEnabler2D::_bind_methods() {
|
2021-06-15 20:33:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_enable_mode", "mode"), &VisibilityEnabler2D::set_enable_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_enable_mode"), &VisibilityEnabler2D::get_enable_mode);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-06-15 20:33:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_enable_node_path", "path"), &VisibilityEnabler2D::set_enable_node_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_enable_node_path"), &VisibilityEnabler2D::get_enable_node_path);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-06-15 20:33:24 +00:00
|
|
|
ADD_GROUP("Enabling", "enable_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "enable_mode", PROPERTY_HINT_ENUM, "Inherit,Always,WhenPaused"), "set_enable_mode", "get_enable_mode");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "enable_node_path"), "set_enable_node_path", "get_enable_node_path");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-06-15 20:33:24 +00:00
|
|
|
BIND_ENUM_CONSTANT(ENABLE_MODE_INHERIT);
|
|
|
|
BIND_ENUM_CONSTANT(ENABLE_MODE_ALWAYS);
|
|
|
|
BIND_ENUM_CONSTANT(ENABLE_MODE_WHEN_PAUSED);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VisibilityEnabler2D::VisibilityEnabler2D() {
|
|
|
|
}
|