Difficult-Rocket/Difficult_Rocket/client/render/sr1_ship.py

283 lines
13 KiB
Python
Raw Normal View History

2022-12-11 10:39:05 +08:00
# -------------------------------
# Difficult Rocket
2023-01-20 14:08:12 +08:00
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
2022-12-11 10:39:05 +08:00
# All rights reserved
# -------------------------------
2023-01-24 23:59:07 +08:00
import random
2022-12-26 11:46:05 +08:00
from xml.etree import ElementTree
2023-01-19 22:29:43 +08:00
from xml.etree.ElementTree import Element
from typing import List, TYPE_CHECKING, Union, Dict, Optional
2022-12-26 11:46:05 +08:00
2022-12-11 10:39:05 +08:00
# third party package
2022-12-26 11:46:05 +08:00
from defusedxml.ElementTree import parse
2022-12-25 23:15:49 +08:00
# pyglet
2023-01-19 22:29:43 +08:00
from pyglet.text import Label
2023-01-21 14:50:30 +08:00
from pyglet.shapes import Line
2022-12-29 10:13:20 +08:00
from pyglet.sprite import Sprite
2023-01-19 22:29:43 +08:00
from pyglet.graphics import Batch, Group
2022-12-11 10:39:05 +08:00
# Difficult Rocket
2022-12-26 11:46:05 +08:00
from Difficult_Rocket import DR_option
2023-01-21 14:50:30 +08:00
from Difficult_Rocket.api.types import Fonts, Options
2023-01-21 11:48:07 +08:00
from Difficult_Rocket.command.line import CommandText
2022-12-26 11:46:05 +08:00
from Difficult_Rocket.client.screen import BaseScreen
2023-01-19 22:29:43 +08:00
from Difficult_Rocket.api.types.SR1 import SR1Textures, SR1PartTexture, SR1PartData, SR1Rotation, xml_bool
2022-12-26 11:46:05 +08:00
if TYPE_CHECKING:
from Difficult_Rocket.client import ClientWindow
2022-12-25 23:15:49 +08:00
2023-01-24 23:59:07 +08:00
if DR_option.use_DR_rust:
2023-01-24 12:25:23 +08:00
from libs.Difficult_Rocket_rs import better_update_parts, PartDatas
2023-01-23 00:01:01 +08:00
2022-12-25 23:15:49 +08:00
2023-01-21 22:50:18 +08:00
def get_sr1_part(part_xml: Element) -> Optional[SR1PartData]:
2023-01-19 22:29:43 +08:00
if part_xml.tag != 'Part':
return None
# print(f"tag: {part.tag} attrib: {part.attrib}")
part_id = int(part_xml.attrib.get('id'))
part_type = part_xml.attrib.get('partType')
part_x = float(part_xml.attrib.get('x'))
part_y = float(part_xml.attrib.get('y'))
part_activate = xml_bool(part_xml.attrib.get('activated'))
part_angle = float(part_xml.attrib.get('angle'))
part_angle_v = float(part_xml.attrib.get('angleV'))
part_editor_angle = int(part_xml.attrib.get('editorAngle'))
part_flip_x = xml_bool(part_xml.attrib.get('flippedX'))
part_flip_y = xml_bool(part_xml.attrib.get('flippedY'))
part_explode = xml_bool(part_xml.attrib.get('exploded'))
if part_type not in SR1PartTexture.part_type_sprite:
part_textures = None
else:
part_textures = SR1PartTexture.get_textures_from_type(part_type)
2023-01-20 13:52:58 +08:00
# 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} '
# f'textures: {SR1PartTexture.get_textures_from_type(part_type)}')
2023-01-23 23:46:43 +08:00
part_data = SR1PartData(x=part_x, y=part_y, id=part_id, type_=part_type,
2023-01-19 22:29:43 +08:00
active=part_activate, angle=part_angle, angle_v=part_angle_v,
editor_angle=part_editor_angle, flip_x=part_flip_x,
flip_y=part_flip_y, explode=part_explode, textures=part_textures)
return part_data
2023-01-21 22:50:18 +08:00
class _SR1ShipRender_Option(Options):
2023-01-21 14:50:30 +08:00
# debug option
2023-01-21 23:30:22 +08:00
debug_d_pos: bool = False
debug_mouse_pos: bool = False
debug_mouse_d_pos: bool = False
2023-01-21 22:50:18 +08:00
SR1ShipRender_Option = _SR1ShipRender_Option()
2023-01-21 14:50:30 +08:00
2022-12-25 23:15:49 +08:00
class SR1ShipRender(BaseScreen):
"""用于渲染 sr1 船的类"""
2022-12-26 11:46:05 +08:00
def __init__(self,
main_window: "ClientWindow",
scale: float):
2022-12-25 23:15:49 +08:00
super().__init__(main_window)
2023-01-19 22:29:43 +08:00
self.rendered = False
2022-12-25 23:15:49 +08:00
self.scale = scale
2023-01-19 22:29:43 +08:00
self.focus = True
2023-01-21 22:50:18 +08:00
self.need_draw = False
2023-01-19 22:29:43 +08:00
self.dx = 0
self.dy = 0
2023-01-21 14:50:30 +08:00
self.debug_line = Line(main_window.width / 2, main_window.height / 2,
main_window.width / 2, main_window.height / 2,
width=3, color=(200, 10, 200, 255))
2023-01-21 23:30:22 +08:00
self.debug_line.visible = SR1ShipRender_Option.debug_d_pos
2023-01-21 14:50:30 +08:00
self.debug_mouse_line = Line(main_window.width / 2, main_window.height / 2,
main_window.width / 2, main_window.height / 2,
width=3, color=(10, 200, 200, 255))
2023-01-21 23:30:22 +08:00
self.debug_mouse_line.visible = SR1ShipRender_Option.debug_mouse_pos
2023-01-21 22:50:18 +08:00
self.debug_mouse_delta_line = Line(main_window.width / 2, main_window.height / 2,
main_window.width / 2, main_window.height / 2,
width=2, color=(200, 200, 10, 255))
2023-01-21 23:30:22 +08:00
self.debug_mouse_delta_line.visible = SR1ShipRender_Option.debug_mouse_d_pos
self.debug_d_pos_label = Label('debug label NODATA', font_name=Fonts.微软等宽无线,
x=main_window.width / 2, y=main_window.height / 2)
self.debug_d_pos_label.visible = SR1ShipRender_Option.debug_d_pos
2023-01-21 14:50:30 +08:00
self.debug_mouse_label = Label('debug mouse_label NODATA', font_name=Fonts.微软等宽无线,
x=main_window.width / 2, y=main_window.height / 2)
2023-01-21 23:30:22 +08:00
self.debug_mouse_label.visible = SR1ShipRender_Option.debug_mouse_pos
2022-12-26 11:46:05 +08:00
self.textures: Union[SR1Textures, None] = None
2023-01-19 22:29:43 +08:00
self.xml_doc: ElementTree = parse('configs/dock1.xml')
2022-12-26 11:46:05 +08:00
self.xml_root: ElementTree.Element = self.xml_doc.getroot()
2022-12-25 23:15:49 +08:00
self.part_batch = Batch()
2022-12-29 10:13:20 +08:00
self.part_group = Group()
2023-01-19 22:29:43 +08:00
self.debug_label = Label(x=20, y=main_window.height - 20, font_size=DR_option.std_font_size,
2023-01-24 12:25:23 +08:00
text='SR1 render!', font_name=Fonts.微软等宽无线,
width=main_window.width - 20, height=20,
anchor_x='left', anchor_y='top',
batch=self.part_batch)
2023-01-19 22:29:43 +08:00
self.part_data: Dict[int, SR1PartData] = {}
2023-01-19 16:32:36 +08:00
self.parts_sprite: Dict[int, Sprite] = {}
2023-01-24 23:59:07 +08:00
if DR_option.use_DR_rust:
2023-01-24 12:25:23 +08:00
self.rust_parts = None
2022-12-25 23:15:49 +08:00
2023-01-19 22:29:43 +08:00
def load_xml(self, file_path: str) -> bool:
try:
cache_doc = parse(file_path)
self.xml_doc = cache_doc
self.xml_root = self.xml_doc.getroot()
return True
except:
return False
2022-12-26 11:46:05 +08:00
def load_textures(self):
self.textures = SR1Textures()
def render_ship(self):
if self.textures is None:
self.load_textures()
2023-01-19 22:29:43 +08:00
self.part_data: Dict[int, SR1PartData] = {}
self.parts_sprite: Dict[int, Sprite] = {}
2023-01-21 12:20:50 +08:00
self.dx = 0
self.dy = 0
self.scale = 1.0
2022-12-26 11:46:05 +08:00
parts = self.xml_root.find('Parts')
2023-01-19 22:29:43 +08:00
for part_xml in parts:
if part_xml.tag != 'Part':
2022-12-26 11:46:05 +08:00
continue # 如果不是部件,则跳过
# print(f"tag: {part.tag} attrib: {part.attrib}")
2022-12-29 10:13:20 +08:00
part_render = True
2023-01-21 22:50:18 +08:00
part = get_sr1_part(part_xml)
2023-01-19 22:29:43 +08:00
if part.id in self.part_data:
print(f'hey! warning! id{part.id}')
self.part_data[part.id] = part
2023-01-19 16:32:36 +08:00
# 下面就是调用 pyglet 去渲染的部分
render_scale = DR_option.gui_scale # 这个是 DR 的缩放比例 可以调节的(
# 主要是 Windows 下有一个缩放系数嘛,我待会试试这玩意能不能获取(估计得 ctypes
# 在不缩放的情况下XML的1个单位长度对应60个像素
2023-01-19 22:29:43 +08:00
render_x = part.x * render_scale * self.scale * 60 + self.window_pointer.width / 2 + self.dx
2023-01-21 14:50:30 +08:00
render_y = part.y * render_scale * self.scale * 60 + self.window_pointer.height / 2 + self.dy
2023-01-19 16:32:36 +08:00
# 你就这里改吧
2023-01-19 22:29:43 +08:00
cache_sprite = Sprite(img=self.textures.get_texture(part.textures),
2023-01-19 16:32:36 +08:00
x=render_x, y=render_y,
batch=self.part_batch, group=self.part_group)
# 你得帮我换算一下 XML 里的 x y 和这里的屏幕像素的关系OK
# 旋转啥的不是大问题, 我找你要那个渲染代码就是要 x y 的换算逻辑
2023-01-19 22:29:43 +08:00
cache_sprite.rotation = SR1Rotation.get_rotation(part.angle)
if part.flip_x:
cache_sprite.scale_x = -1 # 就是直接取反缩放,应该没问题····吧?(待会试试就知道了
if part.flip_y:
cache_sprite.scale_y = -1
cache_sprite.scale = self.scale * DR_option.gui_scale
2023-01-19 16:32:36 +08:00
cache_sprite.x = cache_sprite.x - cache_sprite.scale_x / 2
cache_sprite.y = cache_sprite.y - cache_sprite.scale_y / 2
if not part_render: # 如果不渲染(渲染有毛病)
2023-01-19 22:29:43 +08:00
self.parts_sprite[part.id].visible = False
self.parts_sprite[part.id] = cache_sprite
2023-01-21 22:50:18 +08:00
self.need_draw = False
2023-01-24 23:59:07 +08:00
if DR_option.use_DR_rust:
2023-01-24 12:25:23 +08:00
print(type(self.part_data))
self.rust_parts = PartDatas(self.part_data)
2023-01-24 19:49:46 +08:00
# print(self.rust_parts.get_rust_pointer())
2023-01-19 22:29:43 +08:00
self.rendered = True
2023-01-19 16:32:36 +08:00
2023-01-19 22:29:43 +08:00
def update_parts(self) -> bool:
if not self.rendered:
return False
2023-01-24 23:59:07 +08:00
if DR_option.use_DR_rust:
# print(f'{self.dx=} {self.dy=} {self.scale=}')
# from objprint import op
# op(random.choices(self.parts_sprite), indent=1)
return better_update_parts(self, SR1ShipRender_Option, self.window_pointer,
self.rust_parts, DR_option.gui_scale, 60)
2023-01-24 12:25:23 +08:00
self.debug_line.x2, self.debug_line.y2 = self.dx + (self.window_pointer.width / 2), self.dy + (
self.window_pointer.height / 2)
2023-01-21 23:30:22 +08:00
self.debug_d_pos_label.text = f'x: {self.dx} y: {self.dy}'
2023-01-24 12:25:23 +08:00
self.debug_d_pos_label.position = self.dx + (self.window_pointer.width / 2), self.dy + (
self.window_pointer.height / 2) + 10, 0
2023-01-19 22:29:43 +08:00
for part_id in self.part_data:
# x y scale
2023-01-24 19:49:46 +08:00
self.parts_sprite[part_id].x = self.part_data[part_id].x * DR_option.gui_scale * self.scale * 60 + self.window_pointer.width / 2 + self.dx
self.parts_sprite[part_id].y = self.part_data[part_id].y * DR_option.gui_scale * self.scale * 60 + self.window_pointer.height / 2 + self.dy
2023-01-19 22:29:43 +08:00
self.parts_sprite[part_id].scale = self.scale * DR_option.gui_scale
2022-12-26 11:46:05 +08:00
2022-12-25 23:15:49 +08:00
def on_draw(self):
2023-01-21 22:50:18 +08:00
if self.need_draw:
self.render_ship()
2022-12-29 10:13:20 +08:00
self.part_batch.draw()
2023-01-21 23:30:22 +08:00
self.debug_label.draw()
if SR1ShipRender_Option.debug_d_pos:
2023-01-21 14:50:30 +08:00
self.debug_line.draw()
2023-01-21 23:30:22 +08:00
self.debug_d_pos_label.draw()
if SR1ShipRender_Option.debug_mouse_pos:
2023-01-21 14:50:30 +08:00
self.debug_mouse_line.draw()
self.debug_mouse_label.draw()
2023-01-21 23:30:22 +08:00
if SR1ShipRender_Option.debug_mouse_d_pos:
2023-01-21 22:50:18 +08:00
self.debug_mouse_delta_line.draw()
2022-12-11 10:39:05 +08:00
2023-01-19 22:29:43 +08:00
def on_resize(self, width: int, height: int):
if not self.rendered:
return
2023-01-21 14:50:30 +08:00
self.debug_line.x = width / 2
self.debug_line.y = height / 2
self.debug_mouse_line.x = width / 2
self.debug_mouse_line.y = height / 2
2023-01-21 22:50:18 +08:00
self.debug_mouse_delta_line.x = width / 2
self.debug_mouse_delta_line.y = height / 2
2023-01-19 22:29:43 +08:00
self.update_parts()
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int):
if not self.rendered:
return
2023-01-21 14:50:30 +08:00
mouse_dx = x - (self.window_pointer.width / 2)
mouse_dy = y - (self.window_pointer.height / 2)
self.debug_mouse_line.x2, self.debug_mouse_line.y2 = x, y
2023-01-21 22:50:18 +08:00
if not self.scale * (0.5 ** scroll_y) >= 10:
self.scale = self.scale * (0.5 ** scroll_y)
self.dx += (mouse_dx - self.dx) * (1 - (0.5 ** scroll_y))
self.dy += (mouse_dy - self.dy) * (1 - (0.5 ** scroll_y))
else:
self.scale = 10
2023-01-24 19:49:46 +08:00
self.debug_mouse_delta_line.x2 = (mouse_dx - self.dx) * (1 - (0.5 ** scroll_y)) + (self.window_pointer.width / 2)
self.debug_mouse_delta_line.y2 = (mouse_dy - self.dy) * (1 - (0.5 ** scroll_y)) + (self.window_pointer.height / 2)
2023-01-21 14:50:30 +08:00
self.debug_mouse_label.text = f'x: {mouse_dx} y: {mouse_dy}'
self.debug_mouse_label.position = x, y + 10, 0
2023-01-19 22:29:43 +08:00
self.update_parts()
2023-01-21 22:50:18 +08:00
# print(f'{self.scale=} {self.dx=} {self.dy=} {x=} {y=} {scroll_x=} {scroll_y=} {1 - (0.5 ** scroll_y)=}')
2023-01-19 22:29:43 +08:00
2023-01-21 11:48:07 +08:00
def on_command(self, command: CommandText):
2023-01-22 09:17:01 +08:00
if command.re_match('render'):
2023-01-21 11:48:07 +08:00
# self.render_ship()
2023-01-21 22:50:18 +08:00
self.need_draw = True
2023-01-21 11:48:07 +08:00
print('应该渲染飞船的')
2023-01-22 09:17:01 +08:00
elif command.re_match('sr1'):
if command.re_match('delta'):
2023-01-21 23:30:22 +08:00
SR1ShipRender_Option.debug_d_pos = not SR1ShipRender_Option.debug_mouse_d_pos
self.debug_line.visible = SR1ShipRender_Option.debug_d_pos
self.debug_d_pos_label.visible = SR1ShipRender_Option.debug_d_pos
2023-01-22 09:17:01 +08:00
elif command.re_match('mouse'):
if command.re_match('delta'):
2023-01-21 23:30:22 +08:00
SR1ShipRender_Option.debug_mouse_pos = not SR1ShipRender_Option.debug_mouse_pos
self.debug_mouse_line.visible = SR1ShipRender_Option.debug_mouse_pos
self.debug_mouse_label.visible = SR1ShipRender_Option.debug_mouse_pos
2023-01-21 22:50:18 +08:00
else:
2023-01-21 23:30:22 +08:00
SR1ShipRender_Option.debug_mouse_d_pos = not SR1ShipRender_Option.debug_mouse_d_pos
self.debug_mouse_delta_line.visible = SR1ShipRender_Option.debug_mouse_d_pos
2023-01-21 11:48:07 +08:00
2023-01-19 22:29:43 +08:00
def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int):
if not self.focus:
return
self.dx += dx
self.dy += dy
self.update_parts()
2022-12-26 11:46:05 +08:00
def on_file_drop(self, x: int, y: int, paths: List[str]):
2023-01-19 22:29:43 +08:00
for path in paths:
2023-01-20 13:52:58 +08:00
if self.load_xml(path): # 加载成功一个就停下
2023-01-19 22:29:43 +08:00
break
2022-12-26 11:46:05 +08:00
self.render_ship()
2023-01-21 12:20:50 +08:00
print(paths)
2023-01-23 13:54:05 +08:00
if __name__ == '__main__':
from objprint import op
op(SR1ShipRender_Option)