custom_gui_controls.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. .. _doc_custom_gui_controls:
  2. Custom GUI controls
  3. ===================
  4. So many controls...
  5. -------------------
  6. Yet there are never enough. Creating your own custom controls that act
  7. just the way you want them to is an obsession of almost every GUI
  8. programmer. Godot provides plenty of them, but they may not work exactly
  9. the way you want. Before contacting the developers with a pull-request
  10. to support diagonal scrollbars, at least it will be good to know how to
  11. create these controls easily from script.
  12. Drawing
  13. -------
  14. For drawing, it is recommended to check the :ref:`doc_custom_drawing_in_2d` tutorial.
  15. The same applies. Some functions are worth mentioning due to their
  16. usefulness when drawing, so they will be detailed next:
  17. Checking control size
  18. ~~~~~~~~~~~~~~~~~~~~~
  19. Unlike 2D nodes, "size" is important with controls, as it helps to
  20. organize them in proper layouts. For this, the
  21. :ref:`Control.rect_size <class_Control_property_rect_size>`
  22. property is provided. Checking it during ``_draw()`` is vital to ensure
  23. everything is kept in-bounds.
  24. Checking focus
  25. ~~~~~~~~~~~~~~
  26. Some controls (such as buttons or text editors) might provide input
  27. focus for keyboard or joypad input. Examples of this are entering text
  28. or pressing a button. This is controlled with the
  29. :ref:`Control.focus_mode <class_Control_property_focus_mode>`
  30. property. When drawing, and if the control supports input focus, it is
  31. always desired to show some sort of indicator (highlight, box, etc.) to
  32. indicate that this is the currently focused control. To check for this
  33. status, the :ref:`Control.has_focus() <class_Control_method_has_focus>` method
  34. exists. Example
  35. .. tabs::
  36. .. code-tab:: gdscript GDScript
  37. func _draw():
  38. if has_focus():
  39. draw_selected()
  40. else:
  41. draw_normal()
  42. .. code-tab:: csharp
  43. public override void _Draw()
  44. {
  45. if (HasFocus())
  46. {
  47. DrawSelected()
  48. }
  49. else
  50. {
  51. DrawNormal();
  52. }
  53. }
  54. Sizing
  55. ------
  56. As mentioned before, size is important to controls. This allows
  57. them to lay out properly, when set into grids, containers, or anchored.
  58. Controls, most of the time, provide a *minimum size* to help properly
  59. lay them out. For example, if controls are placed vertically on top of
  60. each other using a :ref:`VBoxContainer <class_VBoxContainer>`,
  61. the minimum size will make sure your custom control is not squished by
  62. the other controls in the container.
  63. To provide this callback, just override
  64. :ref:`Control.get_minimum_size() <class_Control_method_get_minimum_size>`,
  65. for example:
  66. .. tabs::
  67. .. code-tab:: gdscript GDScript
  68. func get_minimum_size():
  69. return Vector2(30, 30)
  70. .. code-tab:: csharp
  71. public override Vector2 _GetMinimumSize()
  72. {
  73. return new Vector2(20, 20);
  74. }
  75. Alternatively, set it using a function:
  76. .. tabs::
  77. .. code-tab:: gdscript GDScript
  78. func _ready():
  79. set_custom_minimum_size(Vector2(30, 30))
  80. .. code-tab:: csharp
  81. public override void _Ready()
  82. {
  83. SetCustomMinimumSize(new Vector2(20, 20));
  84. }
  85. Input
  86. -----
  87. Controls provide a few helpers to make managing input events much easier
  88. than regular nodes.
  89. Input events
  90. ~~~~~~~~~~~~
  91. There are a few tutorials about input before this one, but it's worth
  92. mentioning that controls have a special input method that only works
  93. when:
  94. - The mouse pointer is over the control.
  95. - The button was pressed over this control (control always
  96. captures input until button is released)
  97. - Control provides keyboard/joypad focus via
  98. :ref:`Control.focus_mode <class_Control_property_focus_mode>`.
  99. This function is
  100. :ref:`Control._gui_input() <class_Control_method__gui_input>`.
  101. Simply override it in your control. No processing needs to be set.
  102. .. tabs::
  103. .. code-tab:: gdscript GDScript
  104. extends Control
  105. func _gui_input(event):
  106. if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
  107. print("Left mouse button was pressed!")
  108. .. code-tab:: csharp
  109. public override void _GuiInput(InputEvent @event)
  110. {
  111. if (@event is InputEventMouseButton mbe && mbe.ButtonIndex == (int)ButtonList.Left && mbe.Pressed)
  112. {
  113. GD.Print("Left mouse button was pressed!");
  114. }
  115. }
  116. For more information about events themselves, check the :ref:`doc_inputevent`
  117. tutorial.
  118. Notifications
  119. ~~~~~~~~~~~~~
  120. Controls also have many useful notifications for which no dedicated callback
  121. exists, but which can be checked with the _notification callback:
  122. .. tabs::
  123. .. code-tab:: gdscript GDScript
  124. func _notification(what):
  125. match what:
  126. NOTIFICATION_MOUSE_ENTER:
  127. pass # Mouse entered the area of this control.
  128. NOTIFICATION_MOUSE_EXIT:
  129. pass # Mouse exited the area of this control.
  130. NOTIFICATION_FOCUS_ENTER:
  131. pass # Control gained focus.
  132. NOTIFICATION_FOCUS_EXIT:
  133. pass # Control lost focus.
  134. NOTIFICATION_THEME_CHANGED:
  135. pass # Theme used to draw the control changed;
  136. # update and redraw is recommended if using a theme.
  137. NOTIFICATION_VISIBILITY_CHANGED:
  138. pass # Control became visible/invisible;
  139. # check new status with is_visible().
  140. NOTIFICATION_RESIZED:
  141. pass # Control changed size; check new size
  142. # with get_size().
  143. NOTIFICATION_MODAL_CLOSE:
  144. pass # For modal pop-ups, notification
  145. # that the pop-up was closed.
  146. .. code-tab:: csharp
  147. public override void _Notification(int what)
  148. {
  149. switch (what)
  150. {
  151. case NotificationMouseEnter:
  152. // Mouse entered the area of this control.
  153. break;
  154. case NotificationMouseExit:
  155. // Mouse exited the area of this control.
  156. break;
  157. case NotificationFocusEnter:
  158. // Control gained focus.
  159. break;
  160. case NotificationFocusExit:
  161. // Control lost focus.
  162. break;
  163. case NotificationThemeChanged:
  164. // Theme used to draw the control changed;
  165. // update and redraw is recommended if using a theme.
  166. break;
  167. case NotificationVisibilityChanged:
  168. // Control became visible/invisible;
  169. // check new status with is_visible().
  170. break;
  171. case NotificationResized:
  172. // Control changed size; check new size with get_size().
  173. break;
  174. case NotificationModalClose:
  175. // For modal pop-ups, notification that the pop-up was closed.
  176. break;
  177. }
  178. }