Difficult-Rocket/Difficult_Rocket/guis/widgets.py

235 lines
8.5 KiB
Python
Raw Normal View History

2021-10-01 23:12:01 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2021 by shenjackyuanjie
# All rights reserved
# -------------------------------
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
2021-11-06 14:13:42 +08:00
from Difficult_Rocket import translate
2021-11-20 21:05:28 +08:00
# from libs import pyglet
2021-12-05 23:37:49 +08:00
from libs import pyglet
2021-11-06 14:13:42 +08:00
from libs.pyglet import font
from libs.pyglet.text import Label
2021-11-27 11:34:09 +08:00
from libs.pyglet.window import key
2021-10-01 23:12:01 +08:00
from libs.pyglet.gui import widgets
from libs.pyglet.sprite import Sprite
2021-11-06 14:13:42 +08:00
from libs.pyglet.shapes import Rectangle
2021-10-01 23:12:01 +08:00
from libs.pyglet.image import AbstractImage
2021-11-27 11:34:09 +08:00
from libs.pyglet.graphics import Batch, Group
2021-12-15 23:28:08 +08:00
from libs.pyglet.text.document import FormattedDocument
from libs.pyglet.text.layout import IncrementalTextLayout
2021-11-27 11:34:09 +08:00
# from libs import pyperclip
from libs.pyperclip import paste, copy
2021-10-01 23:12:01 +08:00
__all__ = ['Parts']
2021-10-01 23:12:01 +08:00
class Parts(widgets.WidgetBase):
"""
parts
"""
2021-10-01 23:12:01 +08:00
def __init__(self,
x: int,
y: int,
width: int,
height: int,
textures: AbstractImage,
batch: Batch,
parts_data: dict):
super().__init__(x, y, width, height)
self.sprite = Sprite(img=textures, x=x, y=y, batch=batch)
self._value = 0
2021-11-04 22:35:09 +08:00
class InputBox(widgets.WidgetBase):
"""
input box
"""
def __init__(self,
2021-11-13 17:02:28 +08:00
x: int, y: int, width: int, height: int,
2021-11-06 14:13:42 +08:00
message: str = '',
2021-12-15 23:28:08 +08:00
font_name: str = translate.微软等宽,
2021-11-06 14:13:42 +08:00
font_size: int = 15,
2021-11-20 21:05:28 +08:00
font_bold: bool = False,
font_italic: bool = False,
font_stretch: bool = False,
font_dpi: int = 100,
2021-11-22 06:33:22 +08:00
text_color: [int, int, int] = (187, 187, 187, 255),
out_line_color: [int, int, int] = (37, 116, 176),
cursor_color: [int, int, int] = (187, 187, 187),
select_color: [int, int, int] = (63, 115, 255),
2021-11-06 14:13:42 +08:00
out_line: int = 2,
batch: Batch = Batch(),
group: Group = Group()):
2021-11-04 22:35:09 +08:00
super().__init__(x, y, width, height)
2021-12-05 23:37:49 +08:00
self.enabled = False
2021-11-06 14:13:42 +08:00
self._text = message
2021-12-04 01:15:23 +08:00
self._cursor_poi = 0
2021-11-20 21:05:28 +08:00
self.font = font.load(name=font_name, size=font_size,
2021-12-05 23:37:49 +08:00
bold=font_bold, italic=font_italic, stretch=font_stretch,
2021-11-20 21:05:28 +08:00
dpi=font_dpi)
2021-11-22 06:33:22 +08:00
self.font_height = self.font.ascent - self.font.descent
2021-11-20 21:05:28 +08:00
self.out_bound = out_line
2021-12-15 23:28:08 +08:00
# 基于IncrementalTextLayout的处理系统
self._doc = FormattedDocument(message)
# self._doc.set_style()
self._layout = IncrementalTextLayout(self._doc, self.font, width, height,
batch=batch, group=group)
# 基于Label的处理系统
2021-11-27 11:34:09 +08:00
self._input_box = Label(x=x + out_line, y=y + out_line,
2021-12-15 23:28:08 +08:00
width=width, height=self.font_height + self.out_bound * 2,
2021-11-27 11:34:09 +08:00
color=text_color,
font_name=font_name, font_size=font_size,
batch=batch, group=group,
text=message)
self._out_box = Rectangle(x=x - out_line, y=y - out_line,
color=out_line_color,
width=width + (out_line * 2), height=height + (out_line * 2),
batch=batch, group=group)
2021-11-20 21:05:28 +08:00
self._光标 = Rectangle(x=x + out_line, y=y + out_line,
2021-11-07 20:52:13 +08:00
color=cursor_color,
2021-11-20 21:05:28 +08:00
width=1, height=self.font_height,
2021-11-06 14:13:42 +08:00
batch=batch, group=group)
2021-11-20 21:05:28 +08:00
self._选择框 = Rectangle(x=x, y=y, width=0, height=self.font_height,
2021-11-22 06:33:22 +08:00
color=select_color)
2021-11-20 21:05:28 +08:00
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,
text='')
2021-11-04 22:35:09 +08:00
2021-11-13 17:02:28 +08:00
"""
输入框的属性
"""
2021-12-04 01:15:23 +08:00
# 本身属性
2021-11-04 22:35:09 +08:00
@property
2021-12-04 01:15:23 +08:00
def text(self) -> str: # 输入框的文本
2021-11-06 14:13:42 +08:00
return self._text
2021-11-04 22:35:09 +08:00
2021-11-06 14:13:42 +08:00
@text.setter
2021-11-22 06:33:22 +08:00
def text(self, value) -> None:
2021-11-06 14:13:42 +08:00
assert type(value) is str, 'Input Box\'s text must be string!'
self._text = value
2021-11-27 11:34:09 +08:00
self._input_box.text = value
2021-11-04 22:35:09 +08:00
2021-11-20 21:05:28 +08:00
@property
2021-12-04 01:15:23 +08:00
def cursor_poi(self) -> int: # 光标位置
return self._cursor_poi
@cursor_poi.setter
def cursor_poi(self, value) -> None:
assert type(value) is int, 'Input Box\'s cursor poi must be int!'
self._cursor_poi = value
2021-12-15 23:28:08 +08:00
self._光标.x = self.x + self.out_bound + self._input_box.content_width
2021-12-04 01:15:23 +08:00
# 渲染时属性
@property
def opacity(self) -> int: # 透明度
2021-11-27 11:34:09 +08:00
return self._input_box.opacity
2021-11-13 17:02:28 +08:00
@opacity.setter
2021-11-22 06:33:22 +08:00
def opacity(self, value: int) -> None:
2021-11-13 17:02:28 +08:00
assert type(value) is int, 'Input Box\'s opacity must be int!'
2021-11-27 11:34:09 +08:00
self._input_box.opacity = value
self._out_box.opacity = value
self._选择的字.opacity = value
self._选择框.opacity = value
2021-11-13 17:02:28 +08:00
self._光标.opacity = value
2021-11-27 11:37:07 +08:00
@property
2021-12-04 01:15:23 +08:00
def visible(self) -> bool: # 是否可见
2021-11-27 11:34:09 +08:00
return self._input_box.visible
@visible.setter
def visible(self, value: bool) -> None:
assert type(value) is bool, 'Input Box\'s visible must be bool!'
self._input_box.visible = value
self._out_box.visible = value
self._选择的字.visible = value
self._选择框.visible = value
self._光标.visible = value
2021-11-20 21:05:28 +08:00
@property
2021-11-22 06:33:22 +08:00
def value(self) -> str:
2021-11-20 21:05:28 +08:00
return self._text
2021-11-13 17:02:28 +08:00
"""
事件调用
"""
2021-11-04 22:35:09 +08:00
2021-11-06 14:13:42 +08:00
def _update_position(self):
2021-11-27 11:34:09 +08:00
self._input_box.position = self._x + self.out_bound, self._y + self.out_bound
self._out_box.position = self._x - self.out_bound, self._y - self.out_bound
2021-11-20 21:05:28 +08:00
self._光标.position = self._x + self.out_bound, self._y + self.out_bound
2021-11-13 17:02:28 +08:00
2021-12-04 01:15:23 +08:00
# 输入东西
2021-11-22 06:33:22 +08:00
def on_text(self, text: str):
2021-11-20 21:05:28 +08:00
if self.enabled:
2021-11-22 06:33:22 +08:00
if text in ('\r', '\n'):
if self.text:
self.dispatch_event('on_commit', self.text)
else:
2021-12-04 01:15:23 +08:00
self.text = f'{self.text[:self.cursor_poi]}{text}{self.text[self.cursor_poi:]}'
2021-12-05 23:37:49 +08:00
self.cursor_poi += len(text)
2021-11-13 17:02:28 +08:00
2021-12-04 01:15:23 +08:00
# 移动光标
2021-11-13 17:02:28 +08:00
def on_text_motion(self, motion):
2021-12-04 01:15:23 +08:00
if self.enabled:
# 根据按键处理
# 单格移动光标(上下左右)
if motion in (key.MOTION_UP, key.MOTION_LEFT): # 往上一个移动
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)
# 大前后移动(开头或结尾)
elif motion in (key.MOTION_BEGINNING_OF_LINE, key.MOTION_BEGINNING_OF_FILE, key.MOTION_PREVIOUS_PAGE): # 开头
self.cursor_poi = 0
elif motion in (key.MOTION_END_OF_LINE, key.MOTION_END_OF_FILE, key.MOTION_NEXT_PAGE): # 结尾
self.cursor_poi = len(self.text)
2021-12-05 23:37:49 +08:00
# 删除操作
elif motion == key.MOTION_BACKSPACE:
if self.text: # 如果有文字
self.text = f'{self.text[:self.cursor_poi - 1]}{self.text[self.cursor_poi:]}'
self.cursor_poi = max(0, self._cursor_poi - 1)
elif motion == key.MOTION_DELETE:
if self.text and self.cursor_poi != len(self.text) - 1: # 如果有文字,并且光标不在最后
self.text = f'{self.text[:self.cursor_poi]}{self.text[self.cursor_poi + 1:]}'
# 剪贴板操作
elif motion == key.MOTION_COPY:
pass
elif motion == key.MOTION_PASTE:
paste_text = paste()
self.text = f'{self.text[:self.cursor_poi]}{paste_text}{self.text[self.cursor_poi:]}'
self.cursor_poi += len(paste_text)
2021-11-13 17:02:28 +08:00
def on_text_motion_select(self, motion):
pass
def on_mouse_press(self, x, y, buttons, modifiers):
2021-11-27 11:34:09 +08:00
if self._check_hit(x, y) and self._input_box.visible:
2021-11-20 21:05:28 +08:00
self.enabled = True
else:
self.enabled = False
2021-11-13 17:02:28 +08:00
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
2021-12-05 23:37:49 +08:00
InputBox.register_event_type('on_commit')