Work in progress

The content of this page was not yet updated for Godot 4.3 and may be outdated. If you know how to improve this page or you can confirm that it’s up to date, feel free to open a pull request.

Custom GUI controls

So many controls…

Yet there are never enough. Creating your own custom controls that act just the way you want them to is an obsession of almost every GUI programmer. Godot provides plenty of them, but they may not work exactly the way you want. Before contacting the developers with a pull-request to support diagonal scrollbars, at least it will be good to know how to create these controls easily from script.

Drawing

For drawing, it is recommended to check the Custom drawing in 2D tutorial. The same applies. Some functions are worth mentioning due to their usefulness when drawing, so they will be detailed next:

Checking control size

Unlike 2D nodes, “size” is important with controls, as it helps to organize them in proper layouts. For this, the Control.size property is provided. Checking it during _draw() is vital to ensure everything is kept in-bounds.

Checking focus

Some controls (such as buttons or text editors) might provide input focus for keyboard or joypad input. Examples of this are entering text or pressing a button. This is controlled with the Control.focus_mode property. When drawing, and if the control supports input focus, it is always desired to show some sort of indicator (highlight, box, etc.) to indicate that this is the currently focused control. To check for this status, the Control.has_focus() method exists. Example

GDScriptC#

  1. func _draw():
  2. if has_focus():
  3. draw_selected()
  4. else:
  5. draw_normal()
  1. public override void _Draw()
  2. {
  3. if (HasFocus())
  4. {
  5. DrawSelected()
  6. }
  7. else
  8. {
  9. DrawNormal();
  10. }
  11. }

Sizing

As mentioned before, size is important to controls. This allows them to lay out properly, when set into grids, containers, or anchored. Controls, most of the time, provide a minimum size to help properly lay them out. For example, if controls are placed vertically on top of each other using a VBoxContainer, the minimum size will make sure your custom control is not squished by the other controls in the container.

To provide this callback, just override Control._get_minimum_size(), for example:

GDScriptC#

  1. func _get_minimum_size():
  2. return Vector2(30, 30)
  1. public override Vector2 _GetMinimumSize()
  2. {
  3. return new Vector2(20, 20);
  4. }

Alternatively, set it using a function:

GDScriptC#

  1. func _ready():
  2. set_custom_minimum_size(Vector2(30, 30))
  1. public override void _Ready()
  2. {
  3. CustomMinimumSize = new Vector2(20, 20);
  4. }

Input

Controls provide a few helpers to make managing input events much easier than regular nodes.

Input events

There are a few tutorials about input before this one, but it’s worth mentioning that controls have a special input method that only works when:

  • The mouse pointer is over the control.

  • The button was pressed over this control (control always captures input until button is released)

  • Control provides keyboard/joypad focus via Control.focus_mode.

This function is Control._gui_input(). Simply override it in your control. No processing needs to be set.

GDScriptC#

  1. extends Control
  2. func _gui_input(event):
  3. if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
  4. print("Left mouse button was pressed!")
  1. public override void _GuiInput(InputEvent @event)
  2. {
  3. if (@event is InputEventMouseButton mbe && mbe.ButtonIndex == MouseButton.Left && mbe.Pressed)
  4. {
  5. GD.Print("Left mouse button was pressed!");
  6. }
  7. }

For more information about events themselves, check the Using InputEvent tutorial.

Notifications

Controls also have many useful notifications for which no dedicated callback exists, but which can be checked with the _notification callback:

GDScriptC#

  1. func _notification(what):
  2. match what:
  3. NOTIFICATION_MOUSE_ENTER:
  4. pass # Mouse entered the area of this control.
  5. NOTIFICATION_MOUSE_EXIT:
  6. pass # Mouse exited the area of this control.
  7. NOTIFICATION_FOCUS_ENTER:
  8. pass # Control gained focus.
  9. NOTIFICATION_FOCUS_EXIT:
  10. pass # Control lost focus.
  11. NOTIFICATION_THEME_CHANGED:
  12. pass # Theme used to draw the control changed;
  13. # update and redraw is recommended if using a theme.
  14. NOTIFICATION_VISIBILITY_CHANGED:
  15. pass # Control became visible/invisible;
  16. # check new status with is_visible().
  17. NOTIFICATION_RESIZED:
  18. pass # Control changed size; check new size
  19. # with get_size().
  20. NOTIFICATION_MODAL_CLOSE:
  21. pass # For modal pop-ups, notification
  22. # that the pop-up was closed.
  1. public override void _Notification(int what)
  2. {
  3. switch (what)
  4. {
  5. case NotificationMouseEnter:
  6. // Mouse entered the area of this control.
  7. break;
  8. case NotificationMouseExit:
  9. // Mouse exited the area of this control.
  10. break;
  11. case NotificationFocusEnter:
  12. // Control gained focus.
  13. break;
  14. case NotificationFocusExit:
  15. // Control lost focus.
  16. break;
  17. case NotificationThemeChanged:
  18. // Theme used to draw the control changed;
  19. // update and redraw is recommended if using a theme.
  20. break;
  21. case NotificationVisibilityChanged:
  22. // Control became visible/invisible;
  23. // check new status with is_visible().
  24. break;
  25. case NotificationResized:
  26. // Control changed size; check new size with get_size().
  27. break;
  28. case NotificationModalClose:
  29. // For modal pop-ups, notification that the pop-up was closed.
  30. break;
  31. }
  32. }

User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next