Difficult-Rocket/mods/dr_game/sr1_ship.py

868 lines
29 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
# -------------------------------
2024-05-30 10:18:55 +08:00
import os
2023-01-30 11:59:34 +08:00
import time
2023-04-20 23:51:28 +08:00
import random
2023-05-25 00:40:20 +08:00
import traceback
2023-06-22 01:41:11 +08:00
from pathlib import Path
from typing import List, Dict, Optional, Generator, Tuple
2024-05-30 22:23:24 +08:00
from tkinter import Tk
from tkinter import filedialog
2022-12-25 23:15:49 +08:00
2023-08-12 23:32:43 +08:00
from pyglet.gl import gl
2023-08-13 00:21:22 +08:00
from pyglet.math import Mat4
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-01-19 22:29:43 +08:00
from pyglet.graphics import Batch, Group
2024-05-26 11:25:33 +08:00
from pyglet.shapes import Line, Box
2022-12-11 10:39:05 +08:00
2023-04-30 00:48:42 +08:00
from . import DR_mod_runtime
2023-08-12 01:49:01 +08:00
from .types import SR1Textures, SR1Rotation
2023-04-30 00:48:42 +08:00
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-08-20 21:42:38 +08:00
from Difficult_Rocket.client import ClientWindow
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-08-12 01:49:01 +08:00
from Difficult_Rocket.api.camera import CenterGroupCamera
2022-12-25 23:15:49 +08:00
2024-05-30 22:23:24 +08:00
from Difficult_Rocket.gui.widget.button import (
PressTextButton,
MinecraftWikiButtonTheme,
ButtonThemeOptions,
BaseButtonTheme,
)
2023-12-24 15:51:44 +08:00
from lib_not_dr import loggers
2023-04-30 00:48:42 +08:00
if DR_mod_runtime.use_DR_rust:
2023-12-03 16:54:07 +08:00
from .Difficult_Rocket_rs import (
SR1PartList_rs,
SR1Ship_rs,
SR1PartData_rs,
SR1PartType_rs,
2024-05-22 23:58:14 +08:00
# SR1Connection_rs
2023-12-03 16:54:07 +08:00
)
2023-12-24 15:51:44 +08:00
logger = loggers.get_logger("client.dr_game_sr1_ship")
2023-12-03 16:54:07 +08:00
sr_tr = Tr(lang_path=Path(__file__).parent / "lang")
2023-01-23 00:01:01 +08:00
2022-12-25 23:15:49 +08:00
2023-07-24 09:46:19 +08:00
class SR1ShipRenderStatus(Options): # NOQA
name = "SR1ShipRenderStatus"
# main status
draw_done: bool = False
draw_call: bool = False
update_call: bool = False
focus: bool = True
moving: bool = False
# button status
show_moving: bool = False
show_focus: bool = False
show_scale: bool = False
2023-07-24 09:46:19 +08:00
# debug status
draw_d_pos: bool = False
draw_mouse_pos: bool = False
draw_mouse_d_pos: bool = False
2023-01-21 14:50:30 +08:00
2022-12-25 23:15:49 +08:00
class SR1ShipRender(BaseScreen):
"""用于渲染 sr1 船的类"""
2023-12-03 16:54:07 +08:00
name = "DR_game_sr1_ship_render"
2024-05-30 10:36:41 +08:00
ships_buttons = []
2024-05-30 22:23:24 +08:00
2023-12-03 16:54:07 +08:00
def __init__(self, main_window: ClientWindow):
2022-12-25 23:15:49 +08:00
super().__init__(main_window)
2023-07-12 16:46:32 +08:00
self.logger = logger
2024-05-16 00:33:25 +08:00
logger.info(sr_tr().mod.info.setup.start(), tag="setup")
2023-04-05 10:34:58 +08:00
load_start_time = time.time_ns()
2023-07-24 09:46:19 +08:00
# status
self.status = SR1ShipRenderStatus()
2023-07-28 14:34:31 +08:00
2023-01-19 22:29:43 +08:00
self.dx = 0
self.dy = 0
2023-08-12 20:20:18 +08:00
self.width = main_window.width
self.height = main_window.height
2023-07-21 13:10:21 +08:00
2023-07-02 15:54:50 +08:00
self.main_batch = Batch()
2023-12-03 16:54:07 +08:00
self.group_camera = CenterGroupCamera(
window=main_window,
order=10,
parent=main_window.main_group,
min_zoom=(1 / 2) ** 10,
max_zoom=10,
)
2023-08-12 00:49:46 +08:00
self.part_group = Group(0, parent=self.group_camera)
2023-07-21 13:10:21 +08:00
2023-12-03 16:54:07 +08:00
self.debug_label = Label(
x=20,
y=main_window.height - 100,
font_size=DR_status.std_font_size,
text="SR1 render!",
font_name=Fonts.微软等宽无线,
width=main_window.width - 20,
height=20,
anchor_x="left",
anchor_y="top",
batch=self.main_batch,
group=Group(5, parent=self.part_group),
)
self.render_d_line = Line(
0,
0,
0,
0,
width=5,
color=(200, 200, 10, 255),
batch=self.main_batch,
group=Group(5, parent=self.part_group),
)
2023-07-24 09:46:19 +08:00
self.render_d_line.visible = self.status.draw_d_pos
2023-12-03 16:54:07 +08:00
self.render_d_label = Label(
"debug label NODATA",
font_name=Fonts.微软等宽无线,
2024-05-26 11:25:33 +08:00
x=main_window.width // 2,
y=main_window.height // 2,
2023-12-03 16:54:07 +08:00
)
2023-07-24 09:46:19 +08:00
self.render_d_label.visible = self.status.draw_d_pos
2023-07-12 16:46:32 +08:00
# Optional data
2023-07-21 18:33:15 +08:00
self.textures: SR1Textures = SR1Textures()
2023-07-12 16:46:32 +08:00
self.gen_draw: Optional[Generator] = None
self.rust_ship: Optional[SR1Ship_rs] = None
2023-07-21 23:10:57 +08:00
self.ship_name: Optional[str] = None
2023-07-12 16:46:32 +08:00
# List/Dict data
2024-05-26 11:25:33 +08:00
self.part_sprites: Dict[int, Tuple[Sprite, Box]] = {}
self.part_outlines: Dict[int, List[Line]] = {}
self.connection_lines: List[Line] = []
2023-07-12 16:46:32 +08:00
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-12-03 16:54:07 +08:00
self.part_list_rs = SR1PartList_rs(
"assets/builtin/PartList.xml", "builtin_part_list"
)
2023-07-21 13:10:21 +08:00
2023-12-03 16:54:07 +08:00
self.load_xml("assets/builtin/dock1.xml")
2022-12-25 23:15:49 +08:00
2023-07-21 16:00:03 +08:00
load_end_time = time.time_ns()
2023-12-03 16:54:07 +08:00
logger.info(
sr_tr()
.mod.info.setup.use_time()
2024-05-16 00:33:25 +08:00
.format((load_end_time - load_start_time) / 1000000000),
tag="setup",
2023-12-03 16:54:07 +08:00
)
2023-07-21 16:00:03 +08:00
# Buttons
2024-05-30 20:19:29 +08:00
self.buttons_group = Group(100, parent=main_window.main_group)
self.ships_buttons_group = Group(100, parent=main_window.main_group)
2024-05-30 22:23:24 +08:00
self.enter_game_button = PressEnterGameButton(
window=main_window,
x=500,
y=100,
width=150,
height=30,
text="进入游戏",
batch=self.main_batch,
2024-05-30 20:19:29 +08:00
group=self.buttons_group,
2024-05-30 22:23:24 +08:00
draw_theme=MinecraftWikiButtonTheme,
)
2024-05-30 22:23:24 +08:00
self.select_ship_button = PressSelectShipButton(
window=main_window,
2024-05-30 22:23:24 +08:00
parent_window=self,
x=100,
y=100,
width=150,
height=30,
text="加载飞船",
batch=self.main_batch,
2024-05-30 20:19:29 +08:00
group=self.buttons_group,
draw_theme=MinecraftWikiButtonTheme,
)
2024-05-30 22:23:24 +08:00
main_window.push_handlers(self.enter_game_button)
main_window.push_handlers(self.select_ship_button)
2024-05-27 18:48:34 +08:00
2024-05-30 10:36:41 +08:00
# 扫描所有飞船
2024-05-30 11:18:51 +08:00
self.show_ships_buttons = True
self.ships_buttons_w = 150
self.ships_buttons_h = 30
self.ships_buttons_begin_x = self.width - self.ships_buttons_w
self.ships_buttons_begin_y = 0
self.ships_buttons_end_x = self.width
self.ships_buttons_end_y = self.height
2024-05-30 22:23:24 +08:00
2024-05-30 11:18:51 +08:00
ships_path = "./ships/"
2024-05-30 22:23:24 +08:00
ships_files = self.scan_all_ships_list(ships_path)
2024-05-30 11:18:51 +08:00
2024-05-30 10:36:41 +08:00
for i in range(len(ships_files)):
2024-05-30 22:23:24 +08:00
self.ships_buttons.append(
PressOpenShipButton(
window=main_window,
ship_path=ships_files[i],
parent_window=self,
x=self.ships_buttons_begin_x,
y=self.ships_buttons_end_y - i * self.ships_buttons_h,
width=self.ships_buttons_w,
height=self.ships_buttons_h,
text=ships_files[i][8:],
batch=self.main_batch,
group=self.ships_buttons_group,
draw_theme=MinecraftWikiButtonTheme,
)
)
2024-05-30 10:36:41 +08:00
main_window.push_handlers(self.ships_buttons[-1])
2024-05-30 22:23:24 +08:00
2024-05-30 20:19:29 +08:00
self.ship_list_line_back = Line(
self.ships_buttons_begin_x - 4,
self.ships_buttons_begin_y,
self.ships_buttons_begin_x - 4,
self.ships_buttons_end_y,
width=5,
color=(100, 100, 100, 255),
batch=self.main_batch,
group=self.ships_buttons_group,
)
self.ship_list_line = Line(
2024-05-30 22:23:24 +08:00
x=self.ships_buttons_begin_x,
y=self.ships_buttons_end_y
- (self.ships_buttons_end_y - self.ships_buttons_begin_y) ** 2
/ ((len(ships_files) + 1) * self.ships_buttons_h),
x2=self.ships_buttons_begin_x,
y2=self.ships_buttons_end_y,
2024-05-30 20:19:29 +08:00
width=20,
color=(200, 200, 200, 255),
batch=self.main_batch,
group=self.ships_buttons_group,
)
self.ships_buttons_group.visible = False
2023-07-24 09:46:19 +08:00
@property
def size(self) -> Tuple[int, int]:
2023-12-03 16:54:07 +08:00
"""渲染器的渲染大小"""
2023-07-24 09:46:19 +08:00
return self.width, self.height
@size.setter
def size(self, value: Tuple[int, int]):
if not self.width == value[0] or not self.height == value[1]:
self.width, self.height = value
2023-01-19 22:29:43 +08:00
def load_xml(self, file_path: str) -> bool:
2023-07-21 13:10:21 +08:00
"""
加载 xml 文件
:param file_path:
:return:
"""
2023-01-19 22:29:43 +08:00
try:
2023-04-05 12:13:02 +08:00
start_time = time.time_ns()
2024-05-16 00:33:25 +08:00
logger.info(sr_tr().sr1.ship.xml.loading().format(file_path), tag="load_xml")
2023-12-03 16:54:07 +08:00
self.ship_name = file_path.split("/")[-1].split(".")[0]
2023-05-25 00:40:20 +08:00
if DR_mod_runtime.use_DR_rust:
2023-12-03 16:54:07 +08:00
self.rust_ship = SR1Ship_rs(file_path, self.part_list_rs, "a_new_ship")
2024-05-16 00:33:25 +08:00
logger.info(sr_tr().sr1.ship.xml.load_done(), tag="load_xml")
2023-12-03 16:54:07 +08:00
logger.info(
sr_tr()
.sr1.ship.xml.load_time()
2024-05-16 00:33:25 +08:00
.format((time.time_ns() - start_time) / 1000000000),
tag="load_xml",
2023-12-03 16:54:07 +08:00
)
2023-01-19 22:29:43 +08:00
return True
2023-07-24 09:46:19 +08:00
except Exception:
traceback.print_exc()
2024-05-16 00:33:25 +08:00
self.logger.error(traceback.format_exc(), tag="load_xml")
2023-01-19 22:29:43 +08:00
return False
2024-05-23 21:24:19 +08:00
def part_render_init(
2024-05-23 21:24:19 +08:00
self,
part_data: SR1PartData_rs,
part_type: SR1PartType_rs,
part_group: Group,
line_group: Group,
batch: Batch,
2024-05-26 11:25:33 +08:00
) -> Tuple[Sprite, Box]:
"""
还是重写一下渲染逻辑
把渲染单个部件的逻辑提取出来放到这里
"""
2024-05-30 22:51:37 +08:00
randomer = random.Random(part_data.id)
texture = self.textures.get_texture(part_type.sprite)
part_sprite = Sprite(
img=texture,
x=part_data.x * 60,
y=part_data.y * 60,
2024-05-30 22:51:37 +08:00
z=randomer.random(),
batch=batch,
group=part_group,
)
part_sprite.rotation = part_data.angle_r
part_sprite.scale_x = -1 if part_data.flip_x else 1
part_sprite.scale_y = -1 if part_data.flip_y else 1
line_colors = (
2024-05-30 22:51:37 +08:00
randomer.randrange(0, 255),
randomer.randrange(0, 255),
randomer.randrange(0, 255),
200,
)
width = 4
(x, y), (x2, y2) = part_data.get_part_box_by_type(part_type)
2024-05-26 11:25:33 +08:00
box = Box(
x=x * 30,
y=y * 30,
width=part_type.width * 30,
height=part_type.height * 30,
thickness=width,
color=line_colors,
batch=batch,
group=line_group,
)
2024-05-26 11:25:33 +08:00
box.rotation = part_data.angle_r
return part_sprite, box
def sprite_batch(self, draw_batch: int = 100) -> Generator:
2023-07-21 13:10:21 +08:00
"""
生成 sprite
通过生成器减少一次性渲染的压力
:param each_count: 每次生成的数量 (默认 100) (过大会导致卡顿)
:return: 生成器
"""
2023-02-07 12:02:37 +08:00
count = 0
2023-07-24 09:46:19 +08:00
self.status.draw_done = False
2023-07-21 16:00:03 +08:00
# rust 渲染
2023-07-21 18:33:15 +08:00
if DR_mod_runtime.use_DR_rust:
2024-05-26 11:25:33 +08:00
self.rust_ship: SR1Ship_rs
# 2 先渲染
# 6 后渲染
# 保证部件不会遮盖住连接线
part_group = Group(2, parent=self.part_group)
line_group = Group(6, parent=self.part_group)
# 渲染连接的部件
for part_type, part_data in self.rust_ship.as_list():
# 渲染部件
2024-05-26 11:25:33 +08:00
part_sprite, part_box = self.part_render_init(
part_data, part_type, part_group, line_group, self.main_batch
)
2024-05-26 11:25:33 +08:00
part_box.opacity = 100
self.part_sprites[part_data.id] = (part_sprite, part_box)
# TODO: 连接线渲染
count += 2
if count >= draw_batch:
yield count
count = 0
# 渲染未连接的部件
2024-05-26 11:25:33 +08:00
for part_groups, part_connections in self.rust_ship.disconnected_parts():
for part_type, part_data in part_groups[0]:
# 未连接的需要同时把连接线也给渲染了
# TODO: 连接线渲染
2024-05-26 11:25:33 +08:00
part_sprite, part_box = self.part_render_init(
part_data, part_type, part_group, line_group, self.main_batch
)
2024-05-26 11:25:33 +08:00
part_box.opacity = 100
# 未连接的部件透明度降低
part_sprite.opacity = 100
2024-05-26 11:25:33 +08:00
self.part_sprites[part_data.id] = (part_sprite, part_box)
count += 2
if count >= draw_batch:
2023-07-02 20:16:21 +08:00
yield count
count = 0
# for connect in self.rust_ship.connections().get_raw_data():
# # 连接线
# # parent_part_data = cache[connect[2]][0][1]
# # child_part_data = cache[connect[3]][0][1]
# # color = (
# # random.randrange(100, 255),
# # random.randrange(0, 255),
# # random.randrange(0, 255),
# # 255,
# # )
# # self.part_line_list.append(
# # Line(
# # x=parent_part_data.x * 60,
# # y=parent_part_data.y * 60,
# # x2=child_part_data.x * 60,
# # y2=child_part_data.y * 60,
# # batch=self.main_batch,
# # group=connect_line_group,
# # width=1,
# # color=color,
# # )
# # )
# count += 1
# if count >= draw_batch * 3:
# count = 0
# yield count
2023-07-21 23:10:57 +08:00
2023-07-24 09:46:19 +08:00
self.status.draw_done = True
2023-02-07 12:02:37 +08:00
raise GeneratorExit
def render_ship(self):
2023-07-21 13:10:21 +08:00
"""
渲染船
"""
2023-07-24 09:46:19 +08:00
self.status.draw_done = False
2024-05-16 00:33:25 +08:00
logger.info(sr_tr().sr1.ship.ship.load().format(self.ship_name), tag="ship")
2023-02-07 12:02:37 +08:00
start_time = time.perf_counter_ns()
2024-05-26 11:25:33 +08:00
self.part_sprites = {}
self.connection_lines: List[Line] = []
2023-08-12 01:20:24 +08:00
self.group_camera.reset()
2023-02-07 12:02:37 +08:00
# 调用生成器 减少卡顿
2023-02-19 11:47:15 +08:00
try:
self.gen_draw = self.sprite_batch()
2023-07-24 09:46:19 +08:00
if not self.status.draw_done:
next(self.gen_draw)
2023-07-21 23:10:57 +08:00
except (GeneratorExit, StopIteration):
2023-07-24 09:46:19 +08:00
self.status.draw_done = True
self.status.draw_call = False
full_mass = 0
2023-04-30 00:48:42 +08:00
if DR_mod_runtime.use_DR_rust:
2023-07-21 23:10:57 +08:00
full_mass = self.rust_ship.mass
2023-12-03 16:54:07 +08:00
logger.info(
sr_tr()
.sr1.ship.ship.load_time()
2024-05-16 00:33:25 +08:00
.format((time.perf_counter_ns() - start_time) / 1000000000),
2024-05-22 23:58:14 +08:00
tag="ship",
2023-12-03 16:54:07 +08:00
)
logger.info(
sr_tr()
.sr1.ship.ship.info()
.format(
len(self.rust_ship.as_list()),
f"{full_mass}kg"
if DR_mod_runtime.use_DR_rust
else sr_tr().game.require_DR_rs(),
2024-05-16 00:33:25 +08:00
),
tag="ship",
2023-12-03 16:54:07 +08:00
)
2023-01-19 16:32:36 +08:00
2023-08-20 21:42:38 +08:00
def draw_batch(self, window: ClientWindow):
2023-07-24 09:46:19 +08:00
if self.status.draw_done:
2023-12-03 16:54:07 +08:00
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 + (self.window_pointer.height / 2) + 10,
0,
) # 0 for z
2023-08-12 01:20:24 +08:00
self.render_d_line.x2 = self.group_camera.view_x
self.render_d_line.y2 = self.group_camera.view_y
2023-08-12 23:32:43 +08:00
2024-05-23 21:31:47 +08:00
# 外面这一层 gl 是用来实现子窗口的
2023-08-12 23:32:43 +08:00
gl.glEnable(gl.GL_SCISSOR_TEST)
gl.glScissor(int(self.dx), int(self.dy), int(self.width), int(self.height))
2023-12-03 16:54:07 +08:00
gl.glViewport(
int(self.dx),
int(self.dy),
self.window_pointer.width,
self.window_pointer.height,
)
2023-08-12 00:49:46 +08:00
self.main_batch.draw() # use group camera, no need to with
2023-08-12 23:32:43 +08:00
gl.glViewport(0, 0, self.window_pointer.width, self.window_pointer.height)
gl.glScissor(0, 0, self.window_pointer.width, self.window_pointer.height)
gl.glDisable(gl.GL_SCISSOR_TEST)
2023-07-02 15:54:50 +08:00
2023-12-15 12:45:46 +08:00
def on_draw(self, dt: float, window): # TODO: wait for pyglet 2.1
2023-12-30 22:07:01 +08:00
# def on_draw(self, window: ClientWindow):
2023-07-24 09:46:19 +08:00
if self.status.draw_call:
2023-01-21 22:50:18 +08:00
self.render_ship()
2023-02-07 12:02:37 +08:00
2023-07-24 09:46:19 +08:00
if not self.status.draw_done:
2023-04-05 12:13:02 +08:00
try:
2023-02-07 12:02:37 +08:00
next(self.gen_draw)
2023-07-21 23:10:57 +08:00
except (GeneratorExit, StopIteration):
2023-07-24 09:46:19 +08:00
self.status.draw_done = True
2023-07-12 16:46:32 +08:00
self.logger.info(sr_tr().sr1.ship.ship.render.done())
2023-07-24 09:46:19 +08:00
except TypeError:
pass
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-08-20 21:42:38 +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-07-24 09:46:19 +08:00
if not self.status.draw_done:
2023-01-19 22:29:43 +08:00
return
2023-07-12 16:46:32 +08:00
self.render_d_line.x2 = width // 2
self.render_d_line.y2 = height // 2
2024-05-16 00:33:25 +08:00
self.width = width
self.height = height
2023-01-19 22:29:43 +08:00
2023-12-03 16:54:07 +08:00
def on_mouse_scroll(
self, x: int, y: int, scroll_x: int, scroll_y: int, window: ClientWindow
):
2024-05-30 22:23:24 +08:00
if self.status.focus and (
not self.show_ships_buttons
or (
not (
self.show_ships_buttons
and x >= self.ships_buttons_begin_x
and x <= self.ships_buttons_end_x
and y >= self.ships_buttons_begin_y
and y <= self.ships_buttons_end_y
)
)
):
2023-08-12 20:20:18 +08:00
mouse_dx = x - (self.width / 2) + self.dx
mouse_dy = y - (self.height / 2) + self.dy
2023-07-25 19:54:51 +08:00
# 鼠标缩放位置相对于屏幕中心的位置
2023-08-12 00:49:46 +08:00
mouse_dx_d = mouse_dx - self.group_camera.view_x
mouse_dy_d = mouse_dy - self.group_camera.view_y
2023-07-25 19:54:51 +08:00
# 鼠标相对偏移量的偏移量
if scroll_y == 0:
zoom_d = 1
else:
2023-12-03 16:54:07 +08:00
zoom_d = ((2**scroll_y) - 1) * 0.5 + 1
2023-07-25 19:54:51 +08:00
# 缩放的变换量
2023-08-12 00:49:46 +08:00
if not (self.group_camera.zoom == 10 and scroll_y > 0):
if self.group_camera.zoom * zoom_d >= 10:
zoom_d = 10 / self.group_camera.zoom
self.group_camera.zoom = 10
2023-07-25 19:54:51 +08:00
else:
2023-08-12 00:49:46 +08:00
self.group_camera.zoom *= zoom_d
2023-12-03 16:54:07 +08:00
mouse_dx_d *= 1 - zoom_d
mouse_dy_d *= 1 - zoom_d
2023-08-12 00:49:46 +08:00
self.group_camera.view_x += mouse_dx_d
self.group_camera.view_y += mouse_dy_d
2023-07-25 19:54:51 +08:00
elif self.status.moving:
# 如果是在移动整体渲染位置
size_x, size_y = self.size
size_x += round(scroll_y) * 10
size_y += round(scroll_y) * 10
if size_x < 10:
size_x = 10
if size_y < 10:
size_y = 10
self.size = size_x, size_y
2024-05-30 22:23:24 +08:00
elif (
self.show_ships_buttons
and x >= self.ships_buttons_begin_x
and x <= self.ships_buttons_end_x
and y >= self.ships_buttons_begin_y
and y <= self.ships_buttons_end_y
):
2024-05-30 19:18:44 +08:00
min_y = 9999999
max_y = 0
2024-05-30 11:18:51 +08:00
for ship_button in self.ships_buttons:
2024-05-30 19:18:44 +08:00
min_y = min(min_y, ship_button.y)
max_y = max(max_y, ship_button.y)
2024-05-30 20:19:29 +08:00
if max_y + scroll_y * 50 <= self.height - self.ships_buttons_h:
scroll_y = (self.height - self.ships_buttons_h - max_y) / 50
2024-05-30 22:23:24 +08:00
2024-05-30 20:19:29 +08:00
if min_y + scroll_y * 50 >= 0:
scroll_y = (0 - min_y) / 50
2024-05-30 22:23:24 +08:00
2024-05-30 19:18:44 +08:00
for ship_button in self.ships_buttons:
ship_button.y = ship_button.y + scroll_y * 50
2024-05-30 11:18:51 +08:00
2024-05-30 22:23:24 +08:00
self.ship_list_line.y = self.ship_list_line.y - scroll_y * 50 * (
self.ships_buttons_end_y - self.ships_buttons_begin_y
) / ((len(self.ships_buttons) + 1) * self.ships_buttons_h)
2023-08-20 21:42:38 +08:00
def on_command(self, command: CommandText, window: ClientWindow):
2023-12-03 16:54:07 +08:00
"""解析命令"""
self.logger.info(f"command: {command}")
if command.find("render"):
if command.find("reset"):
2023-08-12 00:49:46 +08:00
self.group_camera.reset()
2023-01-25 11:11:45 +08:00
else:
2023-07-24 09:46:19 +08:00
self.status.draw_call = True
2023-12-03 16:54:07 +08:00
print("应该渲染飞船的")
2023-07-24 09:46:19 +08:00
2023-12-03 16:54:07 +08:00
elif command.find("debug"):
if command.find("delta"):
2023-07-12 16:46:32 +08:00
self.render_d_line.visible = not self.render_d_line.visible
2023-07-24 09:46:19 +08:00
self.status.draw_mouse_d_pos = self.render_d_line.visible
2023-12-03 16:54:07 +08:00
self.logger.info(f"sr1 mouse {self.status.draw_mouse_d_pos}")
elif command.find("ship"):
2023-07-24 09:46:19 +08:00
if self.status.draw_done:
for index, sprite in self.part_sprites.items():
2023-06-18 01:45:00 +08:00
sprite.visible = not sprite.visible
2023-12-03 16:54:07 +08:00
elif command.find("get_buf"):
2023-02-07 18:42:41 +08:00
def screenshot(window):
2023-07-21 13:10:21 +08:00
"""
从窗口截图
:param window:
:return:
"""
2023-07-24 09:46:19 +08:00
from pyglet.gl import GLubyte, GL_RGBA, GL_UNSIGNED_BYTE, glReadPixels
2023-02-08 21:39:06 +08:00
import pyglet
2023-12-03 16:54:07 +08:00
2023-02-07 18:42:41 +08:00
format_str = "RGBA"
2023-06-27 01:06:09 +08:00
buf = (GLubyte * (len(format_str) * window.width * window.height))()
2023-12-03 16:54:07 +08:00
glReadPixels(
0, 0, window.width, window.height, GL_RGBA, GL_UNSIGNED_BYTE, buf
)
return pyglet.image.ImageData(
window.width, window.height, format_str, buf
)
2023-02-07 18:42:41 +08:00
image_data = screenshot(self.window_pointer)
2023-12-03 16:54:07 +08:00
image_data.save("test.png")
elif command.find("gen_img"):
2023-07-24 09:46:19 +08:00
if not self.status.draw_done:
return
if not DR_mod_runtime.use_DR_rust:
# 这个功能依赖于 DR rs (简称,我懒得在Python端实现)
return
img_box = self.rust_ship.img_pos
2023-07-23 11:03:22 +08:00
img_size = (img_box[2] - img_box[0], img_box[3] - img_box[1])
# 中心点是左上角坐标
img_center = (abs(img_box[0]), abs(img_box[3]))
try:
from PIL import Image
except ImportError:
traceback.print_exc()
2023-12-03 16:54:07 +08:00
print("PIL not found")
return
2023-12-03 16:54:07 +08:00
img = Image.new("RGBA", img_size)
2023-07-23 11:03:22 +08:00
part_data = self.rust_ship.as_dict()
for part, sprites in self.part_sprites.items():
2023-07-23 11:03:22 +08:00
for index, sprite in enumerate(sprites):
sprite_img = sprite.image
2023-07-24 09:46:19 +08:00
print(
2024-05-22 23:58:14 +08:00
f"sprite_img: {sprite_img} {part_data[part][index][1].x * 60} "
f"{part_data[part][index][1].y * 60}"
2023-12-03 16:54:07 +08:00
)
2023-07-23 11:03:22 +08:00
img_data = sprite_img.get_image_data()
fmt = img_data.format
2023-12-03 16:54:07 +08:00
if fmt != "RGB":
fmt = "RGBA"
2023-07-23 11:03:22 +08:00
pitch = -(img_data.width * len(fmt))
2023-12-03 16:54:07 +08:00
pil_image = Image.frombytes(
fmt,
(img_data.width, img_data.height),
img_data.get_data(fmt, pitch),
)
2023-07-23 11:03:22 +08:00
2023-12-03 16:54:07 +08:00
pil_image = pil_image.rotate(
-SR1Rotation.get_rotation(part_data[part][index][1].angle),
expand=True,
)
2023-07-23 11:03:22 +08:00
if part_data[part][index][1].flip_y:
pil_image.transpose(Image.FLIP_TOP_BOTTOM)
if part_data[part][index][1].flip_x:
pil_image.transpose(Image.FLIP_LEFT_RIGHT)
2023-12-03 16:54:07 +08:00
img.paste(
pil_image,
(
int(part_data[part][index][1].x * 60 + img_center[0]),
int(-part_data[part][index][1].y * 60 + img_center[1]),
),
)
2023-07-24 09:46:19 +08:00
2023-12-03 16:54:07 +08:00
img.save(f"test{time.time()}.png", "PNG")
2023-01-21 11:48:07 +08:00
2023-12-24 15:51:44 +08:00
elif command.find("save"):
print("应该保存飞船的")
# if command.find("save"):
if not self.status.draw_done:
2023-12-30 22:07:01 +08:00
logger.warn("not draw done", tag="save ship")
2023-12-24 15:51:44 +08:00
return
if not DR_mod_runtime.use_DR_rust:
return
2023-12-30 22:07:01 +08:00
print("saving")
2023-12-24 15:51:44 +08:00
logger.info(sr_tr().sr1.ship.save.start().format(self.rust_ship))
self.rust_ship.save("./test-save.xml")
2023-12-03 16:54:07 +08:00
def on_mouse_drag(
self,
x: int,
y: int,
dx: int,
dy: int,
buttons: int,
modifiers: int,
window: ClientWindow,
):
2023-07-25 19:54:51 +08:00
if self.status.focus:
2023-08-12 00:28:31 +08:00
self.group_camera.view_x += dx
self.group_camera.view_y += dy
2023-07-25 19:54:51 +08:00
self.status.update_call = True
elif self.status.moving:
# 如果是在移动整体渲染位置
self.dx += dx
self.dy += dy
2024-05-30 22:23:24 +08:00
2024-05-30 10:18:55 +08:00
def scan_all_ships_list(self, ships_path: str):
# 当前目录
base_dir = ships_path
# 获取当前目录下的所有文件
files = [os.path.join(base_dir, file) for file in os.listdir(base_dir)]
return files
2023-01-19 22:29:43 +08:00
def begin_ship_render_from_path(self, ship_path: str):
if Path(ship_path).is_dir():
for path in Path(ship_path).glob("*.xml"):
try:
self.load_xml(str(path))
except ValueError:
traceback.print_exc()
if self.load_xml(ship_path):
self.render_ship()
2023-08-20 21:42:38 +08:00
def on_file_drop(self, x: int, y: int, paths: List[str], window: ClientWindow):
2023-07-24 09:46:19 +08:00
if len(paths) > 1:
for path in paths:
try:
self.load_xml(path)
except Exception:
traceback.print_exc()
else:
self.begin_ship_render_from_path(paths[0])
2023-07-24 09:46:19 +08:00
# for path in paths:
# if self.load_xml(path): # 加载成功一个就停下
# break
# self.render_ship()
2023-01-23 13:54:05 +08:00
2023-08-12 20:20:18 +08:00
@property
def view(self):
return self.window_pointer.view
@view.setter
def view(self, value: Mat4):
self.window_pointer.view = value
2024-05-30 22:23:24 +08:00
2024-05-27 18:48:34 +08:00
class PressEnterGameButton(PressTextButton):
def __init__(
self,
window: ClientWindow,
x: int,
y: int,
width: int,
height: int,
text: str,
batch: Optional[Batch] = None,
group: Optional[Group] = None,
theme: Optional[ButtonThemeOptions] = None,
draw_theme: Optional[BaseButtonTheme] = None,
dict_theme: Optional[dict] = None,
):
super().__init__(
x, y, width, height, text, batch, group, theme, draw_theme, dict_theme
)
self.window = window
def on_mouse_release(self, x, y, buttons, modifiers):
if self.pressed and (x, y) in self:
if self.draw_theme:
self.draw_theme.on_disable(self)
else:
self.back_rec.color = self.touched_color
self.pressed = False
logger.info("进入游戏")
2024-05-30 22:23:24 +08:00
2024-05-27 19:38:26 +08:00
class PressSelectShipButton(PressTextButton):
path_var = "./assets/builtin/dock1.xml"
2024-05-30 22:23:24 +08:00
2024-05-27 19:38:26 +08:00
def __init__(
self,
window: ClientWindow,
parent_window,
2024-05-27 19:38:26 +08:00
x: int,
y: int,
width: int,
height: int,
text: str,
batch: Optional[Batch] = None,
group: Optional[Group] = None,
theme: Optional[ButtonThemeOptions] = None,
draw_theme: Optional[BaseButtonTheme] = None,
dict_theme: Optional[dict] = None,
):
super().__init__(
x, y, width, height, text, batch, group, theme, draw_theme, dict_theme
)
self.window = window
self.parent_window = parent_window
2024-05-27 19:38:26 +08:00
def on_mouse_release(self, x, y, buttons, modifiers):
if self.pressed and (x, y) in self:
if self.draw_theme:
self.draw_theme.on_disable(self)
else:
self.back_rec.color = self.touched_color
self.pressed = False
2024-05-27 18:48:34 +08:00
2024-05-30 22:23:24 +08:00
root = Tk() # 创建一个Tkinter.Tk()实例
2024-05-27 19:38:26 +08:00
root.withdraw() # 将Tkinter.Tk()实例隐藏
2024-05-30 22:23:24 +08:00
file_name = filedialog.askopenfilename(
title="选择一个飞船存档",
initialdir="./", # 打开当前程序工作目录
)
self.path_var = file_name
self.parent_window.begin_ship_render_from_path(file_name)
2024-05-30 22:23:24 +08:00
logger.info("加载飞船from " + self.path_var)
def get_ship_path(self):
2024-05-30 22:23:24 +08:00
logger.info("加载飞船from " + self.path_var)
return self.path_var
2024-05-30 10:36:41 +08:00
2024-05-30 22:23:24 +08:00
class PressOpenShipButton(PressTextButton):
2024-05-30 10:36:41 +08:00
def __init__(
self,
window: ClientWindow,
ship_path,
parent_window,
x: int,
y: int,
width: int,
height: int,
text: str,
batch: Optional[Batch] = None,
group: Optional[Group] = None,
theme: Optional[ButtonThemeOptions] = None,
draw_theme: Optional[BaseButtonTheme] = None,
dict_theme: Optional[dict] = None,
):
super().__init__(
x, y, width, height, text, batch, group, theme, draw_theme, dict_theme
)
self.window = window
self.parent_window = parent_window
self.ship_path = ship_path
2024-05-30 22:23:24 +08:00
2024-05-30 11:18:51 +08:00
def set_y(self, y):
self.y = y
2024-05-30 22:23:24 +08:00
2024-05-30 11:18:51 +08:00
def get_y(self):
2024-05-30 22:23:24 +08:00
return self.y
2024-05-30 10:36:41 +08:00
def on_mouse_release(self, x, y, buttons, modifiers):
if self.pressed and (x, y) in self:
if self.draw_theme:
self.draw_theme.on_disable(self)
else:
self.back_rec.color = self.touched_color
self.pressed = False
self.parent_window.begin_ship_render_from_path(self.ship_path)
2024-05-30 22:23:24 +08:00
logger.info("加载飞船from " + self.ship_path)