1/**************************************************************************/
2/* visible_on_screen_notifier_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 "visible_on_screen_notifier_2d.h"
32
33#include "scene/scene_string_names.h"
34
35#ifdef TOOLS_ENABLED
36Rect2 VisibleOnScreenNotifier2D::_edit_get_rect() const {
37 return rect;
38}
39
40bool VisibleOnScreenNotifier2D::_edit_use_rect() const {
41 return true;
42}
43#endif
44
45void VisibleOnScreenNotifier2D::_visibility_enter() {
46 if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
47 return;
48 }
49
50 on_screen = true;
51 emit_signal(SceneStringNames::get_singleton()->screen_entered);
52 _screen_enter();
53}
54void VisibleOnScreenNotifier2D::_visibility_exit() {
55 if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
56 return;
57 }
58
59 on_screen = false;
60 emit_signal(SceneStringNames::get_singleton()->screen_exited);
61 _screen_exit();
62}
63
64void VisibleOnScreenNotifier2D::set_rect(const Rect2 &p_rect) {
65 rect = p_rect;
66 if (is_inside_tree()) {
67 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));
68 }
69 queue_redraw();
70}
71
72Rect2 VisibleOnScreenNotifier2D::get_rect() const {
73 return rect;
74}
75
76void VisibleOnScreenNotifier2D::_notification(int p_what) {
77 switch (p_what) {
78 case NOTIFICATION_ENTER_TREE: {
79 on_screen = false;
80 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));
81 } break;
82
83 case NOTIFICATION_DRAW: {
84 if (Engine::get_singleton()->is_editor_hint()) {
85 draw_rect(rect, Color(1, 0.5, 1, 0.2));
86 }
87 } break;
88
89 case NOTIFICATION_EXIT_TREE: {
90 on_screen = false;
91 RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), false, Rect2(), Callable(), Callable());
92 } break;
93 }
94}
95
96bool VisibleOnScreenNotifier2D::is_on_screen() const {
97 return on_screen;
98}
99
100void VisibleOnScreenNotifier2D::_bind_methods() {
101 ClassDB::bind_method(D_METHOD("set_rect", "rect"), &VisibleOnScreenNotifier2D::set_rect);
102 ClassDB::bind_method(D_METHOD("get_rect"), &VisibleOnScreenNotifier2D::get_rect);
103 ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibleOnScreenNotifier2D::is_on_screen);
104
105 ADD_PROPERTY(PropertyInfo(Variant::RECT2, "rect", PROPERTY_HINT_NONE, "suffix:px"), "set_rect", "get_rect");
106
107 ADD_SIGNAL(MethodInfo("screen_entered"));
108 ADD_SIGNAL(MethodInfo("screen_exited"));
109}
110
111VisibleOnScreenNotifier2D::VisibleOnScreenNotifier2D() {
112 rect = Rect2(-10, -10, 20, 20);
113 set_hide_clip_children(true);
114}
115
116//////////////////////////////////////
117
118void VisibleOnScreenEnabler2D::_screen_enter() {
119 _update_enable_mode(true);
120}
121
122void VisibleOnScreenEnabler2D::_screen_exit() {
123 _update_enable_mode(false);
124}
125
126void VisibleOnScreenEnabler2D::set_enable_mode(EnableMode p_mode) {
127 enable_mode = p_mode;
128 if (is_inside_tree()) {
129 _update_enable_mode(is_on_screen());
130 }
131}
132VisibleOnScreenEnabler2D::EnableMode VisibleOnScreenEnabler2D::get_enable_mode() {
133 return enable_mode;
134}
135
136void VisibleOnScreenEnabler2D::set_enable_node_path(NodePath p_path) {
137 if (enable_node_path == p_path) {
138 return;
139 }
140 enable_node_path = p_path;
141 if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
142 node_id = ObjectID();
143 Node *node = get_node(enable_node_path);
144 if (node) {
145 node_id = node->get_instance_id();
146 _update_enable_mode(is_on_screen());
147 }
148 }
149}
150NodePath VisibleOnScreenEnabler2D::get_enable_node_path() {
151 return enable_node_path;
152}
153
154void VisibleOnScreenEnabler2D::_update_enable_mode(bool p_enable) {
155 Node *node = static_cast<Node *>(ObjectDB::get_instance(node_id));
156 if (node) {
157 if (p_enable) {
158 switch (enable_mode) {
159 case ENABLE_MODE_INHERIT: {
160 node->set_process_mode(PROCESS_MODE_INHERIT);
161 } break;
162 case ENABLE_MODE_ALWAYS: {
163 node->set_process_mode(PROCESS_MODE_ALWAYS);
164 } break;
165 case ENABLE_MODE_WHEN_PAUSED: {
166 node->set_process_mode(PROCESS_MODE_WHEN_PAUSED);
167 } break;
168 }
169 } else {
170 node->set_process_mode(PROCESS_MODE_DISABLED);
171 }
172 }
173}
174void VisibleOnScreenEnabler2D::_notification(int p_what) {
175 switch (p_what) {
176 case NOTIFICATION_ENTER_TREE: {
177 if (Engine::get_singleton()->is_editor_hint()) {
178 return;
179 }
180
181 node_id = ObjectID();
182 Node *node = get_node(enable_node_path);
183 if (node) {
184 node_id = node->get_instance_id();
185 node->set_process_mode(PROCESS_MODE_DISABLED);
186 }
187 } break;
188
189 case NOTIFICATION_EXIT_TREE: {
190 node_id = ObjectID();
191 } break;
192 }
193}
194
195void VisibleOnScreenEnabler2D::_bind_methods() {
196 ClassDB::bind_method(D_METHOD("set_enable_mode", "mode"), &VisibleOnScreenEnabler2D::set_enable_mode);
197 ClassDB::bind_method(D_METHOD("get_enable_mode"), &VisibleOnScreenEnabler2D::get_enable_mode);
198
199 ClassDB::bind_method(D_METHOD("set_enable_node_path", "path"), &VisibleOnScreenEnabler2D::set_enable_node_path);
200 ClassDB::bind_method(D_METHOD("get_enable_node_path"), &VisibleOnScreenEnabler2D::get_enable_node_path);
201
202 ADD_GROUP("Enabling", "enable_");
203 ADD_PROPERTY(PropertyInfo(Variant::INT, "enable_mode", PROPERTY_HINT_ENUM, "Inherit,Always,When Paused"), "set_enable_mode", "get_enable_mode");
204 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "enable_node_path"), "set_enable_node_path", "get_enable_node_path");
205
206 BIND_ENUM_CONSTANT(ENABLE_MODE_INHERIT);
207 BIND_ENUM_CONSTANT(ENABLE_MODE_ALWAYS);
208 BIND_ENUM_CONSTANT(ENABLE_MODE_WHEN_PAUSED);
209}
210
211VisibleOnScreenEnabler2D::VisibleOnScreenEnabler2D() {
212}
213