2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* visible_on_screen_notifier_2d.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-05 00:50:27 +01:00
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
#include "visible_on_screen_notifier_2d.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-10-21 23:37:07 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2021-06-17 02:52:30 +02:00
|
|
|
Rect2 VisibleOnScreenNotifier2D::_edit_get_rect() const {
|
2019-10-21 23:37:07 +02:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
bool VisibleOnScreenNotifier2D::_edit_use_rect() const {
|
2019-10-21 23:37:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenNotifier2D::_visibility_enter() {
|
2021-06-15 22:33:24 +02:00
|
|
|
if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
|
2016-08-09 00:04:12 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-09 00:04:12 +02:00
|
|
|
|
2021-06-15 22:33:24 +02:00
|
|
|
on_screen = true;
|
2023-09-04 17:01:33 +02:00
|
|
|
emit_signal(SceneStringName(screen_entered));
|
2021-06-15 22:33:24 +02:00
|
|
|
_screen_enter();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenNotifier2D::_visibility_exit() {
|
2021-06-15 22:33:24 +02:00
|
|
|
if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
|
2016-08-09 00:04:12 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-09 00:04:12 +02:00
|
|
|
|
2021-06-15 22:33:24 +02:00
|
|
|
on_screen = false;
|
2023-09-04 17:01:33 +02:00
|
|
|
emit_signal(SceneStringName(screen_exited));
|
2021-06-15 22:33:24 +02:00
|
|
|
_screen_exit();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenNotifier2D::set_rect(const Rect2 &p_rect) {
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = p_rect;
|
2015-04-02 12:22:17 +02:00
|
|
|
if (is_inside_tree()) {
|
2021-06-17 02:52:30 +02:00
|
|
|
RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), true, rect, callable_mp(this, &VisibleOnScreenNotifier2D::_visibility_enter), callable_mp(this, &VisibleOnScreenNotifier2D::_visibility_exit));
|
2015-04-02 12:22:17 +02:00
|
|
|
}
|
2022-08-13 23:21:24 +02:00
|
|
|
queue_redraw();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
Rect2 VisibleOnScreenNotifier2D::get_rect() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenNotifier2D::_notification(int p_what) {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2021-06-15 22:33:24 +02:00
|
|
|
on_screen = false;
|
2021-06-17 02:52:30 +02:00
|
|
|
RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), true, rect, callable_mp(this, &VisibleOnScreenNotifier2D::_visibility_enter), callable_mp(this, &VisibleOnScreenNotifier2D::_visibility_exit));
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
case NOTIFICATION_DRAW: {
|
2017-08-19 01:02:56 +02:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
draw_rect(rect, Color(1, 0.5, 1, 0.2));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2021-06-15 22:33:24 +02:00
|
|
|
on_screen = false;
|
|
|
|
RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), false, Rect2(), Callable(), Callable());
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
bool VisibleOnScreenNotifier2D::is_on_screen() const {
|
2021-06-15 22:33:24 +02:00
|
|
|
return on_screen;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenNotifier2D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_rect", "rect"), &VisibleOnScreenNotifier2D::set_rect);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_rect"), &VisibleOnScreenNotifier2D::get_rect);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibleOnScreenNotifier2D::is_on_screen);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "rect", PROPERTY_HINT_NONE, "suffix:px"), "set_rect", "get_rect");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("screen_entered"));
|
|
|
|
ADD_SIGNAL(MethodInfo("screen_exited"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
VisibleOnScreenNotifier2D::VisibleOnScreenNotifier2D() {
|
2017-03-05 16:44:50 +01:00
|
|
|
rect = Rect2(-10, -10, 20, 20);
|
2023-01-26 13:17:26 +01:00
|
|
|
set_hide_clip_children(true);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenEnabler2D::_screen_enter() {
|
2021-06-15 22:33:24 +02:00
|
|
|
_update_enable_mode(true);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenEnabler2D::_screen_exit() {
|
2021-06-15 22:33:24 +02:00
|
|
|
_update_enable_mode(false);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenEnabler2D::set_enable_mode(EnableMode p_mode) {
|
2021-06-15 22:33:24 +02:00
|
|
|
enable_mode = p_mode;
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
_update_enable_mode(is_on_screen());
|
2015-03-09 06:34:56 +01:00
|
|
|
}
|
2021-06-15 22:33:24 +02:00
|
|
|
}
|
2021-06-17 02:52:30 +02:00
|
|
|
VisibleOnScreenEnabler2D::EnableMode VisibleOnScreenEnabler2D::get_enable_mode() {
|
2021-06-15 22:33:24 +02:00
|
|
|
return enable_mode;
|
|
|
|
}
|
2015-03-09 06:34:56 +01:00
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenEnabler2D::set_enable_node_path(NodePath p_path) {
|
2021-06-15 22:33:24 +02:00
|
|
|
if (enable_node_path == p_path) {
|
|
|
|
return;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2021-06-15 22:33:24 +02:00
|
|
|
enable_node_path = p_path;
|
2024-02-16 15:43:19 +01:00
|
|
|
if (enable_node_path.is_empty()) {
|
|
|
|
node_id = ObjectID();
|
|
|
|
return;
|
|
|
|
}
|
2022-12-09 15:42:47 +01:00
|
|
|
if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
|
2021-06-15 22:33:24 +02:00
|
|
|
node_id = ObjectID();
|
|
|
|
Node *node = get_node(enable_node_path);
|
|
|
|
if (node) {
|
|
|
|
node_id = node->get_instance_id();
|
|
|
|
_update_enable_mode(is_on_screen());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 02:52:30 +02:00
|
|
|
NodePath VisibleOnScreenEnabler2D::get_enable_node_path() {
|
2021-06-15 22:33:24 +02:00
|
|
|
return enable_node_path;
|
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenEnabler2D::_update_enable_mode(bool p_enable) {
|
2021-06-15 22:33:24 +02:00
|
|
|
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 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenEnabler2D::_notification(int p_what) {
|
2022-02-15 18:06:48 +01:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
node_id = ObjectID();
|
2024-02-16 15:43:19 +01:00
|
|
|
if (enable_node_path.is_empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
Node *node = get_node(enable_node_path);
|
|
|
|
if (node) {
|
|
|
|
node_id = node->get_instance_id();
|
|
|
|
node->set_process_mode(PROCESS_MODE_DISABLED);
|
|
|
|
}
|
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
node_id = ObjectID();
|
|
|
|
} break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-05-17 23:27:15 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
void VisibleOnScreenEnabler2D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_enable_mode", "mode"), &VisibleOnScreenEnabler2D::set_enable_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_enable_mode"), &VisibleOnScreenEnabler2D::get_enable_mode);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_enable_node_path", "path"), &VisibleOnScreenEnabler2D::set_enable_node_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_enable_node_path"), &VisibleOnScreenEnabler2D::get_enable_node_path);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2021-06-15 22:33:24 +02:00
|
|
|
ADD_GROUP("Enabling", "enable_");
|
2022-05-12 22:03:16 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "enable_mode", PROPERTY_HINT_ENUM, "Inherit,Always,When Paused"), "set_enable_mode", "get_enable_mode");
|
2021-06-15 22:33:24 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "enable_node_path"), "set_enable_node_path", "get_enable_node_path");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-06-15 22:33:24 +02:00
|
|
|
BIND_ENUM_CONSTANT(ENABLE_MODE_INHERIT);
|
|
|
|
BIND_ENUM_CONSTANT(ENABLE_MODE_ALWAYS);
|
|
|
|
BIND_ENUM_CONSTANT(ENABLE_MODE_WHEN_PAUSED);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 02:52:30 +02:00
|
|
|
VisibleOnScreenEnabler2D::VisibleOnScreenEnabler2D() {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|