Skip to content

Commit

Permalink
Add preloading example and run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Weasy666 committed Jan 12, 2023
1 parent de4bb24 commit 5961c5c
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 132 deletions.
5 changes: 4 additions & 1 deletion examples/2d/complex_one_color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Svg2dBundle {
svg,
origin: Origin::Center,
transform: Transform { scale: Vec3::new(2.0, 2.0, 1.0), ..Default::default() },
transform: Transform {
scale: Vec3::new(2.0, 2.0, 1.0),
..Default::default()
},
..Default::default()
});
}
9 changes: 6 additions & 3 deletions examples/2d/multiple_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
..Default::default()
},
Direction::Up
Direction::Up,
));

let svg = asset_server.load("neutron_star.svg");
Expand All @@ -47,7 +47,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
origin: Origin::Center,
..Default::default()
},
Direction::Up
Direction::Up,
));
}

Expand All @@ -57,7 +57,10 @@ enum Direction {
Down,
}

fn svg_movement(time: Res<Time>, mut svg_position: Query<(&mut Direction, &mut Transform), With<Handle<Svg>>>) {
fn svg_movement(
time: Res<Time>,
mut svg_position: Query<(&mut Direction, &mut Transform), With<Handle<Svg>>>,
) {
for (mut direction, mut transform) in &mut svg_position {
match *direction {
Direction::Up => transform.translation.y += 150. * time.delta_seconds(),
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/origin_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
origin: Origin::TopLeft,
..Default::default()
},
common::DontChange
common::DontChange,
));
}
67 changes: 67 additions & 0 deletions examples/2d/preloading.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use bevy::asset::LoadState;
use bevy::prelude::*;
use bevy_svg::prelude::*;

#[path = "../common/lib.rs"]
mod common;

fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor {
title: "preloading".to_string(),
width: 600.0,
height: 600.0,
..Default::default()
},
..Default::default()
}))
.add_plugin(common::CommonPlugin)
.add_plugin(bevy_svg::prelude::SvgPlugin)
.add_startup_system(setup)
.add_system(run)
.run();
}

fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}

#[derive(Default, Eq, PartialEq)]
enum TutorialFsm {
#[default]
Ready,
StartedLoad(Handle<Svg>),
Wait(Handle<Svg>, u8),
Loaded,
}

fn run(mut commands: Commands, asset_server: Res<AssetServer>, mut fsm: Local<TutorialFsm>) {
match &*fsm {
TutorialFsm::Ready => {
let handle = asset_server.load("neutron_star.svg");
*fsm = TutorialFsm::StartedLoad(handle);
}
TutorialFsm::StartedLoad(handle) => {
if asset_server.get_load_state(handle) == LoadState::Loaded {
*fsm = TutorialFsm::Wait(handle.clone(), 60);
}
}
TutorialFsm::Wait(handle, frames) => {
if *frames > 0 {
*fsm = TutorialFsm::Wait(handle.clone(), *frames - 1);
} else {
commands.spawn(Svg2dBundle {
svg: asset_server.get_handle("neutron_star.svg"),
origin: Origin::Center,
..Default::default()
});

*fsm = TutorialFsm::Loaded;
dbg!("We loaded");
}
}
TutorialFsm::Loaded => {}
}
}
27 changes: 15 additions & 12 deletions examples/3d/multiple_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ mod common;

fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor {
title: "3d_multiple_translation".to_string(),
width: 600.0,
height: 600.0,
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor {
title: "3d_multiple_translation".to_string(),
width: 600.0,
height: 600.0,
..Default::default()
},
..Default::default()
},
..Default::default()
}))
}))
.add_plugin(common::CommonPlugin)
.add_plugin(bevy_svg::prelude::SvgPlugin)
.add_startup_system(setup)
Expand All @@ -37,7 +37,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
..Default::default()
},
Direction::Up
Direction::Up,
));

let svg = asset_server.load("neutron_star.svg");
Expand All @@ -51,7 +51,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
..Default::default()
},
Direction::Up
Direction::Up,
));
}

Expand All @@ -61,7 +61,10 @@ enum Direction {
Down,
}

fn svg_movement(time: Res<Time>, mut svg_position: Query<(&mut Direction, &mut Transform), With<Handle<Svg>>>) {
fn svg_movement(
time: Res<Time>,
mut svg_position: Query<(&mut Direction, &mut Transform), With<Handle<Svg>>>,
) {
for (mut direction, mut transform) in &mut svg_position {
match *direction {
Direction::Up => transform.translation.y += 150.0 * time.delta_seconds(),
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/origin_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
..Default::default()
},
common::DontChange
common::DontChange,
));
}
158 changes: 84 additions & 74 deletions examples/common/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ pub struct CommonPlugin;

impl Plugin for CommonPlugin {
fn build(&self, app: &mut App) {
app
.add_plugin(FrameTimeDiagnosticsPlugin::default())
app.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_startup_system(setup_legend)
.add_startup_system(setup_fps_counter)
.add_startup_system(setup_origin_text)
Expand All @@ -25,78 +24,75 @@ fn setup_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
let font_bold = asset_server.load("fonts/FiraSans-Bold.ttf");
let font_medium = asset_server.load("fonts/FiraMono-Medium.ttf");

commands.spawn((
TextBundle::from_sections([
TextSection::new(
"Key Info",
TextStyle {
font: font_bold.clone(),
font_size: 30.0,
color: Color::WHITE,
},
),
TextSection::new(
"\nF",
TextStyle {
font: font_bold.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
" - Toggle Frame Diagnostics",
TextStyle {
font: font_medium.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
"\nO",
TextStyle {
font: font_bold.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
" - Cycle through Origins",
TextStyle {
font: font_medium.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
"\nV",
TextStyle {
font: font_bold.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
" - Toggle visibility",
TextStyle {
font: font_medium.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
])
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(5.0),
right: Val::Px(15.0),
..default()
commands.spawn((TextBundle::from_sections([
TextSection::new(
"Key Info",
TextStyle {
font: font_bold.clone(),
font_size: 30.0,
color: Color::WHITE,
},
),
TextSection::new(
"\nF",
TextStyle {
font: font_bold.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
" - Toggle Frame Diagnostics",
TextStyle {
font: font_medium.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
"\nO",
TextStyle {
font: font_bold.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
" - Cycle through Origins",
TextStyle {
font: font_medium.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
"\nV",
TextStyle {
font: font_bold.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
TextSection::new(
" - Toggle visibility",
TextStyle {
font: font_medium.clone(),
font_size: 20.0,
color: Color::WHITE,
},
),
])
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(5.0),
right: Val::Px(15.0),
..default()
}),
));
},
..default()
}),));
}


#[derive(Component)]
pub struct DontChange;

Expand All @@ -105,7 +101,14 @@ pub struct DontChange;
fn keyboard_input_system(
keyboard_input: Res<Input<KeyCode>>,
mut svg_query: Query<(&mut Origin, &mut Visibility), (With<Handle<Svg>>, Without<DontChange>)>,
mut ui_query: Query<&mut Visibility, (With<Text>, Or<(With<FpsText>, With<OriginText>)>, Without<Handle<Svg>>)>,
mut ui_query: Query<
&mut Visibility,
(
With<Text>,
Or<(With<FpsText>, With<OriginText>)>,
Without<Handle<Svg>>,
),
>,
) {
if keyboard_input.just_pressed(KeyCode::V) {
for (_, mut visible) in svg_query.iter_mut() {
Expand Down Expand Up @@ -139,7 +142,10 @@ struct FpsValues {

impl Default for FpsValues {
fn default() -> Self {
Self { min: 10000.0, max: 0.0 }
Self {
min: 10000.0,
max: 0.0,
}
}
}

Expand Down Expand Up @@ -223,7 +229,11 @@ fn setup_fps_counter(mut commands: Commands, asset_server: Res<AssetServer>) {
));
}

fn fps_text_update_system(diagnostics: Res<Diagnostics>, mut fps_values: Local<FpsValues>, mut query: Query<&mut Text, With<FpsText>>) {
fn fps_text_update_system(
diagnostics: Res<Diagnostics>,
mut fps_values: Local<FpsValues>,
mut query: Query<&mut Text, With<FpsText>>,
) {
for mut text in &mut query {
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(fps_smoothed) = fps.smoothed() {
Expand Down Expand Up @@ -264,7 +274,7 @@ fn setup_origin_text(mut commands: Commands, asset_server: Res<AssetServer>) {
font: font_medium.clone(),
font_size: 20.0,
color: Color::GOLD,
})
}),
])
.with_style(Style {
position_type: PositionType::Absolute,
Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ pub mod prelude {
pub use crate::render::Svg2dBundle;
#[cfg(feature = "3d")]
pub use crate::render::Svg3dBundle;
pub use crate::{
origin::Origin,
plugin::SvgPlugin,
svg::Svg,
};
pub use crate::{origin::Origin, plugin::SvgPlugin, svg::Svg};
pub use lyon_tessellation::{
FillOptions, FillRule, LineCap, LineJoin, Orientation, StrokeOptions,
};
Expand Down
Loading

0 comments on commit 5961c5c

Please sign in to comment.