Created
November 2, 2022 20:37
-
-
Save jasonwinterpixel/1e935eb951605334e68d19a4da25cc36 to your computer and use it in GitHub Desktop.
Godot Scroll Container that shrinks if it's contents' size is below a threshold
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
tool | |
extends Node | |
class_name ShrinkingScrollContainer | |
onready var scroll_container := get_parent() as ScrollContainer | |
export var _scroll_container_contents:NodePath setget set_scroll_container_contents_path | |
onready var scroll_container_contents := get_node_or_null(_scroll_container_contents) as Control | |
export var start_scrolling_at := Vector2.ZERO | |
export var shrink_horizontal := false | |
export var shrink_vertical := false | |
func _ready(): | |
_connect() | |
_refresh() | |
func _refresh(): | |
if scroll_container_contents: | |
var contents_minimum_size := scroll_container_contents.get_minimum_size() | |
if shrink_horizontal: | |
scroll_container.rect_min_size.x = min(contents_minimum_size.x, start_scrolling_at.x) | |
if shrink_vertical: | |
scroll_container.rect_min_size.y = min(contents_minimum_size.y, start_scrolling_at.y) | |
# Stuff to make it work in the editor | |
func _init(): | |
# Redoing the node fetch in _init() | |
# makes the script work after it's recompiled | |
_connect() | |
func set_scroll_container_contents_path(n:NodePath): | |
_scroll_container_contents = n | |
_connect() | |
func _connect(): | |
if scroll_container_contents and scroll_container_contents.is_connected("item_rect_changed", self, "_refresh"): | |
scroll_container_contents.disconnect("item_rect_changed", self, "_refresh") | |
scroll_container_contents = get_node_or_null(_scroll_container_contents) | |
if scroll_container_contents: | |
scroll_container_contents.connect("item_rect_changed", self, "_refresh") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment