2d_transforms.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. .. _doc_viewport_and_canvas_transforms:
  2. Viewport and canvas transforms
  3. ==============================
  4. Introduction
  5. ------------
  6. This tutorial is created after a topic that is a little dark for most
  7. users and explains all the 2D transforms going on for nodes from the
  8. moment they draw their content locally to the time they are drawn into
  9. the screen.
  10. Canvas transform
  11. ----------------
  12. As mentioned in the previous tutorial, :ref:`doc_canvas_layers`, every
  13. CanvasItem node (remember that Node2D and Control based nodes use
  14. CanvasItem as their common root) will reside in a *Canvas Layer*. Every
  15. canvas layer has a transform (translation, rotation, scale, etc.) that
  16. can be accessed as a :ref:`Transform2D <class_Transform2D>`.
  17. Also covered in the previous tutorial, nodes are drawn by default in Layer 0,
  18. in the built-in canvas. To put nodes in a different layer, a :ref:`CanvasLayer
  19. <class_CanvasLayer>` node can be used.
  20. Global canvas transform
  21. -----------------------
  22. Viewports also have a Global Canvas transform (also a
  23. :ref:`Transform2D <class_Transform2D>`). This is the master transform and
  24. affects all individual *Canvas Layer* transforms. Generally this
  25. transform is not of much use, but is used in the CanvasItem Editor
  26. in Godot's editor.
  27. Stretch transform
  28. -----------------
  29. Finally, viewports have a *Stretch Transform*, which is used when
  30. resizing or stretching the screen. This transform is used internally (as
  31. described in :ref:`doc_multiple_resolutions`) but can also be manually set
  32. on each viewport.
  33. Input events received in the :ref:`MainLoop._input_event() <class_MainLoop__input_event>`
  34. callback are multiplied by this transform but lack the ones above. To
  35. convert InputEvent coordinates to local CanvasItem coordinates, the
  36. :ref:`CanvasItem.make_input_local() <class_CanvasItem_make_input_local>`
  37. function was added for convenience.
  38. Transform order
  39. ---------------
  40. For a coordinate in CanvasItem local properties to become an actual
  41. screen coordinate, the following chain of transforms must be applied:
  42. .. image:: img/viewport_transforms2.png
  43. Transform functions
  44. -------------------
  45. Obtaining each transform can be achieved with the following functions:
  46. +----------------------------------+--------------------------------------------------------------------------------------+
  47. | Type | Transform |
  48. +==================================+======================================================================================+
  49. | CanvasItem | :ref:`CanvasItem.get_global_transform() <class_CanvasItem_get_global_transform>` |
  50. +----------------------------------+--------------------------------------------------------------------------------------+
  51. | CanvasLayer | :ref:`CanvasItem.get_canvas_transform() <class_CanvasItem_get_canvas_transform>` |
  52. +----------------------------------+--------------------------------------------------------------------------------------+
  53. | CanvasLayer+GlobalCanvas+Stretch | :ref:`CanvasItem.get_viewport_transform() <class_CanvasItem_get_viewport_transform>` |
  54. +----------------------------------+--------------------------------------------------------------------------------------+
  55. Finally, then, to convert a CanvasItem local coordinates to screen
  56. coordinates, just multiply in the following order:
  57. .. tabs::
  58. .. code-tab:: gdscript GDScript
  59. var screen_coord = get_viewport_transform() * ( get_global_transform() * local_pos )
  60. .. code-tab:: csharp
  61. var screenCord = (GetViewportTransform() * GetGlobalTransform()).Xform(localPos);
  62. Keep in mind, however, that it is generally not desired to work with
  63. screen coordinates. The recommended approach is to simply work in Canvas
  64. coordinates (``CanvasItem.get_global_transform()``), to allow automatic
  65. screen resolution resizing to work properly.
  66. Feeding custom input events
  67. ---------------------------
  68. It is often desired to feed custom input events to the scene tree. With
  69. the above knowledge, to correctly do this, it must be done the following
  70. way:
  71. .. tabs::
  72. .. code-tab:: gdscript GDScript
  73. var local_pos = Vector2(10, 20) # local to Control/Node2D
  74. var ie = InputEventMouseButton.new()
  75. ie.button_index = BUTTON_LEFT
  76. ie.position = get_viewport_transform() * (get_global_transform() * local_pos)
  77. get_tree().input_event(ie)
  78. .. code-tab:: csharp
  79. var localPos = new Vector2(10,20); // local to Control/Node2D
  80. var ie = new InputEventMouseButton();
  81. ie.ButtonIndex = (int)ButtonList.Left;
  82. ie.Position = (GetViewportTransform() * GetGlobalTransform()).Xform(localPos);
  83. GetTree().InputEvent(ie);