feat: 改了一下格式,改了一下位置
This commit is contained in:
parent
f5d23f0548
commit
3b5b6c8a66
@ -13,7 +13,7 @@ gitee: @shenjackyuanjie
|
||||
import ctypes
|
||||
import logging
|
||||
|
||||
from Difficult_Rocket.utils.typings import Options
|
||||
from Difficult_Rocket.api.types import Options
|
||||
|
||||
from libs.MCDR.version import Version
|
||||
|
||||
|
@ -12,7 +12,7 @@ from dataclasses import dataclass
|
||||
from pyglet.image import load, AbstractImage
|
||||
|
||||
# Difficult Rocket
|
||||
from Difficult_Rocket.utils.typings import Options
|
||||
from Difficult_Rocket.api.types import Options
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -18,3 +18,95 @@ gitee: @shenjackyuanjie
|
||||
解析存档文件
|
||||
创建存档文件
|
||||
"""
|
||||
|
||||
from typing import get_type_hints, Type, List, Union, Dict, Any, Callable, Tuple
|
||||
|
||||
|
||||
def get_type_hints_(cls: Type):
|
||||
try:
|
||||
return get_type_hints(cls)
|
||||
except ValueError:
|
||||
return get_type_hints(cls, globalns={})
|
||||
|
||||
|
||||
class OptionsError(Exception):
|
||||
""" option 的错误基类"""
|
||||
|
||||
|
||||
class OptionNameNotDefined(OptionsError):
|
||||
""" 向初始化的 option 里添加了一个不存在于选项里的选项 """
|
||||
|
||||
|
||||
class OptionNotFound(OptionsError):
|
||||
""" 某个选项没有找到 """
|
||||
|
||||
|
||||
class Options:
|
||||
"""
|
||||
Difficult Rocket 的游戏配置的存储基类
|
||||
"""
|
||||
name = 'Option Base'
|
||||
cached_options: Dict[str, Union[str, Any]] = {}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.flush_option()
|
||||
for option, value in kwargs.items():
|
||||
if option not in self.cached_options:
|
||||
raise OptionNameNotDefined(f"option: {option} with value: {value} is not defined")
|
||||
setattr(self, option, value)
|
||||
self.flush_option()
|
||||
|
||||
def option(self) -> Dict[str, Any]:
|
||||
"""
|
||||
获取配置类的所有配置
|
||||
:return: 自己的所有配置
|
||||
"""
|
||||
values = {}
|
||||
for ann in self.__annotations__: # 获取类型注释
|
||||
values[ann] = getattr(self, ann, None)
|
||||
if values[ann] is None:
|
||||
values[ann] = self.__annotations__[ann]
|
||||
|
||||
if not hasattr(self, 'options'):
|
||||
self.options: Dict[str, Union[Callable, object]] = {}
|
||||
for option, a_fun in self.options.items(): # 获取额外内容
|
||||
values[option] = a_fun
|
||||
|
||||
for option, a_fun in values.items(): # 检查是否为 property
|
||||
if a_fun is bool and getattr(self, option, None) is not None:
|
||||
values[option] = False
|
||||
if isinstance(a_fun, property):
|
||||
try:
|
||||
values[option] = getattr(self, option)
|
||||
except AttributeError as e:
|
||||
raise OptionNotFound(f'Option {option} is not found in {self.name}')
|
||||
return values
|
||||
|
||||
def flush_option(self) -> Dict[str, Any]:
|
||||
"""
|
||||
刷新缓存 options 的内容
|
||||
:return: 刷新过的 options
|
||||
"""
|
||||
self.cached_options = self.option()
|
||||
return self.cached_options
|
||||
|
||||
def option_with_len(self) -> List[Union[List[Tuple[str, Any, Any]], int, Any]]:
|
||||
options = self.flush_option()
|
||||
max_len_key = 1
|
||||
max_len_value = 1
|
||||
max_len_value_t = 1
|
||||
option_list = []
|
||||
for key, value in options.items():
|
||||
value_t = type(value) if not isinstance(value, Type) else value
|
||||
max_len_key = max(max_len_key, len(key))
|
||||
max_len_value = max(max_len_value, len(str(value)))
|
||||
max_len_value_t = max(max_len_value_t, len(str(value_t)))
|
||||
option_list.append((key, value, value_t))
|
||||
return [option_list, max_len_key, max_len_value, max_len_value_t]
|
||||
|
||||
@classmethod
|
||||
def add_option(cls, name: str, value: Union[Callable, object]) -> Dict:
|
||||
if not hasattr(cls, 'options'):
|
||||
cls.options: Dict[str, Union[Callable, object]] = {}
|
||||
cls.options[name] = value
|
||||
return cls.options
|
||||
|
@ -37,7 +37,7 @@ from Difficult_Rocket import DR_runtime, DR_option
|
||||
from Difficult_Rocket.utils import tools, translate
|
||||
from Difficult_Rocket.utils.new_thread import new_thread
|
||||
from Difficult_Rocket.client.fps.fps_log import FpsLogger
|
||||
from Difficult_Rocket.client.guis.widgets import CommandBox
|
||||
from Difficult_Rocket.client.guis.widgets import InputBox
|
||||
from Difficult_Rocket.exception.command import CommandError
|
||||
from Difficult_Rocket.client.render.sr1_ship import SR1ShipRender
|
||||
from Difficult_Rocket.client.screen import BaseScreen, DRScreen, DRDEBUGScreen
|
||||
@ -154,7 +154,7 @@ class ClientWindow(Window):
|
||||
# 命令显示
|
||||
self.command_group = pyglet.graphics.Group(0)
|
||||
self.command_tree = tree.CommandTree(tree.DR_command)
|
||||
self.input_box = CommandBox(x=50, y=30, width=300,
|
||||
self.input_box = InputBox(x=50, y=30, width=300,
|
||||
batch=self.label_batch, text='') # 实例化
|
||||
self.input_box.push_handlers(self)
|
||||
self.push_handlers(self.input_box)
|
||||
|
@ -22,7 +22,8 @@ from pyglet.gui import widgets
|
||||
from pyglet.shapes import Rectangle
|
||||
# from pyglet.image import AbstractImage
|
||||
from pyglet.graphics import Batch, Group
|
||||
from pyglet.text.document import FormattedDocument
|
||||
from pyglet.text.caret import Caret
|
||||
from pyglet.text.document import FormattedDocument, UnformattedDocument
|
||||
from pyglet.text.layout import IncrementalTextLayout
|
||||
|
||||
# from libs import pyperclip
|
||||
@ -66,7 +67,7 @@ class TextButton(widgets.WidgetBase):
|
||||
super().__init__(x, y, width, height)
|
||||
self.text = text
|
||||
self.text_label = Label(
|
||||
font_name=font, font_size=font_size)
|
||||
font_name=font, font_size=font_size)
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
@ -78,15 +79,44 @@ class TextButton(widgets.WidgetBase):
|
||||
|
||||
|
||||
if not DR_option.InputBox_use_TextEntry:
|
||||
class InputBox(widgets.WidgetBase):
|
||||
class InputBox(widgets.TextEntry):
|
||||
""" 自定义的输入框 """
|
||||
|
||||
def __init__(self, x: int, y: int, width: int,
|
||||
message: str,
|
||||
batch: Optional[Batch], group: Optional[Group]):
|
||||
|
||||
super().__init__(x=x, y=y, width=width)
|
||||
...
|
||||
def __init__(self, x: int, y: int, width: int,
|
||||
text: str,
|
||||
side_width: int = 2,
|
||||
font_name: str = translate.鸿蒙简体,
|
||||
font_size: str = 13,
|
||||
color: Optional[Tuple[int, int, int, int]] = (255, 255, 255, 255),
|
||||
text_color: Optional[Tuple[int, int, int, int]] = (0, 0, 0, 255),
|
||||
caret_color: Optional[Tuple[int, int, int, int]] = (0, 0, 0),
|
||||
batch: Optional[Batch] = None, group: Optional[Group] = None):
|
||||
self._doc = UnformattedDocument(text)
|
||||
self._doc.set_style(0, len(self._doc.text), dict(color=text_color, font_name=font_name))
|
||||
font = self._doc.get_font()
|
||||
height = font.ascent - font.descent
|
||||
|
||||
self._user_group = group
|
||||
bg_group = Group(order=0, parent=group)
|
||||
fg_group = Group(order=1, parent=group)
|
||||
|
||||
# Rectangular outline with 2-pixel pad:
|
||||
self._pad = p = side_width
|
||||
self._outline = Rectangle(x-p, y-p, width+p+p, height+p+p, color[:3], batch, bg_group)
|
||||
self._outline.opacity = color[3]
|
||||
|
||||
# Text and Caret:
|
||||
self._layout = IncrementalTextLayout(self._doc, width, height, multiline=False, batch=batch, group=fg_group)
|
||||
self._layout.x = x
|
||||
self._layout.y = y
|
||||
self._caret = Caret(self._layout, color=caret_color)
|
||||
self._caret.visible = False
|
||||
|
||||
self._focus = False
|
||||
|
||||
# InputBox.register_event_type('on_commit')
|
||||
|
||||
|
||||
# class InputBox(widgets.WidgetBase):
|
||||
# """
|
||||
# input box
|
||||
@ -283,8 +313,6 @@ if not DR_option.InputBox_use_TextEntry:
|
||||
# def on_commit(self, text: str):
|
||||
# pass
|
||||
|
||||
|
||||
InputBox.register_event_type('on_commit')
|
||||
else:
|
||||
InputBox = widgets.TextEntry
|
||||
# class InputBox(widgets.TextEntry):
|
||||
|
@ -43,7 +43,7 @@ class CommandLineTextEntry(widgets.TextEntry):
|
||||
super().__init__(x=x, y=y, width=width,
|
||||
color=color, text_color=text_color, caret_color=caret_color,
|
||||
batch=batch, group=group, text='')
|
||||
|
||||
...
|
||||
|
||||
|
||||
class CommandLine(widgets.WidgetBase):
|
||||
|
@ -15,7 +15,6 @@ from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .new_thread import new_thread
|
||||
from .typings import get_type_hints_
|
||||
|
||||
__all__ = ['new_thread', 'get_type_hints_']
|
||||
__all__ = ['new_thread']
|
||||
|
||||
|
@ -1,97 +0,0 @@
|
||||
# -------------------------------
|
||||
# Difficult Rocket
|
||||
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
|
||||
# All rights reserved
|
||||
# -------------------------------
|
||||
|
||||
from typing import get_type_hints, Type, List, Union, Dict, Any, Callable, Tuple
|
||||
|
||||
|
||||
def get_type_hints_(cls: Type):
|
||||
try:
|
||||
return get_type_hints(cls)
|
||||
except ValueError:
|
||||
return get_type_hints(cls, globalns={})
|
||||
|
||||
|
||||
class OptionsError(Exception):
|
||||
""" option 的错误基类"""
|
||||
|
||||
|
||||
class OptionNameNotDefined(OptionsError):
|
||||
""" 向初始化的 option 里添加了一个不存在于选项里的选项 """
|
||||
|
||||
|
||||
class OptionNotFound(OptionsError):
|
||||
""" 某个选项没有找到 """
|
||||
|
||||
|
||||
class Options:
|
||||
"""
|
||||
Difficult Rocket 的游戏配置的存储基类
|
||||
"""
|
||||
name = 'Option Base'
|
||||
cached_options: Dict[str, Union[str, Any]] = {}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.flush_option()
|
||||
for option, value in kwargs.items():
|
||||
if option not in self.cached_options:
|
||||
raise OptionNameNotDefined(f"option: {option} with value: {value} is not defined")
|
||||
setattr(self, option, value)
|
||||
self.flush_option()
|
||||
|
||||
def option(self) -> Dict[str, Any]:
|
||||
"""
|
||||
获取配置类的所有配置
|
||||
:return: 自己的所有配置
|
||||
"""
|
||||
values = {}
|
||||
for ann in self.__annotations__: # 获取类型注释
|
||||
values[ann] = getattr(self, ann, None)
|
||||
if values[ann] is None:
|
||||
values[ann] = self.__annotations__[ann]
|
||||
|
||||
if not hasattr(self, 'options'):
|
||||
self.options: Dict[str, Union[Callable, object]] = {}
|
||||
for option, a_fun in self.options.items(): # 获取额外内容
|
||||
values[option] = a_fun
|
||||
|
||||
for option, a_fun in values.items(): # 检查是否为 property
|
||||
if a_fun is bool and getattr(self, option, None) is not None:
|
||||
values[option] = False
|
||||
if isinstance(a_fun, property):
|
||||
try:
|
||||
values[option] = getattr(self, option)
|
||||
except AttributeError as e:
|
||||
raise OptionNotFound(f'Option {option} is not found in {self.name}')
|
||||
return values
|
||||
|
||||
def flush_option(self) -> Dict[str, Any]:
|
||||
"""
|
||||
刷新缓存 options 的内容
|
||||
:return: 刷新过的 options
|
||||
"""
|
||||
self.cached_options = self.option()
|
||||
return self.cached_options
|
||||
|
||||
def option_with_len(self) -> List[Union[List[Tuple[str, Any, Any]], int, Any]]:
|
||||
options = self.flush_option()
|
||||
max_len_key = 1
|
||||
max_len_value = 1
|
||||
max_len_value_t = 1
|
||||
option_list = []
|
||||
for key, value in options.items():
|
||||
value_t = type(value) if not isinstance(value, Type) else value
|
||||
max_len_key = max(max_len_key, len(key))
|
||||
max_len_value = max(max_len_value, len(str(value)))
|
||||
max_len_value_t = max(max_len_value_t, len(str(value_t)))
|
||||
option_list.append((key, value, value_t))
|
||||
return [option_list, max_len_key, max_len_value, max_len_value_t]
|
||||
|
||||
@classmethod
|
||||
def add_option(cls, name: str, value: Union[Callable, object]) -> Dict:
|
||||
if not hasattr(cls, 'options'):
|
||||
cls.options: Dict[str, Union[Callable, object]] = {}
|
||||
cls.options[name] = value
|
||||
return cls.options
|
Loading…
Reference in New Issue
Block a user