From 0319cb5a0e3b71458d24259500c277220f2a6ab3 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Thu, 27 Feb 2025 11:16:52 +0300 Subject: [PATCH] fixed update_card_dimensions --- src/lib.rs | 35 +++++++++++++++++++---------------- src/main.rs | 4 ++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 41e88eb..721f641 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,12 +50,12 @@ pub fn setup( pub fn test_setup_hand( mut commands: Commands, asset_server: Res, - //systems: Res, + systems: Res, query: Single> ) { let hand_id = query.into_inner(); for _ in 0..4 { - create_card(&mut commands, &asset_server, Some(hand_id)); + create_card(&mut commands, &asset_server, Some(hand_id), &systems); } } @@ -63,7 +63,7 @@ pub fn create_card( commands: &mut Commands, asset_server: &Res, hand_entity: Option, - //systems: &Res + systems: &Res ) { let mut card = commands.spawn(( Card, @@ -74,29 +74,32 @@ pub fn create_card( if let Some(hand) = hand_entity { card.set_parent(hand); commands.entity(hand).insert(JustUpdated); - //commands.run_system(systems.update_hand_dimensions); + commands.run_system(systems.update_hand_dimensions); } } +// TODO: replace this const with getting asset dimensions from Assets +const CARD_SPRITE_SIZE: Vec2 = Vec2::new(128., 192.); + pub fn update_hand_dimensions( mut commands: Commands, p_query: Single<(Entity, &mut Sprite, &Children), (Added, With)>, mut c_query: Query<(&mut Transform, &Sprite), (With, Without)>, - assets: Res> ) { let mut hand = p_query.into_inner(); - let mut counter = 0.; - let mut hand_total_width = 0.; - let mut hand_height = 0.; - for &child_id in hand.2.iter() { + let size = hand.2.iter().count(); + let hor_margin = 8.; + let ver_margin = 8.; + let sprite_size = CARD_SPRITE_SIZE; + let hand_total_width = (sprite_size.x + hor_margin) * (size as f32); + let hand_height = ver_margin * 2. + sprite_size.y; + let hand_offset = (hand_total_width - sprite_size.x - hor_margin) / 2.; + + for (index, &child_id) in hand.2.iter().enumerate() { let Ok(mut child) = c_query.get_mut(child_id) else { continue; }; - let sprite_size = assets.get(child.1.image.id()).unwrap().size_f32(); - let hor_margin = Vec2::new(4., 0.); - let ver_margin = Vec2::new(0., 4.); - hand_total_width += sprite_size.x + hor_margin.x; - hand_height = sprite_size.y + ver_margin.y * 2.; - child.0.translation = Vec3::from((ver_margin + (sprite_size + hor_margin) * counter, 0.)); - counter += 1.; + let offset_x = (sprite_size.x + hor_margin) * (index as f32) - hand_offset; + let offset_y = 0.; + child.0.translation = Vec3::from((Vec2::new(offset_x, offset_y), 1.)); } hand.1.custom_size = Some(Vec2::new(hand_total_width, hand_height)); commands.entity(hand.0).remove::(); diff --git a/src/main.rs b/src/main.rs index 63b8ac5..3da7f2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; use evolution_rs::{ plugins::*, - setup, test_setup_hand, update_hand_dimensions + setup, test_setup_hand }; @@ -17,6 +17,6 @@ use evolution_rs::{ fn main() { App::new() .add_plugins((DefaultPlugins,BasePropertiesPlugin)) - .add_systems(Startup, (setup, test_setup_hand, update_hand_dimensions).chain()) + .add_systems(Startup, (setup, test_setup_hand).chain()) .run(); }