Debian Codegraph @

scene/2d/light_occluder_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* light_occluder_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 "light_occluder_2d.h"
32
33 #include "core/config/engine.h"
34 #include "core/math/geometry_2d.h"
35
36 #define LINE_GRAB_WIDTH 8
37
38 #ifdef DEBUG_ENABLED
39 Rect2 OccluderPolygon2D::_edit_get_rect() const {
40 if (rect_cache_dirty) {
41 if (closed) {
42 const Vector2 *r = polygon.ptr();
43 item_rect = Rect2();
44 for (int i = 0; i < polygon.size(); i++) {
45 Vector2 pos = r[i];
46 if (i == 0) {
47 item_rect.position = pos;
48 } else {
49 item_rect.expand_to(pos);
50 }
51 }
52 rect_cache_dirty = false;
53 } else {
54 if (polygon.is_empty()) {
55 item_rect = Rect2();
56 } else {
57 Vector2 d = Vector2(LINE_GRAB_WIDTH, LINE_GRAB_WIDTH);
58 item_rect = Rect2(polygon[0] - d, 2 * d);
59 for (int i = 1; i < polygon.size(); i++) {
60 item_rect.expand_to(polygon[i] - d);
61 item_rect.expand_to(polygon[i] + d);
62 }
63 }
64 }
65 }
66
67 return item_rect;
68 }
69
70 bool OccluderPolygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
71 if (closed) {
72 return Geometry2D::is_point_in_polygon(p_point, Variant(polygon));
73 } else {
74 const real_t d = LINE_GRAB_WIDTH / 2 + p_tolerance;
75 const Vector2 *points = polygon.ptr();
76 for (int i = 0; i < polygon.size() - 1; i++) {
77 Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, points[i], points[i + 1]);
78 if (p.distance_to(p_point) <= d) {
79 return true;
80 }
81 }
82
83 return false;
84 }
85 }
86 #endif // DEBUG_ENABLED
87
88 void OccluderPolygon2D::set_polygon(const Vector<Vector2> &p_polygon) {
89 polygon = p_polygon;
90 rect_cache_dirty = true;
91 RS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, p_polygon, closed);
92 emit_changed();
93 update_configuration_warning();
94 }
95
96 Vector<Vector2> OccluderPolygon2D::get_polygon() const {
97 return polygon;
98 }
99
100 void OccluderPolygon2D::set_closed(bool p_closed) {
101 if (closed == p_closed) {
102 return;
103 }
104 closed = p_closed;
105 if (polygon.size()) {
106 RS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, polygon, closed);
107 }
108 emit_changed();
109 }
110
111 bool OccluderPolygon2D::is_closed() const {
112 return closed;
113 }
114
115 void OccluderPolygon2D::set_cull_mode(CullMode p_mode) {
116 cull = p_mode;
117 RS::get_singleton()->canvas_occluder_polygon_set_cull_mode(occ_polygon, RS::CanvasOccluderPolygonCullMode(p_mode));
118 }
119
120 OccluderPolygon2D::CullMode OccluderPolygon2D::get_cull_mode() const {
121 return cull;
122 }
123
124 RID OccluderPolygon2D::get_rid() const {
125 return occ_polygon;
126 }
127
128 void OccluderPolygon2D::_bind_methods() {
129 ClassDB::bind_method(D_METHOD("set_closed", "closed"), &OccluderPolygon2D::set_closed);
130 ClassDB::bind_method(D_METHOD("is_closed"), &OccluderPolygon2D::is_closed);
131
132 ClassDB::bind_method(D_METHOD("set_cull_mode", "cull_mode"), &OccluderPolygon2D::set_cull_mode);
133 ClassDB::bind_method(D_METHOD("get_cull_mode"), &OccluderPolygon2D::get_cull_mode);
134
135 ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &OccluderPolygon2D::set_polygon);
136 ClassDB::bind_method(D_METHOD("get_polygon"), &OccluderPolygon2D::get_polygon);
137
138 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
139 ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mode", PROPERTY_HINT_ENUM, "Disabled,ClockWise,CounterClockWise"), "set_cull_mode", "get_cull_mode");
140 ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
141
142 BIND_ENUM_CONSTANT(CULL_DISABLED);
143 BIND_ENUM_CONSTANT(CULL_CLOCKWISE);
144 BIND_ENUM_CONSTANT(CULL_COUNTER_CLOCKWISE);
145 }
146
147 OccluderPolygon2D::OccluderPolygon2D() {
148 occ_polygon = RS::get_singleton()->canvas_occluder_polygon_create();
149 }
150
151 OccluderPolygon2D::~OccluderPolygon2D() {
152 ERR_FAIL_NULL(RenderingServer::get_singleton());
153 RS::get_singleton()->free_rid(occ_polygon);
154 }
155
156 void LightOccluder2D::_poly_changed() {
157 #ifdef DEBUG_ENABLED
158 queue_redraw();
159 #endif // DEBUG_ENABLED
160 }
161
162 void LightOccluder2D::_physics_interpolated_changed() {
163 RenderingServer::get_singleton()->canvas_light_occluder_set_interpolated(occluder, is_physics_interpolated());
164 }
165
166 void LightOccluder2D::_notification(int p_what) {
167 switch (p_what) {
168 case NOTIFICATION_ENTER_CANVAS: {
169 RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, get_canvas());
170 RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
171 RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
172 } break;
173
174 case NOTIFICATION_TRANSFORM_CHANGED: {
175 RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
176 } break;
177
178 case NOTIFICATION_VISIBILITY_CHANGED: {
179 RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
180 } break;
181
182 case NOTIFICATION_DRAW: {
183 if (Engine::get_singleton()->is_editor_hint()) {
184 if (occluder_polygon.is_valid()) {
185 Vector<Vector2> poly = occluder_polygon->get_polygon();
186
187 if (poly.size()) {
188 if (occluder_polygon->is_closed()) {
189 Vector<Color> color;
190 color.push_back(Color(0, 0, 0, 0.6));
191 draw_polygon(Variant(poly), color);
192 } else {
193 int ps = poly.size();
194 const Vector2 *r = poly.ptr();
195 for (int i = 0; i < ps - 1; i++) {
196 draw_line(r[i], r[i + 1], Color(0, 0, 0, 0.6), 3);
197 }
198 }
199 }
200 }
201 }
202 } break;
203
204 case NOTIFICATION_EXIT_CANVAS: {
205 RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, RID());
206 } break;
207
208 case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
209 if (is_visible_in_tree() && is_physics_interpolated_and_enabled()) {
210 // Explicitly make sure the transform is up to date in RenderingServer before
211 // resetting. This is necessary because NOTIFICATION_TRANSFORM_CHANGED
212 // is normally deferred, and a client change to transform will not always be sent
213 // before the reset, so we need to guarantee this.
214 RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
215 RS::get_singleton()->canvas_light_occluder_reset_physics_interpolation(occluder);
216 }
217 } break;
218 }
219 }
220
221 #ifdef DEBUG_ENABLED
222 Rect2 LightOccluder2D::_edit_get_rect() const {
223 return occluder_polygon.is_valid() ? occluder_polygon->_edit_get_rect() : Rect2();
224 }
225
226 bool LightOccluder2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
227 return occluder_polygon.is_valid() ? occluder_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;
228 }
229 #endif // DEBUG_ENABLED
230
231 void LightOccluder2D::set_occluder_polygon(const Ref<OccluderPolygon2D> &p_polygon) {
232 #ifdef DEBUG_ENABLED
233 if (occluder_polygon.is_valid()) {
234 occluder_polygon->disconnect_changed(callable_mp(this, &LightOccluder2D::_poly_changed));
235 }
236 #endif // DEBUG_ENABLED
237 occluder_polygon = p_polygon;
238
239 if (occluder_polygon.is_valid()) {
240 RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, occluder_polygon->get_rid());
241 } else {
242 RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, RID());
243 }
244
245 #ifdef DEBUG_ENABLED
246 if (occluder_polygon.is_valid()) {
247 occluder_polygon->connect_changed(callable_mp(this, &LightOccluder2D::_poly_changed));
248 }
249 queue_redraw();
250 #endif // DEBUG_ENABLED
251 }
252
253 Ref<OccluderPolygon2D> LightOccluder2D::get_occluder_polygon() const {
254 return occluder_polygon;
255 }
256
257 void LightOccluder2D::set_occluder_light_mask(int p_mask) {
258 mask = p_mask;
259 RS::get_singleton()->canvas_light_occluder_set_light_mask(occluder, p_mask);
260 }
261
262 int LightOccluder2D::get_occluder_light_mask() const {
263 return mask;
264 }
265
266 PackedStringArray LightOccluder2D::get_configuration_warnings() const {
267 PackedStringArray warnings = Node2D::get_configuration_warnings();
268
269 if (occluder_polygon.is_null()) {
270 warnings.push_back(RTR("An occluder polygon must be set (or drawn) for this occluder to take effect."));
271 }
272
273 if (occluder_polygon.is_valid() && occluder_polygon->get_polygon().is_empty()) {
274 warnings.push_back(RTR("The occluder polygon for this occluder is empty. Please draw a polygon."));
275 }
276
277 return warnings;
278 }
279
280 void LightOccluder2D::set_as_sdf_collision(bool p_enable) {
281 sdf_collision = p_enable;
282 RS::get_singleton()->canvas_light_occluder_set_as_sdf_collision(occluder, sdf_collision);
283 }
284 bool LightOccluder2D::is_set_as_sdf_collision() const {
285 return sdf_collision;
286 }
287
288 void LightOccluder2D::_bind_methods() {
289 ClassDB::bind_method(D_METHOD("set_occluder_polygon", "polygon"), &LightOccluder2D::set_occluder_polygon);
290 ClassDB::bind_method(D_METHOD("get_occluder_polygon"), &LightOccluder2D::get_occluder_polygon);
291
292 ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &LightOccluder2D::set_occluder_light_mask);
293 ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &LightOccluder2D::get_occluder_light_mask);
294
295 ClassDB::bind_method(D_METHOD("set_as_sdf_collision", "enable"), &LightOccluder2D::set_as_sdf_collision);
296 ClassDB::bind_method(D_METHOD("is_set_as_sdf_collision"), &LightOccluder2D::is_set_as_sdf_collision);
297
298 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"), "set_occluder_polygon", "get_occluder_polygon");
299 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sdf_collision"), "set_as_sdf_collision", "is_set_as_sdf_collision");
300 ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
301 }
302
303 LightOccluder2D::LightOccluder2D() {
304 occluder = RS::get_singleton()->canvas_light_occluder_create();
305
306 set_notify_transform(true);
307 set_as_sdf_collision(true);
308 }
309
310 LightOccluder2D::~LightOccluder2D() {
311 ERR_FAIL_NULL(RenderingServer::get_singleton());
312
313 RS::get_singleton()->free_rid(occluder);
314 }