light_occluder_2d.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*************************************************************************/
  2. /* light_occluder_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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 "engine.h"
  32. void OccluderPolygon2D::set_polygon(const PoolVector<Vector2> &p_polygon) {
  33. polygon = p_polygon;
  34. VS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, p_polygon, closed);
  35. emit_changed();
  36. }
  37. PoolVector<Vector2> OccluderPolygon2D::get_polygon() const {
  38. return polygon;
  39. }
  40. void OccluderPolygon2D::set_closed(bool p_closed) {
  41. if (closed == p_closed)
  42. return;
  43. closed = p_closed;
  44. if (polygon.size())
  45. VS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, polygon, closed);
  46. emit_changed();
  47. }
  48. bool OccluderPolygon2D::is_closed() const {
  49. return closed;
  50. }
  51. void OccluderPolygon2D::set_cull_mode(CullMode p_mode) {
  52. cull = p_mode;
  53. VS::get_singleton()->canvas_occluder_polygon_set_cull_mode(occ_polygon, VS::CanvasOccluderPolygonCullMode(p_mode));
  54. }
  55. OccluderPolygon2D::CullMode OccluderPolygon2D::get_cull_mode() const {
  56. return cull;
  57. }
  58. RID OccluderPolygon2D::get_rid() const {
  59. return occ_polygon;
  60. }
  61. void OccluderPolygon2D::_bind_methods() {
  62. ClassDB::bind_method(D_METHOD("set_closed", "closed"), &OccluderPolygon2D::set_closed);
  63. ClassDB::bind_method(D_METHOD("is_closed"), &OccluderPolygon2D::is_closed);
  64. ClassDB::bind_method(D_METHOD("set_cull_mode", "cull_mode"), &OccluderPolygon2D::set_cull_mode);
  65. ClassDB::bind_method(D_METHOD("get_cull_mode"), &OccluderPolygon2D::get_cull_mode);
  66. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &OccluderPolygon2D::set_polygon);
  67. ClassDB::bind_method(D_METHOD("get_polygon"), &OccluderPolygon2D::get_polygon);
  68. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
  69. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mode", PROPERTY_HINT_ENUM, "Disabled,ClockWise,CounterClockWise"), "set_cull_mode", "get_cull_mode");
  70. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  71. BIND_ENUM_CONSTANT(CULL_DISABLED);
  72. BIND_ENUM_CONSTANT(CULL_CLOCKWISE);
  73. BIND_ENUM_CONSTANT(CULL_COUNTER_CLOCKWISE);
  74. }
  75. OccluderPolygon2D::OccluderPolygon2D() {
  76. occ_polygon = VS::get_singleton()->canvas_occluder_polygon_create();
  77. closed = true;
  78. cull = CULL_DISABLED;
  79. }
  80. OccluderPolygon2D::~OccluderPolygon2D() {
  81. VS::get_singleton()->free(occ_polygon);
  82. }
  83. #ifdef DEBUG_ENABLED
  84. void LightOccluder2D::_poly_changed() {
  85. update();
  86. }
  87. #endif
  88. void LightOccluder2D::_notification(int p_what) {
  89. if (p_what == NOTIFICATION_ENTER_CANVAS) {
  90. VS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, get_canvas());
  91. VS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  92. VS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  93. }
  94. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  95. VS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  96. }
  97. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  98. VS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  99. }
  100. if (p_what == NOTIFICATION_DRAW) {
  101. if (Engine::get_singleton()->is_editor_hint()) {
  102. if (occluder_polygon.is_valid()) {
  103. PoolVector<Vector2> poly = occluder_polygon->get_polygon();
  104. if (poly.size()) {
  105. if (occluder_polygon->is_closed()) {
  106. Vector<Color> color;
  107. color.push_back(Color(0, 0, 0, 0.6));
  108. draw_polygon(Variant(poly), color);
  109. } else {
  110. int ps = poly.size();
  111. PoolVector<Vector2>::Read r = poly.read();
  112. for (int i = 0; i < ps - 1; i++) {
  113. draw_line(r[i], r[i + 1], Color(0, 0, 0, 0.6), 3);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. if (p_what == NOTIFICATION_EXIT_CANVAS) {
  121. VS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, RID());
  122. }
  123. }
  124. void LightOccluder2D::set_occluder_polygon(const Ref<OccluderPolygon2D> &p_polygon) {
  125. #ifdef DEBUG_ENABLED
  126. if (occluder_polygon.is_valid())
  127. occluder_polygon->disconnect("changed", this, "_poly_changed");
  128. #endif
  129. occluder_polygon = p_polygon;
  130. if (occluder_polygon.is_valid())
  131. VS::get_singleton()->canvas_light_occluder_set_polygon(occluder, occluder_polygon->get_rid());
  132. else
  133. VS::get_singleton()->canvas_light_occluder_set_polygon(occluder, RID());
  134. #ifdef DEBUG_ENABLED
  135. if (occluder_polygon.is_valid())
  136. occluder_polygon->connect("changed", this, "_poly_changed");
  137. update();
  138. #endif
  139. }
  140. Ref<OccluderPolygon2D> LightOccluder2D::get_occluder_polygon() const {
  141. return occluder_polygon;
  142. }
  143. void LightOccluder2D::set_occluder_light_mask(int p_mask) {
  144. mask = p_mask;
  145. VS::get_singleton()->canvas_light_occluder_set_light_mask(occluder, mask);
  146. }
  147. int LightOccluder2D::get_occluder_light_mask() const {
  148. return mask;
  149. }
  150. String LightOccluder2D::get_configuration_warning() const {
  151. if (!occluder_polygon.is_valid()) {
  152. return TTR("An occluder polygon must be set (or drawn) for this occluder to take effect.");
  153. }
  154. if (occluder_polygon.is_valid() && occluder_polygon->get_polygon().size() == 0) {
  155. return TTR("The occluder polygon for this occluder is empty. Please draw a polygon!");
  156. }
  157. return String();
  158. }
  159. void LightOccluder2D::_bind_methods() {
  160. ClassDB::bind_method(D_METHOD("set_occluder_polygon", "polygon"), &LightOccluder2D::set_occluder_polygon);
  161. ClassDB::bind_method(D_METHOD("get_occluder_polygon"), &LightOccluder2D::get_occluder_polygon);
  162. ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &LightOccluder2D::set_occluder_light_mask);
  163. ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &LightOccluder2D::get_occluder_light_mask);
  164. #ifdef DEBUG_ENABLED
  165. ClassDB::bind_method("_poly_changed", &LightOccluder2D::_poly_changed);
  166. #endif
  167. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"), "set_occluder_polygon", "get_occluder_polygon");
  168. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
  169. }
  170. LightOccluder2D::LightOccluder2D() {
  171. occluder = VS::get_singleton()->canvas_light_occluder_create();
  172. mask = 1;
  173. set_notify_transform(true);
  174. }
  175. LightOccluder2D::~LightOccluder2D() {
  176. VS::get_singleton()->free(occluder);
  177. }