gui_skinning.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. .. _doc_gui_skinning:
  2. Introduction to GUI skinning
  3. ============================
  4. It is essential for a game to provide clear, informative, and yet visually
  5. pleasing user interface to its players. While :ref:`Control <class_Control>`
  6. nodes come with a decently functional look out of the box, there is always
  7. room for uniqueness and case-specific tuning. For this purpose Godot engine
  8. includes a system for GUI skinning (or theming), which allows you to customize
  9. the look of every control in your user interface, including your custom controls.
  10. Here is an example of this system in action — a game with the GUI that is
  11. radically different from the default UI theme of the engine:
  12. .. figure:: img/tank-kings-by-winterpixel-games.png
  13. :align: center
  14. A "Gear Up!" screen in Tank Kings, courtesy of Winterpixel Games
  15. Beyond achieving a unique look for your game, this system also enables developers
  16. to provide customization options to the end users, including accessibility settings.
  17. UI themes are applied in a cascading manner (i.e. they propagate from parent
  18. controls to their children), which means that font settings or adjustments for
  19. colorblind users can be applied in a single place and affect the entire UI tree.
  20. Of course this system can also be used for gameplay purposes: your hero-based game
  21. can change its style for the selected player character, or you can give different
  22. flavors to the sides in your team-based project.
  23. Basics of themes
  24. ----------------
  25. The skinning system is driven by the :ref:`Theme <class_Theme>` resource. Every
  26. Godot project has an inherent default theme that contains the settings used by
  27. the built-in control nodes. This is what gives the controls their distinct look
  28. out of the box. A theme only describes the configuration, however, and it is still
  29. the job of each individual control to use that configuration in the way it requires
  30. to display itself. This is important to remember when implementing
  31. :ref:`your own custom controls <doc_custom_gui_controls>`.
  32. .. note::
  33. Even the Godot editor itself relies on the default theme. But it doesn't look the
  34. same as a Godot project, because it applies its own heavily customized theme on top
  35. of the default one. In principle, this works exactly like it would in your game
  36. as explained :ref:`below <doc_gui_theme_in_project>`.
  37. Theme items
  38. ~~~~~~~~~~~
  39. The configuration that is stored in a theme consists of theme items. Each item has
  40. a unique name and must be one of the following data types:
  41. - **Color**
  42. A :ref:`color <class_Color>` value, which is often used for fonts
  43. and backgrounds. Colors can also be used for modulation of controls
  44. and icons.
  45. - **Constant**
  46. An integer value, which can be used either for numeric properties of
  47. controls (such as the item separation in a :ref:`BoxContainer <class_BoxContainer>`),
  48. or for boolean flags (such as the drawing of relationship lines in a :ref:`Tree <class_Tree>`).
  49. - **Font**
  50. A :ref:`font <class_Font>` resource, which is used by controls that
  51. display text. Fonts contain most text rendering settings, except for
  52. its size and color. On top of that, alignment and text direction are
  53. controlled by individual controls.
  54. - **Font size**
  55. An integer value, which is used alongside a font to determine the
  56. size at which the text should be displayed.
  57. - **Icon**
  58. A :ref:`texture <class_Texture2D>` resource, which is normally used
  59. to display an icon (on a :ref:`Button <class_Button>`, for example).
  60. - **StyleBox**
  61. A :ref:`StyleBox <class_StyleBox>` resource, a collection of configuration
  62. options which define the way a UI panel should be displayed. This is
  63. not limited to the :ref:`Panel <class_Panel>` control, as styleboxes
  64. are used by many controls for their backgrounds and overlays.
  65. Different controls will apply StyleBoxes in a different manner. Most notably,
  66. ``focus`` styleboxes are drawn as an *overlay* to other styleboxes (such as
  67. ``normal`` or ``pressed``) to allow the base stylebox to remain visible.
  68. This means the focus stylebox should be designed as an outline or translucent
  69. box, so that its background can remain visible.
  70. Theme types
  71. ~~~~~~~~~~~
  72. To help with the organization of its items each theme is separated into types,
  73. and each item must belong to a single type. In other words, each theme item
  74. is defined by its name, its data type and its theme type. This combination
  75. must be unique within the theme. For example, there cannot be two color items named
  76. ``font_color`` in a type called ``Label``, but there can be another ``font_color``
  77. item in a type ``LineEdit``.
  78. The default Godot theme comes with multiple theme types already defined,
  79. one for every built-in control node that uses UI skinning. The example above
  80. contains actual theme items present in the default theme. You can refer to the
  81. **Theme Properties** section in the class reference for each control to see
  82. which items are available to it and its child classes.
  83. .. note::
  84. Child classes can use theme items defined for their parent class (``Button``
  85. and its derivatives being a good example of that). In fact, every control can
  86. use every theme item of any theme type, if it needs to (but for the clarity and
  87. predictability we try to avoid that in the engine).
  88. It is important to remember that for child classes that process is automated.
  89. Whenever a built-in control requests a theme item from the theme it can omit
  90. the theme type, and its class name will be used instead. On top of that,
  91. the class names of its parent classes will also be used in turn. This allows
  92. changes to the parent class, such as ``Button``, to affect all derived
  93. classes without the need to customize every one of them.
  94. You can also define your own theme types, and additionally customize both built-in
  95. controls and your own controls. Because built-in controls have no knowledge of
  96. your custom theme types, you must utilize scripts to access those items. All control
  97. nodes have several methods that allow to fetch theme items from the theme that
  98. is applied to them. Those methods accept the theme type as one of the arguments.
  99. .. tabs::
  100. .. code-tab:: gdscript
  101. var accent_color = get_theme_color("accent_color", "MyType")
  102. label.add_theme_color_override("font_color", accent_color)
  103. .. code-tab:: csharp
  104. Color accentColor = GetThemeColor("accent_color", "MyType");
  105. label.AddThemeColorOverride("font_color", accentColor);
  106. To give more customization opportunities types can also be linked together as
  107. type variations. This is another use-case for custom theme types. For example,
  108. a theme can contain a type ``Header`` which can be marked as a variation of
  109. the base ``Label`` type. An individual ``Label`` control can then be set to
  110. use the ``Header`` variation for its type, and every time a theme item is
  111. requested from a theme this variation will be used before any other type. This
  112. allows to store various presets of theme items for the same class of the
  113. control node in the single ``Theme`` resource.
  114. .. warning::
  115. Only variations available from the default theme or defined in the custom
  116. project theme are shown in the Inspector dock as options. You can still
  117. input manually the name of a variation that is defined outside of those
  118. two places, but it is recommended to keep all variations to the project theme.
  119. You can learn more about creating and using theme type variations in a
  120. :ref:`dedicated article <doc_gui_theme_type_variations>`.
  121. Customizing a control
  122. ---------------------
  123. Each control node can be customized directly without the use of themes. This
  124. is called local overrides. Every theme property from the control's class
  125. reference can be overridden directly on the control itself, using either
  126. the Inspector dock, or scripts. This allows to make granular changes to a
  127. particular part of the UI, while not affecting anything else in the project,
  128. including this control's children.
  129. .. figure:: img/themecheck.png
  130. :align: center
  131. Local overrides are less useful for the visual flair of your user interface,
  132. especially if you aim for consistency. However, for layout nodes these are
  133. essential. Nodes such as :ref:`BoxContainer <class_BoxContainer>` and
  134. :ref:`GridContainer <class_GridContainer>` use theme constants for defining
  135. separation between their children, and :ref:`MarginContainer <class_MarginContainer>`
  136. stores its customizable margins in its theme items.
  137. Whenever a control has a local theme item override, this is the value that
  138. it uses. Values provided by the theme are ignored.
  139. .. _doc_gui_theme_in_project:
  140. Customizing a project
  141. ---------------------
  142. Out of the box each project adopts the default project theme provided by Godot. The
  143. default theme itself is constant and cannot be changed, but its items can be overridden
  144. with a custom theme. Custom themes can be applied in two ways: as a project setting,
  145. and as a node property throughout the tree of control nodes.
  146. There are two project settings that can be adjusted to affect your entire project:
  147. :ref:`GUI > Theme > Custom<class_ProjectSettings_property_gui/theme/custom>` allows you to
  148. set a custom project-wide theme, and :ref:`GUI > Theme > Custom Font<class_ProjectSettings_property_gui/theme/custom_font>`
  149. does the same to the default fallback font. When a theme item is requested by a control
  150. node the custom project theme, if present, is checked first. Only if it doesn't have
  151. the item the default theme is checked.
  152. This allows you to configure the default look of every Godot control with a single
  153. theme resource, but you can go more granular than that. Every control node also has
  154. a :ref:`theme <class_Control_property_theme>` property, which allows you to set a
  155. custom theme for the branch of nodes starting with that control. This means that the
  156. control and all of its children, and their children in turn, would first check that
  157. custom theme resource before falling back on the project and the default themes.
  158. .. note::
  159. Instead of changing the project setting you can set the custom theme resource to the
  160. root-most control node of your entire UI branch to almost the same effect. While in the
  161. running project it will behave as expected, individual scenes will still display
  162. using the default theme when previewing or running them directly. To fix that you
  163. can set the same theme resource to the root control of each individual scene.
  164. For example, you can have a certain style for buttons in your project theme, but want
  165. a different look for buttons inside of a popup dialog. You can set a custom theme
  166. resource to the root control of your popup and define a different style for buttons
  167. within that resource. As long as the chain of control nodes between the root of
  168. the popup and the buttons is uninterrupted, those buttons will use the styles defined
  169. in the theme resource that is closest to them. All other controls will still be styled
  170. using the project-wide theme and the default theme styles.
  171. To sum it up, for an arbitrary control its theme item lookup would look something
  172. like this:
  173. #. Check for local overrides of the same data type and name.
  174. #. Using control's type variation, class name and parent class names:
  175. a. Check every control starting from itself and see if it has a theme property set;
  176. b. If it does, check that theme for the matching item of the same name, data and theme type;
  177. c. If there is no custom theme or it doesn't have the item, move to the parent control;
  178. d. Repeat steps a-c. until the root of the tree is reached, or a non-control node is reached.
  179. #. Using control's type variation, class name and parent class names check the project-wide theme, if it's present.
  180. #. Using control's type variation, class name and parent class names check the default theme.
  181. Even if the item doesn't exist in any theme, a corresponding default value for that
  182. data type will be returned.
  183. Beyond controls
  184. ---------------
  185. Naturally, themes are an ideal type of resource for storing configuration for
  186. something visual. While the support for theming is built into control nodes,
  187. other nodes can use them as well, just like any other resource.
  188. An example of using themes for something beyond controls can be a modulation
  189. of sprites for the same units on different teams in a strategy game. A theme
  190. resource can define a collection of colors, and sprites (with a help from scripts)
  191. can use those colors to draw the texture. The main benefit being that you
  192. could make different themes using the same theme items for red, blue, and
  193. green teams, and swap them with a single resource change.