canvas_layer.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*************************************************************************/
  2. /* canvas_layer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 Matrix32 &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. Matrix32 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();
  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 = p_viewport->cast_to<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();
  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::_bind_methods() {
  169. ObjectTypeDB::bind_method(_MD("set_layer", "layer"), &CanvasLayer::set_layer);
  170. ObjectTypeDB::bind_method(_MD("get_layer"), &CanvasLayer::get_layer);
  171. ObjectTypeDB::bind_method(_MD("set_transform", "transform"), &CanvasLayer::set_transform);
  172. ObjectTypeDB::bind_method(_MD("get_transform"), &CanvasLayer::get_transform);
  173. ObjectTypeDB::bind_method(_MD("set_offset", "offset"), &CanvasLayer::set_offset);
  174. ObjectTypeDB::bind_method(_MD("get_offset"), &CanvasLayer::get_offset);
  175. ObjectTypeDB::bind_method(_MD("set_rotation", "radians"), &CanvasLayer::set_rotation);
  176. ObjectTypeDB::bind_method(_MD("get_rotation"), &CanvasLayer::get_rotation);
  177. ObjectTypeDB::bind_method(_MD("set_rotationd", "degrees"), &CanvasLayer::set_rotationd);
  178. ObjectTypeDB::bind_method(_MD("get_rotationd"), &CanvasLayer::get_rotationd);
  179. // TODO: Obsolete those two methods (old name) properly (GH-4397)
  180. ObjectTypeDB::bind_method(_MD("_set_rotationd", "degrees"), &CanvasLayer::_set_rotationd);
  181. ObjectTypeDB::bind_method(_MD("_get_rotationd"), &CanvasLayer::_get_rotationd);
  182. ObjectTypeDB::bind_method(_MD("set_scale", "scale"), &CanvasLayer::set_scale);
  183. ObjectTypeDB::bind_method(_MD("get_scale"), &CanvasLayer::get_scale);
  184. ObjectTypeDB::bind_method(_MD("set_custom_viewport", "viewport:Viewport"), &CanvasLayer::set_custom_viewport);
  185. ObjectTypeDB::bind_method(_MD("get_custom_viewport:Viewport"), &CanvasLayer::get_custom_viewport);
  186. ObjectTypeDB::bind_method(_MD("get_world_2d:World2D"), &CanvasLayer::get_world_2d);
  187. // ObjectTypeDB::bind_method(_MD("get_viewport"),&CanvasLayer::get_viewport);
  188. ADD_PROPERTY(PropertyInfo(Variant::INT, "layer", PROPERTY_HINT_RANGE, "-128,128,1"), _SCS("set_layer"), _SCS("get_layer"));
  189. //ADD_PROPERTY( PropertyInfo(Variant::MATRIX32,"transform",PROPERTY_HINT_RANGE),_SCS("set_transform"),_SCS("get_transform") );
  190. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), _SCS("set_offset"), _SCS("get_offset"));
  191. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rotation"), _SCS("set_rotationd"), _SCS("get_rotationd"));
  192. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), _SCS("set_scale"), _SCS("get_scale"));
  193. }
  194. CanvasLayer::CanvasLayer() {
  195. vp = NULL;
  196. scale = Vector2(1, 1);
  197. rot = 0;
  198. locrotscale_dirty = false;
  199. layer = 1;
  200. canvas = Ref<World2D>(memnew(World2D));
  201. custom_viewport = NULL;
  202. custom_viewport_id = 0;
  203. }