Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
맞춤형 GUI 컨트롤
컨트롤이 너무 많아서...
그러나 결코 충분하지 않습니다. 원하는 대로 작동하는 사용자 정의 컨트롤을 만드는 것은 거의 모든 GUI 프로그래머의 집착입니다. Godot는 많은 기능을 제공하지만, 원하는 방식으로 정확하게 작동하지 않을 수도 있습니다. 대각선 스크롤 막대를 지원하기 위해 풀 요청으로 개발자에게 연락하기 전에 최소한 스크립트에서 이러한 컨트롤을 쉽게 만드는 방법을 아는 것이 좋습니다.
그리기
그림을 그리려면 2D에서 커스텀 그리기 튜토리얼을 확인하는 것이 좋습니다. 동일하게 적용됩니다. 일부 기능은 그림을 그릴 때 유용하기 때문에 언급할 가치가 있으므로 다음에 자세히 설명하겠습니다.
컨트롤 크기 확인 중
2D 노드와 달리 컨트롤에서는 "크기"가 중요합니다. 컨트롤을 적절한 레이아웃으로 구성하는 데 도움이 되기 때문입니다. 이를 위해 Control.size 속성이 제공됩니다. _draw() 중에 이를 확인하는 것은 모든 것이 내부에 유지되는지 확인하는 데 중요합니다.
초점 확인 중
일부 컨트롤(예: 버튼 또는 텍스트 편집기)은 키보드 또는 조이패드 입력에 대한 입력 포커스를 제공할 수 있습니다. 이에 대한 예로는 텍스트를 입력하거나 버튼을 누르는 것이 있습니다. 이는 Control.focus_mode 속성으로 제어됩니다. 그릴 때 컨트롤이 입력 포커스를 지원하는 경우 현재 포커스가 있는 컨트롤임을 나타내기 위해 일종의 표시기(강조 표시, 상자 등)를 표시하는 것이 항상 바람직합니다. 이 상태를 확인하기 위해 Control.has_focus() 메서드가 존재합니다. 예
func _draw():
if has_focus():
draw_selected()
else:
draw_normal()
public override void _Draw()
{
if (HasFocus())
{
DrawSelected()
}
else
{
DrawNormal();
}
}
사이징
앞서 언급했듯이 크기는 컨트롤에 중요합니다. 이를 통해 그리드, 컨테이너 또는 고정으로 설정할 때 적절하게 배치할 수 있습니다. 컨트롤은 대부분의 경우 적절하게 배치하는 데 도움이 되는 *최소 크기*를 제공합니다. 예를 들어 컨트롤이 :ref:`VBoxContainer <class_VBoxContainer>`을 사용하여 수직으로 서로 위에 배치된 경우 최소 크기는 사용자 지정 컨트롤이 컨테이너의 다른 컨트롤에 의해 찌그러지지 않도록 합니다.
이 콜백을 제공하려면 :ref:`Control._get_minimum_size() <class_Control_private_method__get_minimum_size>`를 재정의하세요. 예:
func _get_minimum_size():
return Vector2(30, 30)
public override Vector2 _GetMinimumSize()
{
return new Vector2(20, 20);
}
또는 다음 함수를 사용하여 설정하세요.
func _ready():
set_custom_minimum_size(Vector2(30, 30))
public override void _Ready()
{
CustomMinimumSize = new Vector2(20, 20);
}
입력
컨트롤은 일반 노드보다 입력 이벤트를 훨씬 쉽게 관리할 수 있도록 몇 가지 도우미를 제공합니다.
입력 이벤트
이 튜토리얼 이전에 입력에 대한 몇 가지 튜토리얼이 있지만 컨트롤에는 다음과 같은 경우에만 작동하는 특수 입력 방법이 있다는 점을 언급할 가치가 있습니다.
마우스 포인터가 컨트롤 위에 있습니다.
The button was pressed over this control (control always captures input until button is released)
Control은 :ref:`Control.focus_mode <class_Control_property_focus_mode>`를 통해 키보드/조이패드 포커스를 제공합니다.
이 함수는 :ref:`Control._gui_input() <class_Control_private_method__gui_input>`입니다. 이를 사용하려면 컨트롤에서 재정의하세요. 처리를 설정할 필요가 없습니다.
extends Control
func _gui_input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
print("Left mouse button was pressed!")
public override void _GuiInput(InputEvent @event)
{
if (@event is InputEventMouseButton mbe && mbe.ButtonIndex == MouseButton.Left && mbe.Pressed)
{
GD.Print("Left mouse button was pressed!");
}
}
이벤트 자체에 대한 자세한 내용은 InputEvent 사용하기 튜토리얼을 확인하세요.
알림(Notification)
컨트롤에는 전용 콜백이 없지만 _notification 콜백으로 확인할 수 있는 유용한 알림이 많이 있습니다.
func _notification(what):
match what:
NOTIFICATION_MOUSE_ENTER:
pass # Mouse entered the area of this control.
NOTIFICATION_MOUSE_EXIT:
pass # Mouse exited the area of this control.
NOTIFICATION_FOCUS_ENTER:
pass # Control gained focus.
NOTIFICATION_FOCUS_EXIT:
pass # Control lost focus.
NOTIFICATION_THEME_CHANGED:
pass # Theme used to draw the control changed;
# update and redraw is recommended if using a theme.
NOTIFICATION_VISIBILITY_CHANGED:
pass # Control became visible/invisible;
# check new status with is_visible().
NOTIFICATION_RESIZED:
pass # Control changed size; check new size
# with get_size().
NOTIFICATION_MODAL_CLOSE:
pass # For modal pop-ups, notification
# that the pop-up was closed.
public override void _Notification(int what)
{
switch (what)
{
case NotificationMouseEnter:
// Mouse entered the area of this control.
break;
case NotificationMouseExit:
// Mouse exited the area of this control.
break;
case NotificationFocusEnter:
// Control gained focus.
break;
case NotificationFocusExit:
// Control lost focus.
break;
case NotificationThemeChanged:
// Theme used to draw the control changed;
// update and redraw is recommended if using a theme.
break;
case NotificationVisibilityChanged:
// Control became visible/invisible;
// check new status with is_visible().
break;
case NotificationResized:
// Control changed size; check new size with get_size().
break;
case NotificationModalClose:
// For modal pop-ups, notification that the pop-up was closed.
break;
}
}