Skip to content

Commit

Permalink
Bevy 0.14.0-rc.2, fix off-by-one in buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed Jun 13, 2024
1 parent 0b51179 commit 5693d13
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 68 deletions.
15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ state = ["dep:seldom_state"]
[dependencies]
bevy_turborand = { version = "0.8.1", optional = true }
line_drawing = { version = "1.0", optional = true }
seldom_fn_plugin = "0.6.0"
seldom_map_nav = { version = "0.6.0", optional = true }
seldom_map_nav = { git = "https://github.com/Seldom-SE/seldom_map_nav", rev = "bfa6889", optional = true }
seldom_pixel_macros = { version = "0.1.0", path = "macros" }
seldom_state = { version = "0.10.0", optional = true }
seldom_state = { git = "https://github.com/Seldom-SE/seldom_state", rev = "d0abfd8", optional = true }

[dependencies.bevy]
version = "0.13.0"
version = "0.14.0-rc.2"
default-features = false
features = ["bevy_asset", "bevy_core_pipeline", "bevy_render", "bevy_sprite"]

[dev-dependencies]
bevy = { version = "0.13.0", features = ["png"] }
leafwing-input-manager = "0.13.3"
bevy = { version = "0.14.0-rc.2", features = ["png"] }
leafwing-input-manager = { git = "https://github.com/Leafwing-Studios/leafwing-input-manager", rev = "a603533" }
rand = "0.8.5"
seldom_state = { version = "0.10.0", features = ["leafwing_input"] }
seldom_state = { git = "https://github.com/Seldom-SE/seldom_state", rev = "d0abfd8", features = [
"leafwing_input",
] }

[[example]]
name = "line"
Expand Down
2 changes: 1 addition & 1 deletion src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
text::PxTypefaceData,
};

pub(crate) fn animation_plugin(app: &mut App) {
pub(crate) fn plug(app: &mut App) {
app.configure_sets(
PostUpdate,
PxSet::FinishAnimations
Expand Down
20 changes: 10 additions & 10 deletions src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use bevy::{
reflect::TypePath,
};

use seldom_fn_plugin::FnPluginExt;

use crate::map::PxTilesetData;
use crate::{
filter::PxFilterData,
Expand All @@ -22,18 +20,20 @@ use crate::{

use self::sealed::{PxAssetDataSealed, PxAssetTraitSealed};

pub(crate) fn asset_plugin(app: &mut App) {
app.configure_sets(
pub(crate) fn plug(app: &mut App) {
app.add_plugins((
asset_plug::<PxSpriteData>,
asset_plug::<PxTypefaceData>,
asset_plug::<PxFilterData>,
asset_plug::<PxTilesetData>,
))
.configure_sets(
PostUpdate,
PxSet::LoadAssets.before(PxSet::Draw).in_set(PxSet::Loaded),
)
.fn_plugin(px_asset_plugin::<PxSpriteData>)
.fn_plugin(px_asset_plugin::<PxTypefaceData>)
.fn_plugin(px_asset_plugin::<PxFilterData>)
.fn_plugin(px_asset_plugin::<PxTilesetData>);
);
}

fn px_asset_plugin<D: PxAssetData>(app: &mut App) {
fn asset_plug<D: PxAssetData>(app: &mut App) {
app.init_asset::<PxAsset<D>>()
.init_resource::<LoadingAssets<D>>()
.add_systems(PostUpdate, D::load.in_set(PxSet::LoadAssets));
Expand Down
4 changes: 2 additions & 2 deletions src/button.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{cursor::PxCursorPosition, math::RectExt, prelude::*, set::PxSet};

pub(crate) fn button_plugin(app: &mut App) {
pub(crate) fn plug(app: &mut App) {
app.init_resource::<PxEnableButtons>()
.add_systems(
PreUpdate,
Expand Down Expand Up @@ -191,7 +191,7 @@ fn interact_buttons(
};

if IRect::pos_size_anchor(**position, bounds.size, *anchor)
.contains(cursor_pos - bounds.offset.as_ivec2())
.contains_exclusive(cursor_pos - bounds.offset.as_ivec2())
{
if hovered.is_none() {
button.insert(PxHover);
Expand Down
2 changes: 1 addition & 1 deletion src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

pub(crate) fn camera_plugin(app: &mut App) {
pub(crate) fn plug(app: &mut App) {
app.init_resource::<PxCamera>();
}

Expand Down
2 changes: 1 addition & 1 deletion src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
set::PxSet,
};

pub(crate) fn cursor_plugin(app: &mut App) {
pub(crate) fn plug(app: &mut App) {
app.init_resource::<PxCursor>()
.init_resource::<PxCursorPosition>()
.add_systems(
Expand Down
46 changes: 14 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,8 @@ mod ui;

use std::{marker::PhantomData, path::PathBuf};

use animation::animation_plugin;
use asset::asset_plugin;

use button::button_plugin;
use camera::camera_plugin;
use cursor::cursor_plugin;
use palette::palette_plugin;
#[cfg(feature = "particle")]
use particle::particle_plugin;
use position::{position_plugin, PxLayer};
use position::PxLayer;
use prelude::*;
use screen::screen_plugin;
use seldom_fn_plugin::FnPluginExt;

/// Add to your [`App`] to enable `seldom_pixel`. The type parameter is your custom layer type
/// used for z-ordering. You can make one using [`px_layer`].
Expand All @@ -58,7 +47,18 @@ pub struct PxPlugin<L: PxLayer> {

impl<L: PxLayer> Plugin for PxPlugin<L> {
fn build(&self, app: &mut App) {
app.fn_plugin(px_plugin::<L>(self.screen_size, self.palette_path.clone()));
app.add_plugins((
animation::plug,
asset::plug,
button::plug,
camera::plug,
palette::plug(self.palette_path.clone()),
position::plug,
screen::plug::<L>(self.screen_size),
cursor::plug,
#[cfg(feature = "particle")]
(RngPlugin::default(), particle::plug::<L>),
));
}
}

Expand All @@ -70,25 +70,7 @@ impl<L: PxLayer> PxPlugin<L> {
Self {
screen_size,
palette_path,
_l: default(),
_l: PhantomData,
}
}
}

/// Function called by [`PxPlugin`]. You may instead call it directly or use `seldom_fn_plugin`,
/// which is another crate I maintain.
pub fn px_plugin<L: PxLayer>(screen_size: UVec2, palette_path: PathBuf) -> impl FnOnce(&mut App) {
move |app| {
app.fn_plugin(animation_plugin)
.fn_plugin(asset_plugin)
.fn_plugin(button_plugin)
.fn_plugin(camera_plugin)
.fn_plugin(palette_plugin(palette_path))
.fn_plugin(position_plugin)
.fn_plugin(screen_plugin::<L>(screen_size))
.fn_plugin(cursor_plugin);
#[cfg(feature = "particle")]
app.add_plugins(RngPlugin::default())
.fn_plugin(particle_plugin::<L>);
}
}
4 changes: 2 additions & 2 deletions src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use bevy::{render::render_resource::TextureFormat, utils::HashMap};

use crate::{prelude::*, set::PxSet};

pub(crate) fn palette_plugin(palette_path: PathBuf) -> impl FnOnce(&mut App) {
pub(crate) fn plug(palette_path: PathBuf) -> impl Fn(&mut App) {
move |app| {
app.add_systems(Startup, load_palette(palette_path))
app.add_systems(Startup, load_palette(palette_path.clone()))
.configure_sets(
PreUpdate,
(
Expand Down
2 changes: 1 addition & 1 deletion src/particle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{position::PxLayer, prelude::*, set::PxSet};
// If a day isn't sufficient for your use case, file an issue!
const TIME_OFFSET: Duration = Duration::from_secs(60 * 60 * 24);

pub(crate) fn particle_plugin<L: PxLayer>(app: &mut App) {
pub(crate) fn plug<L: PxLayer>(app: &mut App) {
app.configure_sets(PostUpdate, PxSet::UpdateEmitters.before(PxSet::Draw))
.add_systems(
PostUpdate,
Expand Down
2 changes: 1 addition & 1 deletion src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt::Debug;

use crate::{prelude::*, set::PxSet};

pub(crate) fn position_plugin(app: &mut App) {
pub(crate) fn plug(app: &mut App) {
app.configure_sets(PreUpdate, PxSet::UpdatePosToSubPos)
.add_systems(
PreUpdate,
Expand Down
1 change: 0 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub use crate::{
filter::{PxFilter, PxFilterBundle, PxFilterLayers},
map::{PxMap, PxMapBundle, PxTile, PxTileBundle, PxTileset},
position::{PxAnchor, PxLayer, PxPosition, PxSubPosition, PxVelocity},
px_plugin,
sprite::{PxSprite, PxSpriteBundle},
text::{PxCharacterConfig, PxSeparatorConfig, PxText, PxTextBundle, PxTypeface},
ui::PxRect,
Expand Down
16 changes: 7 additions & 9 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ use crate::{
const SCREEN_SHADER_HANDLE: Handle<Shader> =
Handle::weak_from_u128(0x48CE_4F2C_8B78_5954_08A8_461F_62E1_0E84);

pub(crate) fn screen_plugin<L: PxLayer>(size: UVec2) -> impl FnOnce(&mut App) {
pub(crate) fn plug<L: PxLayer>(size: UVec2) -> impl Fn(&mut App) {
move |app| {
app.world.resource_mut::<Assets<Shader>>().insert(
SCREEN_SHADER_HANDLE,
app.world_mut().resource_mut::<Assets<Shader>>().insert(
SCREEN_SHADER_HANDLE.id(),
Shader::from_wgsl(include_str!("screen.wgsl"), "screen.wgsl"),
);
app.add_plugins(Material2dPlugin::<ScreenMaterial>::default())
Expand Down Expand Up @@ -109,11 +109,10 @@ fn init_screen(
mut images,
mut meshes,
mut screen_materials| {
let mut screen_palette = [default(); 256];
let mut screen_palette = [Vec3::ZERO; 256];

for (i, [r, g, b]) in palette.colors.iter().enumerate() {
let [r, g, b, _] = Color::rgb_u8(*r, *g, *b).as_linear_rgba_f32();
screen_palette[i] = Vec3::new(r, g, b);
screen_palette[i] = Color::srgb_u8(*r, *g, *b).linear().to_vec3();
}

let image = images.add(Image {
Expand Down Expand Up @@ -762,11 +761,10 @@ fn update_screen_palette(
return;
}

let mut screen_palette = [default(); 256];
let mut screen_palette = [Vec3::ZERO; 256];

for (i, [r, g, b]) in palette.colors.iter().enumerate() {
let [r, g, b, _] = Color::rgb_u8(*r, *g, *b).as_linear_rgba_f32();
screen_palette[i] = Vec3::new(r, g, b);
screen_palette[i] = Color::srgb_u8(*r, *g, *b).linear().to_vec3();
}

for screen_material in &screen_materials {
Expand Down

0 comments on commit 5693d13

Please sign in to comment.