portal.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*************************************************************************/
  2. /* portal.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "portal.h"
  31. #include "core/project_settings.h"
  32. #include "scene/resources/surface_tool.h"
  33. #include "servers/visual_server.h"
  34. // FIXME: This will be removed, kept as reference for new implementation
  35. #if 0
  36. bool Portal::_set(const StringName &p_name, const Variant &p_value) {
  37. if (p_name == "shape") {
  38. PoolVector<float> src_coords = p_value;
  39. Vector<Point2> points;
  40. int src_coords_size = src_coords.size();
  41. ERR_FAIL_COND_V(src_coords_size % 2, false);
  42. points.resize(src_coords_size / 2);
  43. for (int i = 0; i < points.size(); i++) {
  44. points[i].x = src_coords[i * 2 + 0];
  45. points[i].y = src_coords[i * 2 + 1];
  46. set_shape(points);
  47. }
  48. } else if (p_name == "enabled") {
  49. set_enabled(p_value);
  50. } else if (p_name == "disable_distance") {
  51. set_disable_distance(p_value);
  52. } else if (p_name == "disabled_color") {
  53. set_disabled_color(p_value);
  54. } else if (p_name == "connect_range") {
  55. set_connect_range(p_value);
  56. } else
  57. return false;
  58. return true;
  59. }
  60. bool Portal::_get(const StringName &p_name, Variant &r_ret) const {
  61. if (p_name == "shape") {
  62. Vector<Point2> points = get_shape();
  63. PoolVector<float> dst_coords;
  64. dst_coords.resize(points.size() * 2);
  65. for (int i = 0; i < points.size(); i++) {
  66. dst_coords.set(i * 2 + 0, points[i].x);
  67. dst_coords.set(i * 2 + 1, points[i].y);
  68. }
  69. r_ret = dst_coords;
  70. } else if (p_name == "enabled") {
  71. r_ret = is_enabled();
  72. } else if (p_name == "disable_distance") {
  73. r_ret = get_disable_distance();
  74. } else if (p_name == "disabled_color") {
  75. r_ret = get_disabled_color();
  76. } else if (p_name == "connect_range") {
  77. r_ret = get_connect_range();
  78. } else
  79. return false;
  80. return true;
  81. }
  82. void Portal::_get_property_list(List<PropertyInfo> *p_list) const {
  83. p_list->push_back(PropertyInfo(Variant::POOL_REAL_ARRAY, "shape"));
  84. p_list->push_back(PropertyInfo(Variant::BOOL, "enabled"));
  85. p_list->push_back(PropertyInfo(Variant::REAL, "disable_distance", PROPERTY_HINT_RANGE, "0,4096,0.01"));
  86. p_list->push_back(PropertyInfo(Variant::COLOR, "disabled_color"));
  87. p_list->push_back(PropertyInfo(Variant::REAL, "connect_range", PROPERTY_HINT_RANGE, "0.1,4096,0.01"));
  88. }
  89. AABB Portal::get_aabb() const {
  90. return aabb;
  91. }
  92. PoolVector<Face3> Portal::get_faces(uint32_t p_usage_flags) const {
  93. if (!(p_usage_flags & FACES_ENCLOSING))
  94. return PoolVector<Face3>();
  95. Vector<Point2> shape = get_shape();
  96. if (shape.size() == 0)
  97. return PoolVector<Face3>();
  98. Vector2 center;
  99. for (int i = 0; i < shape.size(); i++) {
  100. center += shape[i];
  101. }
  102. PoolVector<Face3> ret;
  103. center /= shape.size();
  104. for (int i = 0; i < shape.size(); i++) {
  105. int n = (i + 1) % shape.size();
  106. Face3 f;
  107. f.vertex[0] = Vector3(center.x, center.y, 0);
  108. f.vertex[1] = Vector3(shape[i].x, shape[i].y, 0);
  109. f.vertex[2] = Vector3(shape[n].x, shape[n].y, 0);
  110. ret.push_back(f);
  111. }
  112. return ret;
  113. }
  114. void Portal::set_shape(const Vector<Point2> &p_shape) {
  115. VisualServer::get_singleton()->portal_set_shape(portal, p_shape);
  116. shape = p_shape;
  117. update_gizmo();
  118. }
  119. Vector<Point2> Portal::get_shape() const {
  120. return shape;
  121. }
  122. void Portal::set_connect_range(float p_range) {
  123. connect_range = p_range;
  124. //VisualServer::get_singleton()->portal_set_connect_range(portal,p_range);
  125. }
  126. float Portal::get_connect_range() const {
  127. return connect_range;
  128. }
  129. void Portal::set_enabled(bool p_enabled) {
  130. enabled = p_enabled;
  131. VisualServer::get_singleton()->portal_set_enabled(portal, enabled);
  132. }
  133. bool Portal::is_enabled() const {
  134. return enabled;
  135. }
  136. void Portal::set_disable_distance(float p_distance) {
  137. disable_distance = p_distance;
  138. VisualServer::get_singleton()->portal_set_disable_distance(portal, disable_distance);
  139. }
  140. float Portal::get_disable_distance() const {
  141. return disable_distance;
  142. }
  143. void Portal::set_disabled_color(const Color &p_disabled_color) {
  144. disabled_color = p_disabled_color;
  145. VisualServer::get_singleton()->portal_set_disabled_color(portal, disabled_color);
  146. }
  147. Color Portal::get_disabled_color() const {
  148. return disabled_color;
  149. }
  150. void Portal::_bind_methods() {
  151. ClassDB::bind_method(D_METHOD("set_shape", "points"), &Portal::set_shape);
  152. ClassDB::bind_method(D_METHOD("get_shape"), &Portal::get_shape);
  153. ClassDB::bind_method(D_METHOD("set_enabled", "enable"), &Portal::set_enabled);
  154. ClassDB::bind_method(D_METHOD("is_enabled"), &Portal::is_enabled);
  155. ClassDB::bind_method(D_METHOD("set_disable_distance", "distance"), &Portal::set_disable_distance);
  156. ClassDB::bind_method(D_METHOD("get_disable_distance"), &Portal::get_disable_distance);
  157. ClassDB::bind_method(D_METHOD("set_disabled_color", "color"), &Portal::set_disabled_color);
  158. ClassDB::bind_method(D_METHOD("get_disabled_color"), &Portal::get_disabled_color);
  159. ClassDB::bind_method(D_METHOD("set_connect_range", "range"), &Portal::set_connect_range);
  160. ClassDB::bind_method(D_METHOD("get_connect_range"), &Portal::get_connect_range);
  161. }
  162. Portal::Portal() {
  163. portal = VisualServer::get_singleton()->portal_create();
  164. Vector<Point2> points;
  165. points.push_back(Point2(-1, 1));
  166. points.push_back(Point2(1, 1));
  167. points.push_back(Point2(1, -1));
  168. points.push_back(Point2(-1, -1));
  169. set_shape(points); // default shape
  170. set_connect_range(0.8);
  171. set_disable_distance(50);
  172. set_enabled(true);
  173. set_base(portal);
  174. }
  175. Portal::~Portal() {
  176. VisualServer::get_singleton()->free(portal);
  177. }
  178. #endif