gui_tutorial.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. .. _doc_gui_tutorial:
  2. GUI tutorial
  3. ============
  4. Introduction
  5. ~~~~~~~~~~~~
  6. If there is something that most programmers hate with passion, it is
  7. programming graphical user interfaces (GUIs). It's boring, tedious and
  8. unchallenging. Several aspects make matters worse, such as:
  9. - Pixel alignment of UI elements is difficult (such that it looks just
  10. like the designer intends).
  11. - UIs are changed constantly due to design and usability issues that
  12. appear during testing.
  13. - Handling proper screen re-sizing for different display resolutions.
  14. - Animating several screen components, to make it look less static.
  15. GUI programming is one of the leading causes of programmer burnout.
  16. During the development of Godot (and previous engine iterations),
  17. several techniques and philosophies for UI development were put into
  18. practice, such as immediate mode, containers, anchors, scripting, etc.
  19. This was always done with the main goal of reducing the stress
  20. programmers had to face while putting together user interfaces.
  21. In the end, the resulting UI subsystem in Godot is an efficient solution
  22. to this problem, and works by mixing together a few different
  23. approaches. While the learning curve is a little steeper than in other
  24. toolkits, developers can put together complex user interfaces in very
  25. little time, by sharing the same set of tools with designers and
  26. animators.
  27. Control
  28. ~~~~~~~
  29. The basic node for UI elements is :ref:`Control <class_Control>`
  30. (sometimes called "Widget" or "Box" in other toolkits). Every node that
  31. provides user interface functionality descends from it.
  32. When controls are put in a scene tree as a child of another control,
  33. its coordinates (position, size) are always relative to the parent.
  34. This sets the basis for editing complex user interfaces quickly and
  35. visually.
  36. Input and drawing
  37. ~~~~~~~~~~~~~~~~~
  38. Controls receive input events by means of the
  39. :ref:`Control._input_event() <class_Control__input_event>`
  40. callback. Only one control, the one in focus, will receive
  41. keyboard/joypad events (see
  42. :ref:`Control.set_focus_mode() <class_Control_set_focus_mode>`
  43. and :ref:`Control.grab_focus() <class_Control_grab_focus>`).
  44. Mouse motion events are received by the control directly below the mouse
  45. pointer. When a control receives a mouse button pressed event, all
  46. subsequent motion events are received by the pressed control until that
  47. button is released, even if the pointer moves outside the control
  48. boundary.
  49. Like any class that inherits from :ref:`CanvasItem <class_CanvasItem>`
  50. (Control does), a :ref:`CanvasItem._draw() <class_CanvasItem__draw>`
  51. callback will be received at the beginning and every time the control
  52. needs to be redrawn (the programmer needs to call
  53. :ref:`CanvasItem.update() <class_CanvasItem_update>`
  54. to enqueue the CanvasItem for redraw). If the control is not visible
  55. (yet another CanvasItem property), the control does not receive any
  56. input.
  57. In general though, the programmer does not need to deal with drawing and
  58. input events directly when building UIs (that is more useful when
  59. creating custom controls). Instead, controls emit different kinds of
  60. signals with contextual information for when action occurs. For
  61. example, a :ref:`Button <class_Button>` emits
  62. a "pressed" signal when pressed, a :ref:`Slider <class_Slider>` will
  63. emit a "value_changed" when dragged, etc.
  64. Custom control mini tutorial
  65. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  66. Before going into more depth, creating a custom control will be a good
  67. way to get the picture on how controls works, as they are not as
  68. complex as it might seem.
  69. Additionally, even though Godot comes with dozens of controls for
  70. different purposes, it happens often that it's easier to attain a
  71. specific functionality by creating a new one.
  72. To begin, create a single-node scene. The node is of type "Control" and
  73. has a certain area of the screen in the 2D editor, like this:
  74. .. image:: /img/singlecontrol.png
  75. Add a script to that node, with the following code:
  76. ::
  77. extends Control
  78. var tapped=false
  79. func _draw():
  80. var r = Rect2( Vector2(), get_size() )
  81. if (tapped):
  82. draw_rect(r, Color(1,0,0) )
  83. else:
  84. draw_rect(r, Color(0,0,1) )
  85. func _input_event(ev):
  86. if (ev.type==InputEvent.MOUSE_BUTTON and ev.pressed):
  87. tapped=true
  88. update()
  89. Then run the scene. When the rectangle is clicked/tapped, it will change
  90. color from blue to red. That synergy between the events and the drawing
  91. is pretty much how most controls work internally.
  92. .. image:: /img/ctrl_normal.png
  93. .. image:: /img/ctrl_tapped.png
  94. UI complexity
  95. ~~~~~~~~~~~~~
  96. As mentioned before, Godot includes dozens of controls ready for use
  97. in a user interface. Such controls are divided in two categories. The
  98. first is a small set of controls that work well for creating most game
  99. user interfaces. The second (and most controls are of this type) are
  100. meant for complex user interfaces and uniform skinning through styles. A
  101. description is presented as follows to help understand which one should
  102. be used in which case.
  103. Simplified UI controls
  104. ~~~~~~~~~~~~~~~~~~~~~~
  105. This set of controls is enough for most games, where complex
  106. interactions or ways to present information are not necessary. They can
  107. be skinned easily with regular textures.
  108. - :ref:`Label <class_Label>`: Node used for showing text.
  109. - :ref:`TextureFrame <class_TextureFrame>`: Displays a single texture,
  110. which can be scaled or kept fixed.
  111. - :ref:`TextureButton <class_TextureButton>`: Displays a simple textured
  112. button, states such as pressed, hover, disabled, etc. can be set.
  113. - :ref:`TextureProgress <class_TextureProgress>`: Displays a single
  114. textured progress bar.
  115. Additionally, re-positioning of controls is most efficiently done with
  116. anchors in this case (see the :ref:`doc_size_and_anchors` tutorial for more
  117. information).
  118. In any case, it will happen often that even for simple games, more
  119. complex UI behaviors are required. An example of this is a scrolling
  120. list of elements (for a high score table, for example), which needs a
  121. :ref:`ScrollContainer <class_ScrollContainer>`
  122. and a :ref:`VBoxContainer <class_VBoxContainer>`.
  123. These kind of more advanced controls can be mixed with the regular ones
  124. seamlessly (they are all controls after all).
  125. Complex UI controls
  126. ~~~~~~~~~~~~~~~~~~~
  127. The rest of the controls (and there are dozens of them!) are meant for
  128. another set of scenarios, most commonly:
  129. - Games that require complex UIs, such as PC RPGs, MMOs, strategy,
  130. sims, etc.
  131. - Creating custom development tools to speed up content creation.
  132. - Creating Godot Editor Plugins, to extend the engine functionality.
  133. Re-positioning controls for these kind of interfaces is more commonly
  134. done with containers (see the :ref:`doc_size_and_anchors` tutorial for more
  135. information).