2016-06-18 20:46:12 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* light_occluder_2d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 20:46:12 +08:00
|
|
|
/*************************************************************************/
|
2017-01-02 05:01:57 +08:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 06:11:42 +08:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 20:46:12 +08:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2015-03-02 11:54:10 +08:00
|
|
|
#include "light_occluder_2d.h"
|
|
|
|
|
2017-08-19 07:02:56 +08:00
|
|
|
#include "engine.h"
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void OccluderPolygon2D::set_polygon(const PoolVector<Vector2> &p_polygon) {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
polygon = p_polygon;
|
|
|
|
VS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, p_polygon, closed);
|
2015-03-02 11:54:10 +08:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
PoolVector<Vector2> OccluderPolygon2D::get_polygon() const {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
return polygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OccluderPolygon2D::set_closed(bool p_closed) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (closed == p_closed)
|
2015-03-02 11:54:10 +08:00
|
|
|
return;
|
2017-03-05 23:44:50 +08:00
|
|
|
closed = p_closed;
|
2015-03-09 13:34:56 +08:00
|
|
|
if (polygon.size())
|
2017-03-05 23:44:50 +08:00
|
|
|
VS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, polygon, closed);
|
2015-03-02 11:54:10 +08:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool OccluderPolygon2D::is_closed() const {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
return closed;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void OccluderPolygon2D::set_cull_mode(CullMode p_mode) {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
cull = p_mode;
|
|
|
|
VS::get_singleton()->canvas_occluder_polygon_set_cull_mode(occ_polygon, VS::CanvasOccluderPolygonCullMode(p_mode));
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
OccluderPolygon2D::CullMode OccluderPolygon2D::get_cull_mode() const {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
return cull;
|
|
|
|
}
|
|
|
|
|
|
|
|
RID OccluderPolygon2D::get_rid() const {
|
|
|
|
|
|
|
|
return occ_polygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OccluderPolygon2D::_bind_methods() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_closed", "closed"), &OccluderPolygon2D::set_closed);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_closed"), &OccluderPolygon2D::is_closed);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_cull_mode", "cull_mode"), &OccluderPolygon2D::set_cull_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cull_mode"), &OccluderPolygon2D::get_cull_mode);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &OccluderPolygon2D::set_polygon);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_polygon"), &OccluderPolygon2D::get_polygon);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mode", PROPERTY_HINT_ENUM, "Disabled,ClockWise,CounterClockWise"), "set_cull_mode", "get_cull_mode");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-08-20 23:45:01 +08:00
|
|
|
BIND_ENUM_CONSTANT(CULL_DISABLED);
|
|
|
|
BIND_ENUM_CONSTANT(CULL_CLOCKWISE);
|
|
|
|
BIND_ENUM_CONSTANT(CULL_COUNTER_CLOCKWISE);
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OccluderPolygon2D::OccluderPolygon2D() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
occ_polygon = VS::get_singleton()->canvas_occluder_polygon_create();
|
|
|
|
closed = true;
|
|
|
|
cull = CULL_DISABLED;
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OccluderPolygon2D::~OccluderPolygon2D() {
|
|
|
|
|
|
|
|
VS::get_singleton()->free(occ_polygon);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
void LightOccluder2D::_poly_changed() {
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void LightOccluder2D::_notification(int p_what) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what == NOTIFICATION_ENTER_CANVAS) {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
VS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, get_canvas());
|
|
|
|
VS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
|
|
|
|
VS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
VS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
2016-01-01 21:45:24 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
VS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
|
2016-01-01 21:45:24 +08:00
|
|
|
}
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-08-19 07:02:56 +08:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
if (occluder_polygon.is_valid()) {
|
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
PoolVector<Vector2> poly = occluder_polygon->get_polygon();
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
if (poly.size()) {
|
|
|
|
if (occluder_polygon->is_closed()) {
|
|
|
|
Vector<Color> color;
|
2017-03-05 23:44:50 +08:00
|
|
|
color.push_back(Color(0, 0, 0, 0.6));
|
|
|
|
draw_polygon(Variant(poly), color);
|
2015-03-02 11:54:10 +08:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int ps = poly.size();
|
2017-01-08 05:25:37 +08:00
|
|
|
PoolVector<Vector2>::Read r = poly.read();
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < ps - 1; i++) {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
draw_line(r[i], r[i + 1], Color(0, 0, 0, 0.6), 3);
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what == NOTIFICATION_EXIT_CANVAS) {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
VS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, RID());
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void LightOccluder2D::set_occluder_polygon(const Ref<OccluderPolygon2D> &p_polygon) {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (occluder_polygon.is_valid())
|
2017-03-05 23:44:50 +08:00
|
|
|
occluder_polygon->disconnect("changed", this, "_poly_changed");
|
2015-03-02 11:54:10 +08:00
|
|
|
#endif
|
2017-03-05 23:44:50 +08:00
|
|
|
occluder_polygon = p_polygon;
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
if (occluder_polygon.is_valid())
|
2017-03-05 23:44:50 +08:00
|
|
|
VS::get_singleton()->canvas_light_occluder_set_polygon(occluder, occluder_polygon->get_rid());
|
2015-03-02 11:54:10 +08:00
|
|
|
else
|
2017-03-05 23:44:50 +08:00
|
|
|
VS::get_singleton()->canvas_light_occluder_set_polygon(occluder, RID());
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (occluder_polygon.is_valid())
|
2017-03-05 23:44:50 +08:00
|
|
|
occluder_polygon->connect("changed", this, "_poly_changed");
|
2015-03-02 11:54:10 +08:00
|
|
|
update();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<OccluderPolygon2D> LightOccluder2D::get_occluder_polygon() const {
|
|
|
|
|
|
|
|
return occluder_polygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LightOccluder2D::set_occluder_light_mask(int p_mask) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
mask = p_mask;
|
|
|
|
VS::get_singleton()->canvas_light_occluder_set_light_mask(occluder, mask);
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int LightOccluder2D::get_occluder_light_mask() const {
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2016-05-18 05:27:15 +08:00
|
|
|
String LightOccluder2D::get_configuration_warning() const {
|
|
|
|
|
|
|
|
if (!occluder_polygon.is_valid()) {
|
|
|
|
return TTR("An occluder polygon must be set (or drawn) for this occluder to take effect.");
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (occluder_polygon.is_valid() && occluder_polygon->get_polygon().size() == 0) {
|
2016-05-18 05:27:15 +08:00
|
|
|
return TTR("The occluder polygon for this occluder is empty. Please draw a polygon!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2015-03-02 11:54:10 +08:00
|
|
|
void LightOccluder2D::_bind_methods() {
|
|
|
|
|
2017-08-09 19:19:41 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_occluder_polygon", "polygon"), &LightOccluder2D::set_occluder_polygon);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_occluder_polygon"), &LightOccluder2D::get_occluder_polygon);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &LightOccluder2D::set_occluder_light_mask);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &LightOccluder2D::get_occluder_light_mask);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method("_poly_changed", &LightOccluder2D::_poly_changed);
|
2015-03-02 11:54:10 +08:00
|
|
|
#endif
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"), "set_occluder_polygon", "get_occluder_polygon");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LightOccluder2D::LightOccluder2D() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
occluder = VS::get_singleton()->canvas_light_occluder_create();
|
|
|
|
mask = 1;
|
2017-01-13 07:35:46 +08:00
|
|
|
set_notify_transform(true);
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LightOccluder2D::~LightOccluder2D() {
|
|
|
|
|
|
|
|
VS::get_singleton()->free(occluder);
|
|
|
|
}
|