obsidian/MyObsidianVault/Knowledgebase/Godot/2D/Scrollable Image List.md

46 lines
2.5 KiB
Markdown
Raw Normal View History

2025-04-10 21:47:25 +02:00
To make an `HBoxContainer` dynamically resize with its image contents in Godot 4.3, follow these steps:
## Core Setup
- **Parent with `ScrollContainer`**: Place your `HBoxContainer` inside a `ScrollContainer` to enable scrolling when content exceeds visible area. Set the scroll direction (horizontal/vertical) in the `ScrollContainer` properties[5].
- **Container Settings**:
- Enable `Expand` and set **Stretch Mode** to `Keep Aspect` on all child `TextureRect` nodes[2].
- Apply **Size Flags** (`Fill` and `Expand`) to each `TextureRect` to ensure proportional scaling[2][3].
## Dynamic Resizing
For automatic width adjustment when adding/removing images:
```gdscript
# After adding/removing children:
$HBoxContainer.queue_sort() # Forces layout update
$ScrollContainer.update_scroll() # Refreshes scroll boundaries
```
If automatic resizing fails (common with dynamic content), manually calculate and set the container width:
```gdscript
var total_width = 0
for child in $HBoxContainer.get_children():
total_width += child.size.x
total_width += $HBoxContainer.get_constant("separation") * ($HBoxContainer.get_child_count() - 1)
$HBoxContainer.custom_minimum_size.x = total_width
```
## Alternative Approach
For wrapping behavior, replace `HBoxContainer` with an `HFlowContainer` (ensuring it's inside a fixed-size Control node). This automatically wraps overflowing items to new rows while scaling contents[2][4].
Citations:
[1] https://stackoverflow.com/questions/30974986/dynamically-resize-side-by-side-images-with-different-dimensions-to-the-same-hei
[2] https://www.reddit.com/r/godot/comments/13lbizw/how_to_resize_contents_of_hbox_container/
[3] https://forum.godotengine.org/t/image-scaling-inside-a-hboxcontainer/20753
[4] https://www.youtube.com/watch?v=GMKZU66uZac
[5] https://godotforums.org/d/29145-why-isn-t-the-size-of-a-hbox-container-being-updated-automatically-i-have-to-do-it-manually
[6] https://stackoverflow.com/questions/11093414/css-row-of-images-dynamically-resized-to-fill-container
[7] https://www.sitepoint.com/community/t/dynamically-resize-and-reshape-containers-and-images/421461
[8] https://docs.godotengine.org/en/stable/classes/class_hboxcontainer.html
---
2025-04-12 14:08:46 +02:00
Antwort von Perplexity: https://www.perplexity.ai/search/godot-4-3-ui-scrollable-image-XrHVby6bQ.C3e3jEh_kLow?utm_source=copy_output
_Notes_:
* Sprite2D for ingame sprites, TextureRect for UI, as it inherits from Control
* If using combined nodes as listitems, use TextureRect as Parent Node (recommendation from YT tutorial)