2
0

custom_gui_controls.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 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 very important with controls, as it helps to
  20. organize them in proper layouts. For this, the
  21. :ref:`Control.get_size() <class_Control_get_size>`
  22. method 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.set_focus_mode() <class_Control_set_focus_mode>`
  30. function. When drawing, and if the control supports input focus, it is
  31. always desired to show some sort of indicator (highight, 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_has_focus>`
  34. exists. Example
  35. ::
  36. func _draw():
  37. if (has_focus()):
  38. draw_selected()
  39. else:
  40. draw_normal()
  41. Sizing
  42. ------
  43. As mentioned before, size is very important to controls. This allows
  44. them to lay out properly, when set into grids, containers, or anchored.
  45. Controls most of the time provide a *minimum size* to help to properly
  46. lay them out. For example, if controls are placed vertically on top of
  47. each other using a :ref:`VBoxContainer <class_VBoxContainer>`,
  48. the minimum size will make sure your custom control is not squished by
  49. the other controls in the container.
  50. To provide this callback, just override
  51. :ref:`Control.get_minimum_size() <class_Control_get_minimum_size>`,
  52. for example:
  53. ::
  54. func get_minimum_size():
  55. return Vector2(30,30)
  56. Or alternatively, set it via function:
  57. ::
  58. func _ready():
  59. set_custom_minimum_size( Vector2(30,30) )
  60. Input
  61. -----
  62. Controls provide a few helpers to make managing input events much easier
  63. than regular nodes.
  64. Input events
  65. ~~~~~~~~~~~~
  66. There are a few tutorials about input before this one, but it's worth
  67. mentioning that controls have a special input method that only works
  68. when:
  69. - The mouse pointer is over the control.
  70. - The button was pressed over this control (control always
  71. captures input until button is released)
  72. - Control provides keyboard/joypad focus via
  73. :ref:`Control.set_focus_mode() <class_Control_set_focus_mode>`.
  74. This function is
  75. :ref:`Control._input_event() <class_Control__input_event>`.
  76. Simply override it in your control. No processing needs to be set.
  77. ::
  78. extends Control
  79. func _input_event(ev):
  80. if (ev.type==InputEvent.MOUSE_BUTTON and ev.button_index==BUTTON_LEFT and ev.pressed):
  81. print("Left mouse button was pressed!")
  82. For more information about events themselves, check the :ref:`doc_inputevent`
  83. tutorial.
  84. Notifications
  85. ~~~~~~~~~~~~~
  86. Controls also have many useful notifications for which no callback
  87. exists, but can be checked with the _notification callback:
  88. ::
  89. func _notification(what):
  90. if (what==NOTIFICATION_MOUSE_ENTER):
  91. pass # mouse entered the area of this control
  92. elif (what==NOTIFICATION_MOUSE_EXIT):
  93. pass # mouse exited the area of this control
  94. elif (what==NOTIFICATION_FOCUS_ENTER):
  95. pass # control gained focus
  96. elif (what==NOTIFICATION_FOCUS_EXIT):
  97. pass # control lost focus
  98. elif (what==NOTIFICATION_THEME_CHANGED):
  99. pass # theme used to draw the control changed
  100. # update and redraw is recommended if using a theme
  101. elif (what==NOTIFICATION_VISIBILITY_CHANGED):
  102. pass # control became visible/invisible
  103. # check new status with is_visible()
  104. elif (what==NOTIFICATION_RESIZED):
  105. pass # control changed size, check new size
  106. # with get_size()
  107. elif (what==NOTIFICATION_MODAL_CLOSED):
  108. pass # for modal popups, notification
  109. # that the popup was closed