size_and_anchors.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. .. _doc_size_and_anchors:
  2. Size and anchors
  3. ================
  4. If a game was always going to be run on the same device and at the same
  5. resolution, positioning controls would be a simple matter of setting the
  6. position and size of each one of them. Unfortunately, that is rarely the
  7. case.
  8. While some configurations may be more common than others, devices like
  9. phones, tablets and portable gaming consoles can vary greatly. Therefore,
  10. we often have to account for different aspect ratios, resolutions and user
  11. scaling.
  12. There are several ways to account for this, but for now, let's just imagine
  13. that the screen resolution has changed and the controls need to be
  14. re-positioned. Some will need to follow the bottom of the screen, others
  15. the top of the screen, or maybe the right or left margins.
  16. .. image:: img/anchors.png
  17. This is done by editing the *anchor offsets* of controls, which behave similar
  18. to a margin. To access these settings, you will first need to select the *Custom*
  19. anchor preset.
  20. Each control has four anchor offsets: left, right, bottom, and top, which correspond
  21. to the respective edges of the control. By default, all of
  22. them represent a distance in pixels relative to the top-left corner of
  23. the parent control or (in case there is no parent control) the viewport.
  24. .. image:: img/offset.webp
  25. So to make the control wider you can make the right offset larger and/or
  26. make the left offset smaller. This lets you set the exact placement
  27. and shape of the control.
  28. The *anchor* properties adjust where the offsets are relative *to*.
  29. Each offset has an individual anchor that can be adjusted from the
  30. beginning to the end of the parent. So the vertical (top, bottom) anchors
  31. adjust from ``0.0`` (top of parent) to ``1.0`` (bottom of parent) with ``0.5`` being
  32. the center, and the control offsets will be placed relative to that
  33. point. The horizontal (left, right) anchors similarly adjust from left to
  34. right of the parent.
  35. Note that when you wish the edge of a control to be above or left of the
  36. anchor point, you must change the offset value to be negative.
  37. For example: when horizontal anchors are changed to ``1.0``, the offset values
  38. become relative to the top-right corner of the parent control or viewport.
  39. .. image:: img/offset_end.webp
  40. Adjusting the two horizontal or the two vertical anchors to different
  41. values will make the control change size when the parent control does.
  42. Here, the control is set to anchor its bottom-right corner to the
  43. parent's bottom-right, while the top-left control offsets are still
  44. anchored to the top-left of the parent, so when re-sizing the parent,
  45. the control will always cover it, leaving a 20 pixel offset:
  46. .. image:: img/offset_around.webp
  47. Centering a control
  48. -------------------
  49. To center a control in its parent, set its anchors to ``0.5`` and each offset
  50. to half of its relevant dimension. For example, the code below shows how
  51. a TextureRect can be centered in its parent:
  52. .. tabs::
  53. .. code-tab:: gdscript GDScript
  54. var rect = TextureRect.new()
  55. rect.texture = load("res://icon.png")
  56. rect.anchor_left = 0.5
  57. rect.anchor_right = 0.5
  58. rect.anchor_top = 0.5
  59. rect.anchor_bottom = 0.5
  60. var texture_size = rect.texture.get_size()
  61. rect.offset_left = -texture_size.x / 2
  62. rect.offset_right = texture_size.x / 2
  63. rect.offset_top = -texture_size.y / 2
  64. rect.offset_bottom = texture_size.y / 2
  65. add_child(rect)
  66. .. code-tab:: csharp
  67. var rect = new TextureRect();
  68. rect.Texture = ResourceLoader.Load<Texture>("res://icon.png");
  69. rect.AnchorLeft = 0.5f;
  70. rect.AnchorRight = 0.5f;
  71. rect.AnchorTop = 0.5f;
  72. rect.AnchorBottom = 0.5f;
  73. var textureSize = rect.Texture.GetSize();
  74. rect.OffsetLeft = -textureSize.X / 2;
  75. rect.OffsetRight = textureSize.X / 2;
  76. rect.OffsetTop = -textureSize.Y / 2;
  77. rect.OffsetBottom = textureSize.Y / 2;
  78. AddChild(rect);
  79. Setting each anchor to ``0.5`` moves the reference point for the offsets to
  80. the center of its parent. From there, we set negative offsets so that
  81. the control gets its natural size.
  82. Anchor Presets
  83. --------------
  84. Instead of manually adjusting the offset and anchor values, you can use the
  85. toolbar's Anchor menu, above the viewport. Besides centering, it gives you many
  86. options to align and resize control nodes.
  87. .. image:: img/anchor_presets.webp