-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
182 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
use bevy::{prelude::*, utils::Duration}; | ||
|
||
const SCALE_TIME: u64 = 400; | ||
|
||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, SystemLabel)] | ||
struct ApplyScaling; | ||
|
||
/// This example illustrates the various features of Bevy UI. | ||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.insert_resource(TargetScale { | ||
target_scale: 1.0, | ||
start_scale: 1.0, | ||
target_time: Timer::new(Duration::from_millis(SCALE_TIME), false), | ||
}) | ||
.add_startup_system(setup) | ||
.add_system(apply_scaling.label(ApplyScaling)) | ||
.add_system(change_scaling.before(ApplyScaling)) | ||
.run(); | ||
} | ||
|
||
fn setup( | ||
mut commands: Commands, | ||
asset_server: ResMut<AssetServer>, | ||
mut materials: ResMut<Assets<ColorMaterial>>, | ||
) { | ||
// ui camera | ||
commands.spawn_bundle(UiCameraBundle::default()); | ||
|
||
let text_style = TextStyle { | ||
font: asset_server.load("fonts/FiraMono-Medium.ttf"), | ||
font_size: 16., | ||
color: Color::BLACK, | ||
}; | ||
|
||
// root node | ||
commands | ||
.spawn_bundle(NodeBundle { | ||
style: Style { | ||
size: Size::new(Val::Percent(50.0), Val::Percent(50.0)), | ||
position_type: PositionType::Absolute, | ||
position: Rect { | ||
left: Val::Percent(25.), | ||
top: Val::Percent(25.), | ||
..Default::default() | ||
}, | ||
justify_content: JustifyContent::SpaceAround, | ||
align_items: AlignItems::Center, | ||
..Default::default() | ||
}, | ||
material: materials.add(Color::ANTIQUE_WHITE.into()), | ||
..Default::default() | ||
}) | ||
.with_children(|parent| { | ||
parent | ||
.spawn_bundle(NodeBundle { | ||
style: Style { | ||
size: Size::new(Val::Px(40.), Val::Px(40.)), | ||
..Default::default() | ||
}, | ||
material: materials.add(Color::RED.into()), | ||
..Default::default() | ||
}) | ||
.with_children(|parent| { | ||
parent.spawn_bundle(TextBundle { | ||
text: Text::with_section("Size!", text_style, TextAlignment::default()), | ||
..Default::default() | ||
}); | ||
}); | ||
parent.spawn_bundle(NodeBundle { | ||
style: Style { | ||
size: Size::new(Val::Percent(15.), Val::Percent(15.)), | ||
..Default::default() | ||
}, | ||
material: materials.add(Color::BLUE.into()), | ||
..Default::default() | ||
}); | ||
parent.spawn_bundle(ImageBundle { | ||
style: Style { | ||
size: Size::new(Val::Px(30.0), Val::Px(30.0)), | ||
..Default::default() | ||
}, | ||
material: materials.add(asset_server.load("branding/icon.png").into()), | ||
..Default::default() | ||
}); | ||
}); | ||
} | ||
|
||
fn change_scaling(input: Res<Input<KeyCode>>, mut ui_scale: ResMut<TargetScale>) { | ||
if input.just_pressed(KeyCode::Up) { | ||
let scale = (ui_scale.target_scale * 2.0).min(8.); | ||
ui_scale.set_scale(scale); | ||
info!("Scaling up! Scale: {}", ui_scale.target_scale); | ||
} | ||
if input.just_pressed(KeyCode::Down) { | ||
let scale = (ui_scale.target_scale / 2.0).max(1. / 8.); | ||
ui_scale.set_scale(scale); | ||
info!("Scaling down! Scale: {}", ui_scale.target_scale); | ||
} | ||
} | ||
|
||
struct TargetScale { | ||
target_scale: f64, | ||
start_scale: f64, | ||
target_time: Timer, | ||
} | ||
|
||
impl TargetScale { | ||
fn set_scale(&mut self, scale: f64) { | ||
self.start_scale = self.current_scale(); | ||
self.target_scale = scale; | ||
self.target_time.reset(); | ||
} | ||
|
||
fn current_scale(&self) -> f64 { | ||
let completion = self.target_time.percent(); | ||
let multiplier = ease_in_expo(completion as f64); | ||
self.start_scale + (self.target_scale - self.start_scale) * multiplier | ||
} | ||
} | ||
|
||
fn apply_scaling( | ||
time: Res<Time>, | ||
mut target_scale: ResMut<TargetScale>, | ||
mut ui_scale: ResMut<UiScale>, | ||
) { | ||
let timer = target_scale.target_time.tick(time.delta()); | ||
if timer.finished() && !timer.just_finished() { | ||
return; | ||
} | ||
|
||
ui_scale.scale = target_scale.current_scale(); | ||
} | ||
|
||
fn ease_in_expo(x: f64) -> f64 { | ||
if x == 0. { | ||
0. | ||
} else { | ||
(2.0f64).powf(10. * x - 10.) | ||
} | ||
} |