light_occluder_2d.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include "light_occluder_2d.h"
  31. #include "core/engine.h"
  32. #define LINE_GRAB_WIDTH 8
  33. #ifdef TOOLS_ENABLED
  34. Rect2 OccluderPolygon2D::_edit_get_rect() const {
  35. if (rect_cache_dirty) {
  36. if (closed) {
  37. PoolVector<Vector2>::Read r = polygon.read();
  38. item_rect = Rect2();
  39. for (int i = 0; i < polygon.size(); i++) {
  40. Vector2 pos = r[i];
  41. if (i == 0) {
  42. item_rect.position = pos;
  43. } else {
  44. item_rect.expand_to(pos);
  45. }
  46. }
  47. rect_cache_dirty = false;
  48. } else {
  49. if (polygon.size() == 0) {
  50. item_rect = Rect2();
  51. } else {
  52. Vector2 d = Vector2(LINE_GRAB_WIDTH, LINE_GRAB_WIDTH);
  53. item_rect = Rect2(polygon[0] - d, 2 * d);
  54. for (int i = 1; i < polygon.size(); i++) {
  55. item_rect.expand_to(polygon[i] - d);
  56. item_rect.expand_to(polygon[i] + d);
  57. }
  58. }
  59. }
  60. }
  61. return item_rect;
  62. }
  63. bool OccluderPolygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  64. if (closed) {
  65. return Geometry::is_point_in_polygon(p_point, Variant(polygon));
  66. } else {
  67. const real_t d = LINE_GRAB_WIDTH / 2 + p_tolerance;
  68. PoolVector<Vector2>::Read points = polygon.read();
  69. for (int i = 0; i < polygon.size() - 1; i++) {
  70. Vector2 p = Geometry::get_closest_point_to_segment_2d(p_point, &points[i]);
  71. if (p.distance_to(p_point) <= d) {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. }
  78. #endif
  79. void OccluderPolygon2D::set_polygon(const PoolVector<Vector2> &p_polygon) {
  80. polygon = p_polygon;
  81. rect_cache_dirty = true;
  82. VS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, p_polygon, closed);
  83. emit_changed();
  84. }
  85. PoolVector<Vector2> OccluderPolygon2D::get_polygon() const {
  86. return polygon;
  87. }
  88. void OccluderPolygon2D::set_closed(bool p_closed) {
  89. if (closed == p_closed) {
  90. return;
  91. }
  92. closed = p_closed;
  93. if (polygon.size()) {
  94. VS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, polygon, closed);
  95. }
  96. emit_changed();
  97. }
  98. bool OccluderPolygon2D::is_closed() const {
  99. return closed;
  100. }
  101. void OccluderPolygon2D::set_cull_mode(CullMode p_mode) {
  102. cull = p_mode;
  103. VS::get_singleton()->canvas_occluder_polygon_set_cull_mode(occ_polygon, VS::CanvasOccluderPolygonCullMode(p_mode));
  104. }
  105. OccluderPolygon2D::CullMode OccluderPolygon2D::get_cull_mode() const {
  106. return cull;
  107. }
  108. RID OccluderPolygon2D::get_rid() const {
  109. return occ_polygon;
  110. }
  111. void OccluderPolygon2D::_bind_methods() {
  112. ClassDB::bind_method(D_METHOD("set_closed", "closed"), &OccluderPolygon2D::set_closed);
  113. ClassDB::bind_method(D_METHOD("is_closed"), &OccluderPolygon2D::is_closed);
  114. ClassDB::bind_method(D_METHOD("set_cull_mode", "cull_mode"), &OccluderPolygon2D::set_cull_mode);
  115. ClassDB::bind_method(D_METHOD("get_cull_mode"), &OccluderPolygon2D::get_cull_mode);
  116. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &OccluderPolygon2D::set_polygon);
  117. ClassDB::bind_method(D_METHOD("get_polygon"), &OccluderPolygon2D::get_polygon);
  118. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
  119. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mode", PROPERTY_HINT_ENUM, "Disabled,ClockWise,CounterClockWise"), "set_cull_mode", "get_cull_mode");
  120. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  121. BIND_ENUM_CONSTANT(CULL_DISABLED);
  122. BIND_ENUM_CONSTANT(CULL_CLOCKWISE);
  123. BIND_ENUM_CONSTANT(CULL_COUNTER_CLOCKWISE);
  124. }
  125. OccluderPolygon2D::OccluderPolygon2D() {
  126. occ_polygon = RID_PRIME(VS::get_singleton()->canvas_occluder_polygon_create());
  127. closed = true;
  128. cull = CULL_DISABLED;
  129. rect_cache_dirty = true;
  130. }
  131. OccluderPolygon2D::~OccluderPolygon2D() {
  132. VS::get_singleton()->free(occ_polygon);
  133. }
  134. void LightOccluder2D::_poly_changed() {
  135. #ifdef DEBUG_ENABLED
  136. update();
  137. #endif
  138. }
  139. void LightOccluder2D::_notification(int p_what) {
  140. if (p_what == NOTIFICATION_ENTER_CANVAS) {
  141. VS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, get_canvas());
  142. VS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  143. VS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  144. }
  145. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  146. VS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  147. }
  148. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  149. VS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  150. }
  151. if (p_what == NOTIFICATION_DRAW) {
  152. if (Engine::get_singleton()->is_editor_hint()) {
  153. if (occluder_polygon.is_valid()) {
  154. PoolVector<Vector2> poly = occluder_polygon->get_polygon();
  155. if (poly.size()) {
  156. if (occluder_polygon->is_closed()) {
  157. Vector<Color> color;
  158. color.push_back(Color(0, 0, 0, 0.6));
  159. draw_polygon(Variant(poly), color);
  160. } else {
  161. int ps = poly.size();
  162. PoolVector<Vector2>::Read r = poly.read();
  163. for (int i = 0; i < ps - 1; i++) {
  164. draw_line(r[i], r[i + 1], Color(0, 0, 0, 0.6), 3);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. if (p_what == NOTIFICATION_EXIT_CANVAS) {
  172. VS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, RID());
  173. }
  174. }
  175. #ifdef TOOLS_ENABLED
  176. Rect2 LightOccluder2D::_edit_get_rect() const {
  177. return occluder_polygon.is_valid() ? occluder_polygon->_edit_get_rect() : Rect2();
  178. }
  179. bool LightOccluder2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  180. return occluder_polygon.is_valid() ? occluder_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;
  181. }
  182. #endif
  183. void LightOccluder2D::set_occluder_polygon(const Ref<OccluderPolygon2D> &p_polygon) {
  184. #ifdef DEBUG_ENABLED
  185. if (occluder_polygon.is_valid()) {
  186. occluder_polygon->disconnect("changed", this, "_poly_changed");
  187. }
  188. #endif
  189. occluder_polygon = p_polygon;
  190. if (occluder_polygon.is_valid()) {
  191. VS::get_singleton()->canvas_light_occluder_set_polygon(occluder, occluder_polygon->get_rid());
  192. } else {
  193. VS::get_singleton()->canvas_light_occluder_set_polygon(occluder, RID());
  194. }
  195. #ifdef DEBUG_ENABLED
  196. if (occluder_polygon.is_valid()) {
  197. occluder_polygon->connect("changed", this, "_poly_changed");
  198. }
  199. update();
  200. #endif
  201. }
  202. Ref<OccluderPolygon2D> LightOccluder2D::get_occluder_polygon() const {
  203. return occluder_polygon;
  204. }
  205. void LightOccluder2D::set_occluder_light_mask(int p_mask) {
  206. mask = p_mask;
  207. VS::get_singleton()->canvas_light_occluder_set_light_mask(occluder, mask);
  208. }
  209. int LightOccluder2D::get_occluder_light_mask() const {
  210. return mask;
  211. }
  212. String LightOccluder2D::get_configuration_warning() const {
  213. String warning = Node2D::get_configuration_warning();
  214. if (!occluder_polygon.is_valid()) {
  215. if (warning != String()) {
  216. warning += "\n\n";
  217. }
  218. warning += TTR("An occluder polygon must be set (or drawn) for this occluder to take effect.");
  219. }
  220. if (occluder_polygon.is_valid() && occluder_polygon->get_polygon().size() == 0) {
  221. if (warning != String()) {
  222. warning += "\n\n";
  223. }
  224. warning += TTR("The occluder polygon for this occluder is empty. Please draw a polygon.");
  225. }
  226. return warning;
  227. }
  228. void LightOccluder2D::_bind_methods() {
  229. ClassDB::bind_method(D_METHOD("set_occluder_polygon", "polygon"), &LightOccluder2D::set_occluder_polygon);
  230. ClassDB::bind_method(D_METHOD("get_occluder_polygon"), &LightOccluder2D::get_occluder_polygon);
  231. ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &LightOccluder2D::set_occluder_light_mask);
  232. ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &LightOccluder2D::get_occluder_light_mask);
  233. ClassDB::bind_method("_poly_changed", &LightOccluder2D::_poly_changed);
  234. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"), "set_occluder_polygon", "get_occluder_polygon");
  235. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
  236. }
  237. LightOccluder2D::LightOccluder2D() {
  238. occluder = RID_PRIME(VS::get_singleton()->canvas_light_occluder_create());
  239. mask = 1;
  240. set_notify_transform(true);
  241. }
  242. LightOccluder2D::~LightOccluder2D() {
  243. VS::get_singleton()->free(occluder);
  244. }