From 048ac0a9e42a734bda325dcd1dc0b60e9b51c262 Mon Sep 17 00:00:00 2001 From: Francis De Brabandere Date: Wed, 19 Jun 2024 17:57:55 +0200 Subject: [PATCH] clippy --- examples/create_basic_vpx_file.rs | 72 ++++++++++++++++++++----------- src/vpx/expanded.rs | 9 ++-- src/vpx/gameitem/bumper.rs | 8 ++-- src/vpx/gameitem/flipper.rs | 62 +++++++++++++------------- src/vpx/gameitem/plunger.rs | 60 +++++++++++++------------- src/vpx/gltf.rs | 27 ++++++------ 6 files changed, 130 insertions(+), 108 deletions(-) diff --git a/examples/create_basic_vpx_file.rs b/examples/create_basic_vpx_file.rs index a0743bf..6dafaf9 100644 --- a/examples/create_basic_vpx_file.rs +++ b/examples/create_basic_vpx_file.rs @@ -3,6 +3,8 @@ use vpin::vpx; use vpin::vpx::color::Color; use vpin::vpx::gameitem::bumper::Bumper; use vpin::vpx::gameitem::flipper::Flipper; +use vpin::vpx::gameitem::plunger::Plunger; +use vpin::vpx::gameitem::vertex2d::Vertex2D; use vpin::vpx::gameitem::GameItemEnum; use vpin::vpx::material::Material; use vpin::vpx::VPX; @@ -11,10 +13,12 @@ fn main() -> Result<(), Box> { let mut vpx = VPX::default(); // playfield material - let mut material = Material::default(); - material.name = "Playfield".to_string(); - // material defaults to purple - material.base_color = Color::from_rgb(0x966F33); // Wood + let material = Material { + name: "Playfield".to_string(), + // material defaults to purple + base_color: Color::from_rgb(0x966F33), // Wood + ..Default::default() + }; vpx.gamedata.materials = Some(vec![material]); // black background (default is bluish gray) @@ -22,34 +26,54 @@ fn main() -> Result<(), Box> { vpx.gamedata.playfield_material = "Playfield".to_string(); // add a plunger - let mut plunger = vpx::gameitem::plunger::Plunger::default(); - plunger.name = "Plunger".to_string(); - plunger.center.x = 898.027; - plunger.center.y = 2105.312; + let plunger = Plunger { + name: "Plunger".to_string(), + center: Vertex2D { + x: 898.027, + y: 2105.312, + }, + ..Default::default() + }; + vpx.add_game_item(GameItemEnum::Plunger(plunger)); // add a bumper in the center of the playfield - let mut bumper = Bumper::default(); - bumper.name = "Bumper1".to_string(); - bumper.center.x = (vpx.gamedata.left + vpx.gamedata.right) / 2.; - bumper.center.y = (vpx.gamedata.top + vpx.gamedata.bottom) / 2.; + let bumper = Bumper { + name: "Bumper1".to_string(), + center: Vertex2D { + x: (vpx.gamedata.left + vpx.gamedata.right) / 2., + y: (vpx.gamedata.top + vpx.gamedata.bottom) / 2., + }, + ..Default::default() + }; + vpx.add_game_item(GameItemEnum::Bumper(bumper)); // add 2 flippers - let mut flipper_left = Flipper::default(); - flipper_left.name = "LeftFlipper".to_string(); - flipper_left.center.x = 278.2138; - flipper_left.center.y = 1803.271; - flipper_left.start_angle = 120.5; - flipper_left.end_angle = 70.; + let flipper_left = Flipper { + name: "LeftFlipper".to_string(), + center: Vertex2D { + x: 278.2138, + y: 1803.271, + }, + start_angle: 120.5, + end_angle: 70., + ..Default::default() + }; + vpx.add_game_item(GameItemEnum::Flipper(flipper_left)); - let mut flipper_right = Flipper::default(); - flipper_right.name = "RightFlipper".to_string(); - flipper_right.center.x = 595.869; - flipper_right.center.y = 1803.271; - flipper_right.start_angle = -120.5; - flipper_right.end_angle = -70.; + let flipper_right = Flipper { + name: "RightFlipper".to_string(), + center: Vertex2D { + x: 595.869, + y: 1803.271, + }, + start_angle: -120.5, + end_angle: -70., + ..Default::default() + }; + vpx.add_game_item(GameItemEnum::Flipper(flipper_right)); // add a script diff --git a/src/vpx/expanded.rs b/src/vpx/expanded.rs index 5bc8ece..fa4a68f 100644 --- a/src/vpx/expanded.rs +++ b/src/vpx/expanded.rs @@ -120,7 +120,7 @@ pub fn write>(vpx: &VPX, expanded_dir: &P) -> Result<(), WriteErr Ok(()) } -fn materials_index(materials: &Vec) -> HashMap { +fn materials_index(materials: &[Material]) -> HashMap { let mut index = HashMap::new(); materials.iter().for_each(|m| { index.insert(m.name.clone(), (*m).clone()); @@ -924,7 +924,7 @@ fn write_gameitems>( )?; } let full_table_gltf_path = expanded_dir.as_ref().join("table.gltf"); - write_whole_table_gltf(&vpx, &full_table_gltf_path) + write_whole_table_gltf(vpx, &full_table_gltf_path) .map_err(|e| WriteError::Io(io::Error::new(io::ErrorKind::Other, format!("{}", e))))?; // write the gameitems index as array with names being the type and the name let gameitems_index_path = expanded_dir.as_ref().join("gameitems.json"); @@ -982,7 +982,7 @@ fn write_gameitem_binaries( })?; let gltf_path = gameitems_dir.join(format!("{}.gltf", json_file_name)); let image_rel_path = if !&primitive.image.is_empty() { - let primitive_image = UniCase::new(primitive.image.clone().into()); + let primitive_image = UniCase::new(primitive.image.clone()); if let Some(p) = image_index.get(&primitive_image) { let file_name = p.file_name().unwrap().to_string_lossy().to_string(); Some( @@ -1005,7 +1005,7 @@ fn write_gameitem_binaries( }; let material = if !primitive.material.is_empty() { if let Some(m) = material_index.get(&primitive.material) { - Some(m.name.clone()) + Some(m) } else { eprintln!( "Material not found for primitive {}: {}", @@ -1016,7 +1016,6 @@ fn write_gameitem_binaries( } else { None }; - let material = material_index.get(&primitive.material); write_gltf( gameitem.name().to_string(), &mesh, diff --git a/src/vpx/gameitem/bumper.rs b/src/vpx/gameitem/bumper.rs index a00dd8a..54f1885 100644 --- a/src/vpx/gameitem/bumper.rs +++ b/src/vpx/gameitem/bumper.rs @@ -13,19 +13,19 @@ pub struct Bumper { pub timer_interval: i32, pub threshold: f32, pub force: f32, + /// BSCT (added in ?) pub scatter: Option, - // BSCT (added in ?) pub height_scale: f32, pub ring_speed: f32, pub orientation: f32, + /// RDLI (added in ?) pub ring_drop_offset: Option, - // RDLI (added in ?) pub cap_material: String, pub base_material: String, pub socket_material: String, + /// RIMA (added in ?) pub ring_material: Option, - // RIMA (added in ?) - surface: String, + pub surface: String, pub name: String, pub is_cap_visible: bool, pub is_base_visible: bool, diff --git a/src/vpx/gameitem/flipper.rs b/src/vpx/gameitem/flipper.rs index ce85661..b2fc2d9 100644 --- a/src/vpx/gameitem/flipper.rs +++ b/src/vpx/gameitem/flipper.rs @@ -7,43 +7,43 @@ use super::{vertex2d::Vertex2D, GameItem}; #[derive(Debug, PartialEq, Clone, Dummy)] pub struct Flipper { pub center: Vertex2D, - base_radius: f32, - end_radius: f32, - flipper_radius_max: f32, - return_: f32, + pub base_radius: f32, + pub end_radius: f32, + pub flipper_radius_max: f32, + pub return_: f32, pub start_angle: f32, pub end_angle: f32, - override_physics: u32, - mass: f32, - is_timer_enabled: bool, - timer_interval: i32, - surface: String, - material: String, + pub override_physics: u32, + pub mass: f32, + pub is_timer_enabled: bool, + pub timer_interval: i32, + pub surface: String, + pub material: String, pub name: String, - rubber_material: String, - rubber_thickness_int: u32, // RTHK deprecated - rubber_thickness: Option, // RTHF (added in 10.?) - rubber_height_int: u32, // RHGT deprecated - rubber_height: Option, // RHGF (added in 10.?) - rubber_width_int: u32, // RWDT deprecated - rubber_width: Option, // RHGF (added in 10.?) - strength: f32, - elasticity: f32, - elasticity_falloff: f32, - friction: f32, - ramp_up: f32, - scatter: Option, + pub rubber_material: String, + pub rubber_thickness_int: u32, // RTHK deprecated + pub rubber_thickness: Option, // RTHF (added in 10.?) + pub rubber_height_int: u32, // RHGT deprecated + pub rubber_height: Option, // RHGF (added in 10.?) + pub rubber_width_int: u32, // RWDT deprecated + pub rubber_width: Option, // RHGF (added in 10.?) + pub strength: f32, + pub elasticity: f32, + pub elasticity_falloff: f32, + pub friction: f32, + pub ramp_up: f32, + pub scatter: Option, // SCTR (added in 10.?) - torque_damping: Option, + pub torque_damping: Option, // TODA (added in 10.?) - torque_damping_angle: Option, + pub torque_damping_angle: Option, // TDAA (added in 10.?) - flipper_radius_min: f32, - is_visible: bool, - is_enabled: bool, - height: f32, - image: Option, // IMAG (was missing in 10.01) - is_reflection_enabled: Option, // REEN (was missing in 10.01) + pub flipper_radius_min: f32, + pub is_visible: bool, + pub is_enabled: bool, + pub height: f32, + pub image: Option, // IMAG (was missing in 10.01) + pub is_reflection_enabled: Option, // REEN (was missing in 10.01) // these are shared between all items pub is_locked: bool, diff --git a/src/vpx/gameitem/plunger.rs b/src/vpx/gameitem/plunger.rs index b2425c5..bfa489f 100644 --- a/src/vpx/gameitem/plunger.rs +++ b/src/vpx/gameitem/plunger.rs @@ -109,37 +109,37 @@ impl<'de> Deserialize<'de> for PlungerType { #[derive(Debug, PartialEq, Dummy)] pub struct Plunger { pub center: Vertex2D, - width: f32, - height: f32, - z_adjust: f32, - stroke: f32, - speed_pull: f32, - speed_fire: f32, - plunger_type: PlungerType, - anim_frames: u32, - material: String, - image: String, - mech_strength: f32, - is_mech_plunger: bool, - auto_plunger: bool, - park_position: f32, - scatter_velocity: f32, - momentum_xfer: f32, - is_timer_enabled: bool, - timer_interval: i32, - is_visible: bool, - is_reflection_enabled: Option, // REEN (was missing in 10.01) - surface: String, + pub width: f32, + pub height: f32, + pub z_adjust: f32, + pub stroke: f32, + pub speed_pull: f32, + pub speed_fire: f32, + pub plunger_type: PlungerType, + pub anim_frames: u32, + pub material: String, + pub image: String, + pub mech_strength: f32, + pub is_mech_plunger: bool, + pub auto_plunger: bool, + pub park_position: f32, + pub scatter_velocity: f32, + pub momentum_xfer: f32, + pub is_timer_enabled: bool, + pub timer_interval: i32, + pub is_visible: bool, + pub is_reflection_enabled: Option, // REEN (was missing in 10.01) + pub surface: String, pub name: String, - tip_shape: String, - rod_diam: f32, - ring_gap: f32, - ring_diam: f32, - ring_width: f32, - spring_diam: f32, - spring_gauge: f32, - spring_loops: f32, - spring_end_loops: f32, + pub tip_shape: String, + pub rod_diam: f32, + pub ring_gap: f32, + pub ring_diam: f32, + pub ring_width: f32, + pub spring_diam: f32, + pub spring_gauge: f32, + pub spring_loops: f32, + pub spring_end_loops: f32, // these are shared between all items pub is_locked: bool, diff --git a/src/vpx/gltf.rs b/src/vpx/gltf.rs index 97e678b..ea0a5a0 100644 --- a/src/vpx/gltf.rs +++ b/src/vpx/gltf.rs @@ -12,7 +12,7 @@ use std::error::Error; use std::fs::File; use std::io::Write; use std::mem; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub(crate) enum Output { @@ -66,17 +66,18 @@ pub(crate) fn write_whole_table_gltf( vpx: &VPX, gltf_file_path: &PathBuf, ) -> Result<(), Box> { - let mut root = json::Root::default(); + let root = json::Root::default(); vpx.gameitems.iter().for_each(|gameitem| match gameitem { - GameItemEnum::Primitive(p) => { + GameItemEnum::Primitive(_p) => { // append to binary file and increase buffer_length and offset } - GameItemEnum::Light(l) => { + GameItemEnum::Light(_l) => { // TODO add lights } _ => {} }); + // TODO add playfield write_gltf_file(gltf_file_path, root) } @@ -144,7 +145,7 @@ fn write_vertices_binary(bin_path: PathBuf, vertices: Vec) -> Result<(), } fn write_glb_file( - gltf_file_path: &PathBuf, + gltf_file_path: &Path, root: Root, vertices: Vec, buffer_length: usize, @@ -173,7 +174,7 @@ fn write_glb_file( fn primitive( mesh: &&ReadMesh, output: Output, - bin_path: &PathBuf, + bin_path: &Path, root: &mut Root, material: Index, ) -> (Vec, usize, Primitive) { @@ -206,7 +207,7 @@ fn primitive( .to_str() .expect("Invalid file name") .to_string(); - Some(path.into()) + Some(path) } else { None }, @@ -317,21 +318,20 @@ fn material( name: None, }); - let texture = root.push(json::Texture { + root.push(json::Texture { sampler: Some(sampler), source: image, extensions: Default::default(), extras: Default::default(), name: None, - }); - - texture + }) }); // TODO is this color already in sRGB format? // see https://stackoverflow.com/questions/66469497/gltf-setting-colors-basecolorfactor fn to_srgb(c: u8) -> f32 { // Math.pow(200 / 255, 2.2) + // TODO it's well possible that vpinball already uses sRGB colors (c as f32 / 255.0).powf(2.2) } @@ -352,7 +352,7 @@ fn material( }; }; - let material = root.push(json::Material { + root.push(json::Material { pbr_metallic_roughness: json::material::PbrMetallicRoughness { base_color_texture: texture_opt.map(|texture| json::texture::Info { index: texture, @@ -379,6 +379,5 @@ fn material( // extras: Default::default(), name: Some("material1".to_string()), ..Default::default() - }); - material + }) }