0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Godot 4.2] スクリプト内で Control ノードをアンカー基準で移動させる

Posted at

position の代わりに offset_bottom offset_top offset_left offset_right を操作します。

即座に原点へ戻す

offset_bottom = 0.0
offset_top = 0.0
offset_left = 0.0
offset_right = 0.0

4行書くのが大変です。

Tweenを使って原点へ戻す

create_tween().tween_property(cursor, "offset_bottom", 0.0, 0.5)
create_tween().tween_property(cursor, "offset_top", 0.0, 0.5)
create_tween().tween_property(cursor, "offset_left", 0.0, 0.5)
create_tween().tween_property(cursor, "offset_right", 0.0, 0.5)

同じ Tween インスタンスに続けて書くと順番に実行されてしまうので気をつけましょう。

参考: Tween — Godot Engine (4.x)の日本語のドキュメント

Tweenを使ってカーソルをフォーカスされた要素の側へと移動させる

これがやりたかったことです。

func _on_viewport_gui_focus_changed(focused_node):
    if not _check_node_somehow(focused_node):
        return
    var cursor = _get_cursor_somehow()

    # 先に元の値を残しておかないと消えます。もちろん定数にしてもいいです
    var default_offset_bottom = cursor.offset_bottom
    var default_offset_top = cursor.offset_top
    var default_offset_left = cursor.offset_left
    var default_offset_right = cursor.offset_right

    # 親は切り替わりますが、グローバル座標はそのままです
    cursor.reparent(focused_node)

    create_tween().tween_property(cursor, "offset_bottom", default_offset_bottom, 0.5)
    create_tween().tween_property(cursor, "offset_top", default_offset_top, 0.5)
    create_tween().tween_property(cursor, "offset_left", default_offset_left, 0.5)
    create_tween().tween_property(cursor, "offset_right", default_offset_right, 0.5)

参考文献

サイズとアンカー — Godot Engine (4.x)の日本語のドキュメント
Control — Godot Engine (4.x)の日本語のドキュメント

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?