gui_skinning.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. .. _doc_gui_skinning:
  2. GUI skinning
  3. ============
  4. Themes
  5. ------
  6. All control nodes are skinned through the :ref:`Theme <class_Theme>`
  7. resource. Theme contains all the information required to change the
  8. entire visual styling of all controls.
  9. A Theme can be applied to any control node in the scene. As a result,
  10. all children and grand-children controls will use that same theme, too
  11. (unless another theme is specified further down the tree). If a value is
  12. not found in a theme, it will be searched in themes higher up in the
  13. hierarchy, towards the root. If nothing was found, the default theme is
  14. used. This system allows for flexible overriding of themes in complex
  15. user interfaces.
  16. .. attention::
  17. Don't use the custom theme option in the Project Settings, as there
  18. are known bugs with theme propagation. Instead, apply your theme to the
  19. root Control node's Theme property. It will propagate to instanced scenes
  20. automatically. To get correct theming in the editor for instanced scenes,
  21. you can apply the theme resource to the instanced scene's root node as well.
  22. Creating a theme
  23. ----------------
  24. Themes can be created from any control node. Select a control node in the scene
  25. hierarchy, then in the inspector go to the theme property. From there you can
  26. select **New Theme**.
  27. .. image:: img/new_theme.png
  28. This will create an empty theme and open up the theme editor.
  29. .. image:: img/theme_editor.png
  30. In the theme editor you can customize everything about a theme except for
  31. the default font the theme uses. That can only be customized in the inspector under
  32. the selected theme.
  33. .. image:: img/default_font.png
  34. Theme items
  35. -----------
  36. In the theme editor, next to the default preview window, is where you make changes
  37. to your theme. Clicking the plus button opens the **Add item Type** menu.
  38. .. image:: img/add_item_type.png
  39. From here select the control node you want your theme to modify and click **Ok**. You
  40. should now see theme items for that node in the theme editor. Theme items are what defines
  41. the look of a theme, each kind of item in a theme can be:
  42. - **Color**: A single color, with or without transparency. Colors are
  43. usually applied to fonts and icons.
  44. - **Constant**: A single numerical constant. Generally used
  45. to define spacing between components or alignment.
  46. - **Font**: Every control that uses text can be assigned the fonts
  47. used to draw strings.
  48. - **Icon**: A single image. Textures are not often used, but when
  49. they are, they represent handles to pick or icons in a complex control
  50. (such as a file dialog).
  51. - **StyleBox**: Stylebox is a resource that defines how to draw a
  52. panel in varying sizes (more information on them later).
  53. Every item is associated with:
  54. - A name (the name of the item)
  55. - A Control (the name of the control)
  56. To customize a theme item click on the plus sign next to it. Your theme
  57. will now override the default theme for that item. To modify it click on **Empty**,
  58. then select the new theme item you want to create. Click on it again and you can
  59. now modify it in the inspector.
  60. .. image:: img/theme_item_inspector.png
  61. You can also add custom theme items to a control node under the built in theme items.
  62. In the theme editor, above the theme items, is the **Show Default** toggle. It will hide
  63. or show any theme items that are using the default theme settings. Next to it is the
  64. **Override Defaults** button, which will override the default theme for all theme items
  65. of the currently selected control node.
  66. Manage theme Items
  67. ------------------
  68. Clicking the **Manage Items** button brings up the Manage theme items menu. In
  69. the edit items tab you can view all the theme items for your theme, add a custom
  70. theme item, or a custom control node type.
  71. .. image:: img/manage_items.png
  72. You can also mass delete theme items from here. **Remove Class Items** will remove
  73. all built in theme items you have customized for the control node. **Remove Custom
  74. Items** will remove all the custom theme items for the selected node. And **Remove
  75. All Items** will remove everything.
  76. From the **Import Items** tab you can import theme items from other themes. You can
  77. import items from the default Godot theme, the Godot editor theme, or another custom
  78. theme. You can import all of the theme items for a control node or only one. You need
  79. to select **Data** when importing to actually import the theme item. Otherwise your
  80. theme will just have a blank override for that theme option.
  81. .. image:: img/import_items.png
  82. Preview
  83. -------
  84. The **Default Preview** tab of the theme editor shows you how every control node in
  85. Godot will look with your theme settings applied. If you haven't applied a setting
  86. then the default theme setting will be used.
  87. .. image:: img/default_preview.png
  88. You can also preview how other scenes will look by clicking the **Add Preview** button
  89. and selecting a tscn file that has a control node as the root node.
  90. .. image:: img/scene_preview.png
  91. Theme overrides
  92. ---------------
  93. If only a few controls need to be skinned, it is often not necessary to
  94. create a new theme. Controls offer their theme items as special kinds
  95. of properties. If checked, overriding will take place:
  96. .. image:: img/themecheck.png
  97. As can be seen in the image above, theme items have little check boxes.
  98. If checked, they can be used to override the value of the theme just for
  99. that control.
  100. Changing themes with code
  101. -------------------------
  102. In addition to the theme editor it is possible to change theme items with
  103. code, here is an example:
  104. .. tabs::
  105. .. code-tab:: gdscript GDScript
  106. var theme = Theme.new()
  107. theme.set_color("font_color", "Label", Color.red)
  108. var label = Label.new()
  109. label.theme = theme
  110. .. code-tab:: csharp
  111. var theme = new Theme();
  112. theme.SetColor("fontColor", "Label", new Color(1.0f, 0.0f, 0.0f));
  113. var label = new Label();
  114. label.Theme = theme;
  115. In the example above, a new theme is created. The "font_color" option
  116. is changed and then applied to a label. Therefore, the label's text (and all
  117. children and grandchildren labels) will be red.
  118. It is possible to override those options without using the theme
  119. directly, and only for a specific control, by using the override API in
  120. :ref:`Control.add_color_override() <class_Control_method_add_color_override>`:
  121. .. tabs::
  122. .. code-tab:: gdscript GDScript
  123. var label = Label.new()
  124. label.add_color_override("font_color", Color.red)
  125. .. code-tab:: csharp
  126. var label = new Label();
  127. label.AddColorOverride("fontColor", new Color(1.0f, 0.0f, 0.0f));
  128. In the inline help of Godot (in the Script tab), you can check which theme items
  129. are overridable, or check the :ref:`Control <class_Control>` class reference.