canvas_layer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**************************************************************************/
  2. /* canvas_layer.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 "canvas_layer.h"
  31. #include "scene/main/canvas_item.h"
  32. #include "scene/main/viewport.h"
  33. #include "scene/resources/world_2d.h"
  34. void CanvasLayer::set_layer(int p_xform) {
  35. layer = p_xform;
  36. if (viewport.is_valid()) {
  37. RenderingServer::get_singleton()->viewport_set_canvas_stacking(viewport, canvas, layer, get_index());
  38. vp->gui_set_root_order_dirty();
  39. }
  40. }
  41. int CanvasLayer::get_layer() const {
  42. return layer;
  43. }
  44. void CanvasLayer::set_visible(bool p_visible) {
  45. if (p_visible == visible) {
  46. return;
  47. }
  48. visible = p_visible;
  49. emit_signal(SceneStringName(visibility_changed));
  50. for (int i = 0; i < get_child_count(); i++) {
  51. CanvasItem *c = Object::cast_to<CanvasItem>(get_child(i));
  52. if (c) {
  53. RenderingServer::get_singleton()->canvas_item_set_visible(c->get_canvas_item(), p_visible && c->is_visible());
  54. c->_propagate_visibility_changed(p_visible);
  55. }
  56. }
  57. }
  58. void CanvasLayer::show() {
  59. set_visible(true);
  60. }
  61. void CanvasLayer::hide() {
  62. set_visible(false);
  63. }
  64. bool CanvasLayer::is_visible() const {
  65. return visible;
  66. }
  67. void CanvasLayer::set_transform(const Transform2D &p_xform) {
  68. transform = p_xform;
  69. locrotscale_dirty = true;
  70. if (viewport.is_valid()) {
  71. RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
  72. }
  73. }
  74. Transform2D CanvasLayer::get_transform() const {
  75. return transform;
  76. }
  77. Transform2D CanvasLayer::get_final_transform() const {
  78. if (is_following_viewport()) {
  79. Transform2D follow;
  80. follow.scale(Vector2(get_follow_viewport_scale(), get_follow_viewport_scale()));
  81. if (vp) {
  82. follow = vp->get_canvas_transform() * follow;
  83. }
  84. return follow * transform;
  85. }
  86. return transform;
  87. }
  88. void CanvasLayer::_update_xform() {
  89. transform.set_rotation_and_scale(rot, scale);
  90. transform.set_origin(ofs);
  91. if (viewport.is_valid()) {
  92. RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
  93. }
  94. }
  95. void CanvasLayer::_update_locrotscale() {
  96. ofs = transform.columns[2];
  97. rot = transform.get_rotation();
  98. scale = transform.get_scale();
  99. locrotscale_dirty = false;
  100. }
  101. void CanvasLayer::set_offset(const Vector2 &p_offset) {
  102. if (locrotscale_dirty) {
  103. _update_locrotscale();
  104. }
  105. ofs = p_offset;
  106. _update_xform();
  107. }
  108. Vector2 CanvasLayer::get_offset() const {
  109. if (locrotscale_dirty) {
  110. const_cast<CanvasLayer *>(this)->_update_locrotscale();
  111. }
  112. return ofs;
  113. }
  114. void CanvasLayer::set_rotation(real_t p_radians) {
  115. if (locrotscale_dirty) {
  116. _update_locrotscale();
  117. }
  118. rot = p_radians;
  119. _update_xform();
  120. }
  121. real_t CanvasLayer::get_rotation() const {
  122. if (locrotscale_dirty) {
  123. const_cast<CanvasLayer *>(this)->_update_locrotscale();
  124. }
  125. return rot;
  126. }
  127. void CanvasLayer::set_scale(const Vector2 &p_scale) {
  128. if (locrotscale_dirty) {
  129. _update_locrotscale();
  130. }
  131. scale = p_scale;
  132. _update_xform();
  133. }
  134. Vector2 CanvasLayer::get_scale() const {
  135. if (locrotscale_dirty) {
  136. const_cast<CanvasLayer *>(this)->_update_locrotscale();
  137. }
  138. return scale;
  139. }
  140. void CanvasLayer::_notification(int p_what) {
  141. switch (p_what) {
  142. case NOTIFICATION_ENTER_TREE: {
  143. if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
  144. vp = custom_viewport;
  145. } else {
  146. vp = Node::get_viewport();
  147. }
  148. ERR_FAIL_NULL_MSG(vp, "Viewport is not initialized.");
  149. vp->_canvas_layer_add(this);
  150. viewport = vp->get_viewport_rid();
  151. RenderingServer::get_singleton()->viewport_attach_canvas(viewport, canvas);
  152. RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
  153. _update_follow_viewport();
  154. if (vp) {
  155. get_parent()->connect(SNAME("child_order_changed"), callable_mp(vp, &Viewport::canvas_parent_mark_dirty).bind(get_parent()), CONNECT_REFERENCE_COUNTED);
  156. vp->canvas_parent_mark_dirty(get_parent());
  157. }
  158. } break;
  159. case NOTIFICATION_EXIT_TREE: {
  160. ERR_FAIL_NULL_MSG(vp, "Viewport is not initialized.");
  161. get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(vp, &Viewport::canvas_parent_mark_dirty).bind(get_parent()));
  162. vp->_canvas_layer_remove(this);
  163. RenderingServer::get_singleton()->viewport_remove_canvas(viewport, canvas);
  164. viewport = RID();
  165. _update_follow_viewport(false);
  166. } break;
  167. }
  168. }
  169. void CanvasLayer::update_draw_order() {
  170. if (is_inside_tree()) {
  171. RenderingServer::get_singleton()->viewport_set_canvas_stacking(viewport, canvas, layer, get_index());
  172. }
  173. }
  174. Size2 CanvasLayer::get_viewport_size() const {
  175. if (!is_inside_tree()) {
  176. return Size2(1, 1);
  177. }
  178. ERR_FAIL_NULL_V_MSG(vp, Size2(1, 1), "Viewport is not initialized.");
  179. Rect2 r = vp->get_visible_rect();
  180. return r.size;
  181. }
  182. RID CanvasLayer::get_viewport() const {
  183. return viewport;
  184. }
  185. void CanvasLayer::set_custom_viewport(Node *p_viewport) {
  186. ERR_FAIL_NULL_MSG(p_viewport, "Cannot set viewport to nullptr.");
  187. if (is_inside_tree()) {
  188. vp->_canvas_layer_remove(this);
  189. RenderingServer::get_singleton()->viewport_remove_canvas(viewport, canvas);
  190. viewport = RID();
  191. }
  192. custom_viewport = Object::cast_to<Viewport>(p_viewport);
  193. if (custom_viewport) {
  194. custom_viewport_id = custom_viewport->get_instance_id();
  195. } else {
  196. custom_viewport_id = ObjectID();
  197. }
  198. if (is_inside_tree()) {
  199. if (custom_viewport) {
  200. vp = custom_viewport;
  201. } else {
  202. vp = Node::get_viewport();
  203. }
  204. vp->_canvas_layer_add(this);
  205. viewport = vp->get_viewport_rid();
  206. RenderingServer::get_singleton()->viewport_attach_canvas(viewport, canvas);
  207. RenderingServer::get_singleton()->viewport_set_canvas_stacking(viewport, canvas, layer, get_index());
  208. RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas, transform);
  209. }
  210. }
  211. Node *CanvasLayer::get_custom_viewport() const {
  212. return custom_viewport;
  213. }
  214. void CanvasLayer::reset_sort_index() {
  215. sort_index = 0;
  216. }
  217. int CanvasLayer::get_sort_index() {
  218. return sort_index++;
  219. }
  220. RID CanvasLayer::get_canvas() const {
  221. return canvas;
  222. }
  223. void CanvasLayer::set_follow_viewport(bool p_enable) {
  224. if (follow_viewport == p_enable) {
  225. return;
  226. }
  227. follow_viewport = p_enable;
  228. _update_follow_viewport();
  229. notify_property_list_changed();
  230. }
  231. bool CanvasLayer::is_following_viewport() const {
  232. return follow_viewport;
  233. }
  234. void CanvasLayer::set_follow_viewport_scale(float p_ratio) {
  235. follow_viewport_scale = p_ratio;
  236. _update_follow_viewport();
  237. }
  238. float CanvasLayer::get_follow_viewport_scale() const {
  239. return follow_viewport_scale;
  240. }
  241. void CanvasLayer::_update_follow_viewport(bool p_force_exit) {
  242. if (!is_inside_tree()) {
  243. return;
  244. }
  245. if (p_force_exit || !follow_viewport) {
  246. RS::get_singleton()->canvas_set_parent(canvas, RID(), 1.0);
  247. } else {
  248. RS::get_singleton()->canvas_set_parent(canvas, vp->get_world_2d()->get_canvas(), follow_viewport_scale);
  249. }
  250. }
  251. void CanvasLayer::_validate_property(PropertyInfo &p_property) const {
  252. if (!follow_viewport && p_property.name == "follow_viewport_scale") {
  253. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  254. }
  255. }
  256. void CanvasLayer::_bind_methods() {
  257. ClassDB::bind_method(D_METHOD("set_layer", "layer"), &CanvasLayer::set_layer);
  258. ClassDB::bind_method(D_METHOD("get_layer"), &CanvasLayer::get_layer);
  259. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &CanvasLayer::set_visible);
  260. ClassDB::bind_method(D_METHOD("is_visible"), &CanvasLayer::is_visible);
  261. ClassDB::bind_method(D_METHOD("show"), &CanvasLayer::show);
  262. ClassDB::bind_method(D_METHOD("hide"), &CanvasLayer::hide);
  263. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CanvasLayer::set_transform);
  264. ClassDB::bind_method(D_METHOD("get_transform"), &CanvasLayer::get_transform);
  265. ClassDB::bind_method(D_METHOD("get_final_transform"), &CanvasLayer::get_final_transform);
  266. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &CanvasLayer::set_offset);
  267. ClassDB::bind_method(D_METHOD("get_offset"), &CanvasLayer::get_offset);
  268. ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &CanvasLayer::set_rotation);
  269. ClassDB::bind_method(D_METHOD("get_rotation"), &CanvasLayer::get_rotation);
  270. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &CanvasLayer::set_scale);
  271. ClassDB::bind_method(D_METHOD("get_scale"), &CanvasLayer::get_scale);
  272. ClassDB::bind_method(D_METHOD("set_follow_viewport", "enable"), &CanvasLayer::set_follow_viewport);
  273. ClassDB::bind_method(D_METHOD("is_following_viewport"), &CanvasLayer::is_following_viewport);
  274. ClassDB::bind_method(D_METHOD("set_follow_viewport_scale", "scale"), &CanvasLayer::set_follow_viewport_scale);
  275. ClassDB::bind_method(D_METHOD("get_follow_viewport_scale"), &CanvasLayer::get_follow_viewport_scale);
  276. ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &CanvasLayer::set_custom_viewport);
  277. ClassDB::bind_method(D_METHOD("get_custom_viewport"), &CanvasLayer::get_custom_viewport);
  278. ClassDB::bind_method(D_METHOD("get_canvas"), &CanvasLayer::get_canvas);
  279. ADD_GROUP("Layer", "");
  280. ADD_PROPERTY(PropertyInfo(Variant::INT, "layer", PROPERTY_HINT_RANGE, "-128,128,1"), "set_layer", "get_layer");
  281. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  282. ADD_GROUP("Transform", "");
  283. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
  284. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_RANGE, "-1080,1080,0.1,or_less,or_greater,radians_as_degrees"), "set_rotation", "get_rotation");
  285. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale", PROPERTY_HINT_LINK), "set_scale", "get_scale");
  286. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform", PROPERTY_HINT_NONE, "suffix:px"), "set_transform", "get_transform");
  287. ADD_GROUP("", "");
  288. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", PROPERTY_USAGE_NONE), "set_custom_viewport", "get_custom_viewport");
  289. ADD_GROUP("Follow Viewport", "follow_viewport");
  290. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_viewport_enabled"), "set_follow_viewport", "is_following_viewport");
  291. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "follow_viewport_scale", PROPERTY_HINT_RANGE, "0.001,1000,0.001,or_greater,or_less"), "set_follow_viewport_scale", "get_follow_viewport_scale");
  292. ADD_SIGNAL(MethodInfo("visibility_changed"));
  293. }
  294. CanvasLayer::CanvasLayer() {
  295. canvas = RS::get_singleton()->canvas_create();
  296. }
  297. CanvasLayer::~CanvasLayer() {
  298. ERR_FAIL_NULL(RenderingServer::get_singleton());
  299. RS::get_singleton()->free(canvas);
  300. }