size_and_anchors.rst 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Only TVs nowadays have a standard resolution and aspect ratio.
  9. Everything else, from computer monitors to tablets, portable consoles
  10. and mobile phones have different resolutions and aspect ratios.
  11. There are several ways to handle this, but for now, let's just imagine
  12. that the screen resolution has changed and the controls need to be
  13. re-positioned. Some will need to follow the bottom of the screen, others
  14. the top of the screen, or maybe the right or left margins.
  15. .. image:: img/anchors.png
  16. This is done by editing the *margin* properties of controls. Each
  17. control has four margins: left, right, bottom, and top, which correspond
  18. to the respective edges of the control. By default, all of
  19. them represent a distance in pixels relative to the top-left corner of
  20. the parent control or (in case there is no parent control) the viewport.
  21. .. image:: img/margin.png
  22. So to make the control wider you can make the right margin larger and/or
  23. make the left margin smaller. This lets you set the exact placement
  24. and shape of the control.
  25. The *anchor* properties adjust where the margin distances are relative *to*.
  26. Each margin has an individual anchor that can be adjusted from the
  27. beginning to the end of the parent. So the vertical (top, bottom) anchors
  28. adjust from 0 (top of parent) to 1.0 (bottom of parent) with 0.5 being
  29. the center, and the control margins will be placed relative to that
  30. point. The horizontal (left, right) anchors similarly adjust from left to
  31. right of the parent.
  32. Note that when you wish the edge of a control to be above or left of the
  33. anchor point, you must change the margin value to be negative.
  34. For example: when horizontal anchors are changed to 1, the margin values
  35. become relative to the top-right corner of the parent control or viewport.
  36. .. image:: img/marginend.png
  37. Adjusting the two horizontal or the two vertical anchors to different
  38. values will make the control change size when the parent control does.
  39. Here, the control is set to anchor its bottom-right corner to the
  40. parent's bottom-right, while the top-left control margins are still
  41. anchored to the top-left of the parent, so when re-sizing the parent,
  42. the control will always cover it, leaving a 20 pixel margin:
  43. .. image:: img/marginaround.png
  44. Centering a control
  45. -------------------
  46. To center a control in its parent, set its anchors to 0.5 and each margin
  47. to half of its relevant dimension. For example, the code below shows how
  48. a TextureRect can be centered in its parent:
  49. ::
  50. var rect = TextureRect.new()
  51. rect.texture = load("res://icon.png")
  52. rect.anchor_left = 0.5
  53. rect.anchor_right = 0.5
  54. rect.anchor_top = 0.5
  55. rect.anchor_bottom = 0.5
  56. var texture_size = rect.texture.get_size()
  57. rect.margin_left = -texture_size.x / 2
  58. rect.margin_right = -texture_size.x / 2
  59. rect.margin_top = -texture_size.y / 2
  60. rect.margin_bottom = -texture_size.y / 2
  61. add_child(rect)
  62. Setting each anchor to 0.5 moves the reference point for the margins to
  63. the center of its parent. From there, we set negative margins so that
  64. the control gets its natural size.
  65. Layout Presets
  66. --------------
  67. Instead of manually adjusting the margin and anchor values, you can use the
  68. toolbar's Layout menu, above the viewport. Besides centering, it gives you many
  69. options to align and resize control nodes.
  70. .. image:: img/layout_dropdown_menu.png