feat: update widget

This commit is contained in:
shenjack 2023-01-01 17:58:40 +08:00
parent 43828b97d8
commit 0a14f4a44d
2 changed files with 242 additions and 220 deletions

View File

@ -11,45 +11,47 @@ github: @shenjackyuanjie
gitee: @shenjackyuanjie gitee: @shenjackyuanjie
""" """
from typing import Optional, Union, Tuple
# from libs import pyglet # from libs import pyglet
from pyglet import font from pyglet import font
from pyglet.text import Label, HTMLLabel from pyglet.text import Label, HTMLLabel
from pyglet.window import key # from pyglet.window import key
from pyglet.gui import widgets from pyglet.gui import widgets
from pyglet.sprite import Sprite # from pyglet.sprite import Sprite
from pyglet.shapes import Rectangle from pyglet.shapes import Rectangle
from pyglet.image import AbstractImage # from pyglet.image import AbstractImage
from pyglet.graphics import Batch, Group from pyglet.graphics import Batch, Group
from pyglet.text.document import FormattedDocument from pyglet.text.document import FormattedDocument
from pyglet.text.layout import IncrementalTextLayout from pyglet.text.layout import IncrementalTextLayout
# from libs import pyperclip # from libs import pyperclip
from libs.pyperclip import paste # from libs.pyperclip import paste
from Difficult_Rocket.utils import translate from Difficult_Rocket.utils import translate
from Difficult_Rocket.client.guis.format import html from Difficult_Rocket.client.guis.format import html
from Difficult_Rocket import DR_option from Difficult_Rocket import DR_option
__all__ = ['Parts', 'InputBox'] __all__ = ['InputBox']
class Parts(widgets.WidgetBase): # class Parts(widgets.WidgetBase):
""" # """
parts # parts
""" # """
#
def __init__(self, # def __init__(self,
x: int, # x: int,
y: int, # y: int,
width: int, # width: int,
height: int, # height: int,
textures: AbstractImage, # textures: AbstractImage,
# textures, # # textures,
batch: Batch, # batch: Batch,
parts_data: dict): # parts_data: dict):
super().__init__(x, y, width, height) # super().__init__(x, y, width, height)
self.sprite = Sprite(img=textures, x=x, y=y, batch=batch) # self.sprite = Sprite(img=textures, x=x, y=y, batch=batch)
self._value = 0 # self._value = 0
class TextButton(widgets.WidgetBase): class TextButton(widgets.WidgetBase):
@ -60,10 +62,11 @@ class TextButton(widgets.WidgetBase):
def __init__(self, def __init__(self,
x: int, y: int, width: int, height: int, x: int, y: int, width: int, height: int,
text: str, text: str,
font: str): font: str = translate.鸿蒙简体, font_size: int = 13):
super().__init__(x, y, width, height) super().__init__(x, y, width, height)
self.text = text self.text = text
self.text_label = Label() self.text_label = Label(
font_name=font, font_size=font_size)
@property @property
def value(self): def value(self):
@ -76,200 +79,209 @@ class TextButton(widgets.WidgetBase):
if not DR_option.InputBox_use_TextEntry: if not DR_option.InputBox_use_TextEntry:
class InputBox(widgets.WidgetBase): class InputBox(widgets.WidgetBase):
""" """ 自定义的输入框 """
input box
""" def __init__(self, x: int, y: int, width: int,
message: str,
batch: Optional[Batch], group: Optional[Group]):
def __init__(self, super().__init__(x=x, y=y, width=width)
x: int, y: int, width: int, height: int, ...
message: str = '', # class InputBox(widgets.WidgetBase):
font_name: str = translate.微软等宽, # """
font_size: int = 15, # input box
font_bold: bool = False, # """
font_italic: bool = False, #
font_stretch: bool = False, # def __init__(self,
font_dpi: int = 100, # x: int, y: int, width: int, height: int,
text_color: [int, int, int] = (187, 187, 187, 255), # message: str = '',
out_line_color: [int, int, int] = (37, 116, 176), # font_name: str = translate.微软等宽,
cursor_color: [int, int, int] = (187, 187, 187), # font_size: int = 15,
select_color: [int, int, int] = (63, 115, 255), # font_bold: bool = False,
out_line: int = 2, # font_italic: bool = False,
batch: Batch = None, # font_stretch: bool = False,
group: Group = None): # font_dpi: int = 100,
if batch is None: # text_color: [int, int, int] = (187, 187, 187, 255),
batch = Batch() # out_line_color: [int, int, int] = (37, 116, 176),
if group is None: # cursor_color: [int, int, int] = (187, 187, 187),
group = Group() # select_color: [int, int, int] = (63, 115, 255),
super().__init__(x, y, width, height) # out_line: int = 2,
self.enabled = False # batch: Batch = None,
self._text = message # group: Group = None):
self._cursor_poi = 0 # if batch is None:
self.font = font.load(name=font_name, size=font_size, # batch = Batch()
bold=font_bold, italic=font_italic, stretch=font_stretch, # if group is None:
dpi=font_dpi) # group = Group()
self.font_height = self.font.ascent - self.font.descent # super().__init__(x, y, width, height)
self.out_bound = out_line # self.enabled = False
if DR_option.InputBox_use_TextEntry: # self._text = message
# 基于IncrementalTextLayout的处理系统 # self._cursor_poi = 0
self._doc = FormattedDocument(message) # self.font = font.load(name=font_name, size=font_size,
# self._doc.set_style() # bold=font_bold, italic=font_italic, stretch=font_stretch,
self._layout = IncrementalTextLayout(self._doc, self.font, width, height, # dpi=font_dpi)
batch=batch, group=group) # self.font_height = self.font.ascent - self.font.descent
self._input_box = widgets.TextEntry(x, y, width, height, # self.out_bound = out_line
self._doc, self._layout, # if DR_option.InputBox_use_TextEntry:
batch=batch, group=group) # # 基于IncrementalTextLayout的处理系统
else: # self._doc = FormattedDocument(message)
# 基于Label的处理系统 # # self._doc.set_style()
self._input_box = Label(x=x + out_line, y=y + out_line, # self._layout = IncrementalTextLayout(self._doc, self.font, width, height,
width=width, height=self.font_height + self.out_bound * 2, # batch=batch, group=group)
color=text_color, # self._input_box = widgets.TextEntry(x, y, width, height,
font_name=font_name, font_size=font_size, # self._doc, self._layout,
batch=batch, group=group, # batch=batch, group=group)
text=message) # else:
self._HTML_box = HTMLLabel(x=x + out_line, y=y + out_line + 30, # # 基于Label的处理系统
width=width, height=self.font_height + self.out_bound * 2, # self._input_box = Label(x=x + out_line, y=y + out_line,
batch=batch, group=group, # width=width, height=self.font_height + self.out_bound * 2,
text=message) # color=text_color,
self._out_box = Rectangle(x=x - out_line, y=y - out_line, # font_name=font_name, font_size=font_size,
color=out_line_color, # batch=batch, group=group,
width=width + (out_line * 2), height=height + (out_line * 2), # text=message)
batch=batch, group=group) # self._HTML_box = HTMLLabel(x=x + out_line, y=y + out_line + 30,
self._光标 = Rectangle(x=x + out_line, y=y + out_line, # width=width, height=self.font_height + self.out_bound * 2,
color=cursor_color, # batch=batch, group=group,
width=1, height=self.font_height, # text=message)
batch=batch, group=group) # self._out_box = Rectangle(x=x - out_line, y=y - out_line,
self._选择框 = Rectangle(x=x, y=y, width=0, height=self.font_height, # color=out_line_color,
color=select_color) # width=width + (out_line * 2), height=height + (out_line * 2),
self._选择的字 = Label(x=x, y=y, width=0, height=self.font_height, # batch=batch, group=group)
color=text_color, # self._光标 = Rectangle(x=x + out_line, y=y + out_line,
font_name=font_name, font_size=font_size, # color=cursor_color,
batch=batch, group=group, # width=1, height=self.font_height,
text='') # batch=batch, group=group)
# self._选择框 = Rectangle(x=x, y=y, width=0, height=self.font_height,
""" # color=select_color)
输入框的属性 # self._选择的字 = Label(x=x, y=y, width=0, height=self.font_height,
""" # color=text_color,
# font_name=font_name, font_size=font_size,
# 本身属性 # batch=batch, group=group,
@property # text='')
def text(self) -> str: # 输入框的文本 #
return self._text # """
# 输入框的属性
@text.setter # """
def text(self, value) -> None: #
assert type(value) is str, 'Input Box\'s text must be string!' # # 本身属性
self._text = value # @property
self._input_box.text = value # def text(self) -> str: # 输入框的文本
self._HTML_box.text = html.decode_text2HTML(value, show_style=True) # return self._text
#
@property # @text.setter
def cursor_poi(self) -> int: # 光标位置 # def text(self, value) -> None:
return self._cursor_poi # assert type(value) is str, 'Input Box\'s text must be string!'
# self._text = value
@cursor_poi.setter # self._input_box.text = value
def cursor_poi(self, value) -> None: # self._HTML_box.text = html.decode_text2HTML(value, show_style=True)
assert type(value) is int, 'Input Box\'s cursor poi must be int!' #
self._cursor_poi = value # @property
self._光标.x = self.x + self.out_bound + self._input_box.content_width # def cursor_poi(self) -> int: # 光标位置
# return self._cursor_poi
# 渲染时属性 #
@property # @cursor_poi.setter
def opacity(self) -> int: # 透明度 # def cursor_poi(self, value) -> None:
return self._input_box.opacity # assert type(value) is int, 'Input Box\'s cursor poi must be int!'
# self._cursor_poi = value
@opacity.setter # self._光标.x = self.x + self.out_bound + self._input_box.content_width
def opacity(self, value: int) -> None: #
assert type(value) is int, 'Input Box\'s opacity must be int!' # # 渲染时属性
self._input_box.opacity = value # @property
self._out_box.opacity = value # def opacity(self) -> int: # 透明度
self._选择的字.opacity = value # return self._input_box.opacity
self._选择框.opacity = value #
self._光标.opacity = value # @opacity.setter
# def opacity(self, value: int) -> None:
@property # assert type(value) is int, 'Input Box\'s opacity must be int!'
def visible(self) -> bool: # 是否可见 # self._input_box.opacity = value
return self._input_box.visible # self._out_box.opacity = value
# self._选择的字.opacity = value
@visible.setter # self._选择框.opacity = value
def visible(self, value: bool) -> None: # self._光标.opacity = value
assert type(value) is bool, 'Input Box\'s visible must be bool!' #
self._input_box.visible = value # @property
self._out_box.visible = value # def visible(self) -> bool: # 是否可见
self._选择的字.visible = value # return self._input_box.visible
self._选择框.visible = value #
self._光标.visible = value # @visible.setter
# def visible(self, value: bool) -> None:
@property # assert type(value) is bool, 'Input Box\'s visible must be bool!'
def value(self) -> str: # self._input_box.visible = value
return self._text # self._out_box.visible = value
# self._选择的字.visible = value
""" # self._选择框.visible = value
事件调用 # self._光标.visible = value
""" #
# @property
def _update_position(self): # def value(self) -> str:
self._input_box.position = self._x + self.out_bound, self._y + self.out_bound # return self._text
self._out_box.position = self._x - self.out_bound, self._y - self.out_bound #
self._光标.position = self._x + self.out_bound, self._y + self.out_bound # """
# 事件调用
# 输入东西 # """
def on_text(self, text: str): #
if self.enabled: # def _update_position(self):
if text in ('\r', '\n'): # self._input_box.position = self._x + self.out_bound, self._y + self.out_bound
if self.text: # self._out_box.position = self._x - self.out_bound, self._y - self.out_bound
self.dispatch_event('on_commit', self.text) # self._光标.position = self._x + self.out_bound, self._y + self.out_bound
else: #
self.text = f'{self.text[:self.cursor_poi]}{text}{self.text[self.cursor_poi:]}' # # 输入东西
self.cursor_poi += len(text) # def on_text(self, text: str):
# if self.enabled:
# 移动光标 # if text in ('\r', '\n'):
def on_text_motion(self, motion): # if self.text:
if self.enabled: # self.dispatch_event('on_commit', self.text)
# 根据按键处理 # else:
# 单格移动光标(上下左右) # self.text = f'{self.text[:self.cursor_poi]}{text}{self.text[self.cursor_poi:]}'
if motion in (key.MOTION_UP, key.MOTION_LEFT): # 往上一个移动 # self.cursor_poi += len(text)
self.cursor_poi = max(0, self._cursor_poi - 1) #
elif motion in (key.MOTION_DOWN, key.MOTION_RIGHT): # 往下一个移动 # # 移动光标
self.cursor_poi = min(len(self.text), self._cursor_poi + 1) # def on_text_motion(self, motion):
# 大前后移动(开头或结尾) # if self.enabled:
elif motion in ( # # 根据按键处理
key.MOTION_BEGINNING_OF_LINE, key.MOTION_BEGINNING_OF_FILE, key.MOTION_PREVIOUS_PAGE): # 开头 # # 单格移动光标(上下左右)
self.cursor_poi = 0 # if motion in (key.MOTION_UP, key.MOTION_LEFT): # 往上一个移动
elif motion in (key.MOTION_END_OF_LINE, key.MOTION_END_OF_FILE, key.MOTION_NEXT_PAGE): # 结尾 # self.cursor_poi = max(0, self._cursor_poi - 1)
self.cursor_poi = len(self.text) # elif motion in (key.MOTION_DOWN, key.MOTION_RIGHT): # 往下一个移动
# 删除操作 # self.cursor_poi = min(len(self.text), self._cursor_poi + 1)
elif motion == key.MOTION_BACKSPACE: # # 大前后移动(开头或结尾)
if self.text: # 如果有文字 # elif motion in (
self.text = f'{self.text[:self.cursor_poi - 1]}{self.text[self.cursor_poi:]}' # key.MOTION_BEGINNING_OF_LINE, key.MOTION_BEGINNING_OF_FILE, key.MOTION_PREVIOUS_PAGE): # 开头
self.cursor_poi = max(0, self._cursor_poi - 1) # self.cursor_poi = 0
elif motion == key.MOTION_DELETE: # elif motion in (key.MOTION_END_OF_LINE, key.MOTION_END_OF_FILE, key.MOTION_NEXT_PAGE): # 结尾
if self.text and self.cursor_poi != len(self.text) - 1: # 如果有文字,并且光标不在最后 # self.cursor_poi = len(self.text)
self.text = f'{self.text[:self.cursor_poi]}{self.text[self.cursor_poi + 1:]}' # # 删除操作
# 剪贴板操作 # elif motion == key.MOTION_BACKSPACE:
elif motion == key.MOTION_COPY: # if self.text: # 如果有文字
pass # self.text = f'{self.text[:self.cursor_poi - 1]}{self.text[self.cursor_poi:]}'
elif motion == key.MOTION_PASTE: # self.cursor_poi = max(0, self._cursor_poi - 1)
paste_text = paste() # elif motion == key.MOTION_DELETE:
self.text = f'{self.text[:self.cursor_poi]}{paste_text}{self.text[self.cursor_poi:]}' # if self.text and self.cursor_poi != len(self.text) - 1: # 如果有文字,并且光标不在最后
self.cursor_poi += len(paste_text) # self.text = f'{self.text[:self.cursor_poi]}{self.text[self.cursor_poi + 1:]}'
# # 剪贴板操作
def on_text_motion_select(self, motion): # elif motion == key.MOTION_COPY:
pass # pass
# elif motion == key.MOTION_PASTE:
def on_mouse_press(self, x, y, buttons, modifiers): # paste_text = paste()
if self._check_hit(x, y) and self._input_box.visible: # self.text = f'{self.text[:self.cursor_poi]}{paste_text}{self.text[self.cursor_poi:]}'
self.enabled = True # self.cursor_poi += len(paste_text)
else: #
self.enabled = False # def on_text_motion_select(self, motion):
# pass
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): #
pass # def on_mouse_press(self, x, y, buttons, modifiers):
# if self._check_hit(x, y) and self._input_box.visible:
def on_mouse_release(self, x, y, buttons, modifiers): # self.enabled = True
pass # else:
# self.enabled = False
def on_commit(self, text: str): #
pass # def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
# pass
#
# def on_mouse_release(self, x, y, buttons, modifiers):
# pass
#
# def on_commit(self, text: str):
# pass
InputBox.register_event_type('on_commit') InputBox.register_event_type('on_commit')

View File

@ -14,7 +14,7 @@ gitee: @shenjackyuanjie
# system function # system function
import time import time
from typing import Union from typing import Union, Optional, Tuple
from decimal import Decimal from decimal import Decimal
# from pyglet # from pyglet
@ -30,12 +30,21 @@ from Difficult_Rocket.command.api import CommandText
from Difficult_Rocket.utils.new_thread import new_thread from Difficult_Rocket.utils.new_thread import new_thread
class CommandLineTextEntry(widgets.TextEntry): class CommandLineTextEntry(widgets.TextEntry):
""" """
基于 Text Entry 重写的 Command Line 基于 Text Entry 重写的 Command Line
""" """
def __init__(self, x: int, y: int, width: int,
color: Optional[Tuple[int, int, int, int]] = (255, 255, 255, 255),
text_color: Optional[Tuple[int, int, int, int]] = (0, 0, 0, 255),
caret_color: Optional[Tuple[int, int, int, int]] = (0, 0, 0),
batch: Optional[Batch] = None, group: Optional[Group] = None):
super().__init__(x=x, y=y, width=width,
color=color, text_color=text_color, caret_color=caret_color,
batch=batch, group=group, text='')
class CommandLine(widgets.WidgetBase): class CommandLine(widgets.WidgetBase):
""" """
@ -172,7 +181,8 @@ class CommandLine(widgets.WidgetBase):
self._label[0].visible = True self._label[0].visible = True
time.sleep(wait) time.sleep(wait)
if self._label[0].visible and not self.editing: if self._label[0].visible and not self.editing:
while (self._label[0].opacity >= 30) and self._label[0].visible and (self._label[0] is this) and not self.editing: while (self._label[0].opacity >= 30) and self._label[0].visible and (
self._label[0] is this) and not self.editing:
# (label 的透明度不是 0) and (label 还在显示) and (label 还是载入线程时候的那个label) and (现在不在输入新行) # (label 的透明度不是 0) and (label 还在显示) and (label 还是载入线程时候的那个label) and (现在不在输入新行)
self._label[0].opacity -= 2 self._label[0].opacity -= 2
time.sleep(0.01) time.sleep(0.01)