Feature/py camera #35

Merged
shenjackyuanjie merged 9 commits from feature/py_camera into main 2023-06-25 15:45:54 +08:00
8 changed files with 179 additions and 33 deletions
Showing only changes of commit bbdc5d98ac - Show all commits

View File

@ -18,16 +18,11 @@ if TYPE_CHECKING:
def get_version_str() -> str: ... def get_version_str() -> str: ...
def simluation() -> None: ...
""" 用来测试 rust 的物理模拟能不能用 """
def part_list_read_test(file_name: Optional[str] = "./configs/PartList.xml") -> None: ... def part_list_read_test(file_name: Optional[str] = "./configs/PartList.xml") -> None: ...
def read_ship_test(path: Optional[str] = "./configs/dock1.xml") -> None: ... def read_ship_test(path: Optional[str] = "./configs/dock1.xml") -> None: ...
def load_and_save_test(file_name: str): ...
class Camera_rs: class Camera_rs:
""" 用于闲的没事 用 rust 写一个 camera """ """ 用于闲的没事 用 rust 写一个 camera """

View File

@ -91,7 +91,7 @@ dependencies = [
"autocfg", "autocfg",
"cfg-if", "cfg-if",
"crossbeam-utils", "crossbeam-utils",
"memoffset 0.9.0", "memoffset",
"scopeguard", "scopeguard",
] ]
@ -119,6 +119,7 @@ name = "difficult_rocket_rs"
version = "0.2.5" version = "0.2.5"
dependencies = [ dependencies = [
"pyo3", "pyo3",
"quick-xml",
"rapier2d-f64", "rapier2d-f64",
"serde", "serde",
"serde-xml-rs", "serde-xml-rs",
@ -182,13 +183,10 @@ dependencies = [
] ]
[[package]] [[package]]
name = "memoffset" name = "memchr"
version = "0.8.0" version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "memoffset" name = "memoffset"
@ -350,14 +348,14 @@ dependencies = [
[[package]] [[package]]
name = "pyo3" name = "pyo3"
version = "0.18.3" version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b1ac5b3731ba34fdaa9785f8d74d17448cd18f30cf19e0c7e7b1fdb5272109" checksum = "cffef52f74ec3b1a1baf295d9b8fcc3070327aefc39a6d00656b13c1d0b8885c"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"indoc", "indoc",
"libc", "libc",
"memoffset 0.8.0", "memoffset",
"parking_lot", "parking_lot",
"pyo3-build-config", "pyo3-build-config",
"pyo3-ffi", "pyo3-ffi",
@ -367,9 +365,9 @@ dependencies = [
[[package]] [[package]]
name = "pyo3-build-config" name = "pyo3-build-config"
version = "0.18.3" version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cb946f5ac61bb61a5014924910d936ebd2b23b705f7a4a3c40b05c720b079a3" checksum = "713eccf888fb05f1a96eb78c0dbc51907fee42b3377272dc902eb38985f418d5"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"target-lexicon", "target-lexicon",
@ -377,9 +375,9 @@ dependencies = [
[[package]] [[package]]
name = "pyo3-ffi" name = "pyo3-ffi"
version = "0.18.3" version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd4d7c5337821916ea2a1d21d1092e8443cf34879e53a0ac653fbb98f44ff65c" checksum = "5b2ecbdcfb01cbbf56e179ce969a048fd7305a66d4cdf3303e0da09d69afe4c3"
dependencies = [ dependencies = [
"libc", "libc",
"pyo3-build-config", "pyo3-build-config",
@ -387,9 +385,9 @@ dependencies = [
[[package]] [[package]]
name = "pyo3-macros" name = "pyo3-macros"
version = "0.18.3" version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9d39c55dab3fc5a4b25bbd1ac10a2da452c4aca13bb450f22818a002e29648d" checksum = "b78fdc0899f2ea781c463679b20cb08af9247febc8d052de941951024cd8aea0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"pyo3-macros-backend", "pyo3-macros-backend",
@ -399,15 +397,25 @@ dependencies = [
[[package]] [[package]]
name = "pyo3-macros-backend" name = "pyo3-macros-backend"
version = "0.18.3" version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97daff08a4c48320587b5224cc98d609e3c27b6d437315bd40b605c98eeb5918" checksum = "60da7b84f1227c3e2fe7593505de274dcf4c8928b4e0a1c23d551a14e4e80a0f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "quick-xml"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81b9228215d82c7b61490fec1de287136b5de6f5700f6e58ea9ad61a7964ca51"
dependencies = [
"memchr",
"serde",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.28" version = "1.0.28"

View File

@ -16,11 +16,11 @@ opt-level = 3
[profile.release] [profile.release]
strip = true strip = true
opt-level = "s" opt-level = "s"
codegen-units = 1 # codegen-units = 1
#[dependencies.quick-xml] [dependencies.quick-xml]
#version = "0.27.1" version = "0.29.0"
#features = ["serialize"] features = ["serialize"]
[dependencies.serde] [dependencies.serde]
version = "1.0.164" version = "1.0.164"
@ -37,5 +37,5 @@ version = "0.17.2"
features = ["simd-stable"] features = ["simd-stable"]
[dependencies.pyo3] [dependencies.pyo3]
version = "0.18.3" version = "0.19.0"
features = ["extension-module"] features = ["extension-module"]

View File

@ -44,6 +44,8 @@ fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(test_call, m)?)?; m.add_function(wrap_pyfunction!(test_call, m)?)?;
m.add_function(wrap_pyfunction!(sr1_data::part_list::read_part_list_py, m)?)?; m.add_function(wrap_pyfunction!(sr1_data::part_list::read_part_list_py, m)?)?;
m.add_function(wrap_pyfunction!(sr1_data::ship::py_raw_ship_from_file, m)?)?; m.add_function(wrap_pyfunction!(sr1_data::ship::py_raw_ship_from_file, m)?)?;
m.add_function(wrap_pyfunction!(python::data::load_and_save_test, m)?)?;
m.add_function(wrap_pyfunction!(python::serde_test::test_ship_read_and_write, m)?)?;
m.add_class::<render::camera::CameraRs>()?; m.add_class::<render::camera::CameraRs>()?;
m.add_class::<render::camera::CenterCameraRs>()?; m.add_class::<render::camera::CenterCameraRs>()?;
m.add_class::<render::screen::PartFrame>()?; m.add_class::<render::screen::PartFrame>()?;

View File

@ -55,7 +55,6 @@ pub mod data {
#[pyclass] #[pyclass]
#[pyo3(name = "SR1PartList_rs")] #[pyo3(name = "SR1PartList_rs")]
#[pyo3(text_signature = "(file_path = './configs/PartList.xml', list_name = 'NewPartList')")]
pub struct PySR1PartList { pub struct PySR1PartList {
pub data: SR1PartList, pub data: SR1PartList,
} }
@ -63,6 +62,7 @@ pub mod data {
#[pymethods] #[pymethods]
impl PySR1PartList { impl PySR1PartList {
#[new] #[new]
#[pyo3(text_signature = "(file_path = './configs/PartList.xml', list_name = 'NewPartList')")]
fn new(file_path: String, list_name: String) -> Self { fn new(file_path: String, list_name: String) -> Self {
let raw_part_list: RawPartList = RawPartList::from_file(file_path).unwrap(); let raw_part_list: RawPartList = RawPartList::from_file(file_path).unwrap();
let data = raw_part_list.to_sr_part_list(Some(list_name)); let data = raw_part_list.to_sr_part_list(Some(list_name));
@ -113,7 +113,6 @@ pub mod data {
#[pyclass] #[pyclass]
#[pyo3(name = "SR1Ship_rs")] #[pyo3(name = "SR1Ship_rs")]
#[pyo3(text_signature = "(file_path = './configs/dock1.xml', part_list = './configs/PartList.xml', ship_name = 'NewShip')")]
pub struct PySR1Ship { pub struct PySR1Ship {
pub ship: SR1Ship, pub ship: SR1Ship,
pub part_list: SR1PartList, pub part_list: SR1PartList,
@ -122,6 +121,7 @@ pub mod data {
#[pymethods] #[pymethods]
impl PySR1Ship { impl PySR1Ship {
#[new] #[new]
#[pyo3(text_signature = "(file_path = './configs/dock1.xml', part_list = './configs/PartList.xml', ship_name = 'NewShip')")]
fn new(file_path: String, part_list: String, ship_name: String) -> Self { fn new(file_path: String, part_list: String, ship_name: String) -> Self {
let mut ship = SR1Ship::from_file(file_path, Some(ship_name)).unwrap(); let mut ship = SR1Ship::from_file(file_path, Some(ship_name)).unwrap();
let part_list = SR1PartList::from_file(part_list).unwrap(); let part_list = SR1PartList::from_file(part_list).unwrap();
@ -205,6 +205,15 @@ pub mod data {
Ok(()) Ok(())
} }
} }
#[pyfunction]
pub fn load_and_save_test(file_name: String) -> PyResult<()> {
use crate::sr1_data::ship::RawShip;
use serde_xml_rs::to_string;
let ship = RawShip::from_file(file_name).unwrap();
let _save_string = to_string(&ship);
Ok(())
}
} }
pub mod console { pub mod console {
@ -276,3 +285,126 @@ pub mod console {
} }
} }
} }
pub mod serde_test {
use pyo3::prelude::*;
use quick_xml::de::from_str;
use quick_xml::se::to_string;
use serde::{Deserialize, Serialize};
use std::fs;
type IdType = i64;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename = "Ship")]
pub struct TestShip {
#[serde(rename = "@version")]
pub version: i32,
#[serde(rename = "@liftedOff")]
pub lift_off: i8,
#[serde(rename = "@touchingGround")]
pub touching_ground: i8,
#[serde(rename = "Connections")]
pub connections: Connections,
#[serde(rename = "DisconnectedParts")]
pub disconnected_parts: Option<DisconnectedParts>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DisconnectedParts {
#[serde(rename = "DisconnectedPart")]
pub disconnected_part: Option<Vec<DisconnectedPart>>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DisconnectedPart {
#[serde(rename = "Parts")]
pub parts: Parts,
#[serde(rename = "Connections")]
pub connections: Connections,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Connections {
#[serde(rename = "Connection")]
pub connection: Option<Vec<Connection>>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Connection {
#[serde(rename = "@parentAttachPoint")]
pub parent_attach_point: i32,
#[serde(rename = "@childAttachPoint")]
pub child_attach_point: i32,
#[serde(rename = "@parentPart")]
pub parent_part: IdType,
#[serde(rename = "@childPart")]
pub child_part: IdType,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Parts {
#[serde(rename = "Part")]
pub part: Vec<Part>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Part {
// #[serde(rename = "@Tank")]
// pub tank: Option<Tank>,
// #[serde(rename = "@Engine")]
// pub engine: Option<Engine>,
// #[serde(rename = "@Pod")]
// pub pod: Option<Pod>,
#[serde(rename = "@partType")]
pub part_type_id: String,
#[serde(rename = "@id")]
pub id: i64,
#[serde(rename = "@x")]
pub x: f64,
#[serde(rename = "@y")]
pub y: f64,
#[serde(rename = "@editorAngle")]
pub editor_angle: i32,
#[serde(rename = "@angle")]
pub angle: f64,
#[serde(rename = "@angleV")]
pub angle_v: f64,
#[serde(rename = "@flippedX")]
pub flip_x: Option<i8>,
#[serde(rename = "@flippedY")]
pub flip_y: Option<i8>,
#[serde(rename = "@chuteX")]
pub chute_x: Option<f64>,
#[serde(rename = "@chuteY")]
pub chute_y: Option<f64>,
#[serde(rename = "@chuteAngle")]
pub chute_angle: Option<f64>,
#[serde(rename = "@chuteHeight")]
pub chute_height: Option<f64>,
#[serde(rename = "@extension")]
pub extension: Option<f64>,
#[serde(rename = "@inflate")]
pub inflate: Option<i8>,
#[serde(rename = "@inflation")]
pub inflation: Option<f64>,
#[serde(rename = "@exploded")]
pub exploded: Option<i8>,
#[serde(rename = "@rope")]
pub rope: Option<i8>,
#[serde(rename = "@activated")]
pub activated: Option<i8>,
#[serde(rename = "@deployed")]
pub deployed: Option<i8>,
}
#[pyfunction]
#[pyo3(name = "test_ship_read_and_write")]
pub fn test_ship_read_and_write(file_name: String) -> PyResult<()> {
let file = fs::read_to_string(file_name).unwrap();
let ship: TestShip = from_str(&file).unwrap();
let save_string = to_string(&ship).unwrap();
fs::write("./test-xml-rs.xml", save_string).unwrap();
Ok(())
}
}

View File

@ -611,6 +611,7 @@ pub mod ship {
#[inline] #[inline]
pub fn save(&self, file_name: String) -> Option<()> { pub fn save(&self, file_name: String) -> Option<()> {
let part_list_file = serde_xml_rs::to_string(self); let part_list_file = serde_xml_rs::to_string(self);
print!("{:?}", part_list_file);
if let Ok(part_list_file) = part_list_file { if let Ok(part_list_file) = part_list_file {
fs::write(file_name, part_list_file).unwrap(); fs::write(file_name, part_list_file).unwrap();
Some(()) Some(())

View File

@ -10,3 +10,4 @@ ship.load = "正在加载飞船: {}"
ship.load_time = "飞船加载消耗时间: {} 秒" ship.load_time = "飞船加载消耗时间: {} 秒"
ship.info = "飞船信息:\n- 部件数量: {}\n- 部件重量: {}" ship.info = "飞船信息:\n- 部件数量: {}\n- 部件重量: {}"
ship.render.done = "飞船渲染完成" ship.render.done = "飞船渲染完成"
save.start = "正在保存飞船: {}"

View File

@ -36,7 +36,11 @@ if TYPE_CHECKING:
from Difficult_Rocket.client import ClientWindow from Difficult_Rocket.client import ClientWindow
if DR_mod_runtime.use_DR_rust: if DR_mod_runtime.use_DR_rust:
from .Difficult_Rocket_rs import CenterCamera_rs, SR1PartList_rs, SR1Ship_rs from .Difficult_Rocket_rs import (CenterCamera_rs,
SR1PartList_rs,
SR1Ship_rs,
load_and_save_test,
test_ship_read_and_write)
logger = logging.getLogger('client.dr_game_sr1_ship') logger = logging.getLogger('client.dr_game_sr1_ship')
sr_tr = Tr(lang_path=Path('./mods/dr_game/lang')) sr_tr = Tr(lang_path=Path('./mods/dr_game/lang'))
@ -150,6 +154,8 @@ class SR1ShipRender(BaseScreen):
self.xml_name = file_path self.xml_name = file_path
if DR_mod_runtime.use_DR_rust: if DR_mod_runtime.use_DR_rust:
try: try:
load_and_save_test(self.xml_name)
test_ship_read_and_write(self.xml_name)
self.rust_ship = SR1Ship_rs(file_path, 'configs/PartList.xml', 'a_new_ship') self.rust_ship = SR1Ship_rs(file_path, 'configs/PartList.xml', 'a_new_ship')
print(self.rust_ship.name) print(self.rust_ship.name)
print(self.rust_ship.img_pos) print(self.rust_ship.img_pos)
@ -436,6 +442,7 @@ class SR1ShipRender(BaseScreen):
return return
if not DR_mod_runtime.use_DR_rust: if not DR_mod_runtime.use_DR_rust:
return return
logger.info(sr_tr().sr1.ship.save.start().format(self.rust_ship))
self.rust_ship.save('./test-save.xml') self.rust_ship.save('./test-save.xml')
def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int, window: "ClientWindow"): def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int, window: "ClientWindow"):