TUIX v0.1Alfa
Příklady
Kompletní spustitelné ukázky kódu, které demonstrují funkce TUIX Core. Zkopírujte libovolný příklad, uložte jako .py soubor a spusťte v terminálu.
Základní choice menu
Nejjednodušší možné interaktivní menu – centrovaný box se dvěma tlačítky.
from tuix.core import TuixEngine
engine = TuixEngine()
# Create a choice component
engine.component.create("menu", "choice")
# Set the label and choices
engine.component.set_property("menu", "text", "Do you like TUIX?")
engine.component.set_property("menu", "choices", [
[{"name": "Yes"}, {"name": "No"}]
])
# Render – blocks until user presses Enter
engine.render.draw()Menu s vlastním stylem
Použijte preset styl a přepište vybrané barvy vlastními hodnotami.
from tuix.core import TuixEngine
engine = TuixEngine()
# Switch to the classic preset
engine.styles.set_style("classic")
# Override the selection highlight colour (RGB)
engine.styles.set_custom_style("selected_color", (0, 200, 255))
# Override the border colour (RGB)
engine.styles.set_custom_style("border_color", (255, 165, 0))
# Create a choice component
engine.component.create("styled_menu", "choice")
engine.component.set_property("styled_menu", "text", "Pick a colour scheme:")
engine.component.set_property("styled_menu", "choices", [
[{"name": "Ocean"}, {"name": "Sunset"}, {"name": "Forest"}]
])
engine.render.draw()Víceřádkové rozložení tlačítek
Předáním více vnitřních seznamů do choices vytvoříte řádky tlačítek. Navigace funguje horizontálně (vlevo/vpravo) i vertikálně (nahoru/dolů).
from tuix.core import TuixEngine
engine = TuixEngine()
engine.component.create("grid", "choice")
engine.component.set_property("grid", "text", "Select a difficulty:")
engine.component.set_property("grid", "choices", [
[{"name": "Easy"}, {"name": "Medium"}, {"name": "Hard"}],
[{"name": "Nightmare"}, {"name": "Custom"}]
])
engine.render.draw()Vlastní rozměry a okraje
Přepište výchozí centrovaný layout a nastavte explicitní velikost a pozici.
from tuix.core import TuixEngine
engine = TuixEngine()
# Create a component
engine.component.create("panel", "choice")
engine.component.set_property("panel", "text", "Settings")
engine.component.set_property("panel", "choices", [
[{"name": "Audio"}, {"name": "Video"}],
[{"name": "Controls"}, {"name": "Back"}]
])
# Make it 60% wide, 40% tall
engine.layout.set_dimensions("panel", 0.6, 0.4)
# Pin it 10% from top, 5% from left
engine.layout.margin_mode("panel", "custom", 0.1, 0.05)
engine.render.draw()Reakce na výběr uživatele
Po návratu z engine.render.draw() lze vybrané tlačítko získat ze stavu enginu. InputHandler ukládá finální selected_row a selected_index.
from tuix.core import TuixEngine
import os
engine = TuixEngine()
choices = [
[{"name": "New Game"}, {"name": "Load Game"}],
[{"name": "Settings"}, {"name": "Quit"}]
]
engine.component.create("main", "choice")
engine.component.set_property("main", "text", "Main Menu")
engine.component.set_property("main", "choices", choices)
engine.render.draw()
# After draw() returns, read the selection
row = engine.render.selected_row
idx = engine.render.selected_index
selected_name = choices[row][idx]["name"]
os.system("cls" if os.name == "nt" else "clear")
print(f"You selected: {selected_name}")Použití utility funkcí
TUIX Core exportuje pomocné funkce pro práci s ANSI řetězci a validací barev.
from tuix.core import is_rgb, is_bool, visual_width
# Validate an RGB tuple
print(is_rgb((255, 128, 0))) # True
print(is_rgb((300, 0, 0))) # False – values must be 0-255
print(is_rgb("red")) # False – must be a tuple
# Check boolean type
print(is_bool(True)) # True
print(is_bool(1)) # False – int is not bool
# Measure visible width (ignores ANSI escape codes)
plain = "Hello"
styled = "\x1b[31mHello\x1b[0m" # red text
print(visual_width(plain)) # 5
print(visual_width(styled)) # 5