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.

ScrollContainer

繼承: Container < Control < CanvasItem < Node < Object

被繼承: EditorInspector

需要時能夠為子控制項提供捲軸的容器。

說明

用於在需要時為子控制項提供捲軸的容器。捲軸會自動繪製在右側(垂直捲動條)或底部(水平捲軸),拖動捲軸能夠移動 ScrollContainer 中的可見 Control(及其子項)。捲軸會根據該 Control 的 Control.custom_minimum_size 相對於 ScrollContainer 的大小自動調整抓取器的大小。

教學

屬性

bool

clip_contents

true (overrides Control)

bool

draw_focus_border

false

bool

follow_focus

false

ScrollMode

horizontal_scroll_mode

1

bool

propagate_maximum_size

false (overrides Control)

int

scroll_deadzone

0

ScrollHintMode

scroll_hint_mode

0

int

scroll_horizontal

0

bool

scroll_horizontal_by_default

false

float

scroll_horizontal_custom_step

-1.0

int

scroll_vertical

0

float

scroll_vertical_custom_step

-1.0

bool

tile_scroll_hint

false

ScrollMode

vertical_scroll_mode

1

方法

void

ensure_control_visible(control: Control)

HScrollBar

get_h_scroll_bar()

VScrollBar

get_v_scroll_bar()

主題屬性

Color

scroll_hint_horizontal_color

Color(0, 0, 0, 1)

Color

scroll_hint_vertical_color

Color(0, 0, 0, 1)

int

scrollbar_h_separation

0

int

scrollbar_v_separation

0

Texture2D

scroll_hint_horizontal

Texture2D

scroll_hint_vertical

StyleBox

focus

StyleBox

panel


訊號

scroll_ended() 🔗

使用觸控事件拖動可捲動區域時捲動停止時發出。拖動捲動條時不會發出此訊號,使用滑鼠滾輪捲動或使用鍵盤/遊戲手把事件捲動。

注意:此訊號僅在啟用 ProjectSettings.input_devices/pointing/emulate_touch_from_mouse 時在 Android 或 iOS 或桌面/Web 平台上發出。


scroll_started() 🔗

透過觸控事件拖動可捲動區域時開始捲動時發出。拖動捲動條時不會發出此訊號,使用滑鼠滾輪捲動或使用鍵盤/遊戲手把事件捲動。

注意:此訊號僅在啟用 ProjectSettings.input_devices/pointing/emulate_touch_from_mouse 時在 Android 或 iOS 或桌面/Web 平台上發出。


列舉

enum ScrollMode: 🔗

ScrollMode SCROLL_MODE_DISABLED = 0

停用捲動,捲軸不可見。

ScrollMode SCROLL_MODE_AUTO = 1

啟用捲動,捲軸僅在需要時可見,即容器的內容比容器大時。

ScrollMode SCROLL_MODE_SHOW_ALWAYS = 2

啟用捲動,捲軸始終可見。

ScrollMode SCROLL_MODE_SHOW_NEVER = 3

啟用捲動,捲軸隱藏。

ScrollMode SCROLL_MODE_RESERVE = 4

SCROLL_MODE_AUTO 和 SCROLL_MODE_SHOW_ALWAYS 的結合。僅在必要時顯示捲動條,但是會按照捲動條始終顯示來調整內容的大小。適合需要保證無論捲動條是否可見,內容大小都保持一致的場合。

ScrollMode SCROLL_MODE_MAXIMIZE_FIRST = 5

其行為類似於 SCROLL_MODE_AUTO,但會使 ScrollContainer 根據其內容報告一個最小尺寸(受對應軸向上設定的 Control.custom_maximum_size 限制)。這允許容器優先擴充自身,僅在空間受限時才開始出現捲動條。


enum ScrollHintMode: 🔗

ScrollHintMode SCROLL_HINT_MODE_DISABLED = 0

捲動提示將永遠不會顯示。

ScrollHintMode SCROLL_HINT_MODE_ALL = 1

捲動提示將顯示在頂部和底部(如果是垂直的),或左側和右側(如果是水平的)。

ScrollHintMode SCROLL_HINT_MODE_TOP_AND_LEFT = 2

捲動提示將顯示在頂部(如果是垂直的),或左側(如果是水平的)。

ScrollHintMode SCROLL_HINT_MODE_BOTTOM_AND_RIGHT = 3

捲動提示將顯示在底部(如果是水平的),或右側(如果是垂直的)。


屬性說明

bool draw_focus_border = false 🔗

  • void set_draw_focus_border(value: bool)

  • bool get_draw_focus_border()

如果為 true,則當 ScrollContainer 或其子節點獲得焦點時會繪製 focus。


bool follow_focus = false 🔗

  • void set_follow_focus(value: bool)

  • bool is_following_focus()

如果為 true,則 ScrollContainer 將自動捲動到獲得焦點的子項(包括間接子項)以確保它們完全可見。


ScrollMode horizontal_scroll_mode = 1 🔗

控制水平捲動條是否可用、應該何時可見。


int scroll_deadzone = 0 🔗

  • void set_deadzone(value: int)

  • int get_deadzone()

觸摸捲動的死區。較低的死區使捲動更加敏感。


ScrollHintMode scroll_hint_mode = 0 🔗

捲動提示(指示內容可以向特定方向捲動的指示器)的顯示方式。

注意:如果內容可以同時在垂直和水平方向捲動,則不會顯示提示。


int scroll_horizontal = 0 🔗

  • void set_h_scroll(value: int)

  • int get_h_scroll()

目前的水平捲動值。

注意:如果在 Node._ready() 函式或之前設定該值,則需要用 Object.set_deferred() 包裹,因為捲軸的 Range.max_value 此時尚未初始化。

func _ready():
    set_deferred("scroll_horizontal", 600)

bool scroll_horizontal_by_default = false 🔗

  • void set_scroll_horizontal_by_default(value: bool)

  • bool is_scroll_horizontal_by_default()

如果設定為 true,滑鼠滾輪將水平捲動檢視,而按住 Shift 鍵則進行垂直捲動。

如果設定為 false(預設),滑鼠滾輪將垂直捲動檢視,而按住 Shift 鍵則進行水平捲動。


float scroll_horizontal_custom_step = -1.0 🔗

  • void set_horizontal_custom_step(value: float)

  • float get_horizontal_custom_step()

覆蓋點選內部捲軸的水平增減按鈕或 ScrollBar 處於聚焦狀態下使用方向鍵時使用的 ScrollBar.custom_step。


int scroll_vertical = 0 🔗

  • void set_v_scroll(value: int)

  • int get_v_scroll()

目前的垂直捲動值。

注意:與 scroll_horizontal 一樣,最初設定時需要進行延遲。

func _ready():
    set_deferred("scroll_vertical", 600)

float scroll_vertical_custom_step = -1.0 🔗

  • void set_vertical_custom_step(value: float)

  • float get_vertical_custom_step()

覆蓋點選內部捲軸的垂直增減按鈕或 ScrollBar 處於聚焦狀態下使用方向鍵時使用的 ScrollBar.custom_step。


bool tile_scroll_hint = false 🔗

  • void set_tile_scroll_hint(value: bool)

  • bool is_scroll_hint_tiled()

如果為 true,則捲動提示紋理將平鋪而不是拉伸。見 scroll_hint_mode。


ScrollMode vertical_scroll_mode = 1 🔗

控制垂直捲動條是否可用、應該何時可見。


方法說明

void ensure_control_visible(control: Control) 🔗

確保給定的 control 控制項可見(必須是 ScrollContainer 的直接或間接子節點)。會被 follow_focus 使用。

注意:這不適用於在同一影格內剛剛新增的節點。如果想捲動到一個新新增的子節點,就必須使用 SceneTree.process_frame 等待至下一影格:

add_child(child_node)
await get_tree().process_frame
ensure_control_visible(child_node)

HScrollBar get_h_scroll_bar() 🔗

返回該 ScrollContainer 的水平捲軸 HScrollBar。

警告:這是一個必要的內部節點,移除和釋放它可能會導致當機。如果你想停用或隱藏捲軸,可以使用 horizontal_scroll_mode。


VScrollBar get_v_scroll_bar() 🔗

返回該 ScrollContainer 的垂直捲動條 VScrollBar。

警告:這是一個必要的內部節點,移除和釋放它可能會導致當機。如果你想停用或隱藏捲軸,可以使用 vertical_scroll_mode。


主題屬性說明

Color scroll_hint_horizontal_color = Color(0, 0, 0, 1) 🔗

用於調制 scroll_hint_horizontal 紋理的 Color。


Color scroll_hint_vertical_color = Color(0, 0, 0, 1) 🔗

用於調制 scroll_hint_vertical 紋理的 Color。


int scrollbar_h_separation = 0 🔗

ScrollContainer 的垂直捲動條與其內容之間的間距,單位為像素。如果內容的最小大小大於 ScrollContainer 的大小,則不會新增任何間距。


int scrollbar_v_separation = 0 🔗

ScrollContainer 的水平捲動條與其內容之間的間距,單位為像素。如果內容的最小大小大於 ScrollContainer 的大小,則不會新增任何間距。


Texture2D scroll_hint_horizontal 🔗

當內容仍可水平捲動時將顯示的指示器。請參閱 scroll_hint_mode。


Texture2D scroll_hint_vertical 🔗

當內容仍可垂直捲動時將顯示的指示器。請參閱 scroll_hint_mode。


StyleBox focus 🔗

ScrollContainer 的聚焦邊框 StyleBox。僅在 draw_focus_border 為 true 時使用。


StyleBox panel 🔗

ScrollContainer 的背景 StyleBox。