From 9d3b7827ebcceb4a47952fc90b2b4510b64fe90f Mon Sep 17 00:00:00 2001 From: Torsten Date: Sat, 12 Apr 2025 14:08:46 +0200 Subject: [PATCH] ... --- .../Godot/2D/Scrollable Image List.md | 7 +- .../Just use a custom pygame script.md | 91 +++++++++++++++++++ ...Texture Rect (e.g. for ScrollContainer).md | 40 ++++++++ .../Python/Writing a textfile.md | 15 +++ 4 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 MyObsidianVault/Knowledgebase/Godot/2D/Texture Atlas generation/Just use a custom pygame script.md create mode 100644 MyObsidianVault/Knowledgebase/Godot/2D/Use Sprite Atlas Tile as Texture Rect (e.g. for ScrollContainer).md create mode 100644 MyObsidianVault/Knowledgebase/Python/Writing a textfile.md diff --git a/MyObsidianVault/Knowledgebase/Godot/2D/Scrollable Image List.md b/MyObsidianVault/Knowledgebase/Godot/2D/Scrollable Image List.md index d62626e..b5f8d43 100644 --- a/MyObsidianVault/Knowledgebase/Godot/2D/Scrollable Image List.md +++ b/MyObsidianVault/Knowledgebase/Godot/2D/Scrollable Image List.md @@ -38,4 +38,9 @@ Citations: [8] https://docs.godotengine.org/en/stable/classes/class_hboxcontainer.html --- -Antwort von Perplexity: https://www.perplexity.ai/search/godot-4-3-ui-scrollable-image-XrHVby6bQ.C3e3jEh_kLow?utm_source=copy_output \ No newline at end of file +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) \ No newline at end of file diff --git a/MyObsidianVault/Knowledgebase/Godot/2D/Texture Atlas generation/Just use a custom pygame script.md b/MyObsidianVault/Knowledgebase/Godot/2D/Texture Atlas generation/Just use a custom pygame script.md new file mode 100644 index 0000000..9ee2f6e --- /dev/null +++ b/MyObsidianVault/Knowledgebase/Godot/2D/Texture Atlas generation/Just use a custom pygame script.md @@ -0,0 +1,91 @@ +```python +import pygame +import os + +def create_spritesheet(image_paths, output_path, rows=1, padding=0): + """ + Combine multiple images into a single spritesheet. + + Args: + image_paths (list): List of paths to source images + output_path (str): Path to save the combined spritesheet + rows (int): Number of rows in the spritesheet + padding (int): Pixel padding between images + """ + if not image_paths: + raise ValueError("No images provided") + + # Load all images + images = [] + max_width = 0 + max_height = 0 + + for path in image_paths: + try: + img = pygame.image.load(path)#.convert_alpha() + images.append(img) + max_width = max(max_width, img.get_width()) + max_height = max(max_height, img.get_height()) + except pygame.error as e: + print(f"Failed to load {path}: {e}") + continue + + if not images: + raise ValueError("No valid images loaded") + + # Calculate spritesheet dimensions + cols = (len(images) + rows - 1) // rows + sheet_width = cols * (max_width + padding) - padding + sheet_height = rows * (max_height + padding) - padding + + # Create blank spritesheet surface + spritesheet = pygame.Surface((sheet_width, sheet_height), pygame.SRCALPHA) + + # Blit images onto spritesheet + for i, img in enumerate(images): + row = i // cols + col = i % cols + x = col * (max_width + padding) + y = row * (max_height + padding) + spritesheet.blit(img, (x, y)) + + # Save the spritesheet + pygame.image.save(spritesheet, output_path) + print(f"Spritesheet saved to {output_path}") + + +pygame.init() + +filenames = os.listdir("./tiles") +os.chdir("tiles") + +# List of image paths to combine +image_files = filenames + +cols=8 +rows=len(image_files)//cols + 1 +print(f"cols: {cols}, rows: {rows}") + +## Create and save spritesheet +create_spritesheet(image_files, "../spritesheet/combined_spritesheet.png", rows, padding=1) + +os.chdir("..") + +## Create+Store metadata +mapping = ["x_col,y_col,tilename"] + +for index, filename in enumerate(image_files): + x_col = index // cols + y_row = index - x_col * cols + print(f"{index} - [{x_col},{y_row}] -> {filename}") + tilename = filename.split(".")[0] + mapping.append( f"{x_col},{y_row},{tilename}" ) + +print("Wrote spritesheet/combined_spritesheet.png") + +f = open("mapping.txt","w") +f.write("\n".join(mapping)) +f.close() + +``` + diff --git a/MyObsidianVault/Knowledgebase/Godot/2D/Use Sprite Atlas Tile as Texture Rect (e.g. for ScrollContainer).md b/MyObsidianVault/Knowledgebase/Godot/2D/Use Sprite Atlas Tile as Texture Rect (e.g. for ScrollContainer).md new file mode 100644 index 0000000..340234f --- /dev/null +++ b/MyObsidianVault/Knowledgebase/Godot/2D/Use Sprite Atlas Tile as Texture Rect (e.g. for ScrollContainer).md @@ -0,0 +1,40 @@ +To use a tile from an atlas for a `TextureRect` in a `ScrollContainer` in Godot 4.3, follow these steps: + +1. **Create or Load a TileSet Atlas**: + - Import your sprite sheet into Godot and create a `TileSet` resource. Define the tile regions in the `TileSet` editor[3]. + +2. **Set Up the AtlasTexture**: + - For each tile you want to use, create an `AtlasTexture` resource. Set its `atlas` property to your sprite sheet and define the `region` to crop the specific tile[4]. + +3. **Configure the TextureRect**: + ```gdscript + var atlas_texture = AtlasTexture.new() + atlas_texture.atlas = preload("res://path_to_your_spritesheet.png") + atlas_texture.region = Rect2(x, y, width, height) # Coordinates of the tile + $TextureRect.texture = atlas_texture + ``` + Replace `x`, `y`, `width`, and `height` with the tile's position and dimensions[4]. + +4. **Add to ScrollContainer**: + - Place the `TextureRect` inside a container (e.g., `VBoxContainer`) that is a child of the `ScrollContainer`. Ensure the container's size exceeds the `ScrollContainer`'s viewport to enable scrolling[2][5]. + +**Troubleshooting**: +- If textures don't update after modifying the atlas, avoid using "Make Unique" on `AtlasTexture` instances, as this breaks the link to the original atlas[1]. +- For crashes when adding many `TextureRect` nodes, ensure the `ScrollContainer`'s children are properly nested and avoid excessive updates during runtime[5]. + +This approach leverages Godot's `AtlasTexture` to efficiently reuse tiles from a sprite sheet while maintaining scrollable UI elements. + +Citations: +[1] https://www.reddit.com/r/godot/comments/12cttew/godot_4_textureatlas_questions/ +[2] https://docs.godotengine.org/en/stable/classes/class_scrollcontainer.html +[3] https://docs.godotengine.org/en/stable/tutorials/2d/using_tilesets.html +[4] https://docs.godotengine.org/de/stable/classes/class_atlastexture.html +[5] https://github.com/godotengine/godot/issues/75713 +[6] https://www.youtube.com/watch?v=w3jla1fTZuQ +[7] https://docs.godotengine.org/en/4.3/classes/class_atlastexture.html +[8] https://docs.godotengine.org/en/4.3/classes/class_scrollcontainer.html +[9] https://docs.godotengine.org/en/stable/classes/class_texturerect.html +[10] https://www.youtube.com/watch?v=2o_IiotMnls + +--- +Antwort von Perplexity: https://www.perplexity.ai/search/godot-4-3-use-tile-from-atlas-HHNbSG1FRniUTuYX96GNzA?utm_source=copy_output \ No newline at end of file diff --git a/MyObsidianVault/Knowledgebase/Python/Writing a textfile.md b/MyObsidianVault/Knowledgebase/Python/Writing a textfile.md new file mode 100644 index 0000000..091ed72 --- /dev/null +++ b/MyObsidianVault/Knowledgebase/Python/Writing a textfile.md @@ -0,0 +1,15 @@ + +```python +lines = ["Line 1\n", "Line 2\n", "Line 3\n"] +with open('output.txt', 'w') as f: + f.writelines(lines) # Writes all lines at once [1][3] + +# Alternative with join() +with open('output.txt', 'w') as f: + f.write('\n'.join(lines)) # Joins with newlines automatically [1][4] + +``` + + + +