From 027b760d4e0e91454a574e8f9ea3ea893fd5347a Mon Sep 17 00:00:00 2001 From: Stan Soldatov Date: Thu, 9 Jan 2025 01:53:06 +0100 Subject: [PATCH] Texture schema editor. --- webui/tools/section.py | 3 +- webui/tools/textures.py | 74 +++++++++++++++++++++++++++++++++++++++++ webui/tools/trees.py | 2 +- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 webui/tools/textures.py diff --git a/webui/tools/section.py b/webui/tools/section.py index 0c3f340..1728840 100644 --- a/webui/tools/section.py +++ b/webui/tools/section.py @@ -2,6 +2,7 @@ from tools.background import ConvertImageToObj from tools.dem import GeoTIFFWindowingTool +from tools.textures import TextureSchemaEditorTool from tools.tool import Tool from tools.trees import TreeSchemaEditorTool @@ -24,7 +25,7 @@ def add(cls): class Shemas(Section): title = "📄 Schemas" description = "Tools to work with different schemas." - tools = [TreeSchemaEditorTool] + tools = [TreeSchemaEditorTool, TextureSchemaEditorTool] class TexturesAndDEM(Section): diff --git a/webui/tools/textures.py b/webui/tools/textures.py new file mode 100644 index 0000000..745f172 --- /dev/null +++ b/webui/tools/textures.py @@ -0,0 +1,74 @@ +import json +from typing import Any, NamedTuple + +import streamlit as st +from config import FS25_TEXTURE_SCHEMA_PATH +from tools.textures_data import TEXTURE_URLS +from tools.tool import Tool + +COLUMNS_PER_ROW = 5 + + +class TextureInfo(NamedTuple): + name: str + data: dict[str, Any] + url: str + + +class TextureSchemaEditorTool(Tool): + title = "Texture Schema Editor" + description = "This tool allows you to edit the texture schema for the map generation. " + icon = "🎨" + + def content(self): + with open(FS25_TEXTURE_SCHEMA_PATH, "r", encoding="utf-8") as f: + self.texture_schema = json.load(f) + + texture_infos = [] + for texture in self.texture_schema: + texture_name = texture["name"] + texture_url = TEXTURE_URLS.get(texture_name) + if not texture_url: + continue + + texture_infos.append(TextureInfo(texture_name, texture, texture_url)) + + self.button_container = st.container() + + # Create a grid of images using the number of columns per row + self.text_areas = {} + for i in range(0, len(texture_infos), COLUMNS_PER_ROW): + row = st.columns(COLUMNS_PER_ROW) + for j, texture_info in enumerate(texture_infos[i : i + COLUMNS_PER_ROW]): + with row[j]: + st.image(texture_info.url, use_container_width=True) + text_area = st.text_area( + texture_info.name, + value=json.dumps(texture_info.data, indent=2), + key=texture_info.name, + height=160, + ) + self.text_areas[texture_info.name] = text_area + + with self.button_container: + if st.button("Show updated schema", key="show_updated_texture_schema"): + texture_schema = self.read_schema() + st.success( + "Texture schema was generated, click the copy button to copy it to the " + "clipboard. \n" + "Then paste it into the texture schema input field in the generation tool." + ) + st.json(texture_schema, expanded=False) + + def read_schema(self) -> list[dict[str, str | int]]: + new_schema = [] + for texture_name, texture_data in self.text_areas.items(): + try: + data = json.loads(texture_data) + except json.JSONDecodeError: + st.error(f"Error reading schema for texture name: {texture_name}") + continue + + new_schema.append(data) + + return new_schema diff --git a/webui/tools/trees.py b/webui/tools/trees.py index c4a941a..5a39896 100644 --- a/webui/tools/trees.py +++ b/webui/tools/trees.py @@ -54,7 +54,7 @@ def content(self): self.checkboxes[tree_info] = tree_checkbox with self.button_container: - if st.button("Show updated schema"): + if st.button("Show updated schema", key="show_updated_tree_schema"): tree_schema = self.read_schema() st.success( "Tree schema was generated, click the copy button to copy it to the "