cargo fmt 的另一部分

This commit is contained in:
shenjack 2023-03-14 00:19:01 +08:00
parent ba9750e265
commit 6b0da4b205
4 changed files with 313 additions and 153 deletions

View File

@ -32,9 +32,25 @@ pub mod camera {
impl CenterCameraRs { impl CenterCameraRs {
#[new] #[new]
#[pyo3(signature = (window, zoom=1.0, dx=1.0, dy=1.0, min_zoom=1.0, max_zoom=1.0))] #[pyo3(signature = (window, zoom=1.0, dx=1.0, dy=1.0, min_zoom=1.0, max_zoom=1.0))]
pub fn py_new(window: &PyAny, zoom: f64, dx: f64, dy: f64,min_zoom: f64, max_zoom: f64) -> PyResult<(Self, CameraRs)> { pub fn py_new(
return Ok((CenterCameraRs {}, CameraRs {dx, dy, zoom, min_zoom, max_zoom, window: &PyAny,
window: window.into()})) zoom: f64,
dx: f64,
dy: f64,
min_zoom: f64,
max_zoom: f64,
) -> PyResult<(Self, CameraRs)> {
return Ok((
CenterCameraRs {},
CameraRs {
dx,
dy,
zoom,
min_zoom,
max_zoom,
window: window.into(),
},
));
} }
// pub fn __enter__(py_self: PyRef<Self>) -> PyResult<PyRef<Self>> { // pub fn __enter__(py_self: PyRef<Self>) -> PyResult<PyRef<Self>> {
@ -50,8 +66,14 @@ pub mod camera {
Python::with_gil(|py| -> PyResult<()> { Python::with_gil(|py| -> PyResult<()> {
let view = super_.window.getattr(py, intern!(py, "view"))?; let view = super_.window.getattr(py, intern!(py, "view"))?;
// 获取存储的 view // 获取存储的 view
let x: f64 = super_.window.getattr(py, intern!(py, "width"))?.extract(py)?; let x: f64 = super_
let y: f64 = super_.window.getattr(py, intern!(py, "height"))?.extract(py)?; .window
.getattr(py, intern!(py, "width"))?
.extract(py)?;
let y: f64 = super_
.window
.getattr(py, intern!(py, "height"))?
.extract(py)?;
let x: f64 = x / 2.0 / super_.zoom + (super_.dx / super_.zoom); let x: f64 = x / 2.0 / super_.zoom + (super_.dx / super_.zoom);
let y: f64 = y / 2.0 / super_.zoom + (super_.dy / super_.zoom); let y: f64 = y / 2.0 / super_.zoom + (super_.dy / super_.zoom);
// 计算中心点 // 计算中心点
@ -59,15 +81,17 @@ pub mod camera {
// view.call_method1(py, "translate", (x, y))?; // view.call_method1(py, "translate", (x, y))?;
// view.call_method1(py, "scale", (super_.zoom, super_.zoom))?; // view.call_method1(py, "scale", (super_.zoom, super_.zoom))?;
let args = ((x * super_.zoom, y * super_.zoom, 0), ); let args = ((x * super_.zoom, y * super_.zoom, 0),);
let view_matrix = view.call_method1(py, intern!(py, "translate"), args)?; let view_matrix = view.call_method1(py, intern!(py, "translate"), args)?;
// view_matrix = self.view.translate((x * zoom, y * zoom, 0)) // view_matrix = self.view.translate((x * zoom, y * zoom, 0))
let args = ((super_.zoom, super_.zoom, 1), ); let args = ((super_.zoom, super_.zoom, 1),);
let view_matrix = view_matrix.call_method1(py, intern!(py, "scale"), args)?; let view_matrix = view_matrix.call_method1(py, intern!(py, "scale"), args)?;
// view_matrix = view_matrix.scale((zoom, zoom, 1)) // view_matrix = view_matrix.scale((zoom, zoom, 1))
super_.window.setattr(py, intern!(py, "view"), view_matrix)?; super_
.window
.setattr(py, intern!(py, "view"), view_matrix)?;
// self.view = view_matrix // self.view = view_matrix
Ok(()) Ok(())
})?; })?;
@ -79,9 +103,22 @@ pub mod camera {
impl CameraRs { impl CameraRs {
#[new] #[new]
#[pyo3(signature = (window, zoom=1.0, dx=1.0, dy=1.0, min_zoom=1.0, max_zoom=1.0))] #[pyo3(signature = (window, zoom=1.0, dx=1.0, dy=1.0, min_zoom=1.0, max_zoom=1.0))]
pub fn py_new(window: &PyAny, zoom: f64, dx: f64, dy: f64,min_zoom: f64, max_zoom: f64) -> PyResult<Self> { pub fn py_new(
return Ok(CameraRs {dx, dy, zoom, min_zoom, max_zoom, window: &PyAny,
window: window.into()}) zoom: f64,
dx: f64,
dy: f64,
min_zoom: f64,
max_zoom: f64,
) -> PyResult<Self> {
return Ok(CameraRs {
dx,
dy,
zoom,
min_zoom,
max_zoom,
window: window.into(),
});
} }
pub fn get_view(&self) -> PyResult<PyObject> { pub fn get_view(&self) -> PyResult<PyObject> {
@ -92,7 +129,7 @@ pub mod camera {
#[getter] #[getter]
pub fn get_position(&self) -> (f64, f64) { pub fn get_position(&self) -> (f64, f64) {
return (self.dx, self.dy) return (self.dx, self.dy);
} }
#[setter] #[setter]
@ -117,16 +154,19 @@ pub mod camera {
let view = self.window.getattr(py, intern!(py, "view"))?; let view = self.window.getattr(py, intern!(py, "view"))?;
let x: f64 = self.window.getattr(py, intern!(py, "width"))?.extract(py)?; let x: f64 = self.window.getattr(py, intern!(py, "width"))?.extract(py)?;
let y: f64 = self.window.getattr(py, intern!(py, "height"))?.extract(py)?; let y: f64 = self
.window
.getattr(py, intern!(py, "height"))?
.extract(py)?;
let x: f64 = x / 2.0 / self.zoom + (self.dx / self.zoom); let x: f64 = x / 2.0 / self.zoom + (self.dx / self.zoom);
let y: f64 = y / 2.0 / self.zoom + (self.dy / self.zoom); let y: f64 = y / 2.0 / self.zoom + (self.dy / self.zoom);
// use to get center of the screen // use to get center of the screen
let args = ((x * self.zoom, y * self.zoom, 0), ); let args = ((x * self.zoom, y * self.zoom, 0),);
let view_matrix = view.call_method1(py, intern!(py, "translate"), args)?; let view_matrix = view.call_method1(py, intern!(py, "translate"), args)?;
// view_matrix = self.view.translate((x * zoom, y * zoom, 0)) // view_matrix = self.view.translate((x * zoom, y * zoom, 0))
let args = ((self.zoom, self.zoom, 1), ); let args = ((self.zoom, self.zoom, 1),);
let view_matrix = view_matrix.call_method1(py, intern!(py, "scale"), args)?; let view_matrix = view_matrix.call_method1(py, intern!(py, "scale"), args)?;
// view_matrix = view_matrix.scale((zoom, zoom, 1)) // view_matrix = view_matrix.scale((zoom, zoom, 1))
@ -134,7 +174,7 @@ pub mod camera {
// self.view = view_matrix // self.view = view_matrix
Ok(()) Ok(())
})?; })?;
return Ok(()) return Ok(());
} }
pub fn end(&self) -> PyResult<()> { pub fn end(&self) -> PyResult<()> {
@ -142,20 +182,23 @@ pub mod camera {
let view = self.window.getattr(py, intern!(py, "view"))?; let view = self.window.getattr(py, intern!(py, "view"))?;
let x: f64 = self.window.getattr(py, intern!(py, "width"))?.extract(py)?; let x: f64 = self.window.getattr(py, intern!(py, "width"))?.extract(py)?;
let y: f64 = self.window.getattr(py, intern!(py, "height"))?.extract(py)?; let y: f64 = self
.window
.getattr(py, intern!(py, "height"))?
.extract(py)?;
let x: f64 = x / 2.0 / self.zoom + (self.dx / self.zoom); let x: f64 = x / 2.0 / self.zoom + (self.dx / self.zoom);
let y: f64 = y / 2.0 / self.zoom + (self.dy / self.zoom); let y: f64 = y / 2.0 / self.zoom + (self.dy / self.zoom);
let args = ((1.0 / self.zoom, 1.0 / self.zoom, 1), ); let args = ((1.0 / self.zoom, 1.0 / self.zoom, 1),);
let view_matrix = view.call_method1(py, intern!(py, "scale"), args)?; let view_matrix = view.call_method1(py, intern!(py, "scale"), args)?;
let args = ((-x * self.zoom, -y * self.zoom, 0), ); let args = ((-x * self.zoom, -y * self.zoom, 0),);
let view_matrix = view_matrix.call_method1(py, intern!(py, "translate"), args)?; let view_matrix = view_matrix.call_method1(py, intern!(py, "translate"), args)?;
self.window.setattr(py, intern!(py, "view"), view_matrix)?; self.window.setattr(py, intern!(py, "view"), view_matrix)?;
Ok(()) Ok(())
})?; })?;
return Ok(()) return Ok(());
} }
/// https://github.com/PyO3/pyo3/discussions/2931#discussioncomment-4820729 for finding this /// https://github.com/PyO3/pyo3/discussions/2931#discussioncomment-4820729 for finding this
@ -166,10 +209,15 @@ pub mod camera {
Ok(py_self) Ok(py_self)
} }
pub fn __exit__(&self, _exc_type: PyObject, _exc_value: PyObject, _traceback: PyObject) -> PyResult<()>{ pub fn __exit__(
&self,
_exc_type: PyObject,
_exc_value: PyObject,
_traceback: PyObject,
) -> PyResult<()> {
// println!("exit!"); // println!("exit!");
self.end()?; self.end()?;
return Ok(()) return Ok(());
} }
} }
} }
@ -194,7 +242,7 @@ pub mod screen {
Ok(PartFrame { Ok(PartFrame {
box_size: 111, box_size: 111,
width: 111, width: 111,
height: 111 height: 111,
}) })
} }
} }

View File

@ -10,11 +10,11 @@ pub mod part_list {
use std::fs; use std::fs;
use pyo3::prelude::*; use pyo3::prelude::*;
use serde::{Serialize, Deserialize}; use serde::{Deserialize, Serialize};
// use quick_xml::de::from_str; // use quick_xml::de::from_str;
use serde_xml_rs::{from_str}; use serde_xml_rs::from_str;
use crate::types::sr1::{SR1PartTypeData, SR1PartType, SR1PartAttr, SR1PartList}; use crate::types::sr1::{SR1PartAttr, SR1PartList, SR1PartType, SR1PartTypeData};
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RawPartList { pub struct RawPartList {
@ -238,37 +238,62 @@ pub mod part_list {
let part_attr: Option<SR1PartAttr> = match self.r#type { let part_attr: Option<SR1PartAttr> = match self.r#type {
SR1PartTypeEnum::tank => { SR1PartTypeEnum::tank => {
let tank = self.tank.unwrap(); let tank = self.tank.unwrap();
Some(SR1PartAttr::Tank {fuel: tank.fuel, dry_mass: tank.dry_mass, Some(SR1PartAttr::Tank {
fuel_type: tank.fuel_type.unwrap_or(0)}) fuel: tank.fuel,
}, dry_mass: tank.dry_mass,
fuel_type: tank.fuel_type.unwrap_or(0),
})
}
SR1PartTypeEnum::engine => { SR1PartTypeEnum::engine => {
let engine = self.engine.unwrap(); let engine = self.engine.unwrap();
Some(SR1PartAttr::Engine {power: engine.power, Some(SR1PartAttr::Engine {
consumption: engine.consumption, size: engine.size, power: engine.power,
turn: engine.turn, fuel_type: engine.fuel_type.unwrap_or(0), consumption: engine.consumption,
throttle_exponential: engine.throttle_exponential.unwrap_or(false)}) size: engine.size,
}, turn: engine.turn,
fuel_type: engine.fuel_type.unwrap_or(0),
throttle_exponential: engine.throttle_exponential.unwrap_or(false),
})
}
SR1PartTypeEnum::rcs => { SR1PartTypeEnum::rcs => {
let rcs = self.rcs.unwrap(); let rcs = self.rcs.unwrap();
Some(SR1PartAttr::Rcs {power: rcs.power, consumption: rcs.consumption, Some(SR1PartAttr::Rcs {
size: rcs.size}) power: rcs.power,
}, consumption: rcs.consumption,
size: rcs.size,
})
}
SR1PartTypeEnum::solar => { SR1PartTypeEnum::solar => {
let solar = self.solar.unwrap(); let solar = self.solar.unwrap();
Some(SR1PartAttr::Solar {charge_rate: solar.charge_rate}) Some(SR1PartAttr::Solar {
}, charge_rate: solar.charge_rate,
})
}
SR1PartTypeEnum::lander => { SR1PartTypeEnum::lander => {
let lander = self.lander.unwrap(); let lander = self.lander.unwrap();
Some(SR1PartAttr::Lander {max_angle: lander.max_angle, min_length: lander.min_length, Some(SR1PartAttr::Lander {
max_length: lander.max_length, angle_speed: lander.angle_speed.unwrap_or(0.0), max_angle: lander.max_angle,
length_speed: lander.length_speed.unwrap_or(0.0), width: lander.width}) min_length: lander.min_length,
}, max_length: lander.max_length,
_ => None angle_speed: lander.angle_speed.unwrap_or(0.0),
length_speed: lander.length_speed.unwrap_or(0.0),
width: lander.width,
})
}
_ => None,
}; };
let damage = self.damage.unwrap_or(Damage {disconnect: 0, explode: 0, let damage = self.damage.unwrap_or(Damage {
explosion_power: Some(0u32), explosion_size: Some(0u32) }); disconnect: 0,
let attach_points: Option<Vec<AttachPoint>> = if let Some(attach_points) = &self.attach_points { explode: 0,
Some(attach_points.unzip()) } else { None }; explosion_power: Some(0u32),
explosion_size: Some(0u32),
});
let attach_points: Option<Vec<AttachPoint>> =
if let Some(attach_points) = &self.attach_points {
Some(attach_points.unzip())
} else {
None
};
SR1PartType { SR1PartType {
id: self.id.clone(), id: self.id.clone(),
name: self.name.clone(), name: self.name.clone(),
@ -312,7 +337,10 @@ pub mod part_list {
println!("{}", part_data.id.to_string()); println!("{}", part_data.id.to_string());
part_list.push(part_data.to_sr_part_type()); part_list.push(part_data.to_sr_part_type());
} }
SR1PartList { types: part_list, name: name.unwrap_or("".to_string()) } SR1PartList {
types: part_list,
name: name.unwrap_or("".to_string()),
}
} }
} }
@ -350,9 +378,9 @@ pub mod part_list {
pub mod ship { pub mod ship {
use pyo3::prelude::*; use pyo3::prelude::*;
use serde::{Serialize, Deserialize}; use serde::{Deserialize, Serialize};
// use quick_xml::de::from_str; // use quick_xml::de::from_str;
use serde_xml_rs::{from_str}; use serde_xml_rs::from_str;
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Ship { pub struct Ship {
@ -364,20 +392,20 @@ pub mod ship {
#[serde(rename = "liftedOff")] #[serde(rename = "liftedOff")]
pub lift_off: i8, pub lift_off: i8,
#[serde(rename = "touchingGround")] #[serde(rename = "touchingGround")]
pub touch_ground: i8 pub touch_ground: i8,
} }
// <Ship version="1" liftedOff="0" touchingGround="0"> // <Ship version="1" liftedOff="0" touchingGround="0">
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Parts { pub struct Parts {
#[serde(rename = "Part")] #[serde(rename = "Part")]
pub parts: Vec<Part> pub parts: Vec<Part>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Connections { pub struct Connections {
#[serde(rename = "Connection")] #[serde(rename = "Connection")]
pub connects: Vec<Connection> pub connects: Vec<Connection>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -421,7 +449,7 @@ pub mod ship {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Engine { pub struct Engine {
pub fuel: i64 pub fuel: i64,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -429,12 +457,12 @@ pub mod ship {
#[serde(rename = "Staging")] #[serde(rename = "Staging")]
pub stages: Vec<Staging>, pub stages: Vec<Staging>,
pub name: String, pub name: String,
pub throttle: i8 pub throttle: i8,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Tank { pub struct Tank {
pub fuel: i64 pub fuel: i64,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -449,12 +477,12 @@ pub mod ship {
pub struct Activate { pub struct Activate {
#[serde(rename = "Id")] #[serde(rename = "Id")]
pub id: i64, pub id: i64,
pub moved: i8 // 1 or 0 pub moved: i8, // 1 or 0
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DisconnectedParts { pub struct DisconnectedParts {
pub parts: Vec<DisconnectedPart> pub parts: Vec<DisconnectedPart>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]

View File

@ -6,18 +6,18 @@
* ------------------------------- * -------------------------------
*/ */
use std::collections::HashMap; use crate::types::sr1::SR1PartData;
use std::time;
use pyo3::intern; use pyo3::intern;
use pyo3::prelude::*; use pyo3::prelude::*;
use crate::types::sr1::SR1PartData; use std::collections::HashMap;
use std::time;
#[allow(dead_code)] #[allow(dead_code)]
pub mod types { pub mod types {
use std::collections::HashMap;
use pyo3::intern; use pyo3::intern;
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::types::{PyDict}; use pyo3::types::PyDict;
use std::collections::HashMap;
use crate::types::sr1::SR1PartData; use crate::types::sr1::SR1PartData;
@ -25,7 +25,7 @@ pub mod types {
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,
pub id: usize, pub id: usize,
pub type_: String pub type_: String,
} }
#[pyclass(name = "PartDatas")] #[pyclass(name = "PartDatas")]
@ -44,21 +44,20 @@ pub mod types {
#[new] #[new]
fn py_new(py_part_data: &PyDict) -> PyResult<Self> { fn py_new(py_part_data: &PyDict) -> PyResult<Self> {
let datas: HashMap<i64, SR1PartData> = part_data_tp_SR1PartDatas(py_part_data)?; let datas: HashMap<i64, SR1PartData> = part_data_tp_SR1PartDatas(py_part_data)?;
return Ok(PartDatas { part_structs: datas }) return Ok(PartDatas {
part_structs: datas,
});
} }
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn part_data_to_SR1PartData(input: &PyAny) -> PyResult<SR1PartData> { pub fn part_data_to_SR1PartData(input: &PyAny) -> PyResult<SR1PartData> {
let connections = match input.getattr(intern!(input.py(), "connections")) { let connections = match input.getattr(intern!(input.py(), "connections")) {
Ok(ok) => { Ok(ok) => ok.extract()?,
ok.extract()? _ => None,
}
_ => None
}; };
return Ok(SR1PartData{ return Ok(SR1PartData {
x: input.getattr(intern!(input.py(), "x"))?.extract()?, x: input.getattr(intern!(input.py(), "x"))?.extract()?,
y: input.getattr(intern!(input.py(), "y"))?.extract()?, y: input.getattr(intern!(input.py(), "y"))?.extract()?,
id: input.getattr(intern!(input.py(), "id"))?.extract()?, id: input.getattr(intern!(input.py(), "id"))?.extract()?,
@ -66,14 +65,15 @@ pub mod types {
active: input.getattr(intern!(input.py(), "active"))?.extract()?, active: input.getattr(intern!(input.py(), "active"))?.extract()?,
angle: input.getattr(intern!(input.py(), "angle"))?.extract()?, angle: input.getattr(intern!(input.py(), "angle"))?.extract()?,
angle_v: input.getattr(intern!(input.py(), "angle_v"))?.extract()?, angle_v: input.getattr(intern!(input.py(), "angle_v"))?.extract()?,
editor_angle: input.getattr(intern!(input.py(), "editor_angle"))?.extract()?, editor_angle: input
.getattr(intern!(input.py(), "editor_angle"))?
.extract()?,
flip_x: input.getattr(intern!(input.py(), "flip_x"))?.extract()?, flip_x: input.getattr(intern!(input.py(), "flip_x"))?.extract()?,
flip_y: input.getattr(intern!(input.py(), "flip_y"))?.extract()?, flip_y: input.getattr(intern!(input.py(), "flip_y"))?.extract()?,
explode: input.getattr(intern!(input.py(), "explode"))?.extract()?, explode: input.getattr(intern!(input.py(), "explode"))?.extract()?,
textures: input.getattr(intern!(input.py(), "textures"))?.extract()?, textures: input.getattr(intern!(input.py(), "textures"))?.extract()?,
connections connections, // connections: input.getattr(intern!(input.py(), "connections"))?.extract()?,
// connections: input.getattr(intern!(input.py(), "connections"))?.extract()?, });
})
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -82,18 +82,23 @@ pub mod types {
for key in input.iter() { for key in input.iter() {
result.insert(key.0.extract()?, part_data_to_SR1PartData(key.1)?); result.insert(key.0.extract()?, part_data_to_SR1PartData(key.1)?);
} }
return Ok(result) return Ok(result);
} }
} }
#[pyfunction] #[pyfunction]
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn better_update_parts(render: &PyAny, option: &PyAny, window: &PyAny, pub fn better_update_parts(
parts: &types::PartDatas, sr1_xml_scale: i32) -> PyResult<bool> { render: &PyAny,
if !render.getattr(intern!(render.py(), "rendered"))?.is_true()? { option: &PyAny,
window: &PyAny,
parts: &types::PartDatas,
sr1_xml_scale: i32,
) -> PyResult<bool> {
if !render
.getattr(intern!(render.py(), "rendered"))?
.is_true()?
{
return Ok(false); return Ok(false);
} }
let start_time = time::Instant::now(); let start_time = time::Instant::now();
@ -114,4 +119,4 @@ pub fn better_update_parts(render: &PyAny, option: &PyAny, window: &PyAny,
} }
let run_time = start_time.elapsed(); let run_time = start_time.elapsed();
Ok(true) Ok(true)
} }

View File

@ -6,12 +6,13 @@
* ------------------------------- * -------------------------------
*/ */
pub mod sr1 { pub mod sr1 {
// use super::math::{Shape, Point2D}; // use super::math::{Shape, Point2D};
use crate::sr1_data::part_list::{RawPartList, RawPartType, SR1PartTypeEnum};
use crate::sr1_data::part_list::Damage as RawDamage; use crate::sr1_data::part_list::Damage as RawDamage;
use crate::sr1_data::part_list::{Tank, Engine, Solar, Rcs, Lander, AttachPoint, AttachPoints, Shape as RawShape}; use crate::sr1_data::part_list::{
AttachPoint, AttachPoints, Engine, Lander, Rcs, Shape as RawShape, Solar, Tank,
};
use crate::sr1_data::part_list::{RawPartList, RawPartType, SR1PartTypeEnum};
#[inline] #[inline]
pub fn map_ptype_textures(ptype: String) -> String { pub fn map_ptype_textures(ptype: String) -> String {
@ -44,7 +45,8 @@ pub mod sr1 {
"port-1" => "DockingPort", "port-1" => "DockingPort",
"lander-1" => "LanderLegPreview", "lander-1" => "LanderLegPreview",
_ => "Pod", _ => "Pod",
}.to_string() }
.to_string()
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -61,7 +63,7 @@ pub mod sr1 {
pub flip_y: bool, pub flip_y: bool,
pub explode: bool, pub explode: bool,
pub textures: String, pub textures: String,
pub connections: Option<Vec<((usize, usize), (isize, isize))>> pub connections: Option<Vec<((usize, usize), (isize, isize))>>,
} }
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
@ -88,7 +90,9 @@ pub mod sr1 {
consumption: f64, consumption: f64,
size: f64, size: f64,
}, },
Solar { charge_rate: f64 }, Solar {
charge_rate: f64,
},
Lander { Lander {
max_angle: f64, max_angle: f64,
min_length: f64, min_length: f64,
@ -96,7 +100,7 @@ pub mod sr1 {
angle_speed: f64, angle_speed: f64,
length_speed: f64, length_speed: f64,
width: f64, width: f64,
} },
} }
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
@ -117,7 +121,7 @@ pub mod sr1 {
disconnect: self.disconnect, disconnect: self.disconnect,
explode: self.explode, explode: self.explode,
explosion_power: Some(self.explosion_power), explosion_power: Some(self.explosion_power),
explosion_size: Some(self.explosion_size) explosion_size: Some(self.explosion_size),
} }
} }
} }
@ -167,8 +171,7 @@ pub mod sr1 {
// 部件碰撞箱 // 部件碰撞箱
pub attach_points: Option<Vec<AttachPoint>>, pub attach_points: Option<Vec<AttachPoint>>,
// 部件连接点 // 部件连接点
pub attr: Option<SR1PartAttr> pub attr: Option<SR1PartAttr>, // 部件特殊属性
// 部件特殊属性
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -183,7 +186,7 @@ pub mod sr1 {
for part_type in &self.types { for part_type in &self.types {
types.insert(0, part_type.to_raw_part_type()); types.insert(0, part_type.to_raw_part_type());
} }
RawPartList{part_types: types} RawPartList { part_types: types }
} }
} }
@ -202,7 +205,7 @@ pub mod sr1 {
let mut types: Vec<SR1PartType> = Vec::new(); let mut types: Vec<SR1PartType> = Vec::new();
let name = match name { let name = match name {
Some(name) => name, Some(name) => name,
None => "NewPartList".to_string() None => "NewPartList".to_string(),
}; };
for part_type in part_types { for part_type in part_types {
types.insert(0, part_type); types.insert(0, part_type);
@ -222,42 +225,96 @@ pub mod sr1 {
fn to_raw_part_type(&self) -> RawPartType { fn to_raw_part_type(&self) -> RawPartType {
let tank: Option<Tank> = match &self.attr { let tank: Option<Tank> = match &self.attr {
Some(attr) => { Some(attr) => match attr {
match attr { SR1PartAttr::Tank {
SR1PartAttr::Tank {fuel, dry_mass, fuel_type} => { fuel,
Some(Tank {fuel: *fuel, dry_mass: *dry_mass, fuel_type: Some(*fuel_type)}) dry_mass,
} _ => None } } _ => None }; fuel_type,
} => Some(Tank {
fuel: *fuel,
dry_mass: *dry_mass,
fuel_type: Some(*fuel_type),
}),
_ => None,
},
_ => None,
};
let engine: Option<Engine> = match &self.attr { let engine: Option<Engine> = match &self.attr {
Some(attr) => { Some(attr) => match attr {
match attr { SR1PartAttr::Engine {
SR1PartAttr::Engine {power, consumption, size, turn, fuel_type, throttle_exponential } => { power,
Some(Engine {power: *power, consumption: *consumption, throttle_exponential: Some(*throttle_exponential), consumption,
size: *size, turn: *turn, fuel_type: Some(*fuel_type)}) size,
} _ => None } } _ => None }; turn,
fuel_type,
throttle_exponential,
} => Some(Engine {
power: *power,
consumption: *consumption,
throttle_exponential: Some(*throttle_exponential),
size: *size,
turn: *turn,
fuel_type: Some(*fuel_type),
}),
_ => None,
},
_ => None,
};
let rcs: Option<Rcs> = match &self.attr { let rcs: Option<Rcs> = match &self.attr {
Some(attr) => { Some(attr) => match attr {
match attr { SR1PartAttr::Rcs {
SR1PartAttr::Rcs {power, consumption, size } => { power,
Some(Rcs {power: *power, consumption: *consumption, size: *size }) consumption,
} _ => None } } _ => None }; size,
} => Some(Rcs {
power: *power,
consumption: *consumption,
size: *size,
}),
_ => None,
},
_ => None,
};
let solar: Option<Solar> = match &self.attr { let solar: Option<Solar> = match &self.attr {
Some(attr) => { Some(attr) => match attr {
match attr { SR1PartAttr::Solar { charge_rate } => Some(Solar {
SR1PartAttr::Solar {charge_rate } => { charge_rate: *charge_rate,
Some(Solar {charge_rate: *charge_rate }) }),
} _ => None } } _ => None }; _ => None,
},
_ => None,
};
let lander: Option<Lander> = match &self.attr { let lander: Option<Lander> = match &self.attr {
Some(attr) => { Some(attr) => match attr {
match attr { SR1PartAttr::Lander {
SR1PartAttr::Lander {max_angle, min_length, max_length, angle_speed, length_speed, width } => { max_angle,
Some(Lander {max_angle: *max_angle, min_length: *min_length, max_length: *max_length, min_length,
angle_speed: Some(*angle_speed), length_speed: Some(*length_speed), width: *width }) max_length,
} _ => None } } _ => None }; angle_speed,
length_speed,
width,
} => Some(Lander {
max_angle: *max_angle,
min_length: *min_length,
max_length: *max_length,
angle_speed: Some(*angle_speed),
length_speed: Some(*length_speed),
width: *width,
}),
_ => None,
},
_ => None,
};
let attach_point: Option<AttachPoints> = match &self.attach_points { let attach_point: Option<AttachPoints> = match &self.attach_points {
Some(attach_points) => { Some(attach_points) => {
if attach_points.len() > 0 { if attach_points.len() > 0 {
Some(AttachPoints::new(attach_points.clone())) Some(AttachPoints::new(attach_points.clone()))
} else { None } } _ => None }; } else {
None
}
}
_ => None,
};
RawPartType { RawPartType {
id: self.id.clone(), id: self.id.clone(),
name: self.name.clone(), name: self.name.clone(),
@ -288,7 +345,6 @@ pub mod sr1 {
} }
} }
} }
} }
#[allow(unused)] #[allow(unused)]
@ -305,7 +361,7 @@ pub mod math {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Point2D { pub struct Point2D {
pub x: f64, pub x: f64,
pub y: f64 pub y: f64,
} }
impl Point2D { impl Point2D {
@ -314,7 +370,9 @@ pub mod math {
} }
#[inline] #[inline]
pub fn new_00() -> Self { Point2D { x: 0.0, y: 0.0 } } pub fn new_00() -> Self {
Point2D { x: 0.0, y: 0.0 }
}
#[inline] #[inline]
pub fn distance(&self, other: &Point2D) -> f64 { pub fn distance(&self, other: &Point2D) -> f64 {
@ -341,11 +399,10 @@ pub mod math {
let cos = radius.cos(); let cos = radius.cos();
let x = self.x * cos - self.y * sin; let x = self.x * cos - self.y * sin;
let y = self.x * sin + self.y * cos; let y = self.x * sin + self.y * cos;
Point2D{ x, y } Point2D { x, y }
} }
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct CircularArc { pub struct CircularArc {
pub r: f64, pub r: f64,
@ -369,14 +426,17 @@ pub mod math {
pub end: Point2D, pub end: Point2D,
// end point // end point
} }
impl Rotatable for OneTimeLine { impl Rotatable for OneTimeLine {
fn rotate(&self, angle: f64) -> Self { fn rotate(&self, angle: f64) -> Self {
self.rotate_radius(angle.to_radians()) self.rotate_radius(angle.to_radians())
} }
fn rotate_radius(&self, radius: f64) -> Self { fn rotate_radius(&self, radius: f64) -> Self {
OneTimeLine::point_new(&self.start.rotate_radius(radius), &self.end.rotate_radius(radius)) OneTimeLine::point_new(
&self.start.rotate_radius(radius),
&self.end.rotate_radius(radius),
)
} }
} }
@ -399,38 +459,59 @@ pub mod math {
let x = x.unwrap_or(0.0); let x = x.unwrap_or(0.0);
let y = y.unwrap_or(0.0); let y = y.unwrap_or(0.0);
let angle = angle.unwrap_or(0.0); let angle = angle.unwrap_or(0.0);
Shape { pos: Point2D::new(x, y), angle, bounds } Shape {
pos: Point2D::new(x, y),
angle,
bounds,
}
} }
pub fn new_width_height(width: f64, height: f64, angle: Option<f64>) -> Self { pub fn new_width_height(width: f64, height: f64, angle: Option<f64>) -> Self {
let d_width = width / 2.0; let d_width = width / 2.0;
let d_height = height / 2.0; let d_height = height / 2.0;
let mut edges: Vec<Edge> = vec![ let mut edges: Vec<Edge> = vec![
Edge::OneTimeLine{0: OneTimeLine::pos_new(-d_width, -d_height, d_width, -d_height)}, Edge::OneTimeLine {
Edge::OneTimeLine{0: OneTimeLine::pos_new(d_width, -d_height, d_width, d_height)}, 0: OneTimeLine::pos_new(-d_width, -d_height, d_width, -d_height),
Edge::OneTimeLine{0: OneTimeLine::pos_new(d_width, d_height, -d_width, d_height)}, },
Edge::OneTimeLine{0: OneTimeLine::pos_new(-d_width, d_height, -d_width, -d_height)} Edge::OneTimeLine {
0: OneTimeLine::pos_new(d_width, -d_height, d_width, d_height),
},
Edge::OneTimeLine {
0: OneTimeLine::pos_new(d_width, d_height, -d_width, d_height),
},
Edge::OneTimeLine {
0: OneTimeLine::pos_new(-d_width, d_height, -d_width, -d_height),
},
]; ];
if let Some(angle) = angle { if let Some(angle) = angle {
edges = edges.iter().map(|edge| { edges = edges
match edge { .iter()
.map(|edge| match edge {
Edge::OneTimeLine(line) => { Edge::OneTimeLine(line) => {
let start = line.start.rotate(angle); let start = line.start.rotate(angle);
let end = line.end.rotate(angle); let end = line.end.rotate(angle);
Edge::OneTimeLine(OneTimeLine::point_new(&start, &end)) Edge::OneTimeLine(OneTimeLine::point_new(&start, &end))
}, }
Edge::CircularArc(arc) => { Edge::CircularArc(arc) => {
let pos = arc.pos.rotate(angle); let pos = arc.pos.rotate(angle);
Edge::CircularArc(CircularArc{ r: arc.r, pos, start_angle: arc.start_angle, end_angle: arc.end_angle }) Edge::CircularArc(CircularArc {
r: arc.r,
pos,
start_angle: arc.start_angle,
end_angle: arc.end_angle,
})
} }
} })
}).collect(); .collect();
}
Shape {
pos: Point2D::new_00(),
angle: 0.0,
bounds: edges,
} }
Shape { pos: Point2D::new_00(), angle: 0.0, bounds: edges}
} }
} }
impl OneTimeLine { impl OneTimeLine {
#[inline] #[inline]
pub fn pos_new(x1: f64, y1: f64, x2: f64, y2: f64) -> Self { pub fn pos_new(x1: f64, y1: f64, x2: f64, y2: f64) -> Self {
@ -453,23 +534,23 @@ pub mod math {
(Some(k), None) => { (Some(k), None) => {
k_ = k; k_ = k;
b_ = point.y - (k * point.x) b_ = point.y - (k * point.x)
}, }
(None, Some(b)) => { (None, Some(b)) => {
b_ = b; b_ = b;
k_ = (point.y - b) / point.x; k_ = (point.y - b) / point.x;
}, }
(Some(k), Some(b)) => { (Some(k), Some(b)) => {
k_ = k; k_ = k;
b_ = b; b_ = b;
}, }
_ => { _ => {
k_ = point.y / point.x; k_ = point.y / point.x;
b_ = 0.0; b_ = 0.0;
} }
} }
OneTimeLine{ OneTimeLine {
start: *point, start: *point,
end: Point2D::new(0.0, b_) end: Point2D::new(0.0, b_),
} }
} }
@ -494,10 +575,8 @@ pub mod dr {
pub c_type: ConnectType, pub c_type: ConnectType,
pub d_pos: f64, pub d_pos: f64,
pub angel: f64, pub angel: f64,
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum PartType { pub enum PartType {
Pod, Pod,
@ -513,7 +592,7 @@ pub mod dr {
Battery, Battery,
Dock, Dock,
Port, Port,
Lander Lander,
} }
pub struct DRPartData { pub struct DRPartData {
@ -528,7 +607,7 @@ pub mod dr {
pub angle_v: f64, pub angle_v: f64,
pub flip_x: bool, pub flip_x: bool,
pub flip_y: bool, pub flip_y: bool,
pub connections: Option<Vec<usize>> pub connections: Option<Vec<usize>>,
} }
impl DRPartData { impl DRPartData {