gui_skinning.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. Theme types
  66. ~~~~~~~~~~~
  67. To help with the organization of its items each theme is separated into types,
  68. and each item must belong to a single type. In other words, each theme item
  69. is defined by its name, its data type and its theme type. This combination
  70. must be unique within the theme. For example, there cannot be two color items named
  71. ``font_color`` in a type called ``Label``, but there can be another ``font_color``
  72. item in a type ``LineEdit``.
  73. The default Godot theme comes with multiple theme types already defined,
  74. one for every built-in control node that uses UI skinning. The example above
  75. contains actual theme items present in the default theme. You can refer to the
  76. **Theme Properties** section in the class reference for each control to see
  77. which items are available to it and its child classes.
  78. .. note::
  79. Child classes can use theme items defined for their parent class (``Button``
  80. and its derivatives being a good example of that). In fact, every control can
  81. use every theme item of any theme type, if it needs to (but for the clarity and
  82. predictability we try to avoid that in the engine).
  83. It is important to remember that for child classes that process is automated.
  84. Whenever a built-in control requests a theme item from the theme it can omit
  85. the theme type, and its class name will be used instead. On top of that,
  86. the class names of its parent classes will also be used in turn. This allows
  87. changes to the parent class, such as ``Button``, to affect all derived
  88. classes without the need to customize every one of them.
  89. You can also define your own theme types, and additionally customize both built-in
  90. controls and your own controls. Because built-in controls have no knowledge of
  91. your custom theme types, you must utilize scripts to access those items. All control
  92. nodes have several methods that allow to fetch theme items from the theme that
  93. is applied to them. Those methods accept the theme type as one of the arguments.
  94. .. tabs::
  95. .. code-tab:: gdscript
  96. var accent_color = get_theme_color("accent_color", "MyType")
  97. label.add_color_override("font_color", accent_color)
  98. .. code-tab:: csharp
  99. Color accentColor = GetThemeColor("accent_color", "MyType");
  100. label.AddColorOverride("font_color", accentColor);
  101. To give more customization opportunities types can also be linked together as
  102. type variations. This is another use-case for custom theme types. For example,
  103. a theme can contain a type ``Header`` which can be marked as a variation of
  104. the base ``Label`` type. An individual ``Label`` control can then be set to
  105. use the ``Header`` variation for its type, and every time a theme item is
  106. requested from a theme this variation will be used before any other type. This
  107. allows to store various presets of theme items for the same class of the
  108. control node in the single ``Theme`` resource.
  109. .. warning::
  110. Only variations available from the default theme or defined in the custom
  111. project theme are shown in the Inspector dock as options. You can still
  112. input manually the name of a variation that is defined outside of those
  113. two places, but it is recommended to keep all variations to the project theme.
  114. You can learn more about creating and using theme type variations in a
  115. :ref:`dedicated article <doc_gui_theme_type_variations>`.
  116. Customizing a control
  117. ---------------------
  118. Each control node can be customized directly without the use of themes. This
  119. is called local overrides. Every theme property from the control's class
  120. reference can be overridden directly on the control itself, using either
  121. the Inspector dock, or scripts. This allows to make granular changes to a
  122. particular part of the UI, while not affecting anything else in the project,
  123. including this control's children.
  124. .. figure:: img/themecheck.png
  125. :align: center
  126. Local overrides are less useful for the visual flair of your user interface,
  127. especially if you aim for consistency. However, for layout nodes these are
  128. essential. Nodes such as :ref:`BoxContainer <class_BoxContainer>` and
  129. :ref:`GridContainer <class_GridContainer>` use theme constants for defining
  130. separation between their children, and :ref:`MarginContainer <class_MarginContainer>`
  131. stores its customizable margins in its theme items.
  132. Whenever a control has a local theme item override, this is the value that
  133. it uses. Values provided by the theme are ignored.
  134. .. _doc_gui_theme_in_project:
  135. Customizing a project
  136. ---------------------
  137. Out of the box each project adopts the default project theme provided by Godot. The
  138. default theme itself is constant and cannot be changed, but its items can be overridden
  139. with a custom theme. Custom themes can be applied in two ways: as a project setting,
  140. and as a node property throughout the tree of control nodes.
  141. There are two project settings that can be adjusted to affect your entire project:
  142. :ref:`gui/theme/custom<class_ProjectSettings_property_gui/theme/custom>` allows you to
  143. set a custom project-wide theme, and :ref:`gui/theme/custom_font<class_ProjectSettings_property_gui/theme/custom_font>`
  144. does the same to the default fallback font. When a theme item is requested by a control
  145. node the custom project theme, if present, is checked first. Only if it doesn't have
  146. the item the default theme is checked.
  147. This allows you to configure the default look of every Godot control with a single
  148. theme resource, but you can go more granular than that. Every control node also has
  149. a :ref:`theme <class_Control_property_theme>` property, which allows you to set a
  150. custom theme for the branch of nodes starting with that control. This means that the
  151. control and all of its children, and their children in turn, would first check that
  152. custom theme resource before falling back on the project and the default themes.
  153. .. note::
  154. Instead of changing the project setting you can set the custom theme resource to the
  155. root-most control node of your entire UI branch to almost the same effect. While in the
  156. running project it will behave as expected, individual scenes will still display
  157. using the default theme when previewing or running them directly. To fix that you
  158. can set the same theme resource to the root control of each individual scene.
  159. For example, you can have a certain style for buttons in your project theme, but want
  160. a different look for buttons inside of a popup dialog. You can set a custom theme
  161. resource to the root control of your popup and define a different style for buttons
  162. within that resource. As long as the chain of control nodes between the root of
  163. the popup and the buttons is uninterrupted, those buttons will use the styles defined
  164. in the theme resource that is closest to them. All other controls will still be styled
  165. using the project-wide theme and the default theme styles.
  166. To sum it up, for an arbitrary control its theme item lookup would look something
  167. like this:
  168. #. Check for local overrides of the same data type and name.
  169. #. Using control's type variation, class name and parent class names:
  170. a. Check every control starting from itself and see if it has a theme property set;
  171. b. If it does, check that theme for the matching item of the same name, data and theme type;
  172. c. If there is no custom theme or it doesn't have the item, move to the parent control;
  173. d. Repeat steps a-c. until the root of the tree is reached, or a non-control node is reached.
  174. #. Using control's type variation, class name and parent class names check the project-wide theme, if it's present.
  175. #. Using control's type variation, class name and parent class names check the default theme.
  176. Even if the item doesn't exist in any theme, a corresponding default value for that
  177. data type will be returned.
  178. Beyond controls
  179. ---------------
  180. Naturally, themes are an ideal type of resource for storing configuration for
  181. something visual. While the support for theming is built into control nodes,
  182. other nodes can use them as well, just like any other resource.
  183. An example of using themes for something beyond controls can be a modulation
  184. of sprites for the same units on different teams in a strategy game. A theme
  185. resource can define a collection of colors, and sprites (with a help from scripts)
  186. can use those colors to draw the texture. The main benefit being that you
  187. could make different themes using the same theme items for red, blue, and
  188. green teams, and swap them with a single resource change.