size_and_anchors.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. :article_outdated: True
  2. .. _doc_size_and_anchors:
  3. Size and anchors
  4. ================
  5. If a game was always going to be run on the same device and at the same
  6. resolution, positioning controls would be a simple matter of setting the
  7. position and size of each one of them. Unfortunately, that is rarely the
  8. case.
  9. Only TVs nowadays have a standard resolution and aspect ratio.
  10. Everything else, from computer monitors to tablets, portable consoles
  11. and mobile phones have different resolutions and aspect ratios.
  12. There are several ways to handle 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 *margin* properties of controls. Each
  18. control has four margins: left, right, bottom, and top, which correspond
  19. to the respective edges of the control. By default, all of
  20. them represent a distance in pixels relative to the top-left corner of
  21. the parent control or (in case there is no parent control) the viewport.
  22. .. image:: img/margin.png
  23. So to make the control wider you can make the right margin larger and/or
  24. make the left margin smaller. This lets you set the exact placement
  25. and shape of the control.
  26. The *anchor* properties adjust where the margin distances are relative *to*.
  27. Each margin has an individual anchor that can be adjusted from the
  28. beginning to the end of the parent. So the vertical (top, bottom) anchors
  29. adjust from 0 (top of parent) to 1.0 (bottom of parent) with 0.5 being
  30. the center, and the control margins will be placed relative to that
  31. point. The horizontal (left, right) anchors similarly adjust from left to
  32. right of the parent.
  33. Note that when you wish the edge of a control to be above or left of the
  34. anchor point, you must change the margin value to be negative.
  35. For example: when horizontal anchors are changed to 1, the margin values
  36. become relative to the top-right corner of the parent control or viewport.
  37. .. image:: img/marginend.png
  38. Adjusting the two horizontal or the two vertical anchors to different
  39. values will make the control change size when the parent control does.
  40. Here, the control is set to anchor its bottom-right corner to the
  41. parent's bottom-right, while the top-left control margins are still
  42. anchored to the top-left of the parent, so when re-sizing the parent,
  43. the control will always cover it, leaving a 20 pixel margin:
  44. .. image:: img/marginaround.png
  45. Centering a control
  46. -------------------
  47. To center a control in its parent, set its anchors to 0.5 and each margin
  48. to half of its relevant dimension. For example, the code below shows how
  49. a TextureRect can be centered in its parent:
  50. .. tabs::
  51. .. code-tab:: gdscript GDScript
  52. var rect = TextureRect.new()
  53. rect.texture = load("res://icon.png")
  54. rect.anchor_left = 0.5
  55. rect.anchor_right = 0.5
  56. rect.anchor_top = 0.5
  57. rect.anchor_bottom = 0.5
  58. var texture_size = rect.texture.get_size()
  59. rect.offset_left = -texture_size.x / 2
  60. rect.offset_right = texture_size.x / 2
  61. rect.offset_top = -texture_size.y / 2
  62. rect.offset_bottom = texture_size.y / 2
  63. add_child(rect)
  64. .. code-tab:: csharp
  65. var rect = new TextureRect();
  66. rect.Texture = ResourceLoader.Load<Texture>("res://icon.png");
  67. rect.AnchorLeft = 0.5f;
  68. rect.AnchorRight = 0.5f;
  69. rect.AnchorTop = 0.5f;
  70. rect.AnchorBottom = 0.5f;
  71. var textureSize = rect.Texture.GetSize();
  72. rect.OffsetLeft = -textureSize.X / 2;
  73. rect.OffsetRight = textureSize.X / 2;
  74. rect.OffsetTop = -textureSize.Y / 2;
  75. rect.OffsetBottom = textureSize.Y / 2;
  76. AddChild(rect);
  77. Setting each anchor to 0.5 moves the reference point for the margins to
  78. the center of its parent. From there, we set negative margins so that
  79. the control gets its natural size.
  80. Layout Presets
  81. --------------
  82. Instead of manually adjusting the margin and anchor values, you can use the
  83. toolbar's Layout menu, above the viewport. Besides centering, it gives you many
  84. options to align and resize control nodes.
  85. .. image:: img/layout_dropdown_menu.png