Difficult-Rocket/Difficult_Rocket/guis/widgets.py

174 lines
5.4 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-11-06 14:13:42 +08:00
from libs.pyglet import font
from libs.pyglet.text import Label
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-11-04 22:35:09 +08:00
from libs.pyglet.graphics import Batch, Group
2021-10-01 23:12:01 +08:00
from libs.pyglet.image import AbstractImage
__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 = '',
font_name: str = translate.鸿蒙简体,
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-06 14:13:42 +08:00
text_color: [int, int, int] = (0, 0, 0, 255),
out_line_color: [int, int, int] = (255, 255, 255),
2021-11-07 20:52:13 +08:00
cursor_color: [int, int, int] = (255, 255, 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-11-06 14:13:42 +08:00
self._text = message
2021-11-20 21:05:28 +08:00
self._text_position = 0
self.font = font.load(name=font_name, size=font_size,
blod=font_bold, italic=font_italic, stretch=font_stretch,
dpi=font_dpi)
self.font_height = self.font.ascent - self.font.descent0
self._select_position = [self.x, self.y, self.x, self.y + self.font_height]
self.out_bound = out_line
2021-11-06 14:13:42 +08:00
self._输入框 = Label(x=x + out_line, y=y + out_line,
width=width, height=height,
color=text_color,
font_name=font_name, font_size=font_size,
batch=batch, group=group,
text=message)
2021-11-20 21:05:28 +08:00
self._外框 = Rectangle(x=x - out_line, y=y - out_line,
2021-11-06 14:13:42 +08:00
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,
color=(63, 115, 255))
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-11-04 22:35:09 +08:00
@property
2021-11-06 14:13:42 +08:00
def text(self):
return self._text
2021-11-04 22:35:09 +08:00
2021-11-06 14:13:42 +08:00
@text.setter
def text(self, value):
assert type(value) is str, 'Input Box\'s text must be string!'
self._text = value
self._输入框.text = value
2021-11-04 22:35:09 +08:00
2021-11-20 21:05:28 +08:00
@property
def select_position(self):
return self._select_position
@select_position.setter
def select_position(self, value):
if value > len(self.text):
value = len(self.text)
if value > 0:
self._select_position = self.x
for glyphs in self.font.get_glyphs(self.text)[:value]:
self._select_position += glyphs.width
else:
self._select_position = self.x
2021-11-06 14:13:42 +08:00
@property
2021-11-13 17:02:28 +08:00
def opacity(self):
return self._输入框.opacity
@opacity.setter
def opacity(self, value: int):
assert type(value) is int, 'Input Box\'s opacity must be int!'
self._输入框.opacity = value
self._外框.opacity = value
self._光标.opacity = value
2021-11-20 21:05:28 +08:00
@property
def value(self):
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-20 21:05:28 +08:00
self._输入框.position = self._x + self.out_bound, self._y + self.out_bound
self._外框.position = self._x - self.out_bound, self._y - self.out_bound
self._光标.position = self._x + self.out_bound, self._y + self.out_bound
2021-11-13 17:02:28 +08:00
def on_text(self, text):
2021-11-20 21:05:28 +08:00
if self.enabled:
pass
2021-11-13 17:02:28 +08:00
def on_text_motion(self, motion):
pass
def on_text_motion_select(self, motion):
pass
def on_mouse_press(self, x, y, buttons, modifiers):
2021-11-20 21:05:28 +08:00
if self._check_hit(x, y):
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