Debian Codegraph @

scene/2d/visible_on_screen_notifier_2d.cpp

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