diff --git a/Difficult_Rocket/__init__.py b/Difficult_Rocket/__init__.py index ab6aab9..79ec0a5 100644 --- a/Difficult_Rocket/__init__.py +++ b/Difficult_Rocket/__init__.py @@ -92,7 +92,7 @@ class _DR_runtime(Options): _DR_runtime.add_option('language', _DR_runtime.language) -DR_option = DR_option() +dDR_option = DR_option() DR_runtime = _DR_runtime() if DR_option.playing: diff --git a/Difficult_Rocket/api/screen.py b/Difficult_Rocket/api/screen.py index 37487eb..47f7b5b 100644 --- a/Difficult_Rocket/api/screen.py +++ b/Difficult_Rocket/api/screen.py @@ -12,7 +12,7 @@ from typing import List from pyglet.event import EventDispatcher # Difficult Rocket function - +from Difficult_Rocket.command.api import CommandText if typing.TYPE_CHECKING: from Difficult_Rocket.client import ClientWindow @@ -388,14 +388,14 @@ class BaseScreen(EventDispatcher): """ - def on_command(self, command): + def on_command(self, command: CommandText): """ :param command: :return: """ - def on_message(self, message): + def on_message(self, message: str): """ :param message: diff --git a/Difficult_Rocket/api/types/SR1/__init__.py b/Difficult_Rocket/api/types/SR1/__init__.py new file mode 100644 index 0000000..fe50832 --- /dev/null +++ b/Difficult_Rocket/api/types/SR1/__init__.py @@ -0,0 +1,61 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- + +# pyglet +import pyglet +from pyglet.image import load, AbstractImage + +# Difficult Rocket +from Difficult_Rocket.utils.typings import Options + + +class SR1Textures(Options): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.flush_option() + for image_name in self.cached_options: + setattr(self, image_name, load(f'textures/parts/{image_name}.png')) + self.flush_option() + Battery: AbstractImage = None + Beam: AbstractImage = None + CoverBottom: AbstractImage = None + CoverStretch: AbstractImage = None + CoverTop: AbstractImage = None + DetacherRadial: AbstractImage = None + DetacherVertical: AbstractImage = None + DockingConnector: AbstractImage = None + DockingPort: AbstractImage = None + EngineIon: AbstractImage = None + EngineLarge: AbstractImage = None + EngineMedium: AbstractImage = None + EngineSmall: AbstractImage = None + EngineTiny: AbstractImage = None + Fuselage: AbstractImage = None + LanderLegJoint: AbstractImage = None + LanderLegLower: AbstractImage = None + LanderLegPreview: AbstractImage = None + LanderLegUpper: AbstractImage = None + NoseCone: AbstractImage = None + Parachute: AbstractImage = None + ParachuteCanister: AbstractImage = None + ParachuteCanisterSide: AbstractImage = None + Pod: AbstractImage = None + Puffy750: AbstractImage = None + RcsBlock: AbstractImage = None + SideTank: AbstractImage = None + SolarPanel: AbstractImage = None + SolarPanelBase: AbstractImage = None + SolidRocketBooster: AbstractImage = None + TankLarge: AbstractImage = None + TankMedium: AbstractImage = None + TankSmall: AbstractImage = None + TankTiny: AbstractImage = None + Wheel: AbstractImage = None + Wing: AbstractImage = None + + + + diff --git a/Difficult_Rocket/client/__init__.py b/Difficult_Rocket/client/__init__.py index 3d786a9..1a4e740 100644 --- a/Difficult_Rocket/client/__init__.py +++ b/Difficult_Rocket/client/__init__.py @@ -31,15 +31,16 @@ from pyglet.window import Window from pyglet.window import key, mouse # Difficult_Rocket function -from Difficult_Rocket import DR_runtime from Difficult_Rocket.command import line, tree from Difficult_Rocket.utils.translate import tr -from Difficult_Rocket.client.screen import BaseScreen, DRScreen, DRDEBUGScreen +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 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 class Client: @@ -62,7 +63,8 @@ class Client: fullscreen=tools.format_bool(self.config['window']['full_screen']), caption=self.caption, resizable=tools.format_bool(self.config['window']['resizable']), - visible=tools.format_bool(self.config['window']['visible'])) + visible=tools.format_bool(self.config['window']['visible']), + file_drops=True) self.logger.info(tr.lang('client', 'setup.done')) end_time = time.time_ns() self.use_time = end_time - start_time @@ -90,7 +92,10 @@ def _call_screen_after(func: Callable) -> Callable: result = func(self, *args, **kwargs) for a_screen in self.screen_list: if hasattr(a_screen, func.__name__): - getattr(a_screen, func.__name__)(*args, **kwargs) + try: + getattr(a_screen, func.__name__)(*args, **kwargs) + except: + traceback.print_exc() return result warped.__signature__ = inspect.signature(func) @@ -102,7 +107,10 @@ def _call_screen_before(func: Callable) -> Callable: def warped(self, *args, **kwargs): for a_screen in self.screen_list: if hasattr(a_screen, func.__name__): - getattr(a_screen, func.__name__)(*args, **kwargs) + try: + getattr(a_screen, func.__name__)(*args, **kwargs) + except: + traceback.print_exc() result = func(self, *args, **kwargs) return result @@ -167,6 +175,7 @@ class ClientWindow(Window): self.screen_list: List[BaseScreen] self.screen_list.append(DRDEBUGScreen(self)) self.screen_list.append(DRScreen(self)) + self.screen_list.append(SR1ShipRender(self, DR_option.gui_scale)) def load_fonts(self) -> None: fonts_folder_path = self.main_config['runtime']['fonts_folder'] diff --git a/Difficult_Rocket/client/render/sr1_ship.py b/Difficult_Rocket/client/render/sr1_ship.py index 48d4e60..dae902c 100644 --- a/Difficult_Rocket/client/render/sr1_ship.py +++ b/Difficult_Rocket/client/render/sr1_ship.py @@ -4,33 +4,76 @@ # All rights reserved # ------------------------------- +from typing import List, TYPE_CHECKING, Union +from xml.etree import ElementTree + # third party package -from defusedxml.ElementTree import DefusedXMLParser +from defusedxml.ElementTree import parse # pyglet from pyglet.graphics import Batch from pyglet.resource import texture # Difficult Rocket -from client.screen import BaseScreen -from Difficult_Rocket.client import ClientWindow +from Difficult_Rocket import DR_option +from Difficult_Rocket.api.types.SR1 import SR1Textures +from Difficult_Rocket.command.line import CommandText +from Difficult_Rocket.client.screen import BaseScreen + +if TYPE_CHECKING: + from Difficult_Rocket.client import ClientWindow class SR1ShipRender(BaseScreen): """用于渲染 sr1 船的类""" - def __init__(self, x: float, y: float, - scale: float, - xml_doc: DefusedXMLParser, - main_window: "ClientWindow"): + def __init__(self, + main_window: "ClientWindow", + scale: float): super().__init__(main_window) - self.x, self.y = x, y self.scale = scale - self.xml_doc = xml_doc + self.textures: Union[SR1Textures, None] = None + self.xml_doc = parse('configs/dock1.xml') + self.xml_root: ElementTree.Element = self.xml_doc.getroot() self.part_batch = Batch() + def load_textures(self): + self.textures = SR1Textures() + + def render_ship(self): + if self.textures is None: + self.load_textures() + parts = self.xml_root.find('Parts') + for part in parts: + if part.tag != 'Part': + continue # 如果不是部件,则跳过 + # print(f"tag: {part.tag} attrib: {part.attrib}") + part_id = part.attrib.get('id') + part_type = part.attrib.get('partType') + part_x = part.attrib.get('x') + part_y = part.attrib.get('y') + part_activate = part.attrib.get('activated') or 0 + part_angle = part.attrib.get('angle') + part_angle_v = part.attrib.get('angleV') + part_editor_angle = part.attrib.get('editorAngle') + part_flip_x = part.attrib.get('flippedX') or 0 + part_flip_y = part.attrib.get('flippedY') or 0 + part_explode = part.attrib.get('exploded') or 0 + if part_id not in self.textures.cached_options: + print('Textures None found!') + print(f'id: {part_id:<4} type: {part_type:<10} x: {part_x} y: {part_y} activated: {part_activate} ' + f'angle: {part_angle} angle_v: {part_angle_v} editor_angle: {part_editor_angle} ' + f'flip_x: {part_flip_x} flip_y: {part_flip_y} explode: {part_explode}') + def on_draw(self): ... - def on_command(self, command): + def on_file_drop(self, x: int, y: int, paths: List[str]): + self.scale = DR_option.gui_scale + self.render_ship() ... + + def on_command(self, command: CommandText): + if command.match('render'): + print('rua, render ship!') + self.render_ship() diff --git a/Difficult_Rocket/utils/tools.py b/Difficult_Rocket/utils/tools.py index ce37967..569e1e8 100644 --- a/Difficult_Rocket/utils/tools.py +++ b/Difficult_Rocket/utils/tools.py @@ -19,10 +19,12 @@ import logging import configparser from typing import Union -from xml.dom.minidom import parse +from xml.etree import ElementTree import rtoml +from defusedxml.ElementTree import parse + from Difficult_Rocket.exception.unsupport import NoMoreJson5 # logger @@ -36,14 +38,16 @@ file_error = {FileNotFoundError: 'no {filetype} file was founded!:\n file name: Exception: 'get some {error_type} error when read {filetype} file {filename}! \n file type: {} \n file name: {} \n stack: {stack}'} -def load_file(file_name: str, stack: Union[str, list, dict] = None, raise_error: bool = True) -> Union[dict, list]: +def load_file(file_name: str, + stack: Union[str, list, dict] = None, + raise_error: bool = True) -> Union[dict, list]: f_type = file_name[file_name.rfind('.') + 1:] # 从最后一个.到末尾 (截取文件格式) get_file = NotImplementedError('解析失败,请检查文件类型/文件内容/文件是否存在!') try: if f_type == 'xml': - xml_load = parse(file_name) + xml_load: ElementTree.ElementTree = parse(file_name) if stack is not None: - get_file = xml_load.getElementsByTagName(stack) + get_file = xml_load.find(stack) elif (f_type == 'config') or (f_type == 'conf') or (f_type == 'ini'): get_file = configparser.ConfigParser() get_file.read(file_name) diff --git a/configs/dock1.xml b/configs/dock1.xml new file mode 100644 index 0000000..2db613c --- /dev/null +++ b/configs/dock1.xml @@ -0,0 +1,635 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/configs/main.toml b/configs/main.toml index d31e97d..8f42202 100644 --- a/configs/main.toml +++ b/configs/main.toml @@ -8,8 +8,8 @@ fonts_folder = "libs/fonts" [window] style = "None" -width = 1219 -height = 835 +width = 937 +height = 602 visible = true gui_scale = 1 caption = "Difficult Rocket {version}"