• HOME
  • Diary
  • Godot 4系でコントロールノード内のノードにフォーカスがあっているかどうかの判定

Godot 4系でコントロールノード内のノードにフォーカスがあっているかどうかの判定

# 現在フォーカスを持っているコントロールを取得する
var focused_control = get_viewport().get_focus_owner()

# focused_controlが取得できていて、それがmenu自身か、menuの子孫であるかを確認する
if focused_control and ($menu == focused_control or $menu.is_ancestor_of(focused_control)):
	print("menuか、その子要素がフォーカスを持っています")

https://docs.godotengine.org/ja/4.x/classes/class_node.html#class-node-method-is-ancestor-of

SceneTree.get_focus_owner()は4系では使えない。

「.is_ancestor_of()が重要になる。」

あとはpint()でシングルなものだけでなくノードの構造を.print_tree()で出力できる。

「.find_next_valid_focus()」
要素内での最初にフォーカスできるコントロールを探す