gui_tutorial.rst 7.3 KB

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