gui_skinning.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. .. _doc_gui_skinning:
  2. GUI skinning
  3. ============
  4. Oh, beautiful GUI!
  5. ------------------
  6. This tutorial is about advanced skinning of a user interface. Most
  7. games generally don't need this, as they end up just relying on
  8. :ref:`Label <class_Label>`, :ref:`TextureRect <class_TextureRect>`,
  9. :ref:`TextureButton <class_TextureButton>` and
  10. :ref:`TextureProgress <class_TextureProgress>`.
  11. However, many types of games often need complex user interfaces, like
  12. MMOs, traditional RPGs, Simulators, Strategy, etc. These kinds of
  13. interface are also common in some games that include editors to create
  14. content, or interfaces for network connectivity.
  15. Godot's user interface uses these kinds of control with the default theme,
  16. but they can be skinned to resemble pretty much any kind of user
  17. interface.
  18. Theme
  19. -----
  20. The GUI is skinned through the :ref:`Theme <class_Theme>`
  21. resource. Theme contains all the information required to change the
  22. entire visual styling of all controls. Theme options are named, so it's
  23. not obvious which name changes what (especially from code), but several
  24. tools are provided. The ultimate place to look at what each theme option
  25. is for each control, which will always be more up to date than any
  26. documentation, is the file `scene/resources/default_theme/default_theme.cpp
  27. <https://github.com/godotengine/godot/blob/master/scene/resources/default_theme/default_theme.cpp>`__.
  28. The rest of this document will explain the different tools used to
  29. customize the theme.
  30. A Theme can be applied to any control in the scene. As a result, all
  31. children and grand-children controls will use that same theme, too
  32. (unless another theme is specified further down the tree). If a value is
  33. not found in a theme, it will be searched in themes higher up in the
  34. hierarchy, towards the root. If nothing was found, the default theme is
  35. used. This system allows for flexible overriding of themes in complex
  36. user interfaces.
  37. .. attention::
  38. Don't use the custom theme option in the Project Settings, as there
  39. are known bugs with theme propagation. Instead, apply your theme to the
  40. root Control node's Theme property. It will propagate to instanced scenes
  41. automatically. To get correct theming in the editor for instanced scenes,
  42. you can apply the theme resource to the instanced scene's root node as well.
  43. Theme options
  44. -------------
  45. Each kind of option in a theme can be:
  46. - **An integer constant**: A single numerical constant. Generally used
  47. to define spacing between components or alignment.
  48. - **A Color**: A single color, with or without transparency. Colors are
  49. usually applied to fonts and icons.
  50. - **A Texture**: A single image. Textures are not often used, but when
  51. they are, they represent handles to pick or icons in a complex control
  52. (such as a file dialog).
  53. - **A Font**: Every control that uses text can be assigned the fonts
  54. used to draw strings.
  55. - **A StyleBox**: Stylebox is a resource that defines how to draw a
  56. panel in varying sizes (more information on them later).
  57. Every option is associated with:
  58. - A name (the name of the option)
  59. - A Control (the name of the control)
  60. An example usage:
  61. .. tabs::
  62. .. code-tab:: gdscript GDScript
  63. var theme = Theme.new()
  64. theme.set_color("font_color", "Label", Color.red)
  65. var label = Label.new()
  66. label.theme = theme
  67. .. code-tab:: csharp
  68. var theme = new Theme();
  69. theme.SetColor("fontColor", "Label", new Color(1.0f, 0.0f, 0.0f));
  70. var label = new Label();
  71. label.Theme = theme;
  72. In the example above, a new theme is created. The "font_color" option
  73. is changed and then applied to a label. Therefore, the label's text (and all
  74. children and grandchildren labels) will be red.
  75. It is possible to override those options without using the theme
  76. directly, and only for a specific control, by using the override API in
  77. :ref:`Control.add_color_override() <class_Control_method_add_color_override>`:
  78. .. tabs::
  79. .. code-tab:: gdscript GDScript
  80. var label = Label.new()
  81. label.add_color_override("font_color", Color.red)
  82. .. code-tab:: csharp
  83. var label = new Label();
  84. label.AddColorOverride("fontColor", new Color(1.0f, 0.0f, 0.0f));
  85. In the inline help of Godot (in the Script tab), you can check which theme options
  86. are overridable, or check the :ref:`Control <class_Control>` class reference.
  87. Customizing a control
  88. ---------------------
  89. If only a few controls need to be skinned, it is often not necessary to
  90. create a new theme. Controls offer their theme options as special kinds
  91. of properties. If checked, overriding will take place:
  92. .. image:: img/themecheck.png
  93. As can be seen in the image above, theme options have little check boxes.
  94. If checked, they can be used to override the value of the theme just for
  95. that control.
  96. Creating a theme
  97. ----------------
  98. The simplest way to create a theme is to edit a theme resource. Create a
  99. Theme from the resource menu; the editor will appear immediately.
  100. After that, save it (for example, with the name mytheme.theme):
  101. .. image:: img/sb2.png
  102. This will create an empty theme that can later be loaded and assigned to
  103. controls.
  104. Example: theming a button
  105. --------------------------
  106. Download these assets (:download:`skin_assets.zip <files/skin_assets.zip>`)
  107. and add them to your project. Open the theme editor, click on "Edit Theme"
  108. and select "Add Class Items":
  109. .. image:: img/themeci.png
  110. A menu will appear prompting the type of control to create. Select
  111. "Button":
  112. .. image:: img/themeci2.png
  113. Immediately, all button theme options will appear in the property
  114. editor, where they can be edited:
  115. .. image:: img/themeci3.png
  116. From ``Styles``, open the "Normal" drop-down menu next to where it probably
  117. says "null" and create a "New StyleBoxTexture", then
  118. edit it. A texture stylebox contains a texture and the size of the margins
  119. that will not stretch when the texture is stretched.
  120. This is called nine-patch or "3x3" stretching:
  121. .. image:: img/sb1.png
  122. Repeat the steps and add the other assets. There is no hover or disabled
  123. image in the example files, so use the same stylebox as in normal. Set
  124. the supplied font as the button font and change the font color to black.
  125. Soon, your button will look different and retro:
  126. .. image:: img/sb2.png
  127. Save this theme to the .theme file. Go to the 2D editor and create a few
  128. buttons:
  129. .. image:: img/skinbuttons1.png
  130. Now, go to the root node of the scene and locate the "theme" property,
  131. replace it with the theme that was just created. It should look like this:
  132. .. image:: img/skinbuttons2.png
  133. Congratulations! You have created a reusable GUI Theme!