From 33d57888266fd6df2d3bdb2f789ba944c36d12d7 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 20 Aug 2023 21:42:38 +0800 Subject: [PATCH] better widget --- Difficult_Rocket/client/__init__.py | 4 +- Difficult_Rocket/gui/widget/button.py | 53 ++++++++++++++++++++------- mods/dr_game/sr1_ship.py | 29 +++++++++------ nuitka_build.py | 2 +- 4 files changed, 61 insertions(+), 27 deletions(-) diff --git a/Difficult_Rocket/client/__init__.py b/Difficult_Rocket/client/__init__.py index 25ba1d6..192e3b2 100644 --- a/Difficult_Rocket/client/__init__.py +++ b/Difficult_Rocket/client/__init__.py @@ -247,7 +247,7 @@ class ClientWindow(Window): batch=self.main_batch, text='', group=Group(1000, parent=self.main_group)) # 实例化 self.input_box.push_handlers(self) self.input_box.set_handler('on_commit', self.on_input) - self.set_handlers(self.input_box) + self.push_handlers(self.input_box) self.input_box.enabled = True # 设置刷新率 # pyglet.clock.schedule_interval(self.draw_update, float(self.SPF)) @@ -424,7 +424,7 @@ class ClientWindow(Window): ... @_call_screen_after - def on_mouse_motion(self, x, y, dx, dy) -> None: + def on_mouse_motion(self, x, y, dx, dy): ... @_call_screen_after diff --git a/Difficult_Rocket/gui/widget/button.py b/Difficult_Rocket/gui/widget/button.py index f977a50..127705e 100644 --- a/Difficult_Rocket/gui/widget/button.py +++ b/Difficult_Rocket/gui/widget/button.py @@ -15,19 +15,36 @@ from typing import Optional, Union, Tuple # from libs import pyglet # from pyglet import font -from pyglet.text import Label, HTMLLabel -from pyglet.window import key +from pyglet.text import Label +from pyglet.window import mouse from pyglet.gui import widgets # from pyglet.sprite import Sprite from pyglet.shapes import Rectangle # from pyglet.image import AbstractImage from pyglet.graphics import Batch, Group -from pyglet.text.caret import Caret -from pyglet.text.document import UnformattedDocument -from pyglet.text.layout import IncrementalTextLayout from Difficult_Rocket.api.types import Fonts -from Difficult_Rocket import DR_status + +# from Difficult_Rocket import DR_status + + +RGBA = Tuple[int, int, int, int] + + +class BaseTheme: + """ + 用于定义主题的类 + """ + + def __init__(self, + main_color: RGBA = (39, 73, 114, 256), + secondary_color: RGBA = (66, 150, 250, 256), + main_font: str = Fonts.鸿蒙简体, + ): + self.main_color = main_color + self.secondary_color = secondary_color + self.main_font = main_font + class PressTextButton(widgets.WidgetBase): @@ -47,15 +64,17 @@ class PressTextButton(widgets.WidgetBase): group: Optional[Group] = None, ): super().__init__(x, y, width, height) - self.back_ground_batch = Batch() + self.back_ground_batch = batch or Batch() self.front_batch = batch or Batch() if group: - self.front_group = Group(order=5, parent=group) - self.back_ground_group = Group(order=10, parent=group) + self.front_group = Group(order=10, parent=group) + self.back_ground_group = Group(order=5, parent=group) else: self.front_group = Group(order=5) self.back_ground_group = Group(order=10) + self.pressed = False + self.untouched_color = (39, 73, 114, 255) self.touched_color = (66, 150, 250, 255) self.hit_color = (15, 135, 250, 255) @@ -63,7 +82,8 @@ class PressTextButton(widgets.WidgetBase): self.text = text self.text_label = Label(font_name=font, font_size=font_size, - batch=self.front_batch, group=self.front_group) + batch=self.front_batch, group=self.front_group, + x=self._x, y=self._y, text=self.text) self.back_rec = Rectangle(x=self._x, y=self._y, width=self._width, height=self._height, color=self.untouched_color, # ImGui color batch=self.back_ground_batch, group=self.back_ground_group) @@ -76,18 +96,25 @@ class PressTextButton(widgets.WidgetBase): return item in self.back_rec def on_mouse_motion(self, x, y, dx, dy): - if (x, y) in self: + 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): - if (x, y) in self: + 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 diff --git a/mods/dr_game/sr1_ship.py b/mods/dr_game/sr1_ship.py index e524af9..1ebdb75 100644 --- a/mods/dr_game/sr1_ship.py +++ b/mods/dr_game/sr1_ship.py @@ -25,13 +25,12 @@ from .types import SR1Textures, SR1Rotation # Difficult Rocket from Difficult_Rocket import DR_status from Difficult_Rocket.utils.translate import Tr +from Difficult_Rocket.client import ClientWindow from Difficult_Rocket.api.types import Fonts, Options from Difficult_Rocket.command.line import CommandText from Difficult_Rocket.client.screen import BaseScreen from Difficult_Rocket.api.camera import CenterGroupCamera - -if TYPE_CHECKING: - from Difficult_Rocket.client import ClientWindow +from Difficult_Rocket.api.gui.widget import PressTextButton if DR_mod_runtime.use_DR_rust: from .Difficult_Rocket_rs import (SR1PartList_rs, @@ -63,7 +62,7 @@ class SR1ShipRender(BaseScreen): """用于渲染 sr1 船的类""" def __init__(self, - main_window: "ClientWindow"): + main_window: ClientWindow): super().__init__(main_window) self.logger = logger logger.info(sr_tr().mod.info.setup.start()) @@ -96,6 +95,12 @@ class SR1ShipRender(BaseScreen): x=main_window.width / 2, y=main_window.height / 2) self.render_d_label.visible = self.status.draw_d_pos + self.test_button = PressTextButton(x=100, y=100, + width=100, height=20, text='test button', + batch=self.main_batch, group=Group(5, parent=main_window.main_group)) + # self.test_button.push_handlers(main_window) + main_window.push_handlers(self.test_button) + # Optional data self.textures: SR1Textures = SR1Textures() self.gen_draw: Optional[Generator] = None @@ -269,7 +274,7 @@ class SR1ShipRender(BaseScreen): len(self.rust_ship.as_list()), f'{full_mass}kg' if DR_mod_runtime.use_DR_rust else sr_tr().game.require_DR_rs())) - def draw_batch(self, window: "ClientWindow"): + def draw_batch(self, window: ClientWindow): if self.status.draw_done: self.render_d_label.text = f'x: {self.group_camera.view_x} y: {self.group_camera.view_y}' self.render_d_label.position = self.group_camera.view_x + (self.window_pointer.width / 2), self.group_camera.view_y + ( @@ -286,7 +291,7 @@ class SR1ShipRender(BaseScreen): gl.glDisable(gl.GL_SCISSOR_TEST) # def on_draw(self, dt: float, window): # TODO: wait for pyglet 2.1 - def on_draw(self, window: "ClientWindow"): + def on_draw(self, window: ClientWindow): if self.status.draw_call: self.render_ship() @@ -301,14 +306,15 @@ class SR1ShipRender(BaseScreen): self.debug_label.draw() - def on_resize(self, width: int, height: int, window: "ClientWindow"): + def on_resize(self, width: int, height: int, window: ClientWindow): self.debug_label.y = height - 100 if not self.status.draw_done: return self.render_d_line.x2 = width // 2 self.render_d_line.y2 = height // 2 + self.test_button._update_position() - def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int, window: "ClientWindow"): + def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int, window: ClientWindow): if not self.status.draw_done: return if self.status.focus: @@ -344,7 +350,7 @@ class SR1ShipRender(BaseScreen): size_y = 10 self.size = size_x, size_y - def on_command(self, command: CommandText, window: "ClientWindow"): + def on_command(self, command: CommandText, window: ClientWindow): """ 解析命令 """ self.logger.info(f'command: {command}') if command.find('render'): @@ -434,7 +440,7 @@ class SR1ShipRender(BaseScreen): logger.info(sr_tr().sr1.ship.save.start().format(self.rust_ship)) self.rust_ship.save('./test-save.xml') - def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int, window: "ClientWindow"): + def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int, window: ClientWindow): if self.status.focus: self.group_camera.view_x += dx self.group_camera.view_y += dy @@ -444,7 +450,7 @@ class SR1ShipRender(BaseScreen): self.dx += dx self.dy += dy - def on_file_drop(self, x: int, y: int, paths: List[str], window: "ClientWindow"): + def on_file_drop(self, x: int, y: int, paths: List[str], window: ClientWindow): if len(paths) > 1: for path in paths: try: @@ -474,6 +480,7 @@ class SR1ShipRender(BaseScreen): self.window_pointer.view = value + if __name__ == '__main__': from objprint import op diff --git a/nuitka_build.py b/nuitka_build.py index 6d26431..9d138ac 100644 --- a/nuitka_build.py +++ b/nuitka_build.py @@ -84,7 +84,7 @@ if __name__ == '__main__': else: compiler.output_path = Path(f'./build/nuitka-{platform.system().lower()}') - print(compiler.as_markdown(longest=70)) + print(compiler.as_markdown()) print(compiler.gen_subprocess_cmd())