Last active
January 8, 2022 20:47
-
-
Save NovemberDev/3799f3025c438c051a66b607c5389b88 to your computer and use it in GitHub Desktop.
Reusable TransitionPanel-Script for Godot UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Panel | |
# Add this script to a panel node | |
# Panel <-- contains script | |
# |- Page1 | |
# |--Button <-- calls get_node("../../").transitionTo("Page2") | |
# |- Page2 | |
# |--Button <-- calls get_node("../../").transitionTo("Page1") | |
var old_ui : Control | |
var target_ui : Control | |
var TRANSITION_SPEED = 10.0 | |
func _ready(): | |
rect_clip_content = true | |
for child in get_children(): | |
child.rect_position.x = child.rect_size.x | |
child.visible = true | |
if target_ui == null: | |
target_ui = get_child(0) | |
func _process(delta): | |
var old_distance = 0 | |
if old_ui != null: | |
old_distance = old_ui.rect_position.distance_to(Vector2(-old_ui.rect_size.x, 0)) | |
if old_distance > 0.1: | |
old_ui.rect_position = lerp(old_ui.rect_position, Vector2(-old_ui.rect_size.x, 0), TRANSITION_SPEED * delta) | |
var target_distance = target_ui.rect_position.distance_to(Vector2.ZERO) | |
if target_distance > 0.1: | |
target_ui.rect_position = lerp(target_ui.rect_position, Vector2.ZERO, TRANSITION_SPEED * delta) | |
if target_distance < 0.1 and old_distance < 0.1: | |
set_process(false) | |
# call get_node("../../").transitionTo("Page2") from Page1/Button | |
# from a nested child button | |
func transitionTo(target_node_name): | |
var node = get_node(target_node_name) | |
if target_ui != null: | |
old_ui = target_ui | |
target_ui = node | |
target_ui.rect_position.x = target_ui.rect_size.x | |
var focus = get_focus_owner() | |
if focus != null: | |
focus.release_focus() | |
set_process(true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment