canvas_layer.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*************************************************************************/
  2. /* canvas_layer.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 "canvas_layer.h"
  31. #include "viewport.h"
  32. void CanvasLayer::set_layer(int p_xform) {
  33. layer = p_xform;
  34. if (viewport.is_valid())
  35. VisualServer::get_singleton()->viewport_set_canvas_layer(viewport, canvas->get_canvas(), layer);
  36. }
  37. int CanvasLayer::get_layer() const {
  38. return layer;
  39. }
  40. void CanvasLayer::set_transform(const Transform2D &p_xform) {
  41. transform = p_xform;
  42. locrotscale_dirty = true;
  43. if (viewport.is_valid())
  44. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas->get_canvas(), transform);
  45. }
  46. Transform2D CanvasLayer::get_transform() const {
  47. return transform;
  48. }
  49. void CanvasLayer::_update_xform() {
  50. transform.set_rotation_and_scale(rot, scale);
  51. transform.set_origin(ofs);
  52. if (viewport.is_valid())
  53. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas->get_canvas(), transform);
  54. }
  55. void CanvasLayer::_update_locrotscale() {
  56. ofs = transform.elements[2];
  57. rot = transform.get_rotation();
  58. scale = transform.get_scale();
  59. locrotscale_dirty = false;
  60. }
  61. void CanvasLayer::set_offset(const Vector2 &p_offset) {
  62. if (locrotscale_dirty)
  63. _update_locrotscale();
  64. ofs = p_offset;
  65. _update_xform();
  66. }
  67. Vector2 CanvasLayer::get_offset() const {
  68. if (locrotscale_dirty)
  69. const_cast<CanvasLayer *>(this)->_update_locrotscale();
  70. return ofs;
  71. }
  72. void CanvasLayer::set_rotation(real_t p_radians) {
  73. if (locrotscale_dirty)
  74. _update_locrotscale();
  75. rot = p_radians;
  76. _update_xform();
  77. }
  78. real_t CanvasLayer::get_rotation() const {
  79. if (locrotscale_dirty)
  80. const_cast<CanvasLayer *>(this)->_update_locrotscale();
  81. return rot;
  82. }
  83. void CanvasLayer::set_rotationd(real_t p_degrees) {
  84. set_rotation(Math::deg2rad(p_degrees));
  85. }
  86. real_t CanvasLayer::get_rotationd() const {
  87. return Math::rad2deg(get_rotation());
  88. }
  89. // Kept for compatibility after rename to {s,g}et_rotationd.
  90. // Could be removed after a couple releases.
  91. void CanvasLayer::_set_rotationd(real_t p_degrees) {
  92. WARN_PRINT("Deprecated method CanvasLayer._set_rotationd(): This method was renamed to set_rotationd. Please adapt your code accordingly, as the old method will be obsoleted.");
  93. set_rotationd(p_degrees);
  94. }
  95. real_t CanvasLayer::_get_rotationd() const {
  96. WARN_PRINT("Deprecated method CanvasLayer._get_rotationd(): This method was renamed to get_rotationd. Please adapt your code accordingly, as the old method will be obsoleted.");
  97. return get_rotationd();
  98. }
  99. void CanvasLayer::set_scale(const Vector2 &p_scale) {
  100. if (locrotscale_dirty)
  101. _update_locrotscale();
  102. scale = p_scale;
  103. _update_xform();
  104. }
  105. Vector2 CanvasLayer::get_scale() const {
  106. if (locrotscale_dirty)
  107. const_cast<CanvasLayer *>(this)->_update_locrotscale();
  108. return scale;
  109. }
  110. Ref<World2D> CanvasLayer::get_world_2d() const {
  111. return canvas;
  112. }
  113. void CanvasLayer::_notification(int p_what) {
  114. switch (p_what) {
  115. case NOTIFICATION_ENTER_TREE: {
  116. if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
  117. vp = custom_viewport;
  118. } else {
  119. vp = Node::get_viewport();
  120. }
  121. ERR_FAIL_COND(!vp);
  122. viewport = vp->get_viewport_rid();
  123. VisualServer::get_singleton()->viewport_attach_canvas(viewport, canvas->get_canvas());
  124. VisualServer::get_singleton()->viewport_set_canvas_layer(viewport, canvas->get_canvas(), layer);
  125. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas->get_canvas(), transform);
  126. } break;
  127. case NOTIFICATION_EXIT_TREE: {
  128. VisualServer::get_singleton()->viewport_remove_canvas(viewport, canvas->get_canvas());
  129. viewport = RID();
  130. } break;
  131. }
  132. }
  133. Size2 CanvasLayer::get_viewport_size() const {
  134. if (!is_inside_tree())
  135. return Size2(1, 1);
  136. Rect2 r = vp->get_visible_rect();
  137. return r.size;
  138. }
  139. RID CanvasLayer::get_viewport() const {
  140. return viewport;
  141. }
  142. void CanvasLayer::set_custom_viewport(Node *p_viewport) {
  143. ERR_FAIL_NULL(p_viewport);
  144. if (is_inside_tree()) {
  145. VisualServer::get_singleton()->viewport_remove_canvas(viewport, canvas->get_canvas());
  146. viewport = RID();
  147. }
  148. custom_viewport = Object::cast_to<Viewport>(p_viewport);
  149. if (custom_viewport) {
  150. custom_viewport_id = custom_viewport->get_instance_id();
  151. } else {
  152. custom_viewport_id = 0;
  153. }
  154. if (is_inside_tree()) {
  155. if (custom_viewport)
  156. vp = custom_viewport;
  157. else
  158. vp = Node::get_viewport();
  159. viewport = vp->get_viewport_rid();
  160. VisualServer::get_singleton()->viewport_attach_canvas(viewport, canvas->get_canvas());
  161. VisualServer::get_singleton()->viewport_set_canvas_layer(viewport, canvas->get_canvas(), layer);
  162. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, canvas->get_canvas(), transform);
  163. }
  164. }
  165. Node *CanvasLayer::get_custom_viewport() const {
  166. return custom_viewport;
  167. }
  168. void CanvasLayer::reset_sort_index() {
  169. sort_index = 0;
  170. }
  171. int CanvasLayer::get_sort_index() {
  172. return sort_index++;
  173. }
  174. void CanvasLayer::_bind_methods() {
  175. ClassDB::bind_method(D_METHOD("set_layer", "layer"), &CanvasLayer::set_layer);
  176. ClassDB::bind_method(D_METHOD("get_layer"), &CanvasLayer::get_layer);
  177. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CanvasLayer::set_transform);
  178. ClassDB::bind_method(D_METHOD("get_transform"), &CanvasLayer::get_transform);
  179. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &CanvasLayer::set_offset);
  180. ClassDB::bind_method(D_METHOD("get_offset"), &CanvasLayer::get_offset);
  181. ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &CanvasLayer::set_rotation);
  182. ClassDB::bind_method(D_METHOD("get_rotation"), &CanvasLayer::get_rotation);
  183. ClassDB::bind_method(D_METHOD("set_rotationd", "degrees"), &CanvasLayer::set_rotationd);
  184. ClassDB::bind_method(D_METHOD("get_rotationd"), &CanvasLayer::get_rotationd);
  185. // TODO: Obsolete those two methods (old name) properly (GH-4397)
  186. ClassDB::bind_method(D_METHOD("_set_rotationd", "degrees"), &CanvasLayer::_set_rotationd);
  187. ClassDB::bind_method(D_METHOD("_get_rotationd"), &CanvasLayer::_get_rotationd);
  188. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &CanvasLayer::set_scale);
  189. ClassDB::bind_method(D_METHOD("get_scale"), &CanvasLayer::get_scale);
  190. ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &CanvasLayer::set_custom_viewport);
  191. ClassDB::bind_method(D_METHOD("get_custom_viewport"), &CanvasLayer::get_custom_viewport);
  192. ClassDB::bind_method(D_METHOD("get_world_2d"), &CanvasLayer::get_world_2d);
  193. //ClassDB::bind_method(D_METHOD("get_viewport"),&CanvasLayer::get_viewport);
  194. ADD_PROPERTY(PropertyInfo(Variant::INT, "layer", PROPERTY_HINT_RANGE, "-128,128,1"), "set_layer", "get_layer");
  195. //ADD_PROPERTY( PropertyInfo(Variant::MATRIX32,"transform",PROPERTY_HINT_RANGE),"set_transform","get_transform") ;
  196. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  197. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rotation"), "set_rotationd", "get_rotationd");
  198. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
  199. }
  200. CanvasLayer::CanvasLayer() {
  201. vp = NULL;
  202. scale = Vector2(1, 1);
  203. rot = 0;
  204. locrotscale_dirty = false;
  205. layer = 1;
  206. canvas = Ref<World2D>(memnew(World2D));
  207. custom_viewport = NULL;
  208. custom_viewport_id = 0;
  209. sort_index = 0;
  210. }