Difficult-Rocket/Difficult_Rocket/gui/widget/button.py
2023-09-02 11:46:55 +08:00

196 lines
6.8 KiB
Python

# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
from typing import Optional, Union, Tuple
# from libs import pyglet
import pyglet
from pyglet.text import Label
from pyglet.gui import widgets
from pyglet.window import mouse
# from pyglet.sprite import Sprite
from pyglet.shapes import Rectangle
# from pyglet.image import AbstractImage
from pyglet.graphics import Batch, Group
from Difficult_Rocket.api.types import Fonts
# from Difficult_Rocket import DR_status
RGBA = Tuple[int, int, int, int]
class BaseTheme:
"""
用于定义主题的类
"""
def __init__(self,
main_color: RGBA = (39, 73, 114, 255),
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):
"""
自带 字符 + 材质 的按钮,就不用单独做材质了
"""
def __init__(self,
x: int,
y: int,
width: int,
height: int,
text: str,
batch: Optional[Batch] = None,
group: Optional[Group] = None,
theme: Optional[ButtonTheme] = None,
):
super().__init__(x, y, width, height)
self.main_batch = batch or Batch()
self.front_group = Group(order=10, parent=group)
self.back_ground_group = Group(order=5, parent=group)
self.pressed = False
self.theme = theme or ButtonTheme()
self.untouched_color = self.theme.main_color
self.touched_color = self.theme.secondary_color
self.hit_color = self.theme.hit_color
# from ImGui
self.text = text
self.text_label = Label(font_name=self.theme.font_theme.main_font, font_size=self.theme.font_theme.font_size,
batch=self.main_batch, group=self.front_group,
x=self._x, y=self._y, width=self._width,
height=self._height,)
self.font = pyglet.font.load(self.theme.font_theme.main_font,
self.theme.font_theme.font_size,
bold=self.theme.font_theme.font_bold,
italic=self.theme.font_theme.font_italic,
stretch=self.theme.font_theme.font_stretch)
self.font_height = self.font.ascent - self.font.descent
self.back_rec = Rectangle(x=self._x, y=self._y,
width=self._width, height=self._height,
color=self.untouched_color, # ImGui color
batch=self.main_batch, group=self.back_ground_group)
self.value = text # 重新分配一下高度和宽度的位置
@property
def value(self):
return self.text
@value.setter
def value(self, value):
self.text = value
self.text_label.text = value
text_width = self.text_label.content_width
self.text_label.x = self._x + (self.width - text_width) // 2
self.text_label.y = self._y + (self.height - self.font_height) // 2 + (self.font_height * 0.2) # 修正一下位置
def __contains__(self, item):
return item in self.back_rec
def on_mouse_motion(self, x, y, dx, dy):
if (x, y) in self.back_rec:
self.back_rec.color = self.touched_color
else:
self.pressed = False
self.back_rec.color = self.untouched_color
def on_mouse_press(self, x, y, buttons, modifiers) -> bool:
if (x, y) in self and buttons == mouse.LEFT:
self.back_rec.color = self.hit_color
self.dispatch_event('on_press', x, y)
self.pressed = True
return True
return False
def on_mouse_release(self, x, y, buttons, modifiers):
if self.pressed and (x, y) in self:
self.back_rec.color = self.touched_color
self.pressed = False
def _update_position(self):
self.text_label.position = self._x, self._y
self.back_rec.position = self._x, self._y
self.back_rec.width = self._width
self.back_rec.height = self._height
...
def on_press(self, x, y):
# import random
self.value += "1"
# next_rgb = [random.randint(50, 255) for _ in range(3)]
# self.untouched_color = (*next_rgb, 255)
PressTextButton.register_event_type('on_press')