Octopus_Carnival/ono_sendai/widgets.py

119 lines
3.3 KiB
Python
Raw Normal View History

2019-08-17 17:40:16 +02:00
from picotui.widgets import *
from picotui.defs import *
from colorama import Style, Fore
red = f'{Fore.RED}'.encode('utf-8')
2019-08-17 18:21:18 +02:00
blue = f'{Fore.BLUE}'.encode('utf-8')
2019-08-17 17:40:16 +02:00
green = f'{Fore.GREEN}'.encode('utf-8')
white = f'{Fore.WHITE}'.encode('utf-8')
class WColoredButton(WButton):
color = C_GREEN
def __init__(self, w, text, color):
super().__init__(w, text)
self.color = color
def redraw(self):
self.goto(self.x, self.y)
if self.disabled:
self.attr_color(C_WHITE, C_GRAY)
else:
self.attr_color(C_WHITE, self.color)
self.wr(self.t.center(self.w))
self.attr_reset()
def handle_mouse(self, *args, **kwargs):
r = super().handle_mouse(args, kwargs)
self.on_click(self)
return r
2019-08-18 17:19:11 +02:00
def handle_key(self, inp):
2019-08-21 18:28:41 +02:00
if inp in [KEY_ENTER, b's', b'd', b'm', b'p']:
2019-08-20 16:39:33 +02:00
return inp
2019-08-18 17:19:11 +02:00
pass
2019-08-17 17:40:16 +02:00
def on_click(self, *args, **kwargs):
pass
class WColoredFrame(WFrame):
color = None
rst = f'{Style.RESET_ALL}'.encode('utf-8')
def __init__(self, w, h, title="", color=white):
title = color.decode('utf-8') + title + self.rst.decode('utf-8')
super().__init__(w, h, title)
self.color = color
2019-08-20 16:39:33 +02:00
2019-08-18 17:19:11 +02:00
def handle_key(self, inp):
2019-08-21 18:28:41 +02:00
if inp in [KEY_ENTER, b's', b'd', b'm', b'p']:
2019-08-20 16:39:33 +02:00
return inp
2019-08-18 17:19:11 +02:00
pass
2019-08-17 17:40:16 +02:00
def draw_box(self, left, top, width, height):
# Use http://www.utf8-chartable.de/unicode-utf8-table.pl
# for utf-8 pseudographic reference
bottom = top + height - 1
self.goto(left, top)
# "┌"
self.wr(self.color+b"\xe2\x94\x8c"+self.rst)
# "─"
hor = self.color+ b"\xe2\x94\x80" * (width - 2) +self.rst
self.wr(hor)
# "┐"
self.wr(self.color + b"\xe2\x94\x90" + self.rst)
self.goto(left, bottom)
# "└"
self.wr(self.color + b"\xe2\x94\x94" + self.rst)
self.wr(hor)
# "┘"
self.wr(self.color + b"\xe2\x94\x98" + self.rst)
top += 1
while top < bottom:
# "│"
bar = self.color + b"\xe2\x94\x82" + self.rst
self.goto(left, top)
self.wr(bar)
self.goto(left + width - 1, top)
self.wr(bar)
top += 1
class WCardRadioButton(WRadioButton):
isHand = False
2019-08-18 17:19:11 +02:00
def __init__(self, items, id, cb, isHand=False):
2019-08-17 17:40:16 +02:00
super().__init__(items)
self.choice = 1
self.isHand = isHand
2019-08-18 17:19:11 +02:00
self.cb = cb
self.id = id
def handle_key(self, inp):
2019-08-21 18:28:41 +02:00
if inp in [KEY_ENTER, b's', b'd', b'm', b'p']:
2019-08-20 16:39:33 +02:00
return inp
pass
2019-08-17 17:40:16 +02:00
def redraw(self):
i = 0
if self.focus:
self.attr_color(C_B_BLUE, None)
for t in self.items:
self.goto(self.x, self.y + i)
self.wr("(*) " if self.choice == i and i > 1
else "[-] " if self.choice == 0 and i == 0 and not self.isHand
else "[ ] " if i == 0 and not self.isHand
else "( ) " if i > 1 else " ")
self.wr(t)
i += 1
self.attr_reset()
def handle_mouse(self, x, y):
newchoice = y - self.y
self.choice = 1 if self.choice == newchoice else newchoice
2019-08-18 17:19:11 +02:00
if self.choice != 1:
self.cb(self.id)
2019-08-17 17:40:16 +02:00
self.redraw()
self.signal("changed")