diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-14 11:30:57 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-14 11:30:57 +0200 |
commit | 1f9bba90256f0baf605636a93037f1debdcb4623 (patch) | |
tree | 3d2490a3900692719ee4350e1e711a1696054079 | |
parent | 9087617e2e023010f5386f222303292dbd015996 (diff) |
Making keyboard reader match only the start of the buffer.
-rw-r--r-- | tui/static.py | 22 | ||||
-rw-r--r-- | tui/tui.py | 33 |
2 files changed, 30 insertions, 25 deletions
diff --git a/tui/static.py b/tui/static.py index cb934f0..ecd80b9 100644 --- a/tui/static.py +++ b/tui/static.py @@ -13,17 +13,21 @@ class Action(Enum): RIGHT = 6 UP = 7 DOWN = 8 + TOP = 9 + BOTTOM = 10 + BEGIN = 11 + END = 12 + NOOP = 13 +# ActionKeys - Ohjelma vertaa syötteen alkua näihin ja palauttaa ekan ActionKeys = { - '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 -} - - -ActionEscKeys = { - '': Action.QUIT, 'A': Action.UP, 'D': Action.LEFT, - 'C': Action.RIGHT, 'B': Action.DOWN + "\033[A": Action.UP, "\033[D": Action.LEFT, + "\033[C": Action.RIGHT, '\033[B': Action.DOWN, "\033[5~": Action.TOP, + "\033[6~": Action.BOTTOM, "\033[7~": Action.BEGIN,"\033[8~": Action.END, + "\033[": Action.NOOP, "\033": Action.QUIT, + "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, } @dataclass @@ -2,7 +2,7 @@ # pylint: disable = multiple-imports import termios, fcntl, sys, os from time import sleep -from tui.static import Action, ActionKeys, ActionEscKeys, Colors, TileTypes +from tui.static import Action, ActionKeys, Colors, TileTypes class Tui(): @@ -21,30 +21,36 @@ 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): """ asettaa tekstin värin """ if color in range(16): print(end=f"\033[{'1;' if color//8 else ''}3{color%8}m") + def set_bg(self, color): """ asettaa tekstin taustan värin""" if color in range(8): print(end=f"\033[4{color}m") + def cursor_up(self, lines): """ liikuttaa kursoria ylöspäin""" print(end=f"\033[{lines}F") + def reset_color(self): """ resetoi tekstin värin ja muut attribuutit perusarvoille """ print(end="\033[0m") + def draw_tile(self, tile, hilighted): """ "piirtää" yhden ruudun """ for ch, colors in zip(TileTypes[tile].text, TileTypes[tile].colors): @@ -54,6 +60,7 @@ class Tui(): print(end=ch) self.reset_color() + def draw_matrix(self, matrix, hx, hy): """ "piirtää" ruudukon """ self.cursor_up(len(matrix[0])) @@ -63,27 +70,21 @@ class Tui(): self.draw_tile(matrix[x][y], x == hx and y == hy) print() + def read_action(self): """ lukee näppäimistölä käyttäjän toiminnon """ - escape = 0 while True: + # Ehkä riittää jos näppäimiä luetaan 50x sekunnissa + sleep(0.02) try: - # Ehkä riittää jos näppäimiä luetaan 200x sekunnissa - sleep(0.005) - c = sys.stdin.read(1) + keycode = sys.stdin.read(16) except KeyboardInterrupt: return Action.QUIT - if escape: - if c in "[0123456789": - continue - if c in ActionEscKeys: - return ActionEscKeys[c] - escape = 0 - continue - if c in ActionKeys: - return ActionKeys[c] - if c == '\033': - escape = 1 + if keycode: + for key, action in ActionKeys.items(): + if keycode.startswith(key): + return action + def matrix_selector(self, matrix, x, y): """ piirtää ruudukon ja antaa käyttäjän valita nuolinäppäimillä """ |