From db79b187aa788507dd0ad2db4283a14a8ff3cda2 Mon Sep 17 00:00:00 2001 From: shenjack-mac <3695888@qq.com> Date: Thu, 2 Feb 2023 12:11:29 +0800 Subject: [PATCH] try extend camera --- Difficult_Rocket/client/render/sr1_ship.py | 61 +++++++++++----------- libs/Difficult_Rocket_rs/__init__.py | 19 ++++++- libs/Difficult_Rocket_rs/src/src/lib.rs | 1 + libs/Difficult_Rocket_rs/src/src/render.rs | 54 ++++++++++++++++--- 4 files changed, 97 insertions(+), 38 deletions(-) diff --git a/Difficult_Rocket/client/render/sr1_ship.py b/Difficult_Rocket/client/render/sr1_ship.py index 3a895f8..36bca43 100644 --- a/Difficult_Rocket/client/render/sr1_ship.py +++ b/Difficult_Rocket/client/render/sr1_ship.py @@ -116,7 +116,8 @@ class SR1ShipRender(BaseScreen): self.part_data: Dict[int, SR1PartData] = {} self.parts_sprite: Dict[int, Sprite] = {} if DR_option.use_DR_rust: - self.camera_rs = Camera_rs(main_window) + self.camera_rs = Camera_rs(main_window, + min_zoom=(1/2) ** 4, max_zoom=1 << 5) self.rust_parts = None def load_xml(self, file_path: str) -> bool: @@ -138,8 +139,9 @@ class SR1ShipRender(BaseScreen): self.part_data: Dict[int, SR1PartData] = {} self.parts_sprite: Dict[int, Sprite] = {} self.scale = 1.0 - self.dx = 0 - self.dy = 0 + if DR_option.use_DR_rust: + self.camera_rs.dx = 0 + self.camera_rs.dy = 0 parts = self.xml_root.find('Parts') for part_xml in parts: if part_xml.tag != 'Part': @@ -151,11 +153,11 @@ class SR1ShipRender(BaseScreen): print(f'hey! warning! id{part.id}') self.part_data[part.id] = part # 下面就是调用 pyglet 去渲染的部分 - render_scale = DR_option.gui_scale # 这个是 DR 的缩放比例 可以调节的( + # render_scale = DR_option.gui_scale # 这个是 DR 的缩放比例 可以调节的( # 主要是 Windows 下有一个缩放系数嘛,我待会试试这玩意能不能获取(估计得 ctypes # 在不缩放的情况下,XML的1个单位长度对应60个像素 - render_x = part.x * render_scale * self.scale * 60 + self.window_pointer.width / 2 - render_y = part.y * render_scale * self.scale * 60 + self.window_pointer.height / 2 + render_x = part.x * 60 + self.window_pointer.width / 2 + render_y = part.y * 60 + self.window_pointer.height / 2 # 你就这里改吧 cache_sprite = Sprite(img=self.textures.get_texture(part.textures), x=render_x, y=render_y, @@ -185,22 +187,21 @@ class SR1ShipRender(BaseScreen): def update_parts(self) -> bool: if not self.rendered: return False - self.debug_line.x2, self.debug_line.y2 = self.dx + (self.window_pointer.width / 2), self.dy + ( + self.debug_line.x2, self.debug_line.y2 = self.camera_rs.dx + (self.window_pointer.width / 2), self.dy + ( self.window_pointer.height / 2) - self.debug_d_pos_label.text = f'x: {self.dx} y: {self.dy}' - self.debug_d_pos_label.position = self.dx + (self.window_pointer.width / 2), self.dy + ( + self.debug_d_pos_label.text = f'x: {self.camera_rs.dx} y: {self.camera_rs.dy}' + self.debug_d_pos_label.position = self.camera_rs.dx + (self.window_pointer.width / 2), self.dy + ( self.window_pointer.height / 2) + 10, 0 - if DR_option.use_DR_rust: - # print(f'{self.dx=} {self.dy=} {self.scale=}') - # from objprint import op - # op(random.choices(self.parts_sprite), indent=1) - return better_update_parts(self, SR1ShipRender_Option, self.window_pointer, - self.rust_parts, DR_option.gui_scale, 60) - for part_id in self.part_data: - # x y scale - self.parts_sprite[part_id].x = self.part_data[part_id].x * DR_option.gui_scale * self.scale * 60 + self.window_pointer.width / 2 - self.parts_sprite[part_id].y = self.part_data[part_id].y * DR_option.gui_scale * self.scale * 60 + self.window_pointer.height / 2 - self.parts_sprite[part_id].scale = self.scale * DR_option.gui_scale + # if DR_option.use_DR_rust: + # # print(f'{self.dx=} {self.dy=} {self.scale=}') + # # from objprint import op + # # op(random.choices(self.parts_sprite), indent=1) + # return better_update_parts(self, SR1ShipRender_Option, self.window_pointer, + # self.rust_parts, 60) + # for part_id in self.part_data: + # # x y scale + # self.parts_sprite[part_id].x = self.part_data[part_id].x * 60 + self.window_pointer.width / 2 + # self.parts_sprite[part_id].y = self.part_data[part_id].y * 60 + self.window_pointer.height / 2 self.need_update_parts = False def on_draw(self): @@ -242,13 +243,13 @@ class SR1ShipRender(BaseScreen): mouse_dy = y - (self.window_pointer.height / 2) self.debug_mouse_line.x2, self.debug_mouse_line.y2 = x, y if self.scale * (0.5**scroll_y) < 10: - self.scale = self.scale * (0.5 ** scroll_y) - self.dx += (mouse_dx - self.dx) * (1 - (0.5 ** scroll_y)) - self.dy += (mouse_dy - self.dy) * (1 - (0.5 ** scroll_y)) + self.camera_rs.zoom = self.camera_rs.zoom * (0.5 ** scroll_y) + self.camera_rs.dx += (mouse_dx - self.camera_rs.dx) * (1 - (0.5 ** scroll_y)) + self.camera_rs.dy += (mouse_dy - self.camera_rs.dy) * (1 - (0.5 ** scroll_y)) else: - self.scale = 10 - self.debug_mouse_delta_line.x2 = (mouse_dx - self.dx) * (1 - (0.5 ** scroll_y)) + (self.window_pointer.width / 2) - self.debug_mouse_delta_line.y2 = (mouse_dy - self.dy) * (1 - (0.5 ** scroll_y)) + (self.window_pointer.height / 2) + self.camera_rs.zoom = 10 + self.debug_mouse_delta_line.x2 = (mouse_dx - self.camera_rs.dx) * (1 - (0.5 ** scroll_y)) + (self.window_pointer.width / 2) + self.debug_mouse_delta_line.y2 = (mouse_dy - self.camera_rs.dy) * (1 - (0.5 ** scroll_y)) + (self.window_pointer.height / 2) self.debug_mouse_label.text = f'x: {mouse_dx} y: {mouse_dy}' self.debug_mouse_label.position = x, y + 10, 0 self.need_update_parts = True @@ -259,8 +260,8 @@ class SR1ShipRender(BaseScreen): if command.re_match('render'): if command.re_match('reset'): self.scale = 1 - self.dx = 0 - self.dy = 0 + self.camera_rs.dx = 0 + self.camera_rs.dy = 0 else: # self.render_ship() self.need_draw = True @@ -286,8 +287,8 @@ class SR1ShipRender(BaseScreen): def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int): if not self.focus: return - self.dx += dx - self.dy += dy + self.camera_rs.dx += dx + self.camera_rs.dy += dy self.need_update_parts = True # self.update_parts() diff --git a/libs/Difficult_Rocket_rs/__init__.py b/libs/Difficult_Rocket_rs/__init__.py index 5797bd9..b20f6c8 100644 --- a/libs/Difficult_Rocket_rs/__init__.py +++ b/libs/Difficult_Rocket_rs/__init__.py @@ -6,7 +6,7 @@ from .lib import * -from typing import TYPE_CHECKING, Dict +from typing import TYPE_CHECKING, Dict, Tuple if TYPE_CHECKING: from Difficult_Rocket.client.screen import BaseScreen @@ -29,7 +29,6 @@ if TYPE_CHECKING: option: SR1ShipRender_Option, window: BaseScreen, parts: PartDatas, - global_scale: float, sr1_xml_scale: int) -> bool: ... class Camera_rs: @@ -40,6 +39,18 @@ if TYPE_CHECKING: min_zoom: float = 1.0, max_zoom: float = 1.0): ... + @property + def dx(self) -> float: ... + + @property + def dy(self) -> float: ... + + @property + def zoom(self) -> float: ... + + @property + def position(self) -> Tuple[float, float]: ... + def begin(self) -> None: ... def end(self) -> None: ... @@ -47,3 +58,7 @@ if TYPE_CHECKING: def __enter__(self, window) -> None: ... def __exit__(self, exc_type, exc_val, exc_tb) -> None: ... + + + class CenterCamera_rs(Camera_rs): + """ 用于依旧闲的没事 用 rust 写一个中央对齐的 camera """ diff --git a/libs/Difficult_Rocket_rs/src/src/lib.rs b/libs/Difficult_Rocket_rs/src/src/lib.rs index f445308..b82e8fc 100644 --- a/libs/Difficult_Rocket_rs/src/src/lib.rs +++ b/libs/Difficult_Rocket_rs/src/src/lib.rs @@ -33,5 +33,6 @@ fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(sr1_render::better_update_parts, m)?)?; m.add_class::()?; m.add_class::()?; + m.add_class::()?; Ok(()) } \ No newline at end of file diff --git a/libs/Difficult_Rocket_rs/src/src/render.rs b/libs/Difficult_Rocket_rs/src/src/render.rs index 0bfdb5d..0402b02 100644 --- a/libs/Difficult_Rocket_rs/src/src/render.rs +++ b/libs/Difficult_Rocket_rs/src/src/render.rs @@ -11,8 +11,8 @@ pub mod camera { use pyo3::intern; use pyo3::prelude::*; - #[pyclass(name = "Camera_rs")] - pub struct CameraRs{ + #[pyclass(name = "Camera_rs", subclass)] + pub struct CameraRs { pub window: Py, #[pyo3(get, set)] pub dx: f64, @@ -25,6 +25,9 @@ pub mod camera { pub min_zoom: f64, } + #[pyclass(extends = CameraRs, name = "CenterCamera_rs")] + pub struct CenterCameraRs; + #[pymethods] impl CameraRs { #[new] @@ -67,7 +70,7 @@ pub mod camera { Python::with_gil(|py| -> PyResult<()> { let view = self.window.getattr(py, intern!(py, "view"))?; - let args = ((-self.dx * self.zoom, -self.dy * self.zoom, 0), ); + let args = ((self.dx * self.zoom, self.dy * self.zoom, 0), ); let view_matrix = view.call_method1(py, intern!(py, "translate"), args)?; let args = ((self.zoom, self.zoom, 1), ); @@ -86,7 +89,7 @@ pub mod camera { let args = ((1.0 / self.zoom, 1.0 / self.zoom, 1), ); let view_matrix = view.call_method1(py, intern!(py, "scale"), args)?; - let args = ((self.dx * self.zoom, self.dy * self.zoom, 0), ); + let args = ((-self.dx * self.zoom, -self.dy * self.zoom, 0), ); let view_matrix = view_matrix.call_method1(py, intern!(py, "translate"), args)?; self.window.setattr(py, intern!(py, "view"), view_matrix)?; @@ -108,8 +111,47 @@ pub mod camera { self.end()?; return Ok(()) } - - } + // #[pymethods] + // impl CenterCameraRs { + // #[new] + // pub fn py_new() -> (Self, CameraRs) { + // (CenterCameraRs, CameraRs) + // } + // + // pub fn begin(self_: PyRef<'_, Self>) -> PyResult<()> { + // let super = self_.as_ref(); + // Python::with_gil(|py| -> PyResult<()> { + // // let x = self + // let view = super.window.getattr(py, intern!(py, "view"))?; + // + // let args = ((super.dx * super.zoom, super.dy * super.zoom, 0), ); + // let view_matrix = view.call_method1(py, intern!(py, "translate"), args)?; + // + // let args = ((super.zoom, super.zoom, 1), ); + // let view_matrix = view_matrix.call_method1(py, intern!(py, "scale"), args)?; + // + // super.window.setattr(py, intern!(py, "view"), view_matrix)?; + // Ok(()) + // })?; + // return Ok(()) + // } + + // pub fn end(&self) -> PyResult<()> { + // Python::with_gil(|py| -> PyResult<()> { + // let view = self.window.getattr(py, intern!(py, "view"))?; + // + // let args = ((1.0 / self.zoom, 1.0 / self.zoom, 1), ); + // let view_matrix = view.call_method1(py, intern!(py, "scale"), args)?; + // + // let args = ((-self.dx * self.zoom, -self.dy * self.zoom, 0), ); + // let view_matrix = view_matrix.call_method1(py, intern!(py, "translate"), args)?; + // + // self.window.setattr(py, intern!(py, "view"), view_matrix)?; + // Ok(()) + // })?; + // return Ok(()) + // } + // } }