import string
import pygame
from Pinyin2Hanzi import DefaultDagParams
from Pinyin2Hanzi import dag
class TextBox:
def __init__(self, w, h, x, y, font=None, callback=None):
"""
:param w:文本框宽度
:param h:文本框高度
:param x:文本框坐标
:param y:文本框坐标
:param font:文本框中使用的字体
:param callback:在文本框按下回车键之后的回调函数
"""
self.width = w
self.height = h
self.x = x
self.y = y
self.text = "" # 文本框内容
self.callback = callback
# 创建背景surface
self.__surface = pygame.Surface((w, h))
# 如果font为None,那么效果可能不太好,建议传入font,更好调节
if font is None:
self.font = pygame.font.SysFont('microsoftyaheimicrosoftyaheiui', 16)
else:
self.font = font
self.dagparams = DefaultDagParams()
self.state = 0 # 0初始状态 1输入拼音状态
self.page = 1 # 第几页
self.limit = 5 # 显示几个汉字
self.pinyin = ''
self.word_list = [] # 候选词列表
self.word_list_surf = None # 候选词surface
self.buffer_text = '' # 联想缓冲区字符串
def create_word_list_surf(self):
"""
创建联想词surface
"""
word_list = [str(index + 1) + '.' + word for index, word in enumerate(self.word_list)] text = " ".join(word_list)
self.word_list_surf = self.font.render(text, True, (255, 255, 255))
def draw(self, dest_surf):
# 创建文字surf
text_surf = self.font.render(self.text, True, (255, 255, 255))
# 绘制背景色
dest_surf.blit(self.__surface, (self.x, self.y))
# 绘制文字
dest_surf.blit(text_surf, (self.x, self.y + (self.height - text_surf.get_height())),
(0, 0, self.width, self.height))
# 绘制联想词
if self.state == 1:
dest_surf.blit(self.word_list_surf,
(self.x, self.y + (self.height - text_surf.get_height()) - 30),