Difficult-Rocket/Difficult_Rocket/gui/widget/button.py
shenjack f9eeafe322
好活!
readme update

看起来更像 Dear ImGui 一些(looks more like Dear ImGui

and some intersting feature to the button

remove debug

确认一下action

404 修改

writing theme

looks good

better?

a ?

alpha=255 not 256

looks better

try new pyglet first

看起来好一些

sync pyglet

水一手

这波必须得水一手了,要不然commit太少了(确信

丢点正经东西上去

顺手继承一下Options

补充docs

坏了,忘记水commit了(

至少我能早睡了(

这里还能水一点来着(

试试再说

reee

保证能跑(

同步lib not dr 的修改

忘记带上 None了

还是加上一个额外的判断参数吧

刷点commit也不错

先更新一下依赖版本

水commit啦

理论可行,实践开始!

构建参数喜加一

reeeee

更新一下 pyproject 的依赖

fix typing

looks better

水一个(

测试?

sync pyglet to master

A | Try use rust-cache

looks good?

what?

C | sync pyglet

A | 添加了一个 Button Draw Theme

A | Magic Number (确信)

A | 尽量不继承Options

sync pyglet

A | Add theme

A | Add more Theme information

Enhance | Theme

sync pyglet

Add | add unifont

Enhance | use os.walk in font loading

Enhance | Use i18n in font loading

Enhance | doc

sync pyglet

Add | add 3.12 build option to build_rs

Fix | Button position have a z

sync pyglet

A | Logger.py 启动!

sync pyglet

Changed | Bump pyo3 to 0.20.0

add logger.py update

logger!

Add | more logger!

Add | lib-not-dr

some lib-not-dr
2023-11-20 20:12:56 +08:00

184 lines
5.7 KiB
Python

# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
from typing import Optional, 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 Options, FontData
# from Difficult_Rocket import DR_status
RGBA = Tuple[int, int, int, int]
class ButtonDrawTheme:
"""
直接绘制按钮的风格
"""
name = 'ButtonDrawTheme'
def __init__(self,
batch: Batch,
group: Group):
self.batch = batch
self.group = group
a = (72, 73, 74)
b = (109, 109, 110)
c = (88, 91, 92)
d = (124, 124, 125)
e = (49, 50, 51)
touch_a = (49, 50, 51)
touch_b = (90, 91, 92)
touch_c = (71, 72, 72)
touch_d = (106, 107, 108)
def on_touch(self, x, y) -> None:
"""
当鼠标在按钮上的时候
:param x: 鼠标绝对位置
:param y: 鼠标绝对位置
:return:
"""
def on_move_away(self) -> None:
"""
当鼠标移出按钮的时候
:return:
"""
def on_hit(self, x: int, y: int) -> None:
"""
当鼠标点击按钮的时候
:param x: 鼠标绝对位置
:param y: 鼠标绝对位置
:return:
"""
def on_release(self) -> None:
"""
当鼠标松开按钮的时候
:return:
"""
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 __str__(self):
return self.as_markdown()
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[ButtonThemeOptions] = None,
):
super().__init__(x, y, width, height)
self.main_batch = batch or Batch()
self.user_batch = batch is not None
self.front_group = Group(order=10, parent=group)
self.back_ground_group = Group(order=5, parent=group)
self.pressed = False
self.theme = theme or ButtonThemeOptions()
self.untouched_color = self.theme.untouched_color
self.touched_color = self.theme.touched_color
self.hit_color = self.theme.hit_color
# from ImGui
self.text = text
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,
x=self._x, y=self._y, width=self._width,
height=self._height,)
self.font = pyglet.font.load(self.theme.font_theme.font_name,
self.theme.font_theme.font_size,
bold=self.theme.font_theme.bold,
italic=self.theme.font_theme.italic,
stretch=self.theme.font_theme.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_draw(self):
if not self.user_batch:
self.main_batch.draw()
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, 0
self.back_rec.position = self._x, self._y
self.back_rec.width = self._width
self.back_rec.height = self._height
PressTextButton.register_event_type('on_press')