Skip to content

Commit

Permalink
chore(gui): improve wheel sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
francisdb committed Jan 22, 2025
1 parent 3b36b45 commit 6efbbc7
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 87 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vpxgui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ bevy_mini_fps = "0.1.0"
#eframe = "0.30.0"
#egui_extras = { version = "0.30.0", features = ["image"] }
crossbeam-channel = "0.5.14"
image = { version = "0.25.5", default-features = true, features = ["jpeg", "png"] }
is_executable = "1.0.4"

[dev-dependencies]
Expand Down
18 changes: 10 additions & 8 deletions vpxgui/src/dmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::guifrontend::Globals;
use crate::wheel::{WheelInfo, BOTTOM_MARGIN};
use bevy::color::palettes::css::{GHOST_WHITE, GOLDENROD};
use bevy::color::Color;
use bevy::math::Vec3;
Expand Down Expand Up @@ -31,25 +31,28 @@ struct DmdBundle {
dmd: Dmd,
}

const DMD_WIDTH: f32 = 512.;
const DMD_HEIGHT: f32 = 128.;

pub(crate) fn dmd_plugin(app: &mut App) {
app.add_systems(Startup, create_dmd);
app.add_systems(Update, dmd_update);
}

fn dmd_update(
mut dmd_query: Query<(&mut Node, &mut Visibility), With<Dmd>>,
globals: Res<Globals>,
wheel_info: Res<WheelInfo>,
window_query: Query<&Window, With<PrimaryWindow>>,
) {
let window = window_query.single();
let width = window.width();
let height = window.height();
let wheel_size = wheel_info.wheel_size;
for (mut node, mut visibility) in dmd_query.iter_mut() {
//let (mut node1, mut visibility) = &query.p3().get_single_mut();
let wsize = globals.wheel_size;
//println!("node: {:?}", node);
node.left = Val::Px((width / 2.) - 256.0);
node.top = Val::Px(height - wsize - 108.);
node.left = Val::Px((width / 2.) - DMD_WIDTH / 2.);
node.top = Val::Px(height - wheel_size - DMD_HEIGHT - BOTTOM_MARGIN);

// node.top = Val::Px((-(height / 2.0)) + wsize + 20.);
//transform.translation = Vec3::new(0. - 326.0, (-(height / 2.0)) + wsize + 20., 0.);
Expand All @@ -63,13 +66,12 @@ fn create_dmd(mut commands: Commands, window_query: Query<&Window, With<PrimaryW
let window_height = window.height();
commands.spawn(DmdBundle {
node: Node {
width: Val::Px(512.),
height: Val::Px(128.),
width: Val::Px(DMD_WIDTH),
height: Val::Px(DMD_HEIGHT),
//left: Val::Px(10.),
left: Val::Px(window_width / 6.),
top: Val::Px(window_height / 2.),
border: UiRect::all(Val::Px(2.)),

..Default::default()
},
visibility: Visibility::Hidden,
Expand Down
12 changes: 6 additions & 6 deletions vpxgui/src/flippers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::guifrontend::Globals;
use crate::wheel::WheelInfo;
use bevy::math::{Quat, Vec3};
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
Expand Down Expand Up @@ -30,16 +30,16 @@ fn update_flippers(
Query<(&mut Transform, &mut Visibility), With<FlipperLeft>>,
Query<(&mut Transform, &mut Visibility), With<FlipperRight>>,
)>,
globals: Res<Globals>,
wheel_info: Res<WheelInfo>,
window_query: Query<&Window, With<PrimaryWindow>>,
) {
let window = window_query.single();
let height = window.height();

for (mut transform, mut visibility) in set.p0().iter_mut() {
transform.translation = Vec3::new(
(globals.wheel_size / 3.0) * -1.0,
(-(height / 2.)) + (globals.wheel_size / 4.),
(wheel_info.wheel_size / 2.) * -1.0,
(-(height / 2.)) + (wheel_info.wheel_size / 4.),
Z_LEVEL,
);
*visibility = Visibility::Visible;
Expand All @@ -48,8 +48,8 @@ fn update_flippers(
// TODO why should flippers get closer to each other when the window height is reduced?
for (mut transform, mut visibility) in set.p1().iter_mut() {
transform.translation = Vec3::new(
globals.wheel_size / 3.0,
(-(height / 2.0)) + (globals.wheel_size / 4.),
wheel_info.wheel_size / 2.,
(-(height / 2.0)) + (wheel_info.wheel_size / 4.),
Z_LEVEL,
);
*visibility = Visibility::Visible;
Expand Down
2 changes: 0 additions & 2 deletions vpxgui/src/guifrontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub struct VpxTables {

#[derive(Resource, Debug)]
pub(crate) struct Globals {
pub wheel_size: f32,
pub vpinball_running: bool,
}

Expand Down Expand Up @@ -124,7 +123,6 @@ pub fn guifrontend(config: ResolvedConfig, vpx_files_with_tableinfo: Vec<Indexed
indexed_tables: tables,
})
.insert_resource(Globals {
wheel_size: 100.0, // will be updated when loading wheels
vpinball_running: false,
})
.insert_resource(ClearColor(Color::srgb(0.1, 0.1, 0.1)))
Expand Down
2 changes: 1 addition & 1 deletion vpxgui/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub(crate) fn display_table_line(table: &IndexedTable) -> String {
.unwrap()
.to_string();
Some(table.table_info.table_name.to_owned())
.filter(|s| !s.clone().unwrap_or_default().is_empty())
.filter(|s| !s.clone().unwrap_or_default().trim().is_empty())
.map(|s| {
match s {
Some(name) => capitalize_first_letter(&name),
Expand Down
1 change: 1 addition & 0 deletions vpxgui/src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ fn update_loading_data(
} else {
loading_data.confirmation_frames_count += 1;
if loading_data.confirmation_frames_count == loading_data.confirmation_frames_target {
info!("All assets loaded.");
game_state.set(LoadingState::Ready);
}
}
Expand Down
101 changes: 33 additions & 68 deletions vpxgui/src/wheel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::guifrontend::{Config, Globals, VpxTables};
use crate::guifrontend::{Config, VpxTables};
use crate::list::SelectedItem;
use crate::loading::{LoadingData, LoadingState};
use bevy::image::Image;
Expand All @@ -7,7 +7,6 @@ use bevy::math::Vec3;
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_asset::{AssetId, AssetServer};
use image::ImageReader;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

Expand All @@ -16,6 +15,11 @@ pub struct AssetPaths {
pub paths: HashMap<AssetId<Image>, String>,
}

#[derive(Resource, Default)]
pub(crate) struct WheelInfo {
pub wheel_size: f32,
}

#[derive(Component)]
pub struct Wheel {
pub item_number: usize,
Expand All @@ -33,35 +37,37 @@ struct WheelBundle {
}

pub(crate) fn wheel_plugin(app: &mut App) {
app.insert_resource(AssetPaths {
paths: HashMap::new(),
});
app.insert_resource(AssetPaths::default());
app.insert_resource(WheelInfo::default());
app.add_systems(Startup, create_wheels);
app.add_systems(Update, update_selected_wheel);
}

pub const BOTTOM_MARGIN: f32 = 40.;

fn update_selected_wheel(
mut wheel_query: Query<(&mut Visibility, &Wheel, &mut Transform)>,
mut globals: ResMut<Globals>,
mut wheel_query: Query<(&mut Visibility, &mut Sprite, &Wheel, &mut Transform)>,
mut wheel_info: ResMut<WheelInfo>,
selected_item_res: Res<SelectedItem>,
window_query: Query<&Window, With<PrimaryWindow>>,
) {
let selected_item = selected_item_res.index.unwrap_or(0);
let window = window_query.single();
let height = window.height();
globals.wheel_size = window.height() / 3.;
let wheel_size = globals.wheel_size;
let window_height = window.height();
wheel_info.wheel_size = derive_wheel_size(window);
let wheel_size = wheel_info.wheel_size;
// update currently selected item to new value
for (mut visibility, wheel, mut transform) in wheel_query.iter_mut() {
for (mut visibility, mut sprite, wheel, mut transform) in wheel_query.iter_mut() {
if wheel.item_number != selected_item {
*visibility = Visibility::Hidden;
// transform.translation = Vec3::new(0., width, 0.);
} else {
sprite.custom_size = Some(Vec2::new(wheel_size, wheel_size));
*visibility = Visibility::Visible;
// *transform = Transform::from_xyz(0., 0., 0.);
transform.translation = Vec3::new(0., (-(height / 2.0)) + (wheel_size / 2.) + 20., 0.);
//transform.translation = Vec3::new(0., -(height - (height / 2.75 + (scale * 2.))), 0.);
// println!("Selected {}",&wheel.launchpath.as_os_str().to_string_lossy());
transform.translation = Vec3::new(
0.,
(-(window_height / 2.0)) + (wheel_size / 2.) + BOTTOM_MARGIN,
0.,
);
}
}
}
Expand All @@ -76,37 +82,13 @@ fn create_wheels(
config: Res<Config>,
vpx_tables: Res<VpxTables>,
mut asset_paths: ResMut<AssetPaths>,
mut globals: ResMut<Globals>,
mut wheel_info: ResMut<WheelInfo>,
) {
let tables = &vpx_tables.indexed_tables;

let window = window_query.single();
// Set default wheel size to a third of the window height
// TODO move this from globals to a specific resource for this module
globals.wheel_size = window.height() / 3.;

// let mut orentation = Horizontal;
// if height > width {
// orentation = Vertical;
// } else {
// orentation = Horizontal;
// };

//let mut scale = width/10.;
//let mut entities=0.;
//let locations = [
// -(width/2.)+scale,
// -(scale*2.),
// 0.,
// (scale*2.),
// (width/2.) - (scale),
// ];
//let mut handles =[];

let mut transform = Transform::from_xyz(0., 0., 0.);

//let mut transform = Transform::from_xyz(0., -(height-(height/2.+(scale*2.))), 0.);
//let mut transform = Transform::from_xyz(locations[xlocation], -(height-(height/2.+(scale*2.))), 0.);
wheel_info.wheel_size = derive_wheel_size(window);
let wheel_size = wheel_info.wheel_size;

// Create blank wheel
// tries [table_path]/wheels/blankwheel.png first
Expand All @@ -129,28 +111,8 @@ fn create_wheels(
loading_data
.loading_assets
.push(wheel_image_handle.clone().into());
// Normalizing the dimensions of wheels so they are all the same size.
// using imagesize crate as it is a very fast way to get the dimensions.
let (_wheel_width, _wheel_height) = (0., 0.);

// TODO below code is blocking, should be offloaded to a thread?
let wheel_height = if wheel_path.exists() {
let image = ImageReader::open(&wheel_path)
.unwrap()
.into_dimensions()
.unwrap();
let (_wheel_width, wheel_height) = image;
wheel_height
} else {
1000
};
// wheel_size.wheel_size = (height / 3.) / (size.height as f32);
// Normalize icons to 1/3 the screen height
transform.scale = Vec3::new(
(window.height() / 5.) / (wheel_height as f32),
(window.height() / 5.) / (wheel_height as f32),
100.0,
);

let transform = Transform::from_xyz(0., 0., 0.);

debug!(
"Wheel asset for table {} = {} {}",
Expand All @@ -167,10 +129,10 @@ fn create_wheels(
.paths
.insert(wheel_image_handle.id(), table_name.to_owned());

// Wheel
commands.spawn(WheelBundle {
sprite: Sprite {
image: wheel_image_handle.clone(),
custom_size: Some(Vec2::new(wheel_size, wheel_size)),
..default()
},
transform,
Expand All @@ -180,7 +142,10 @@ fn create_wheels(
},
});
}
info!("Wheels loaded");

info!("Wheels assets loading...");
game_state.set(LoadingState::Loading);
}

fn derive_wheel_size(window: &Window) -> f32 {
window.height() / 3.
}

0 comments on commit 6efbbc7

Please sign in to comment.