diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-13 16:33:41 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-13 16:33:41 +0200 |
commit | cab1ca5140de3232719645cbd1cdc2d660db2128 (patch) | |
tree | 16f2341dd0cb460bde4e9105c020a5ed2402a134 | |
parent | 41f6ff58c4800c85e8c2d53498eddc3590acfde9 (diff) |
Refactoring TUI code.
-rw-r--r-- | app.py | 2 | ||||
-rw-r--r-- | tui/tui.py | 158 |
2 files changed, 114 insertions, 46 deletions
@@ -1,7 +1,7 @@ from board.board import Board from tui.tui import Tui, Action -b = Board(10) +b = Board(13) t = Tui() x, y = 0, 0 @@ -1,10 +1,61 @@ import termios, fcntl, sys, os from enum import Enum +from dataclasses import dataclass + class Action(Enum): - QUIT = 0 - OPEN = 1 - FLAG = 2 + QUIT = 0 # Pelin lopetus + OPEN = 1 # Ruudun avaaminen + FLAG = 2 # Ruudun liputus + HINT = 3 # Anna vihjeet + AUTO = 4 # Pelaa automaattisesti + LEFT = 5 + RIGHT = 6 + UP = 7 + DOWN = 8 + + +class Colors: + BLACK = 0 + RED = 1 + GREEN = 2 + YELLOW = 3 + BLUE = 4 + MAGENTA = 5 + CYAN = 6 + WHITE = 7 + GRAY = 8 + BRIGHT_RED = 9 + BRIGHT_GREEN = 0xA + BRIGHT_YELLOW = 0xB + BRIGHT_BLUE = 0xC + BRIGHT_MAGENTA = 0xD + BRIGHT_CYAN = 0xE + BRIGHT_WHITE = 0xF + + +@dataclass +class TileType: + text: str # Teksti + colors: [] # Lista (väri, tausta) pareja tekstille + + +tile_types = { + 0: TileType( "[ ]", [(0x7,0), (0x7,0), (0x7,0)] ), + 1: TileType( "[1]", [(0xA,0), (0xA,0), (0xA,0)] ), + 2: TileType( "[2]", [(0xB,0), (0xB,0), (0xB,0)] ), + 3: TileType( "[3]", [(0xD,0), (0xD,0), (0xD,0)] ), + 4: TileType( "[4]", [(0x9,0), (0x9,0), (0x9,0)] ), + 5: TileType( "[5]", [(0x9,0), (0x9,0), (0x9,0)] ), + 6: TileType( "[6]", [(0x9,0), (0x9,0), (0x9,0)] ), + 7: TileType( "[7]", [(0x9,0), (0x9,0), (0x9,0)] ), + 8: TileType( "[8]", [(0x9,0), (0x9,0), (0x9,0)] ), + 9: TileType( "[¤]", [(0xF,1), (0xF,1), (0xF,1)] ), + 10: TileType( "[#]", [(0x8,7), (0x8,7), (0x8,7)] ), + 11: TileType( "[B]", [(0x8,7), (0x1,7), (0x8,7)] ), + 12: TileType( "[?]", [(0x8,7), (0x3,7), (0x8,7)] ) +} + class Tui(): def __init__(self): @@ -21,45 +72,41 @@ class Tui(): self.oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, self.oldflags | os.O_NONBLOCK) + def __del__(self): # palautetaan terminaali takaisin alkupetäiseen uskoon fd = sys.stdin.fileno() termios.tcsetattr(fd, termios.TCSAFLUSH, self.oldterm) fcntl.fcntl(fd, fcntl.F_SETFL, self.oldflags) + def set_color(self, color): if color>=0 and color<16: print(end=f"\033[{'1;' if color//8 else ''}3{color%8}m") + def set_bg(self, color): if color>=0 and color<8: print(end=f"\033[4{color}m") + def cursor_up(self, lines): print(end=f"\033[{lines}F") + def reset_color(self): print(end="\033[0m") + def draw_tile(self, tile, hilighted): - chars_and_colors = ( - (' ', 7, 0), ('1', 10, 0), ('2', 11, 0), - ('3', 13, 0), ('4', 9, 0), ('5', 9, 0), - ('6', 9, 0), ('7', 9, 0), ('8', 9, 0), - ('¤', 15, 1), ('#', 8, 7), ('B', 8, 7), - ('?', 8, 7) - ) - - if hilighted: - self.set_color(14) - self.set_bg(6) - else: - self.set_color(chars_and_colors[tile][1]) - self.set_bg(chars_and_colors[tile][2]) - - print(end=f"[{chars_and_colors[tile][0]}]") - self.reset_color() + for i in range(len(tile_types[tile].text)): + color, bg = tile_types[tile].colors[i] + self.set_color(Colors.BLACK if hilighted else color) + self.set_bg(Colors.CYAN if hilighted else bg) + print(end=tile_types[tile].text[i]) + self.reset_color() + def draw_matrix(self, matrix, hx, hy ): self.cursor_up(len(matrix[0])) for y in range(len(matrix[0])): @@ -67,36 +114,57 @@ class Tui(): self.draw_tile( matrix[x][y], x == hx and y == hy ) print() - - def matrix_selector(self, matrix, x, y ): - self.draw_matrix(matrix, x, y) + + + def read_action(self): + actions = { + 'w': Action.UP, 'a': Action.LEFT, 's': Action.DOWN, + 'd': Action.RIGHT, ' ': Action.OPEN, '\n': Action.OPEN, + 'f': Action.FLAG, 'm': Action.FLAG, 'q': Action.QUIT + } + esc_actions = { + '': Action.QUIT, 'A': Action.UP, 'D': Action.LEFT, + 'C': Action.RIGHT, 'B': Action.DOWN + } + escape = 0 while True: try: c = sys.stdin.read(1) except: continue - match c: - case 'A' | 'w': - y-=1 - case 'D' | 'a': - x-=1 - case 'B' | 's': - y+=1 - case 'C' | 'd': - x+=1 - case 'q': - return (Action.QUIT,-1,-1) - case ' ' | '\n': - return (Action.OPEN, x, y) - case 'f' | 'm': - return (Action.FLAG, x, y) - case _: + if escape: + if c in "[0123456789": continue - x = 0 if x < 0 else x - y = 0 if y < 0 else y - x = len(matrix)-1 if x >= len(matrix) else x - y = len(matrix[0])-1 if y >= len(matrix[0]) else y - + if c in esc_actions: + return esc_actions[c] + escape = 0 + continue + else: + if c == '\033': + escape = 1 + continue + if c in actions: + return actions[c] + + + + def matrix_selector(self, matrix, x, y ): + self.draw_matrix(matrix, x, y) + while True: + action = self.read_action() + match action: + case Action.QUIT: + return (action,x,y) + case Action.OPEN | Action.FLAG: + if matrix[x][y]>=10: + return (action,x,y) + case Action.UP: + y = y-1 if y>0 else 0 + case Action.LEFT: + x = x-1 if x>0 else 0 + case Action.DOWN: + y = y+1 if y<len(matrix[0])-1 else y + case Action.RIGHT: + x = x+1 if x<len(matrix)-1 else x self.draw_matrix(matrix, x, y) -
\ No newline at end of file |