Compare commits

...

18 Commits

Author SHA1 Message Date
0524b52084
add init 2024-06-10 19:39:14 +08:00
65d0c77e01
add zed config file 2024-06-10 00:04:42 +08:00
1264e76f0e
fix some button 2024-06-09 20:21:27 +08:00
441c1c072c
Fix oldself not pass to reloaded mod error 2024-06-09 20:14:16 +08:00
3495799a89
Fix some warning 2024-06-09 20:06:08 +08:00
309eee76e3
0.3.7 的 dr game deving 2024-06-09 17:27:43 +08:00
f462821909
一些小东西 2024-06-09 17:26:48 +08:00
aa6ab62fc8
rework on shipeditor 2024-06-09 17:14:10 +08:00
bf8286aafa
add assert and if to button 2024-06-09 17:13:55 +08:00
d3d8184255
fmt ships 2024-06-09 17:11:34 +08:00
2bc21266ab add game_layout 2024-06-07 14:34:16 +08:00
8c37cf694f 按钮的list_back也会跟着按钮跑了 2024-06-06 20:09:56 +08:00
e5ff3d13b2 Revert "not this"
This reverts commit 5eb3ffb5a4.
2024-06-06 19:57:57 +08:00
5eb3ffb5a4 not this 2024-06-06 19:57:10 +08:00
0a94b7c11d 2
2
2024-06-06 19:49:53 +08:00
228102d0c4 1
1
2024-06-06 19:46:51 +08:00
e983fc2bbc
清理代码.ing 2024-06-05 07:49:51 +08:00
d6179422d5
费劲.png 2024-06-05 07:42:57 +08:00
12 changed files with 1756 additions and 387 deletions

13
.zed/settings.json Normal file
View File

@ -0,0 +1,13 @@
// Folder-specific settings
//
// For a full list of overridable settings, and general information on folder-specific settings,
// see the documentation: https://zed.dev/docs/configuring-zed#folder-specific-settings
{
"lsp": {
"rust-analyzer": {
"initialization_options": {
"linkedProjects": ["./mods/dr_game/Difficult_Rocket_rs/src/Cargo.toml"]
}
}
}
}

View File

@ -4,7 +4,7 @@
# All rights reserved
# -------------------------------
from typing import Optional, Tuple
from typing import Optional, Tuple, Type
# from libs import pyglet
import pyglet
@ -247,6 +247,8 @@ class MinecraftWikiButtonTheme(BaseButtonTheme):
def on_update(self, button) -> None:
super().on_update(button)
self.list_back.x = self.x
self.list_back.y = self.y
if self.enable and self.drag_list:
button.text_label.y = (
self.y
@ -254,7 +256,7 @@ class MinecraftWikiButtonTheme(BaseButtonTheme):
+ (button.font_height * 0.2)
+ self.list_pad // 2
)
self.back_ground.x = self.x + (self.pad * 2)
self.back_ground.y = self.y + (self.pad * 2)
self.back_ground.position = self.x + (self.pad * 2), self.y + (self.pad * 2)
@ -267,8 +269,6 @@ class MinecraftWikiButtonTheme(BaseButtonTheme):
self.cover_back2.y = self.y + self.pad
self.cover_back2.position = self.x + self.pad, self.y + self.pad
self.cover_back2.height = self.height - (self.pad * 3)
self.list_back.x = self.x
self.list_back.y = self.y
else:
button.text_label.y = (
self.y
@ -326,7 +326,7 @@ class PressTextButton(widgets.WidgetBase):
batch: Optional[Batch] = None,
group: Optional[Group] = None,
theme: Optional[ButtonThemeOptions] = None,
draw_theme: Optional[type(BaseButtonTheme)] = None,
draw_theme: Optional[Type[BaseButtonTheme]] = None,
dict_theme: Optional[dict] = None,
):
super().__init__(x, y, width, height)
@ -384,12 +384,11 @@ class PressTextButton(widgets.WidgetBase):
height=self._height,
batch=self.main_batch,
group=self.back_ground_group,
theme=self.dict_theme,
)
self.draw_theme.on_update(self)
self.value = text # 重新分配一下高度和宽度的位置
@property
def x(self) -> int:
return self._x
@ -398,8 +397,10 @@ class PressTextButton(widgets.WidgetBase):
def x(self, value: int) -> None:
self._x = value
self.text_label.x = value
self.draw_theme.x = value
self.draw_theme.on_update(self)
if self.draw_theme:
assert isinstance(self.draw_theme, BaseButtonTheme)
self.draw_theme.x = value
self.draw_theme.on_update(self)
@property
def y(self) -> int:
@ -409,8 +410,10 @@ class PressTextButton(widgets.WidgetBase):
def y(self, value: int) -> None:
self._y = value
self.text_label.y = value + 4
self.draw_theme.y = value
self.draw_theme.on_update(self)
if self.draw_theme:
assert isinstance(self.draw_theme, BaseButtonTheme)
self.draw_theme.y = value
self.draw_theme.on_update(self)
@property
def value(self):
@ -457,6 +460,7 @@ class PressTextButton(widgets.WidgetBase):
else:
self.pressed = False
if self.draw_theme:
assert isinstance(self.draw_theme, BaseButtonTheme)
self.draw_theme.on_disable(self)
else:
self.back_rec.color = self.untouched_color
@ -465,6 +469,7 @@ class PressTextButton(widgets.WidgetBase):
if (x, y) in self:
if buttons == mouse.LEFT:
if self.draw_theme:
assert isinstance(self.draw_theme, BaseButtonTheme)
self.draw_theme.on_enable(x, y, self)
else:
self.back_rec.color = self.hit_color
@ -474,19 +479,21 @@ class PressTextButton(widgets.WidgetBase):
else:
self.pressed = False
if self.draw_theme:
assert isinstance(self.draw_theme, BaseButtonTheme)
self.draw_theme.on_disable(self)
else:
self.back_rec.color = self.untouched_color
self.dispatch_event("on_release", x, y)
return False
def on_mouse_release(self, x, y, buttons, modifiers):
if self.pressed and (x, y) in self:
if self.draw_theme:
assert isinstance(self.draw_theme, BaseButtonTheme)
self.draw_theme.on_disable(self)
else:
self.back_rec.color = self.touched_color
self.pressed = False
self.dispatch_event("on_release", self, x, y)
def _update_position(self):
self.text_label.position = self._x, self._y, 0

View File

@ -9,7 +9,7 @@ import time
import traceback
import importlib
from pathlib import Path
from typing import List, Dict, Optional, TypeVar
from typing import List, Dict, Optional, TypeVar, Type, TYPE_CHECKING
from Difficult_Rocket.mod.api import ModInfo
from Difficult_Rocket.utils.translate import tr
@ -17,7 +17,10 @@ from Difficult_Rocket.api.types import Options
from lib_not_dr.loggers import config
Game = TypeVar("Game")
if TYPE_CHECKING:
from Difficult_Rocket.main import Game
else:
Game = TypeVar("Game")
logger = config.get_logger_from_old("mod_manager", "client")
ONE_FILE_SUFFIX = (".py", ".pyc", ".pyd")
@ -37,8 +40,9 @@ class ModManager(Options):
find_mod_paths: Dict[str, Path] = {}
loaded_mod_modules: Dict[str, ModInfo] = {}
def init(self, **kwargs) -> bool:
def init(self, **kwargs) -> None:
self.logger = config.get_logger_from_old("mod_manager", "client")
return None
def get_mod_module(self, mod_name: str) -> Optional[ModInfo]:
"""
@ -70,7 +74,7 @@ class ModManager(Options):
.format(mod, event_name, e, traceback.format_exc())
)
def load_mod(self, mod_path: Path) -> Optional[type(ModInfo)]:
def load_mod(self, mod_path: Path) -> Optional[Type[ModInfo]]:
"""
加载指定路径下的 mod
:param mod_path: mod 的路径
@ -100,7 +104,7 @@ class ModManager(Options):
tr().mod.load.faild.no_mod_class().format(mod_path), tag="load"
)
return None
mod_class: type(ModInfo) = loading_mod.mod_class # 获取 mod 类
mod_class: Type[ModInfo] = loading_mod.mod_class # 获取 mod 类
if mod_class.mod_id not in self.find_mod_paths:
self.find_mod_paths[mod_class.mod_id] = mod_path
return mod_class
@ -147,7 +151,7 @@ class ModManager(Options):
self,
extra_path: Optional[List[Path]] = None,
extra_mod_path: Optional[List[Path]] = None,
) -> List[type(ModInfo)]:
) -> List[Type[ModInfo]]:
"""
加载所有 mod (可提供额外的 mod 路径)
:param extra_path: 额外的 mod 路径
@ -175,7 +179,7 @@ class ModManager(Options):
)
return mods
def init_mods(self, mods: List[type(ModInfo)]):
def init_mods(self, mods: List[Type[ModInfo]]):
"""
加载 mod
:param mods: 要加载的 mod ModInfo
@ -236,7 +240,7 @@ class ModManager(Options):
unload = self.unload_mod(mod_id, game)
if unload is None:
return
mod_class: Optional[ModInfo] = None
mod_class: Optional[Type[ModInfo]] = None
if unload.mod_id not in self.find_mod_paths:
self.logger.warn(
tr().mod.reload.faild.not_find().format(unload.mod_id), tag="reload"
@ -252,5 +256,7 @@ class ModManager(Options):
if mod_class is not None:
self.init_mods([mod_class])
if mod_id in self.loaded_mod_modules and mod_class is not None:
self.loaded_mod_modules[mod_id].on_load(game=game, old_self=mod_class)
self.loaded_mod_modules[mod_id].on_load(
game=game, old_self=self.loaded_mod_modules[mod_id]
)
self.logger.info(tr().mod.reload.success().format(mod_id), tag="reload")

View File

@ -12,7 +12,7 @@ package_path = "Difficult_Rocket_rs"
setup(
name="Difficult_Rocket_rs",
version="0.3.1",
version="0.3.5",
author="shenjackyuanjie",
author_email="3695888@qq.com",
rust_extensions=[

View File

@ -728,7 +728,7 @@ impl SR1Ship {
};
}
fn write_parts(parts: &Vec<SR1PartData>, writer: &mut Writer<Cursor<Vec<u8>>>, save_status: &SaveStatus) {
fn write_parts(parts: &[SR1PartData], writer: &mut Writer<Cursor<Vec<u8>>>, save_status: &SaveStatus) {
writer.write_event(Event::Start(BytesStart::new("Parts"))).unwrap();
for part in parts.iter() {
let mut part_attr = BytesStart::new("Part");

View File

@ -77,7 +77,7 @@ DR_mod_runtime = _DR_mod_runtime()
class DR_mod(ModInfo): # NOQA
mod_id = "difficult_rocket_mod"
name = "Difficult Rocket mod"
version = Version("0.3.6")
version = Version("0.3.7")
writer = "shenjackyuanjie"
link = "shenjack.top"
@ -100,9 +100,9 @@ class DR_mod(ModInfo): # NOQA
game.console_class = RustConsole # 替换掉原来的 console 类
if old_self:
from .sr1_ship import SR1ShipRender
from .sr1_ship import SR1ShipEditor
game.client.window.add_sub_screen("SR1_ship", SR1ShipRender)
game.client.window.add_sub_screen("SR1_ship", SR1ShipEditor)
else:
self.config.flush_option()
logger.info("on_load")

View File

@ -1,6 +1,8 @@
from . import DR_mod_runtime
from Difficult_Rocket.main import Console
from typing import Optional
if DR_mod_runtime.use_DR_rust:
from .Difficult_Rocket_rs import Console_rs
@ -20,7 +22,7 @@ class RustConsole(Console):
def init(self, **kwargs) -> None:
self.console = Console_rs()
def get_command(self) -> str:
def get_command(self) -> Optional[str]:
return self.console.get_command()
def new_command(self) -> None:

View File

@ -0,0 +1,37 @@
# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
from typing import Optional, Tuple
from pyglet.graphics import Batch, Group
from Difficult_Rocket.client import ClientWindow
from Difficult_Rocket.api.screen import BaseScreen
# from Difficult_Rocket.main import Game
from Difficult_Rocket.gui.widget.button import (
PressTextButton,
MinecraftWikiButtonTheme,
)
from lib_not_dr import loggers
# from . import DR_mod_runtime
logger = loggers.config.get_logger_from_old("client.dr_game_layout", "client")
class GameLayout(BaseScreen):
"""
DR game 菜单
"""
name = "DR_game_layout"
def __init__(self, main_window: ClientWindow):
super().__init__(main_window)
self.main_batch = Batch()
self.main_group = Group(parent=main_window.main_group, order=1)
def on_draw(self, dt: float, window: ClientWindow):
self.main_batch.draw()

View File

@ -9,12 +9,10 @@ from pyglet.graphics import Batch, Group
from Difficult_Rocket.client import ClientWindow
from Difficult_Rocket.api.screen import BaseScreen
from Difficult_Rocket.main import Game
# from Difficult_Rocket.main import Game
from Difficult_Rocket.gui.widget.button import (
PressTextButton,
MinecraftWikiButtonTheme,
ButtonThemeOptions,
BaseButtonTheme,
)
from lib_not_dr import loggers
@ -93,8 +91,7 @@ class Menu(BaseScreen):
# batch=self.main_batch,
# group=self.main_group,
# )
self.enter_ship_editor_button = PressEnterShipEditorButton(
window=main_window,
self.enter_ship_editor_button = PressTextButton(
x=100,
y=100,
width=150,
@ -102,8 +99,17 @@ class Menu(BaseScreen):
text="进入编辑器",
batch=self.main_batch,
group=self.main_group,
draw_theme=MinecraftWikiButtonTheme
draw_theme=MinecraftWikiButtonTheme,
dict_theme={"pop_out": True}
)
def on_release(button: PressTextButton, x, y):
from .sr1_ship import SR1ShipEditor
main_window.remove_sub_screen("DR_game_menu")
main_window.add_sub_screen("SR1_ship", SR1ShipEditor)
logger.info("added SR1_ship screen", tag="dr_game")
self.enter_ship_editor_button.set_handler("on_release", on_release)
# main_window.push_handlers(self.wiki_button1)
# main_window.push_handlers(self.wiki_button2)
# main_window.push_handlers(self.wiki_button3)
@ -113,39 +119,3 @@ class Menu(BaseScreen):
def on_draw(self, dt: float, window: ClientWindow):
self.main_batch.draw()
class PressEnterShipEditorButton(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
from .sr1_ship import SR1ShipRender
self.window.remove_sub_screen("DR_game_menu")
self.window.add_sub_screen("SR1_ship", SR1ShipRender)
logger.info("added SR1_ship screen", tag="dr_game")

View File

@ -19,6 +19,7 @@ from pyglet.text import Label
from pyglet.sprite import Sprite
from pyglet.graphics import Batch, Group
from pyglet.shapes import Line, Box
# from pyglet.window import mouse
from . import DR_mod_runtime
from .types import SR1Textures, SR1Rotation
@ -75,11 +76,25 @@ class SR1ShipRenderStatus(Options): # NOQA
draw_mouse_d_pos: bool = False
class SR1ShipRender(BaseScreen):
"""用于渲染 sr1 船的类"""
class SR1ShipSelecter(BaseScreen):
"""
SR1 飞船选择器
考虑到 sss 写的那一大堆东西实在有点离谱
所以单独拿出来写个screen
我估计以后还得有一堆类似重构(瘫倒)
"""
name = "DR_game_sr1_ship_selecter"
name = "DR_game_sr1_ship_render"
ships_buttons = []
def __init__(self, main_window: ClientWindow):
super().__init__(main_window)
self.main_batch = Batch()
self.main_group = Group()
class SR1ShipEditor(BaseScreen):
"""SR1 飞船编辑器"""
name = "DR_game_sr1_ship_editor"
def __init__(self, main_window: ClientWindow):
super().__init__(main_window)
@ -95,6 +110,7 @@ class SR1ShipRender(BaseScreen):
self.height = main_window.height
self.main_batch = Batch()
self.buttons_batch = Batch()
self.ships_buttons_batch = Batch()
self.group_camera = CenterGroupCamera(
window=main_window,
@ -168,91 +184,106 @@ class SR1ShipRender(BaseScreen):
self.buttons_group = Group(100, parent=main_window.main_group)
self.ships_buttons_group = Group(100, parent=main_window.main_group)
self.enter_game_button = PressEnterGameButton(
window=main_window,
x=500,
y=100,
width=150,
height=30,
text="进入游戏",
batch=self.main_batch,
group=self.buttons_group,
draw_theme=MinecraftWikiButtonTheme,
)
# self.enter_game_button = PressEnterGameButton(
# window=main_window,
# parent_window=main_window,
# x=500,
# y=100,
# width=150,
# height=30,
# text="进入游戏",
# batch=self.buttons_batch,
# group=self.buttons_group,
# draw_theme=MinecraftWikiButtonTheme,
# )
self.select_ship_button = PressSelectShipButton(
window=main_window,
parent_window=self,
x=100,
y=100,
width=150,
height=30,
text="加载飞船",
batch=self.main_batch,
group=self.buttons_group,
draw_theme=MinecraftWikiButtonTheme,
)
# self.select_ship_button = PressSelectShipButton(
# window=main_window,
# parent_window=self,
# x=100,
# y=100,
# width=150,
# height=30,
# text="加载飞船",
# batch=self.buttons_batch,
# group=self.buttons_group,
# draw_theme=MinecraftWikiButtonTheme,
# )
main_window.push_handlers(self.enter_game_button)
main_window.push_handlers(self.select_ship_button)
# main_window.push_handlers(self.enter_game_button)
# main_window.push_handlers(self.select_ship_button)
# 扫描所有飞船
self.show_ships_buttons = False
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 - self.ships_buttons_h * 5
# self.show_ships_buttons = False
# 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 - self.ships_buttons_h * 1
ships_path = "./ships/"
ships_files = self.scan_all_ships_list(ships_path)
# self.control_ships_list_button = PressControlShipsListButton(
# window=main_window,
# parent_window=self,
# x=self.ships_buttons_begin_x,
# y=self.ships_buttons_end_y,
# width=self.ships_buttons_w,
# height=self.ships_buttons_h,
# text="飞船列表",
# batch=self.buttons_batch,
# group=self.buttons_group,
# draw_theme=MinecraftWikiButtonTheme,
# )
# main_window.push_handlers(self.control_ships_list_button)
for i in range(len(ships_files)):
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.ships_buttons_batch,
group=self.ships_buttons_group,
draw_theme=MinecraftWikiButtonTheme,
)
)
# ships_path = "./ships/"
# ships_files = self.scan_all_ships_list(ships_path)
main_window.push_handlers(self.ships_buttons[-1])
# for i in range(len(ships_files)):
# 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 + 1) * self.ships_buttons_h,
# width=self.ships_buttons_w,
# height=self.ships_buttons_h,
# text=ships_files[i][8:],
# batch=self.ships_buttons_batch,
# group=self.ships_buttons_group,
# draw_theme=MinecraftWikiButtonTheme,
# )
# )
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.ships_buttons_batch,
group=self.ships_buttons_group,
)
# main_window.push_handlers(self.ships_buttons[-1])
self.ship_list_line = Line(
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,
width=20,
color=(200, 200, 200, 255),
batch=self.ships_buttons_batch,
group=self.ships_buttons_group,
)
# 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.ships_buttons_batch,
# group=self.ships_buttons_group,
# )
# self.ship_list_line = Line(
# 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,
# width=20,
# color=(200, 200, 200, 255),
# batch=self.ships_buttons_batch,
# group=self.ships_buttons_group,
# )
# self.ships_buttons_group.visible = False
self.show_ships_buttons = True
# self.show_ships_buttons = True
@property
def size(self) -> Tuple[int, int]:
@ -305,9 +336,9 @@ class SR1ShipRender(BaseScreen):
texture = self.textures.get_texture(part_type.sprite)
part_sprite = Sprite(
img=texture,
x=part_data.x * 60,
y=part_data.y * 60,
z=randomer.random(),
x=int(part_data.x * 60),
y=int(part_data.y * 60),
z=randomer.randint(-100, 100),
batch=batch,
group=part_group,
)
@ -368,15 +399,18 @@ class SR1ShipRender(BaseScreen):
count = 0
# 渲染未连接的部件
for (part_groups, part_connections) in self.rust_ship.disconnected_parts():
self.logger.info(part_groups[0], tag="sprite_batch")
for (part_type, part_data) in part_groups[0]:
for part_groups, part_connections in self.rust_ship.disconnected_parts():
for part_type, part_data in part_groups:
# 出于一些魔法原因, 这玩意居然能跑, part_groups 不需要加 [0]
# 未连接的需要同时把连接线也给渲染了
# TODO: 连接线渲染
part_sprite, part_box = self.part_render_init(
part_data, part_type, part_group, line_group, self.main_batch
)
part_box.opacity = 100
part_box._thickness = 2
part_box._update_vertices()
# ignore, pyglet 没写这个的 @property, 等我 issue + pr
# 未连接的部件透明度降低
part_sprite.opacity = 100
self.part_sprites[part_data.id] = (part_sprite, part_box)
@ -477,8 +511,9 @@ class SR1ShipRender(BaseScreen):
self.window_pointer.height,
)
self.main_batch.draw() # use group camera, no need to with
if self.show_ships_buttons:
self.ships_buttons_batch.draw()
# if self.show_ships_buttons:
# self.ships_buttons_batch.draw()
self.buttons_batch.draw() # use group camera, no need to with
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)
@ -490,6 +525,8 @@ class SR1ShipRender(BaseScreen):
if not self.status.draw_done:
try:
assert isinstance(self.gen_draw, Generator), \
f"self.gen_graw is not a Generator, but a {type(self.gen_draw)}"
next(self.gen_draw)
except (GeneratorExit, StopIteration):
self.status.draw_done = True
@ -511,18 +548,19 @@ class SR1ShipRender(BaseScreen):
def on_mouse_scroll(
self, x: int, y: int, scroll_x: int, scroll_y: int, window: ClientWindow
):
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
)
)
):
# 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
# )
# )
# ):
if self.status.focus:
mouse_dx = x - (self.width / 2) + self.dx
mouse_dy = y - (self.height / 2) + self.dy
# 鼠标缩放位置相对于屏幕中心的位置
@ -542,8 +580,8 @@ class SR1ShipRender(BaseScreen):
self.group_camera.zoom *= zoom_d
mouse_dx_d *= 1 - zoom_d
mouse_dy_d *= 1 - zoom_d
self.group_camera.view_x += mouse_dx_d
self.group_camera.view_y += mouse_dy_d
self.group_camera.view_x += int(mouse_dx_d)
self.group_camera.view_y += int(mouse_dy_d)
elif self.status.moving:
# 如果是在移动整体渲染位置
size_x, size_y = self.size
@ -554,31 +592,37 @@ class SR1ShipRender(BaseScreen):
if size_y < 10:
size_y = 10
self.size = size_x, size_y
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
):
min_y = 9999999
max_y = 0
for ship_button in self.ships_buttons:
min_y = min(min_y, ship_button.y)
max_y = max(max_y, ship_button.y)
# 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
# ):
# min_y = 9999999
# max_y = 0
# for ship_button in self.ships_buttons:
# min_y = min(min_y, ship_button.y)
# max_y = max(max_y, ship_button.y)
if max_y + scroll_y * 50 <= self.ships_buttons_end_y - self.ships_buttons_h:
scroll_y = (self.ships_buttons_end_y - self.ships_buttons_h - max_y) / 50
# if max_y + scroll_y * 50 <= self.ships_buttons_end_y - self.ships_buttons_h:
# scroll_y = (self.ships_buttons_end_y - self.ships_buttons_h - max_y) / 50
if min_y + scroll_y * 50 >= 0:
scroll_y = (0 - min_y) / 50
# if min_y + scroll_y * 50 >= 0:
# scroll_y = (0 - min_y) / 50
for ship_button in self.ships_buttons:
ship_button.y = ship_button.y + scroll_y * 50
# for ship_button in self.ships_buttons:
# ship_button.y = ship_button.y + scroll_y * 50
# """
# if ship_button.y >= self.ships_buttons_begin_y and ship_button.y <= self.ships_buttons_end_y - self.ships_buttons_h:
# ship_button.x = self.ships_buttons_begin_x
# else:
# ship_button.x = self.width + self.ships_buttons_w
# """
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)
# 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)
def on_command(self, command: CommandText, window: ClientWindow):
"""解析命令"""
@ -597,7 +641,7 @@ class SR1ShipRender(BaseScreen):
self.logger.info(f"sr1 mouse {self.status.draw_mouse_d_pos}")
elif command.find("ship"):
if self.status.draw_done:
for index, sprite in self.part_sprites.items():
for index, sprite in self.part_sprites.values():
sprite.visible = not sprite.visible
elif command.find("get_buf"):
@ -622,61 +666,61 @@ class SR1ShipRender(BaseScreen):
image_data = screenshot(self.window_pointer)
image_data.save("test.png")
elif command.find("gen_img"):
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
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()
print("PIL not found")
return
img = Image.new("RGBA", img_size)
part_data = self.rust_ship.as_dict()
for part, sprites in self.part_sprites.items():
for index, sprite in enumerate(sprites):
sprite_img = sprite.image
print(
f"sprite_img: {sprite_img} {part_data[part][index][1].x * 60} "
f"{part_data[part][index][1].y * 60}"
)
img_data = sprite_img.get_image_data()
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),
)
# elif command.find("gen_img"):
# 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
# 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()
# print("PIL not found")
# return
# img = Image.new("RGBA", img_size)
# part_data = self.rust_ship.as_dict()
# for sprites, box in self.part_sprites.values():
# for index, sprite in enumerate(sprites):
# sprite_img = sprite.image
# print(
# f"sprite_img: {sprite_img} {part_data[part][index][1].x * 60} "
# f"{part_data[part][index][1].y * 60}"
# )
# img_data = sprite_img.get_image_data()
# 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),
# )
pil_image = pil_image.rotate(
-SR1Rotation.get_rotation(part_data[part][index][1].angle),
expand=True,
)
# pil_image = pil_image.rotate(
# -SR1Rotation.get_rotation(part_data[part][index][1].angle),
# expand=True,
# )
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)
# if part_data[part][index][1].flip_y:
# pil_image.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
# if part_data[part][index][1].flip_x:
# pil_image.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
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]),
),
)
# 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]),
# ),
# )
img.save(f"test{time.time()}.png", "PNG")
# img.save(f"test{time.time()}.png", "PNG")
elif command.find("save"):
print("应该保存飞船的")
@ -709,34 +753,30 @@ class SR1ShipRender(BaseScreen):
self.dx += dx
self.dy += dy
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
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()
# 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()
def on_file_drop(self, x: int, y: int, paths: List[str], window: ClientWindow):
if len(paths) > 1:
for path in paths:
try:
self.load_xml(path)
except Exception:
traceback.print_exc()
if len(paths) == 1:
# only file/path
...
else:
self.begin_ship_render_from_path(paths[0])
...
# 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])
# for path in paths:
# if self.load_xml(path): # 加载成功一个就停下
# break
@ -751,129 +791,166 @@ class SR1ShipRender(BaseScreen):
self.window_pointer.view = value
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
# class PressEnterGameButton(PressTextButton):
# def __init__(
# self,
# window: ClientWindow,
# 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
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
# 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("进入游戏")
# from .game_layout import GameLayout
# self.parent_window.remove_sub_screen("SR1_ship")
# self.parent_window.add_sub_screen("Dr_game_layout", GameLayout)
# logger.info("added Dr_game_layout screen", tag="dr_game")
# logger.info("进入游戏")
# class PressSelectShipButton(PressTextButton):
# path_var = "./assets/builtin/dock1.xml"
# def __init__(
# self,
# window: ClientWindow,
# 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
# 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
# root = Tk() # 创建一个Tkinter.Tk()实例
# root.withdraw() # 将Tkinter.Tk()实例隐藏
# file_name = filedialog.askopenfilename(
# title="选择一个飞船存档",
# initialdir="./", # 打开当前程序工作目录
# )
# self.path_var = file_name
# self.parent_window.begin_ship_render_from_path(file_name)
# logger.info("加载飞船from " + self.path_var)
# def get_ship_path(self):
# logger.info("加载飞船from " + self.path_var)
# return self.path_var
# class PressControlShipsListButton(PressTextButton):
# def __init__(
# self,
# window: ClientWindow,
# 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
# 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.show_ships_buttons = not(self.parent_window.show_ships_buttons)
# logger.info("显示飞船列表")
# class PressOpenShipButton(PressTextButton):
# 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
class PressSelectShipButton(PressTextButton):
path_var = "./assets/builtin/dock1.xml"
# def set_y(self, y):
# self.y = y
def __init__(
self,
window: ClientWindow,
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
# def get_y(self):
# return self.y
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
# def on_mouse_release(self, x, y, buttons, modifiers):
# if (
# self.parent_window.show_ships_buttons
# and x >= self.parent_window.ships_buttons_begin_x
# and x <= self.parent_window.ships_buttons_end_x
# and y >= self.parent_window.ships_buttons_begin_y
# and y <= self.parent_window.ships_buttons_end_y
# ):
# 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
root = Tk() # 创建一个Tkinter.Tk()实例
root.withdraw() # 将Tkinter.Tk()实例隐藏
file_name = filedialog.askopenfilename(
title="选择一个飞船存档",
initialdir="./", # 打开当前程序工作目录
)
self.path_var = file_name
self.parent_window.begin_ship_render_from_path(file_name)
logger.info("加载飞船from " + self.path_var)
def get_ship_path(self):
logger.info("加载飞船from " + self.path_var)
return self.path_var
class PressOpenShipButton(PressTextButton):
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
def set_y(self, y):
self.y = y
def get_y(self):
return self.y
def on_mouse_release(self, x, y, buttons, modifiers):
if (
self.parent_window.show_ships_buttons
and x >= self.parent_window.ships_buttons_begin_x
and x <= self.parent_window.ships_buttons_end_x
and y >= self.parent_window.ships_buttons_begin_y
and y <= self.parent_window.ships_buttons_end_y
):
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)
logger.info("加载飞船from " + self.ship_path)
# self.parent_window.begin_ship_render_from_path(self.ship_path)
# logger.info("加载飞船from " + self.ship_path)

12
ships/A
View File

@ -1 +1,11 @@
<Ship version="1" liftedOff="0" touchingGround="0"><DisconnectedParts/><Parts><Part partType="pod-1" id="1" x="0.000000" y="0.750000" angle="0.000000" angleV="0.000000" editorAngle="0"><Pod throttle="0.000000" name=""><Staging currentStage="0"/></Pod></Part></Parts><Connections/></Ship>
<Ship version="1" liftedOff="0" touchingGround="0">
<DisconnectedParts />
<Parts>
<Part partType="pod-1" id="1" x="0.000000" y="0.750000" angle="0.000000" angleV="0.000000" editorAngle="0">
<Pod throttle="0.000000" name="">
<Staging currentStage="0" />
</Pod>
</Part>
</Parts>
<Connections />
</Ship>

1249
ships/A72

File diff suppressed because one or more lines are too long