看起来好一些

This commit is contained in:
shenjack 2023-09-03 00:29:05 +08:00
parent 9c2bc30c77
commit 7ee98a4e01
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -23,7 +23,7 @@ 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 Difficult_Rocket.api.types import Fonts from Difficult_Rocket.api.types import Fonts, Options, FontData
# from Difficult_Rocket import DR_status # from Difficult_Rocket import DR_status
@ -31,70 +31,16 @@ from Difficult_Rocket.api.types import Fonts
RGBA = Tuple[int, int, int, int] RGBA = Tuple[int, int, int, int]
class BaseTheme: class ButtonThemeOptions(Options):
""" """ 基于 Options 写的 ButtonTheme """
用于定义主题的类 name = 'ButtonTheme'
""" untouched_color: RGBA = (39, 73, 114, 255)
touched_color: RGBA = (66, 150, 250, 255)
hit_color: RGBA = (15, 135, 250, 255)
font_theme: FontData = FontData()
def __init__(self, def __str__(self):
main_color: RGBA = (39, 73, 114, 255), return self.as_markdown()
secondary_color: RGBA = (66, 150, 250, 255),
main_font: str = Fonts.鸿蒙简体,
):
self.main_color = main_color
self.secondary_color = secondary_color
self.main_font = main_font
def __repr__(self) -> str:
return (f'<{self.__class__.__name__} main_color={self.main_color} '
f'secondary_color={self.secondary_color} main_font={self.main_font}>')
class FontTheme(BaseTheme):
"""
字体的主题
"""
def __init__(self,
main_color: RGBA = (39, 73, 114, 255),
secondary_color: RGBA = (66, 150, 250, 255),
main_font: str = Fonts.鸿蒙简体,
font_size: Optional[int] = 15,
font_bold: Optional[bool] = False,
font_italic: Optional[bool] = False,
font_stretch: Optional[bool] = False,
):
super().__init__(main_color, secondary_color, main_font)
self.font_size = font_size
self.font_bold = font_bold
self.font_italic = font_italic
self.font_stretch = font_stretch
def __repr__(self) -> str:
return (f'<{self.__class__.__name__} main_color={self.main_color} '
f'secondary_color={self.secondary_color} main_font={self.main_font} '
f'font_size={self.font_size} font_bold={self.font_bold} '
f'font_italic={self.font_italic} font_stretch={self.font_stretch}>')
class ButtonTheme(BaseTheme):
"""
Button 的主题
"""
def __init__(self,
main_color: RGBA = (39, 73, 114, 255),
secondary_color: RGBA = (66, 150, 250, 255),
hit_color: RGBA = (15, 135, 250, 255),
main_font: str = Fonts.鸿蒙简体,
font_theme: Optional[FontTheme] = None,
):
super().__init__(main_color, secondary_color, main_font)
self.font_theme = font_theme or FontTheme()
self.hit_color = hit_color
def __repr__(self) -> str:
return (f'<{self.__class__.__name__} main_color={self.main_color} '
f'secondary_color={self.secondary_color} main_font={self.main_font} '
f'font_theme={self.font_theme} hit_color={self.hit_color}>')
class PressTextButton(widgets.WidgetBase): class PressTextButton(widgets.WidgetBase):
@ -110,7 +56,7 @@ class PressTextButton(widgets.WidgetBase):
text: str, text: str,
batch: Optional[Batch] = None, batch: Optional[Batch] = None,
group: Optional[Group] = None, group: Optional[Group] = None,
theme: Optional[ButtonTheme] = None, theme: Optional[ButtonThemeOptions] = None,
): ):
super().__init__(x, y, width, height) super().__init__(x, y, width, height)
self.main_batch = batch or Batch() self.main_batch = batch or Batch()
@ -118,23 +64,24 @@ class PressTextButton(widgets.WidgetBase):
self.back_ground_group = Group(order=5, parent=group) self.back_ground_group = Group(order=5, parent=group)
self.pressed = False self.pressed = False
self.theme = theme or ButtonTheme() self.theme = theme or ButtonThemeOptions()
print(self.theme)
self.untouched_color = self.theme.main_color self.untouched_color = self.theme.untouched_color
self.touched_color = self.theme.secondary_color self.touched_color = self.theme.touched_color
self.hit_color = self.theme.hit_color self.hit_color = self.theme.hit_color
# from ImGui # from ImGui
self.text = text self.text = text
self.text_label = Label(font_name=self.theme.font_theme.main_font, font_size=self.theme.font_theme.font_size, self.text_label = Label(font_name=self.theme.font_theme.font_name, font_size=self.theme.font_theme.font_size,
batch=self.main_batch, group=self.front_group, batch=self.main_batch, group=self.front_group,
x=self._x, y=self._y, width=self._width, x=self._x, y=self._y, width=self._width,
height=self._height,) height=self._height,)
self.font = pyglet.font.load(self.theme.font_theme.main_font, self.font = pyglet.font.load(self.theme.font_theme.font_name,
self.theme.font_theme.font_size, self.theme.font_theme.font_size,
bold=self.theme.font_theme.font_bold, bold=self.theme.font_theme.bold,
italic=self.theme.font_theme.font_italic, italic=self.theme.font_theme.italic,
stretch=self.theme.font_theme.font_stretch) stretch=self.theme.font_theme.stretch)
self.font_height = self.font.ascent - self.font.descent self.font_height = self.font.ascent - self.font.descent
self.back_rec = Rectangle(x=self._x, y=self._y, self.back_rec = Rectangle(x=self._x, y=self._y,
width=self._width, height=self._height, width=self._width, height=self._height,