custom_gui_controls.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. Custom GUI Controls
  2. ===================
  3. So Many Controls..
  4. ------------------
  5. Yet there are never enough. Creating your own custom controls that act
  6. just the way you want them is an obsession of almost every GUI
  7. programmer. Godot provides plenty of them, but they may not work exactly
  8. the way you want. Before contacting the developers with a pull-request
  9. to support diagonal scrollbars, at least it will be good to know how to
  10. create these controls easily from script.
  11. Drawing
  12. -------
  13. For drawing, it is recommended to check the [[Custom Draw 2D]] tutorial.
  14. The same applies. Some functions are worth mentioning due to their
  15. usefulness when drawing, so they will be detailed next:
  16. Checking Control Size
  17. ~~~~~~~~~~~~~~~~~~~~~
  18. Unlike 2D nodes, "size" is very important with controls, as it helps to
  19. organize them in proper layouts. For this, the
  20. `Control.get\_size() <https://github.com/okamstudio/godot/wiki/class_control#get_size>`__
  21. method is provided. Checking it during \_draw() is vital to ensure
  22. everything is kept in-bounds.
  23. Checking Focus
  24. ~~~~~~~~~~~~~~
  25. Some controls (such as buttons or text editors) might provide input
  26. focus for keyboard or joypad input. Examples of this are entering text
  27. or pressing a button. This is controlled with the
  28. `Control.set\_focus\_mode() <https://github.com/okamstudio/godot/wiki/class_control#set_focus_mode>`__
  29. function. When drawing, and if the control supports input focus, it is
  30. always desired to show some sort of indicator (highight, box, etc) to
  31. indicate that this is the currently focused control. To check for this
  32. status, the
  33. `Control.has\_focus() <https://github.com/okamstudio/godot/wiki/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
  48. `VBoxContainer <https://github.com/okamstudio/godot/wiki/class_vboxcontainer>`__,
  49. the minimum size will make sure your custom control is not squished by
  50. the other controls in the container.
  51. To provide this callback, just override
  52. `Control.get\_minimum\_size() <https://github.com/okamstudio/godot/wiki/class_control#get_minimum_size>`__,
  53. for example:
  54. ::
  55. func get_minimum_size():
  56. return Vector2(30,30)
  57. Or alternatively, set it via function:
  58. ::
  59. func _ready():
  60. set_custom_minimum_size( Vector2(30,30) )
  61. Input
  62. -----
  63. Controls provide a few helpers to make managing input events much esier
  64. than regular nodes.
  65. Input Events
  66. ~~~~~~~~~~~~
  67. There are a few tutorials about input before this one, but it's worth
  68. mentioning that controls have a special input method that only works
  69. when:
  70. - The mouse pointer is over the control.
  71. - The left button was pressed over this control (control always
  72. captures input until button si released)
  73. - Control provides keyboard/joypad focus via
  74. `Control.set\_focus\_mode <https://github.com/okamstudio/godot/wiki/class_control#set_focus_mode>`__.
  75. This function is
  76. `Control.\_input\_event(event) <https://github.com/okamstudio/godot/wiki/class_control#_input_event>`__.
  77. Simply override it in your control. No processing needs to be set.
  78. ::
  79. extends Control
  80. func _input_event(ev):
  81. if (ev.type==InputEvent.MOUSE_BUTTON and ev.button_index==BUTTON_LEFT and ev.pressed):
  82. print("Left mouse button was pressed!")
  83. For more information about events themselves, check the [[Input Events]]
  84. tutorial.
  85. Notifications
  86. ~~~~~~~~~~~~~
  87. Controls also have many useful notifications for which no callback
  88. exists, but can be checked with the \_notification callback:
  89. ::
  90. func _notification(what):
  91. if (what==NOTIFICATION_MOUSE_ENTER):
  92. pass # mouse entered the area of this control
  93. elif (what==NOTIFICATION_MOUSE_EXIT):
  94. pass # mouse exited the area of this control
  95. elif (what==NOTIFICATION_FOCUS_ENTER):
  96. pass # control gained focus
  97. elif (what==NOTIFICATION_FOCUS_EXIT):
  98. pass # control lost focus
  99. elif (what==NOTIFICATION_THEME_CHANGED):
  100. pass # theme used to draw the control changed
  101. # update and redraw is recommended if using a theme
  102. elif (what==NOTIFICATION_VISIBILITY_CHANGED):
  103. pass # control became visible/invisible
  104. # check new status with is_visible()
  105. elif (what==NOTIFICATION_THEME_CHANGED):
  106. pass # theme used to draw the control changed
  107. # update and redraw is recommended if using a theme
  108. elif (what==NOTIFICATION_RESIZED):
  109. pass # control changed size, check new size
  110. # with get_size()
  111. elif (what==NOTIFICATION_MODAL_CLOSED):
  112. pass # for modal popups, notification
  113. # that the popup was closed
  114. *Juan Linietsky, Ariel Manzur, Distributed under the terms of the `CC
  115. By <https://creativecommons.org/licenses/by/3.0/legalcode>`__ license.*