gui_skinning.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Skinning a GUI
  2. ==============
  3. Oh Beautiful GUI!
  4. ~~~~~~~~~~~~~~~~~
  5. This tutorial is about advanced skinning of an user interface. Most
  6. games generally don't need this, as they end up just relying on
  7. `Label <https://github.com/okamstudio/godot/wiki/class_label>`__,
  8. `TextureFrame <https://github.com/okamstudio/godot/wiki/class_textureframe>`__,
  9. `TextureButton <https://github.com/okamstudio/godot/wiki/class_texturebutton>`__
  10. and
  11. `TextureProgress <https://github.com/okamstudio/godot/wiki/class_textureprogress>`__.
  12. However, many types of games often need complex user interfaces, like
  13. MMOs, traditional RPGs, Simulators, Strategy, etc. These kind of
  14. interfaces are also common in some games that include editors to create
  15. content, or interfaces for network connectivity.
  16. Godot user interface uses these kind of controls with the default theme,
  17. but they can be skinned to resemble pretty much any kind of user
  18. interface.
  19. Theme
  20. ~~~~~
  21. The GUI is skinned through the
  22. `Theme <https://github.com/okamstudio/godot/wiki/class_theme>`__
  23. resource. Theme contains all the information required to change the
  24. entire visual styling of all controls. Theme options are named, so it's
  25. not obvious which name changes what (specialy from code), but several
  26. tools are provided. The ultimate place to look at what each theme option
  27. is for each control, which will always be more up to date than any
  28. documentation is the file
  29. https://github.com/okamstudio/godot/blob/master/scene/resources/default\_theme/default\_theme.cpp.
  30. The rest of this document will explain the different tools used to
  31. customize the theme.
  32. A Theme can be applied to any control in the scene. As a result, all
  33. children and grand-children controls will use that same theme too
  34. (unless another theme is specified further down the tree). If a value is
  35. not found in a theme, it will be searched in themes higher up in the
  36. hierarchy towards the root. If nothing was found, the default theme is
  37. used. This system allows for flexible overriding of themes in complex
  38. user interfaces.
  39. Theme Options
  40. ~~~~~~~~~~~~~
  41. Each kind of option in a theme can be:
  42. - **An integer constant**: A single numerical constant. Generally used
  43. to define spacing between compoments or alignment.
  44. - **A Color**: A single color, with or without transparency. Colors are
  45. usually applied to fonts and icons.
  46. - **A Texture**: A single image. Textures are not often used, but when
  47. they are they represent handles to pick or icons in a complex control
  48. (such as file dialog).
  49. - **A Font**: Every control that uses text can be assigned the fonts
  50. used to draw strings.
  51. - **A StyleBox**: Stylebox is a resource that defines how to draw a
  52. panel in varying sizes (more information on them later).
  53. Every option is associated to:
  54. - A name (the name of the option)
  55. - A Control (the name of the control)
  56. An example usage:
  57. ::
  58. var t = Theme.new()
  59. t.set_color("font_color","Label",Color(1.0,1.0,1.0))
  60. var l = Label.new()
  61. l.set_theme(t)
  62. In the example above, a new theme is created. The "font\_color" option
  63. is changed and then applied to a label. As a result, the label (and all
  64. children and grand children labels) will use that color.
  65. It is possible to override those options without using the theme
  66. directly and only for a specific control by using the override API in
  67. `Control <https://github.com/okamstudio/godot/wiki/class_control#add_color_override>`__:
  68. ::
  69. var l = Label.new()
  70. l.add_color_override("font_color",Color(1.0,1.0,1.0))
  71. In the inline help of Godot (help tab) you can check which theme options
  72. are overrideable. This is not yet available in the wiki class reference,
  73. but will be soon.
  74. Customizing a Control
  75. ~~~~~~~~~~~~~~~~~~~~~
  76. If only a few controls need to be skinned. It is often not neccesary to
  77. create a new theme. Controls offer their theme options as special kind
  78. of properties. If checked, overriding will take place:
  79. .. image:: /img/themecheck.png
  80. As can be see in the image above, theme options have little check-boxes.
  81. If checked, they can be used to override the value of the theme just for
  82. that control.
  83. Creating a Theme
  84. ~~~~~~~~~~~~~~~~
  85. The simplest way to create a theme is to edit a theme resource. Create a
  86. Theme from the resource menu, the editor will appear immediately.
  87. Following to this, save it (to, as example, myheme.thm):
  88. .. image:: /img/themecheck.png
  89. This will create an empty theme that can later be loaded and assigned to
  90. controls.
  91. Example: Themeing a Button
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. Take some assets attachment:skin\_assets.zip, go to the "theme" menu and
  94. select `Add Class Item <>`__
  95. .. image:: /img/themeci.png
  96. A menu will appear promting the type of control to create. Select
  97. `Button <>`__
  98. .. image:: /img/themeci2.png
  99. Immediately, all button theme options will appear in the property
  100. editor, where they can be edited:
  101. .. image:: /img/themeci3.png
  102. Select the "normal" stylebox and create a new "StyleBoxTexture", then
  103. edit it. A texture stylebox basically contains a texture and the size of
  104. the margins that will not stretch when the texture is stretched. This is
  105. called "3x3" stretching:
  106. .. image:: /img/sb1.png
  107. Repeat the steps and add the other assets. There is no hover or disabled
  108. image in the example files, so use the same stylebox as in normal. Set
  109. the supplied font as the button font and change the font color to black.
  110. Soon, your button will look different and retro:
  111. .. image:: /img/sb2.png
  112. Save this theme to the .thm file. Go to the 2D editor and create a few
  113. buttons:
  114. .. image:: /img/skinbuttons1.png
  115. Now, go to the root node of the scene and locate the "theme" property,
  116. replace it by the theme that was just created. It should look like this:
  117. .. image:: /img/skinbuttons2.png
  118. Congratulations! You have created a reusable GUI Theme!
  119. *Juan Linietsky, Ariel Manzur, Distributed under the terms of the `CC
  120. By <https://creativecommons.org/licenses/by/3.0/legalcode>`__ license.*