Debian Codegraph @

scene/3d/visible_on_screen_notifier_3d.cpp

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