Skip to content

Commit

Permalink
upgrade to bevy 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
foxzool committed Nov 30, 2024
1 parent a1992bb commit 90f769e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ alloc_instead_of_core = "warn"


[dependencies]
bevy = { version = "0.15.0-rc.3", features = ["bevy_sprite_picking_backend", "jpeg"] }
bevy = { version = "0.15.0", features = ["bevy_sprite_picking_backend", "jpeg"] }
#bevy = { git = "https://github.com/bevyengine/bevy", features = ["bevy_sprite_picking_backend"] }
jigsaw_puzzle_generator = { path = "jigsaw_puzzle_generator" }
rand = "0.8.5"
Expand Down
28 changes: 14 additions & 14 deletions src/gameplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ fn setup_game_ui(
// exit button
builder
.spawn((
UiImage::new(asset_server.load("icons/cross.png")),
ImageNode::new(asset_server.load("icons/cross.png")),
Node {
height: Val::Px(40.),
..default()
Expand All @@ -975,7 +975,7 @@ fn setup_game_ui(
// shuffle button
builder
.spawn((
UiImage::new(asset_server.load("icons/four-arrows.png")),
ImageNode::new(asset_server.load("icons/four-arrows.png")),
Node {
height: Val::Px(40.),
..default()
Expand All @@ -997,7 +997,7 @@ fn setup_game_ui(
})
.with_children(|builder| {
builder.spawn((
UiImage::new(asset_server.load("icons/zoom_out.png")),
ImageNode::new(asset_server.load("icons/zoom_out.png")),
Node {
height: Val::Px(30.),
margin: UiRect {
Expand All @@ -1016,7 +1016,7 @@ fn setup_game_ui(

// zoom in button
builder.spawn((
UiImage::new(asset_server.load("icons/zoom_in.png")),
ImageNode::new(asset_server.load("icons/zoom_in.png")),
Node {
height: Val::Px(30.),
margin: UiRect {
Expand All @@ -1039,7 +1039,7 @@ fn setup_game_ui(
builder.spawn(Node::default()).with_children(|p| {
// idea
p.spawn((
UiImage::new(asset_server.load("icons/lamp.png")),
ImageNode::new(asset_server.load("icons/lamp.png")),
Node {
height: Val::Px(40.),
margin: UiRect::axes(Val::Px(0.), Val::Px(5.)),
Expand All @@ -1063,7 +1063,7 @@ fn setup_game_ui(
),))
.with_children(|p| {
p.spawn((
UiImage {
ImageNode {
image: asset_server.load("icons/puzzle_s.png"),
flip_x: true,
..default()
Expand All @@ -1076,7 +1076,7 @@ fn setup_game_ui(
));

p.spawn((
UiImage::new(asset_server.load("icons/puzzle_e.png")),
ImageNode::new(asset_server.load("icons/puzzle_e.png")),
Node {
height: Val::Px(30.),
margin: UiRect {
Expand All @@ -1092,7 +1092,7 @@ fn setup_game_ui(
));

p.spawn((
UiImage::new(asset_server.load("icons/puzzle_s.png")),
ImageNode::new(asset_server.load("icons/puzzle_s.png")),
Node {
height: Val::Px(40.),
margin: UiRect::axes(Val::Px(2.), Val::Px(5.)),
Expand All @@ -1108,7 +1108,7 @@ fn setup_game_ui(

// background hint
p.spawn((
UiImage::new(asset_server.load("icons/ghost.png")),
ImageNode::new(asset_server.load("icons/ghost.png")),
Node {
height: Val::Px(40.),
margin: UiRect::axes(Val::Px(0.), Val::Px(5.)),
Expand Down Expand Up @@ -1163,7 +1163,7 @@ fn setup_game_ui(
position_type: PositionType::Absolute,
..default()
},
UiImage::new(asset_server.load("icons/photo.png")),
ImageNode::new(asset_server.load("icons/photo.png")),
HintImageButton,
Visibility::Visible,
))
Expand All @@ -1188,7 +1188,7 @@ fn setup_game_ui(

// pause button
p.spawn((
UiImage::new(asset_server.load("icons/pause.png")),
ImageNode::new(asset_server.load("icons/pause.png")),
Node {
height: Val::Px(40.),
margin: UiRect {
Expand All @@ -1207,7 +1207,7 @@ fn setup_game_ui(
);
// fullscreen button
p.spawn((
UiImage::new(asset_server.load("icons/fullscreen.png")),
ImageNode::new(asset_server.load("icons/fullscreen.png")),
Node {
height: Val::Px(40.),
..default()
Expand Down Expand Up @@ -1424,7 +1424,7 @@ fn hint_image_click(
commands
.entity(*small_hint_image)
.insert((
UiImage::new(origin_image.0.clone()),
ImageNode::new(origin_image.0.clone()),
Node {
width: Val::Px(400.0),
// aspect_ratio: Some(aspect_ratio),
Expand All @@ -1444,5 +1444,5 @@ fn hint_small_image_click(
small_img: Single<Entity, (With<SmallHintImage>, Without<HintImageButton>)>,
) {
**hint = Visibility::Visible;
commands.entity(*small_img).remove::<UiImage>();
commands.entity(*small_img).remove::<ImageNode>();
}
87 changes: 55 additions & 32 deletions src/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use crate::{
despawn_screen, AnimeCamera, AppState, OriginImage, SelectGameMode, SelectPiece,
ANIMATION_LAYERS, HOVERED_BUTTON, NORMAL_BUTTON, PRESSED_BUTTON,
};
use bevy::animation::{AnimationTarget, AnimationTargetId};
use bevy::animation::{
animated_field, AnimationEntityMut, AnimationEvaluationError, AnimationTarget,
AnimationTargetId,
};
use bevy::color::palettes::basic::BLACK;
use bevy::prelude::*;
use bevy::window::WindowResized;
use core::any::TypeId;

pub(crate) fn menu_plugin(app: &mut App) {
app.init_resource::<LoadedImages>()
Expand Down Expand Up @@ -48,18 +52,33 @@ struct MenuTimer(Timer);
#[derive(Event)]
struct ShowTitleAnime;

#[derive(Reflect)]
#[derive(Reflect, Clone)]
struct TextColorProperty;

impl AnimatableProperty for TextColorProperty {
type Component = TextColor;

type Property = Srgba;

fn get_mut(component: &mut Self::Component) -> Option<&mut Self::Property> {
match component.0 {
Color::Srgba(ref mut color) => Some(color),
_ => None,
fn evaluator_id(&self) -> EvaluatorId {
EvaluatorId::Type(TypeId::of::<Self>())
}

fn get_mut<'a>(
&self,
entity: &'a mut AnimationEntityMut,
) -> Result<&'a mut Self::Property, AnimationEvaluationError> {
let text_color = entity
.get_mut::<TextColor>()
.ok_or(AnimationEvaluationError::ComponentNotPresent(TypeId::of::<
TextColor,
>(
)))?
.into_inner();
match text_color.0 {
Color::Srgba(ref mut color) => Ok(color),
_ => Err(AnimationEvaluationError::PropertyNotPresent(TypeId::of::<
Srgba,
>(
))),
}
}
}
Expand Down Expand Up @@ -102,26 +121,30 @@ fn show_title(
let title_animation_target_id = AnimationTargetId::from_name(&title);
animation.add_curve_to_target(
title_animation_target_id,
UnevenSampleAutoCurve::new([0.0, 0.5, 1.0, 2.0, 3.0].into_iter().zip([
Vec3::new(start_pos.0, start_pos.1, 0.0),
Vec3::new(start_pos.0, start_pos.1 + 50.0, 0.0),
Vec3::new(start_pos.0, start_pos.1 + 110.0, 0.0),
Vec3::new(start_pos.0, start_pos.1 + 180.0, 0.0),
]))
.map(TranslationCurve)
.expect("should be able to build translation curve because we pass in valid samples"),
AnimatableCurve::new(
animated_field!(Transform::translation),
UnevenSampleAutoCurve::new([0.0, 0.5, 1.0, 2.0, 3.0].into_iter().zip([
Vec3::new(start_pos.0, start_pos.1, 0.0),
Vec3::new(start_pos.0, start_pos.1 + 50.0, 0.0),
Vec3::new(start_pos.0, start_pos.1 + 110.0, 0.0),
Vec3::new(start_pos.0, start_pos.1 + 180.0, 0.0),
]))
.expect("should be able to build translation curve because we pass in valid samples"),
),
);

animation.add_curve_to_target(
title_animation_target_id,
AnimatableKeyframeCurve::new([0.0, 1.0, 2.0, 3.0].into_iter().zip([
Srgba::new(0.0, 0.0, 0.0, 0.1),
Srgba::new(0.0, 0.0, 0.0, 0.3),
Srgba::new(0.0, 0.0, 0.0, 0.6),
Srgba::new(0.0, 0.0, 0.0, 1.0),
]))
.map(AnimatableCurve::<TextColorProperty, _>::from_curve)
.expect("should be able to build translation curve because we pass in valid samples"),
AnimatableCurve::new(
TextColorProperty,
AnimatableKeyframeCurve::new([0.0, 1.0, 2.0, 3.0].into_iter().zip([
Srgba::new(0.0, 0.0, 0.0, 0.1),
Srgba::new(0.0, 0.0, 0.0, 0.3),
Srgba::new(0.0, 0.0, 0.0, 0.6),
Srgba::new(0.0, 0.0, 0.0, 1.0),
]))
.expect("should be able to build translation curve because we pass in valid samples"),
),
);

// Create the animation graph
Expand Down Expand Up @@ -177,7 +200,7 @@ fn setup_menu(
justify_content: JustifyContent::SpaceBetween,
..default()
},
UiImage::new(asset_server.load("images/puzzle.jpg")),
ImageNode::new(asset_server.load("images/puzzle.jpg")),
OnMenuScreen,
))
.id();
Expand Down Expand Up @@ -245,7 +268,7 @@ fn setup_menu(
.with_children(|p| {
// up arrow
p.spawn((
UiImage {
ImageNode {
image: down_arrow.clone(),
flip_y: true,
..default()
Expand Down Expand Up @@ -278,7 +301,7 @@ fn setup_menu(
));
// down arrow
p.spawn((
UiImage::new(down_arrow.clone()),
ImageNode::new(down_arrow.clone()),
Node {
width: Val::Px(30.0),
height: Val::Px(30.0),
Expand Down Expand Up @@ -320,7 +343,7 @@ fn setup_menu(
.with_children(|p| {
// up arrow
p.spawn((
UiImage {
ImageNode {
image: down_arrow.clone(),
flip_y: true,
..default()
Expand Down Expand Up @@ -353,7 +376,7 @@ fn setup_menu(
));
// down arrow
p.spawn((
UiImage::new(down_arrow.clone()),
ImageNode::new(down_arrow.clone()),
Node {
width: Val::Px(30.0),
height: Val::Px(30.0),
Expand Down Expand Up @@ -557,7 +580,7 @@ fn show_origin_image(
) {
commands
.entity(*container)
.insert(UiImage::new(origin_image.0.clone()));
.insert(ImageNode::new(origin_image.0.clone()));
}

fn show_images(
Expand All @@ -568,7 +591,7 @@ fn show_images(
for image in loaded_images.0.iter() {
let child_node = commands
.spawn((
UiImage::new(image.clone()),
ImageNode::new(image.clone()),
Node {
margin: UiRect::axes(Val::Px(10.0), Val::Px(0.0)),
..default()
Expand All @@ -583,7 +606,7 @@ fn show_images(
|trigger: Trigger<Pointer<Click>>,
mut origin_image: ResMut<OriginImage>,
dragging: Res<Dragging>,
image_query: Query<&UiImage>| {
image_query: Query<&ImageNode>| {
if dragging.0 {
return;
}
Expand Down

0 comments on commit 90f769e

Please sign in to comment.