-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKanjiTerminal.py
57 lines (50 loc) · 1.94 KB
/
KanjiTerminal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import displayio as dpio
from RictyKanji import draw_char, set_palette, han2zen
import time
import bitmaptools as bmt
class KanjiTerminal(dpio.Group) :
def __init__(self, width, height, scroll = 1) :
"""Set up Kanji terminal of (width, height) size. Number of scroll
lines at a time can be specified. A Hankaku-character is
displayed in Zenkaku.
"""
super().__init__()
self.bmp = dpio.Bitmap(width << 4, height << 4, 4)
self.pal = dpio.Palette(4)
set_palette(self.pal)
self.tg = dpio.TileGrid(self.bmp, pixel_shader = self.pal)
self.width = width
self.height = height
self.scroll = max(1, min(height-1, scroll))
self.delay = None # scroll delay
self.curs_x = 0
self.curs_y = 0
self.append(self.tg)
def cls(self) :
"""Clear whole screen and move cursor at (0, 0)."""
self.bmp.fill(0)
self.curs_x = 0
self.curs_y = 0
def locate(self, x, y) :
"""Move cursor to (x, y) position."""
self.curs_x = max(0, min(x, self.width -1))
self.curs_y = max(0, min(y, self.height -1))
def vscroll(self, n = None) :
"""Scroll up n lines"""
if n is None : n = self.scroll
self.curs_y = max(0, self.curs_y - n)
n <<= 4
bmp = self.bmp
bmp.blit(0, 0, bmp, x1 = 0, y1 = n, x2 = bmp.width, y2 = bmp.height)
bmt.fill_region(bmp, 0, bmp.height - n, bmp.width, bmp.height, 0)
def print(self, txt, delay = 0) :
for c in han2zen(txt) :
if self.curs_x >= self.width or c == "\n":
self.curs_x = 0
self.curs_y += 1
if self.curs_y >= self.height :
self.vscroll()
if c != "\n" :
draw_char(self.bmp, self.curs_x << 4, self.curs_y << 4, c)
self.curs_x += 1
time.sleep(delay)