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-06-17 00:06:39 +08:00
|
|
|
|
# import math
|
2023-01-30 11:59:34 +08:00
|
|
|
|
import time
|
2023-04-20 23:51:28 +08:00
|
|
|
|
import random
|
2023-06-17 14:36:43 +08:00
|
|
|
|
import logging
|
2023-05-25 00:40:20 +08:00
|
|
|
|
import traceback
|
2023-06-22 01:41:11 +08:00
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
2023-01-19 22:29:43 +08:00
|
|
|
|
from xml.etree.ElementTree import Element
|
2023-02-25 09:18:41 +08:00
|
|
|
|
from typing import List, TYPE_CHECKING, Union, Dict, Optional, Generator
|
2022-12-26 11:46:05 +08:00
|
|
|
|
from defusedxml.ElementTree import parse
|
2022-12-25 23:15:49 +08:00
|
|
|
|
|
2023-02-19 21:02:14 +08:00
|
|
|
|
from pyglet.math import Vec4
|
2023-01-19 22:29:43 +08:00
|
|
|
|
from pyglet.text import Label
|
2022-12-29 10:13:20 +08:00
|
|
|
|
from pyglet.sprite import Sprite
|
2023-06-17 00:06:39 +08:00
|
|
|
|
# from pyglet.image import Texture
|
2023-01-19 22:29:43 +08:00
|
|
|
|
from pyglet.graphics import Batch, Group
|
2023-06-18 01:20:27 +08:00
|
|
|
|
from pyglet.shapes import Line, Rectangle
|
2022-12-11 10:39:05 +08:00
|
|
|
|
|
2023-04-30 00:48:42 +08:00
|
|
|
|
from . import DR_mod_runtime
|
|
|
|
|
|
2022-12-11 10:39:05 +08:00
|
|
|
|
# Difficult Rocket
|
2023-06-16 23:36:24 +08:00
|
|
|
|
from Difficult_Rocket import DR_status
|
2023-06-22 01:41:11 +08:00
|
|
|
|
from Difficult_Rocket.utils.translate import Tr
|
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-06-25 15:00:37 +08:00
|
|
|
|
from Difficult_Rocket.utils.camera import CenterCamera
|
2023-05-24 00:43:11 +08:00
|
|
|
|
from .types 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-04-30 00:48:42 +08:00
|
|
|
|
if DR_mod_runtime.use_DR_rust:
|
2023-06-25 15:00:37 +08:00
|
|
|
|
from .Difficult_Rocket_rs import (SR1PartList_rs,
|
2023-06-24 21:16:32 +08:00
|
|
|
|
SR1Ship_rs,
|
|
|
|
|
load_and_save_test,
|
|
|
|
|
test_ship_read_and_write)
|
2023-04-05 01:27:34 +08:00
|
|
|
|
|
2023-06-17 14:36:43 +08:00
|
|
|
|
logger = logging.getLogger('client.dr_game_sr1_ship')
|
2023-06-22 01:41:11 +08:00
|
|
|
|
sr_tr = Tr(lang_path=Path('./mods/dr_game/lang'))
|
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-02-12 12:26:18 +08:00
|
|
|
|
return SR1PartData(x=part_x, y=part_y, id=part_id, p_type=part_type,
|
2023-02-07 12:02:37 +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)
|
2023-01-19 22:29:43 +08:00
|
|
|
|
|
|
|
|
|
|
2023-01-21 22:50:18 +08:00
|
|
|
|
class _SR1ShipRender_Option(Options):
|
2023-01-21 14:50:30 +08:00
|
|
|
|
# debug option
|
2023-02-07 12:02:37 +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,
|
2023-02-12 12:26:18 +08:00
|
|
|
|
main_window: "ClientWindow"):
|
2022-12-25 23:15:49 +08:00
|
|
|
|
super().__init__(main_window)
|
2023-06-22 01:41:11 +08:00
|
|
|
|
logger.info(sr_tr().mod.info.setup.start())
|
2023-04-05 10:34:58 +08:00
|
|
|
|
load_start_time = time.time_ns()
|
2023-01-19 22:29:43 +08:00
|
|
|
|
self.rendered = False
|
|
|
|
|
self.focus = True
|
2023-01-21 22:50:18 +08:00
|
|
|
|
self.need_draw = False
|
2023-02-07 12:02:37 +08:00
|
|
|
|
self.drawing = False
|
2023-02-19 21:02:14 +08:00
|
|
|
|
self.gen_draw: Optional[Generator] = None
|
2023-01-25 20:13:36 +08:00
|
|
|
|
self.need_update_parts = 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-05-25 22:50:45 +08:00
|
|
|
|
# self.xml_name = 'configs/dock1.xml'
|
|
|
|
|
# self.xml_doc: ElementTree = parse('configs/dock1.xml')
|
|
|
|
|
# self.xml_root: ElementTree.Element = self.xml_doc.getroot()
|
|
|
|
|
self.load_xml('configs/dock1.xml')
|
2023-06-06 18:58:07 +08:00
|
|
|
|
self.part_box_batch = Batch()
|
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-06-22 02:05:12 +08:00
|
|
|
|
self.debug_label = Label(x=20, y=main_window.height - 100, font_size=DR_status.std_font_size,
|
2023-01-24 12:25:23 +08:00
|
|
|
|
text='SR1 render!', font_name=Fonts.微软等宽无线,
|
|
|
|
|
width=main_window.width - 20, height=20,
|
2023-02-02 16:10:21 +08:00
|
|
|
|
anchor_x='left', anchor_y='top')
|
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-06-06 18:58:07 +08:00
|
|
|
|
self.part_box_dict: Dict[int, Rectangle] = {}
|
2023-06-18 01:20:27 +08:00
|
|
|
|
self.part_line_box: Dict[int, List[Line]] = {}
|
2023-04-05 10:34:58 +08:00
|
|
|
|
load_end_time = time.time_ns()
|
2023-06-22 01:41:11 +08:00
|
|
|
|
logger.info(sr_tr().mod.info.setup.use_time().format(
|
2023-04-05 10:34:58 +08:00
|
|
|
|
(load_end_time - load_start_time) / 1000000000))
|
2023-06-25 15:00:37 +08:00
|
|
|
|
self.camera = CenterCamera(main_window,
|
|
|
|
|
min_zoom=(1 / 2) ** 10, max_zoom=10)
|
2023-04-30 00:48:42 +08:00
|
|
|
|
if DR_mod_runtime.use_DR_rust:
|
2023-01-24 12:25:23 +08:00
|
|
|
|
self.rust_parts = None
|
2023-04-05 12:13:02 +08:00
|
|
|
|
self.part_list_rs = SR1PartList_rs('configs/PartList.xml', 'default_part_list')
|
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:
|
2023-04-05 12:13:02 +08:00
|
|
|
|
start_time = time.time_ns()
|
2023-06-22 01:41:11 +08:00
|
|
|
|
logger.info(sr_tr().sr1.ship.xml.loading().format(file_path))
|
2023-01-19 22:29:43 +08:00
|
|
|
|
cache_doc = parse(file_path)
|
|
|
|
|
self.xml_doc = cache_doc
|
|
|
|
|
self.xml_root = self.xml_doc.getroot()
|
2023-04-05 12:13:02 +08:00
|
|
|
|
self.xml_name = file_path
|
2023-05-25 00:40:20 +08:00
|
|
|
|
if DR_mod_runtime.use_DR_rust:
|
|
|
|
|
try:
|
2023-06-24 21:16:32 +08:00
|
|
|
|
load_and_save_test(self.xml_name)
|
|
|
|
|
test_ship_read_and_write(self.xml_name)
|
2023-05-25 00:40:20 +08:00
|
|
|
|
self.rust_ship = SR1Ship_rs(file_path, 'configs/PartList.xml', 'a_new_ship')
|
|
|
|
|
print(self.rust_ship.name)
|
|
|
|
|
print(self.rust_ship.img_pos)
|
|
|
|
|
except Exception:
|
|
|
|
|
traceback.print_exc()
|
2023-06-22 01:41:11 +08:00
|
|
|
|
logger.info(sr_tr().sr1.ship.xml.load_done())
|
|
|
|
|
logger.info(sr_tr().sr1.ship.xml.load_time().format(
|
2023-04-05 12:13:02 +08:00
|
|
|
|
(time.time_ns() - start_time) / 1000000000))
|
2023-01-19 22:29:43 +08:00
|
|
|
|
return True
|
2023-03-11 15:20:06 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
2023-01-19 22:29:43 +08:00
|
|
|
|
return False
|
|
|
|
|
|
2022-12-26 11:46:05 +08:00
|
|
|
|
def load_textures(self):
|
|
|
|
|
self.textures = SR1Textures()
|
|
|
|
|
|
2023-02-07 12:02:37 +08:00
|
|
|
|
def gen_sprite(self, part_datas: Dict[int, SR1PartData], each_count: int = 100) -> Generator:
|
|
|
|
|
count = 0
|
|
|
|
|
self.drawing = True
|
|
|
|
|
for part_id, part in part_datas.items():
|
2023-01-19 16:32:36 +08:00
|
|
|
|
# 下面就是调用 pyglet 去渲染的部分
|
2023-06-16 23:36:24 +08:00
|
|
|
|
# render_scale = DR_status.gui_scale # 这个是 DR 的缩放比例 可以调节的(
|
2023-01-19 16:32:36 +08:00
|
|
|
|
# 主要是 Windows 下有一个缩放系数嘛,我待会试试这玩意能不能获取(估计得 ctypes
|
|
|
|
|
# 在不缩放的情况下,XML的1个单位长度对应60个像素
|
2023-02-07 00:28:07 +08:00
|
|
|
|
render_x = part.x * 60
|
|
|
|
|
render_y = part.y * 60
|
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-04-20 23:51:28 +08:00
|
|
|
|
x=render_x, y=render_y, z=random.random(),
|
2023-01-19 16:32:36 +08:00
|
|
|
|
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
|
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
|
2023-01-19 22:29:43 +08:00
|
|
|
|
self.parts_sprite[part.id] = cache_sprite
|
2023-05-25 00:40:20 +08:00
|
|
|
|
|
2023-06-06 18:58:07 +08:00
|
|
|
|
if DR_mod_runtime.use_DR_rust:
|
2023-06-18 01:20:27 +08:00
|
|
|
|
part_debug_box = self.rust_ship.get_part_box(part.id)
|
2023-06-18 01:26:03 +08:00
|
|
|
|
if part_debug_box:
|
|
|
|
|
# 线框
|
|
|
|
|
part_line_box = []
|
2023-06-18 01:45:00 +08:00
|
|
|
|
width = 4
|
2023-06-18 01:29:42 +08:00
|
|
|
|
color = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255), random.randrange(100, 200))
|
2023-06-18 01:26:03 +08:00
|
|
|
|
part_line_box.append(Line(x=part_debug_box[0][0] * 30, y=part_debug_box[0][1] * 30,
|
|
|
|
|
x2=part_debug_box[0][0] * 30, y2=part_debug_box[1][1] * 30,
|
|
|
|
|
batch=self.part_box_batch, width=width, color=color))
|
|
|
|
|
part_line_box.append(Line(x=part_debug_box[0][0] * 30, y=part_debug_box[1][1] * 30,
|
|
|
|
|
x2=part_debug_box[1][0] * 30, y2=part_debug_box[1][1] * 30,
|
|
|
|
|
batch=self.part_box_batch, width=width, color=color))
|
|
|
|
|
part_line_box.append(Line(x=part_debug_box[1][0] * 30, y=part_debug_box[1][1] * 30,
|
|
|
|
|
x2=part_debug_box[1][0] * 30, y2=part_debug_box[0][1] * 30,
|
|
|
|
|
batch=self.part_box_batch, width=width, color=color))
|
|
|
|
|
part_line_box.append(Line(x=part_debug_box[1][0] * 30, y=part_debug_box[0][1] * 30,
|
|
|
|
|
x2=part_debug_box[0][0] * 30, y2=part_debug_box[0][1] * 30,
|
|
|
|
|
batch=self.part_box_batch, width=width, color=color))
|
|
|
|
|
self.part_line_box[part.id] = part_line_box
|
2023-02-07 12:02:37 +08:00
|
|
|
|
# if not part_render: # 如果不渲染(渲染有毛病)
|
|
|
|
|
# self.parts_sprite[part.id].visible = False
|
|
|
|
|
count += 1
|
|
|
|
|
if count >= each_count:
|
|
|
|
|
count = 0
|
|
|
|
|
yield each_count
|
|
|
|
|
self.drawing = False
|
|
|
|
|
raise GeneratorExit
|
|
|
|
|
|
|
|
|
|
def render_ship(self):
|
|
|
|
|
if self.textures is None:
|
|
|
|
|
self.load_textures()
|
2023-06-22 01:41:11 +08:00
|
|
|
|
logger.info(sr_tr().sr1.ship.ship.load().format(self.xml_name))
|
2023-02-07 12:02:37 +08:00
|
|
|
|
start_time = time.perf_counter_ns()
|
|
|
|
|
self.part_data: Dict[int, SR1PartData] = {}
|
|
|
|
|
self.parts_sprite: Dict[int, Sprite] = {}
|
2023-06-18 01:45:00 +08:00
|
|
|
|
self.part_line_box = {}
|
2023-06-25 15:00:37 +08:00
|
|
|
|
self.camera.zoom = 1.0
|
|
|
|
|
self.camera.dx = 0
|
|
|
|
|
self.camera.dy = 0
|
2023-02-07 12:02:37 +08:00
|
|
|
|
parts = self.xml_root.find('Parts')
|
|
|
|
|
for part_xml in parts:
|
|
|
|
|
if part_xml.tag != 'Part':
|
|
|
|
|
continue # 如果不是部件,则跳过
|
|
|
|
|
part = get_sr1_part(part_xml)
|
|
|
|
|
if part.id in self.part_data:
|
|
|
|
|
print(f'hey! warning! id{part.id}')
|
|
|
|
|
self.part_data[part.id] = part
|
|
|
|
|
# 调用生成器 减少卡顿
|
2023-02-19 11:47:15 +08:00
|
|
|
|
try:
|
2023-02-07 12:02:37 +08:00
|
|
|
|
self.gen_draw = self.gen_sprite(self.part_data)
|
|
|
|
|
next(self.gen_draw)
|
2023-02-19 11:47:15 +08:00
|
|
|
|
except GeneratorExit:
|
|
|
|
|
self.drawing = False
|
2023-02-07 12:02:37 +08:00
|
|
|
|
self.need_draw = False
|
2023-04-05 16:43:20 +08:00
|
|
|
|
full_mass = 0
|
2023-04-30 00:48:42 +08:00
|
|
|
|
if DR_mod_runtime.use_DR_rust:
|
2023-04-05 16:43:20 +08:00
|
|
|
|
for part in self.part_data:
|
|
|
|
|
full_mass += self.part_list_rs.get_part_type(self.part_data[part].p_type).mass * 500
|
2023-06-22 01:41:11 +08:00
|
|
|
|
logger.info(sr_tr().sr1.ship.ship.load_time().format(
|
2023-04-05 11:53:40 +08:00
|
|
|
|
(time.perf_counter_ns() - start_time) / 1000000000))
|
2023-06-22 01:41:11 +08:00
|
|
|
|
logger.info(sr_tr().sr1.ship.ship.info().format(
|
|
|
|
|
len(self.part_data), f'{full_mass}kg' if DR_mod_runtime.use_DR_rust else sr_tr().game.require_DR_rs()))
|
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-06-25 15:00:37 +08:00
|
|
|
|
self.debug_line.x2, self.debug_line.y2 = self.camera.dx + (
|
|
|
|
|
self.window_pointer.width / 2), self.camera.dy + (
|
2023-02-07 12:02:37 +08:00
|
|
|
|
self.window_pointer.height / 2)
|
2023-06-25 15:00:37 +08:00
|
|
|
|
self.debug_d_pos_label.text = f'x: {self.camera.dx} y: {self.camera.dy}'
|
|
|
|
|
self.debug_d_pos_label.position = self.camera.dx + (self.window_pointer.width / 2), self.camera.dy + (
|
2023-02-07 12:02:37 +08:00
|
|
|
|
self.window_pointer.height / 2) + 10, 0
|
2023-01-25 20:15:17 +08:00
|
|
|
|
self.need_update_parts = False
|
2022-12-26 11:46:05 +08:00
|
|
|
|
|
2023-04-19 00:42:31 +08:00
|
|
|
|
def on_draw(self, window: "ClientWindow"):
|
2023-01-21 22:50:18 +08:00
|
|
|
|
if self.need_draw:
|
|
|
|
|
self.render_ship()
|
2023-02-07 12:02:37 +08:00
|
|
|
|
|
|
|
|
|
if self.drawing:
|
2023-04-05 12:13:02 +08:00
|
|
|
|
try:
|
2023-02-07 12:02:37 +08:00
|
|
|
|
next(self.gen_draw)
|
2023-04-05 12:13:02 +08:00
|
|
|
|
except GeneratorExit:
|
|
|
|
|
self.drawing = False
|
2023-06-22 01:41:11 +08:00
|
|
|
|
logger.info(sr_tr().sr1.ship.ship.render.done())
|
2023-02-07 12:02:37 +08:00
|
|
|
|
|
2023-01-25 20:13:36 +08:00
|
|
|
|
if self.need_update_parts:
|
|
|
|
|
self.update_parts()
|
|
|
|
|
self.need_update_parts = False
|
2023-02-07 12:02:37 +08:00
|
|
|
|
|
2023-06-25 15:00:37 +08:00
|
|
|
|
with self.camera:
|
2023-06-06 18:58:07 +08:00
|
|
|
|
self.part_box_batch.draw()
|
2023-06-06 19:04:07 +08:00
|
|
|
|
self.part_batch.draw()
|
2023-06-06 23:52:27 +08:00
|
|
|
|
self.part_box_batch.draw()
|
2023-02-07 12:02:37 +08:00
|
|
|
|
|
2023-01-21 23:30:22 +08:00
|
|
|
|
self.debug_label.draw()
|
2023-02-07 12:02:37 +08:00
|
|
|
|
|
2023-01-21 23:30:22 +08:00
|
|
|
|
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-04-19 00:42:31 +08:00
|
|
|
|
def on_resize(self, width: int, height: int, window: "ClientWindow"):
|
2023-06-22 02:05:12 +08:00
|
|
|
|
self.debug_label.y = height - 100
|
2023-01-19 22:29:43 +08:00
|
|
|
|
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()
|
|
|
|
|
|
2023-04-19 00:42:31 +08:00
|
|
|
|
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int, window: "ClientWindow"):
|
2023-01-19 22:29:43 +08:00
|
|
|
|
if not self.rendered:
|
|
|
|
|
return
|
2023-04-19 00:42:31 +08:00
|
|
|
|
mouse_dx = x - (window.width / 2)
|
|
|
|
|
mouse_dy = y - (window.height / 2)
|
2023-02-19 21:02:14 +08:00
|
|
|
|
# 鼠标缩放位置相对于屏幕中心的位置
|
2023-06-25 15:00:37 +08:00
|
|
|
|
mouse_dx_d = mouse_dx - self.camera.dx
|
|
|
|
|
mouse_dy_d = mouse_dy - self.camera.dy
|
2023-02-19 21:02:14 +08:00
|
|
|
|
# 鼠标相对偏移量的偏移量
|
2023-02-26 14:06:48 +08:00
|
|
|
|
if scroll_y == 0:
|
2023-02-19 21:02:14 +08:00
|
|
|
|
zoom_d = 1
|
|
|
|
|
else:
|
|
|
|
|
zoom_d = ((2 ** scroll_y) - 1) * 0.5 + 1
|
|
|
|
|
# 缩放的变换量
|
2023-06-25 15:00:37 +08:00
|
|
|
|
if not (self.camera.zoom == 10 and scroll_y > 0):
|
|
|
|
|
if self.camera.zoom * zoom_d >= 10:
|
|
|
|
|
zoom_d = 10 / self.camera.zoom
|
|
|
|
|
self.camera.zoom = 10
|
2023-04-05 14:19:20 +08:00
|
|
|
|
else:
|
2023-06-25 15:00:37 +08:00
|
|
|
|
self.camera.zoom *= zoom_d
|
2023-02-19 21:02:14 +08:00
|
|
|
|
mouse_dx_d *= (1 - zoom_d)
|
|
|
|
|
mouse_dy_d *= (1 - zoom_d)
|
2023-06-25 15:00:37 +08:00
|
|
|
|
self.camera.dx += mouse_dx_d
|
|
|
|
|
self.camera.dy += mouse_dy_d
|
2023-02-19 21:02:14 +08:00
|
|
|
|
|
|
|
|
|
self.debug_mouse_line.x2, self.debug_mouse_line.y2 = x, y
|
2023-06-25 15:00:37 +08:00
|
|
|
|
self.debug_mouse_delta_line.x2 = (mouse_dx - self.camera.dx) * (1 - (0.5 ** scroll_y)) + (
|
2023-04-19 00:42:31 +08:00
|
|
|
|
window.width / 2)
|
2023-06-25 15:00:37 +08:00
|
|
|
|
self.debug_mouse_delta_line.y2 = (mouse_dy - self.camera.dy) * (1 - (0.5 ** scroll_y)) + (
|
2023-04-19 00:42:31 +08:00
|
|
|
|
window.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-25 20:13:36 +08:00
|
|
|
|
self.need_update_parts = True
|
|
|
|
|
# self.update_parts()
|
2023-01-19 22:29:43 +08:00
|
|
|
|
|
2023-04-19 00:42:31 +08:00
|
|
|
|
def on_command(self, command: CommandText, window: "ClientWindow"):
|
2023-05-24 00:43:11 +08:00
|
|
|
|
if command.find('render'):
|
|
|
|
|
if command.find('reset'):
|
2023-06-25 15:00:37 +08:00
|
|
|
|
self.camera.zoom = 1
|
|
|
|
|
self.camera.dx = 0
|
|
|
|
|
self.camera.dy = 0
|
2023-02-19 21:02:14 +08:00
|
|
|
|
self.window_pointer.view = Vec4()
|
2023-01-25 11:11:45 +08:00
|
|
|
|
else:
|
|
|
|
|
self.need_draw = True
|
2023-01-21 11:48:07 +08:00
|
|
|
|
print('应该渲染飞船的')
|
2023-05-24 00:43:11 +08:00
|
|
|
|
elif command.find('debug'):
|
|
|
|
|
if command.find('delta'):
|
2023-06-18 02:06:58 +08:00
|
|
|
|
# SR1ShipRender_Option.debug_d_pos = not SR1ShipRender_Option.debug_mouse_d_pos
|
|
|
|
|
self.debug_line.visible = not self.debug_line.visible
|
|
|
|
|
self.debug_d_pos_label.visible = not self.debug_d_pos_label.visible
|
|
|
|
|
SR1ShipRender_Option.debug_d_pos = self.debug_line.visible
|
|
|
|
|
logger.debug('sr1 delta')
|
2023-05-24 00:43:11 +08:00
|
|
|
|
elif command.find('mouse'):
|
|
|
|
|
if command.find('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-06-18 02:06:58 +08:00
|
|
|
|
logger.debug(f'sr1 mouse delta {SR1ShipRender_Option.debug_mouse_pos}')
|
2023-01-21 22:50:18 +08:00
|
|
|
|
else:
|
2023-06-18 02:06:58 +08:00
|
|
|
|
self.debug_mouse_delta_line.visible = not self.debug_mouse_delta_line.visible
|
|
|
|
|
SR1ShipRender_Option.debug_mouse_d_pos = self.debug_mouse_delta_line.visible
|
|
|
|
|
logger.debug(f'sr1 mouse {SR1ShipRender_Option.debug_mouse_d_pos}')
|
2023-06-18 01:45:00 +08:00
|
|
|
|
elif command.find('ship'):
|
|
|
|
|
if self.rendered:
|
|
|
|
|
for index, sprite in self.parts_sprite.items():
|
|
|
|
|
sprite.visible = not sprite.visible
|
2023-06-06 18:58:07 +08:00
|
|
|
|
|
2023-05-24 00:43:11 +08:00
|
|
|
|
elif command.find('get_buf'):
|
2023-02-07 18:42:41 +08:00
|
|
|
|
|
|
|
|
|
def screenshot(window):
|
2023-04-05 01:27:34 +08:00
|
|
|
|
from libs.pyglet.gl import GLubyte, GL_RGBA, GL_UNSIGNED_BYTE, \
|
2023-02-19 21:02:14 +08:00
|
|
|
|
glReadPixels
|
2023-02-08 21:39:06 +08:00
|
|
|
|
import pyglet
|
2023-04-01 17:47:43 +08:00
|
|
|
|
|
2023-02-07 18:42:41 +08:00
|
|
|
|
width = window.width
|
|
|
|
|
height = window.height
|
|
|
|
|
format_str = "RGBA"
|
2023-04-01 17:47:43 +08:00
|
|
|
|
|
2023-02-07 18:42:41 +08:00
|
|
|
|
buf = (GLubyte * (len(format_str) * width * height))()
|
|
|
|
|
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buf)
|
|
|
|
|
return pyglet.image.ImageData(width, height, format_str, buf)
|
|
|
|
|
|
|
|
|
|
image_data = screenshot(self.window_pointer)
|
|
|
|
|
image_data.save('test.png')
|
2023-05-24 00:43:11 +08:00
|
|
|
|
elif command.find('gen_img'):
|
2023-05-03 00:08:26 +08:00
|
|
|
|
if not self.rendered:
|
|
|
|
|
return
|
2023-05-25 22:50:45 +08:00
|
|
|
|
if not DR_mod_runtime.use_DR_rust:
|
|
|
|
|
# 这个功能依赖于 DR rs (简称,我懒得在Python端实现)
|
|
|
|
|
return
|
|
|
|
|
img_box = self.rust_ship.img_pos
|
|
|
|
|
img_size = (img_box[2] - img_box[0] + 1000, img_box[3] - img_box[1] + 1000)
|
|
|
|
|
# 中心点是左上角坐标
|
|
|
|
|
img_center = (abs(img_box[0]), abs(img_box[3]))
|
|
|
|
|
print(f"img_box: {img_box} img_size: {img_size} img_center: {img_center}")
|
|
|
|
|
try:
|
|
|
|
|
from PIL import Image
|
|
|
|
|
except ImportError:
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
print('PIL not found')
|
|
|
|
|
return
|
|
|
|
|
img = Image.new('RGBA', img_size)
|
|
|
|
|
for part, sprite in self.parts_sprite.items():
|
2023-06-01 23:41:37 +08:00
|
|
|
|
sprite_img = sprite.image
|
|
|
|
|
print(f"sprite_img: {sprite_img} {sprite_img.width} {sprite_img.height}")
|
|
|
|
|
img_data = sprite_img.get_image_data()
|
2023-05-25 22:50:45 +08:00
|
|
|
|
fmt = img_data.format
|
|
|
|
|
if fmt != 'RGB':
|
|
|
|
|
fmt = 'RGBA'
|
|
|
|
|
pitch = -(img_data.width * len(fmt))
|
|
|
|
|
pil_image = Image.frombytes(fmt, (img_data.width, img_data.height), img_data.get_data(fmt, pitch))
|
2023-06-01 23:41:37 +08:00
|
|
|
|
pil_image = pil_image.rotate(SR1Rotation.get_rotation(self.part_data[part].angle), expand=True)
|
2023-05-25 22:50:45 +08:00
|
|
|
|
if self.part_data[part].flip_y:
|
|
|
|
|
pil_image.transpose(Image.FLIP_TOP_BOTTOM)
|
|
|
|
|
if self.part_data[part].flip_x:
|
|
|
|
|
pil_image.transpose(Image.FLIP_LEFT_RIGHT)
|
2023-06-06 18:58:07 +08:00
|
|
|
|
img.paste(pil_image, (int(self.part_data[part].x * 60 + img_center[0]), int(-self.part_data[part].y * 60 + img_center[1])), pil_image)
|
2023-05-25 22:50:45 +08:00
|
|
|
|
img.save(f'test{time.time()}.png', 'PNG')
|
2023-01-21 11:48:07 +08:00
|
|
|
|
|
2023-06-23 01:12:33 +08:00
|
|
|
|
elif command.find('test'):
|
|
|
|
|
if command.find('save'):
|
|
|
|
|
if not self.rendered:
|
|
|
|
|
return
|
|
|
|
|
if not DR_mod_runtime.use_DR_rust:
|
|
|
|
|
return
|
2023-06-24 21:16:32 +08:00
|
|
|
|
logger.info(sr_tr().sr1.ship.save.start().format(self.rust_ship))
|
2023-06-23 01:12:33 +08:00
|
|
|
|
self.rust_ship.save('./test-save.xml')
|
|
|
|
|
|
2023-04-19 00:42:31 +08:00
|
|
|
|
def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int, window: "ClientWindow"):
|
2023-01-19 22:29:43 +08:00
|
|
|
|
if not self.focus:
|
|
|
|
|
return
|
2023-06-25 15:00:37 +08:00
|
|
|
|
self.camera.dx += dx
|
|
|
|
|
self.camera.dy += dy
|
2023-01-25 20:13:36 +08:00
|
|
|
|
self.need_update_parts = True
|
|
|
|
|
# self.update_parts()
|
2023-01-19 22:29:43 +08:00
|
|
|
|
|
2023-04-19 00:42:31 +08:00
|
|
|
|
def on_file_drop(self, x: int, y: int, paths: List[str], window: "ClientWindow"):
|
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-23 13:54:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
from objprint import op
|
|
|
|
|
|
|
|
|
|
op(SR1ShipRender_Option)
|