Difficult-Rocket/Difficult_Rocket/gui/widget/button.py

184 lines
5.7 KiB
Python
Raw Normal View History

2021-10-01 23:12:01 +08:00
# -------------------------------
# Difficult Rocket
2023-01-20 14:08:12 +08:00
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
2021-10-01 23:12:01 +08:00
# All rights reserved
# -------------------------------
2023-09-13 00:20:05 +08:00
from typing import Optional, Tuple
2023-01-01 17:58:40 +08:00
2021-11-06 19:07:32 +08:00
# from libs import pyglet
2023-08-22 00:31:25 +08:00
import pyglet
2023-08-20 21:42:38 +08:00
from pyglet.text import Label
2022-10-29 19:13:00 +08:00
from pyglet.gui import widgets
2023-08-22 00:31:25 +08:00
from pyglet.window import mouse
2023-01-01 17:58:40 +08:00
# from pyglet.sprite import Sprite
2022-10-29 19:13:00 +08:00
from pyglet.shapes import Rectangle
2023-01-01 17:58:40 +08:00
# from pyglet.image import AbstractImage
2022-10-29 19:13:00 +08:00
from pyglet.graphics import Batch, Group
2022-12-20 16:31:08 +08:00
2023-09-13 00:20:05 +08:00
from Difficult_Rocket.api.types import Options, FontData
2023-08-20 21:42:38 +08:00
# from Difficult_Rocket import DR_status
RGBA = Tuple[int, int, int, int]
2023-09-29 18:23:08 +08:00
class ButtonDrawTheme:
2023-09-29 00:48:00 +08:00
"""
直接绘制按钮的风格
"""
name = 'ButtonDrawTheme'
2023-09-29 18:23:08 +08:00
def __init__(self,
batch: Batch,
group: Group):
self.batch = batch
self.group = group
2023-09-29 00:56:26 +08:00
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)
2023-09-29 18:23:08 +08:00
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:
"""
2023-09-29 00:56:26 +08:00
2023-09-29 00:48:00 +08:00
2023-09-03 00:29:05 +08:00
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()
2023-09-02 01:38:05 +08:00
2023-09-03 00:29:05 +08:00
def __str__(self):
return self.as_markdown()
2023-09-02 01:59:56 +08:00
2023-09-02 01:38:05 +08:00
2023-08-20 02:01:15 +08:00
class PressTextButton(widgets.WidgetBase):
2022-12-20 16:31:08 +08:00
"""
2023-08-12 00:28:31 +08:00
自带 字符 + 材质 的按钮就不用单独做材质了
2022-12-20 16:31:08 +08:00
"""
def __init__(self,
2023-08-12 00:28:31 +08:00
x: int,
y: int,
width: int,
height: int,
2022-12-20 16:31:08 +08:00
text: str,
2023-08-20 01:52:22 +08:00
batch: Optional[Batch] = None,
group: Optional[Group] = None,
2023-09-03 00:29:05 +08:00
theme: Optional[ButtonThemeOptions] = None,
2023-08-20 01:52:22 +08:00
):
2022-12-20 16:31:08 +08:00
super().__init__(x, y, width, height)
2023-09-02 02:11:52 +08:00
self.main_batch = batch or Batch()
2023-09-29 00:48:00 +08:00
self.user_batch = batch is not None
2023-09-02 01:59:56 +08:00
self.front_group = Group(order=10, parent=group)
self.back_ground_group = Group(order=5, parent=group)
2023-08-20 21:42:38 +08:00
self.pressed = False
2023-09-03 00:29:05 +08:00
self.theme = theme or ButtonThemeOptions()
2023-09-02 01:38:05 +08:00
2023-09-03 00:29:05 +08:00
self.untouched_color = self.theme.untouched_color
self.touched_color = self.theme.touched_color
2023-09-02 01:38:05 +08:00
self.hit_color = self.theme.hit_color
2023-08-20 02:01:15 +08:00
# from ImGui
2022-12-20 16:31:08 +08:00
self.text = text
2023-09-03 00:29:05 +08:00
self.text_label = Label(font_name=self.theme.font_theme.font_name, font_size=self.theme.font_theme.font_size,
2023-09-02 02:11:52 +08:00
batch=self.main_batch, group=self.front_group,
2023-08-22 00:31:25 +08:00
x=self._x, y=self._y, width=self._width,
height=self._height,)
2023-09-03 00:29:05 +08:00
self.font = pyglet.font.load(self.theme.font_theme.font_name,
2023-09-02 02:11:52 +08:00
self.theme.font_theme.font_size,
2023-09-03 00:29:05 +08:00
bold=self.theme.font_theme.bold,
italic=self.theme.font_theme.italic,
stretch=self.theme.font_theme.stretch)
2023-08-22 00:31:25 +08:00
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,
2023-08-20 02:01:15 +08:00
color=self.untouched_color, # ImGui color
2023-09-02 02:11:52 +08:00
batch=self.main_batch, group=self.back_ground_group)
2022-12-20 16:31:08 +08:00
2023-08-22 00:31:25 +08:00
self.value = text # 重新分配一下高度和宽度的位置
2022-12-20 16:31:08 +08:00
@property
def value(self):
return self.text
2023-08-22 00:31:25 +08:00
@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) # 修正一下位置
2023-08-20 02:01:15 +08:00
def __contains__(self, item):
return item in self.back_rec
2023-09-29 00:48:00 +08:00
def on_draw(self):
if not self.user_batch:
self.main_batch.draw()
2023-08-20 02:01:15 +08:00
def on_mouse_motion(self, x, y, dx, dy):
2023-08-20 21:42:38 +08:00
if (x, y) in self.back_rec:
2023-08-20 02:01:15 +08:00
self.back_rec.color = self.touched_color
else:
2023-08-20 21:42:38 +08:00
self.pressed = False
2023-08-20 02:01:15 +08:00
self.back_rec.color = self.untouched_color
2023-08-20 21:42:38 +08:00
def on_mouse_press(self, x, y, buttons, modifiers) -> bool:
if (x, y) in self and buttons == mouse.LEFT:
2023-08-20 02:01:15 +08:00
self.back_rec.color = self.hit_color
self.dispatch_event('on_press', x, y)
2023-08-20 21:42:38 +08:00
self.pressed = True
2023-08-20 02:01:15 +08:00
return True
return False
2023-08-20 21:42:38 +08:00
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
2022-12-20 16:31:08 +08:00
def _update_position(self):
self.text_label.position = self._x, self._y
2023-08-20 02:01:15 +08:00
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')