Skip to content

Commit

Permalink
Draw in render world
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed Aug 4, 2024
1 parent a17c991 commit 9872fbe
Show file tree
Hide file tree
Showing 20 changed files with 578 additions and 530 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ kiddo = { version = "4.2" }
seldom_singleton = "0.2.0"
bevy_turborand = { version = "0.9.0", optional = true }
seldom_map_nav = { version = "0.7.0", optional = true }
seldom_pixel_macros = { version = "0.1.0", path = "macros" }
seldom_pixel_macros = { version = "0.2.0-dev", path = "macros" }
seldom_state = { version = "0.11.0", optional = true }

[dependencies.bevy]
Expand Down
Binary file added assets/filter/identity.px_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions assets/filter/identity.px_filter.png.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(
meta_format_version: "1.0",
asset: Load(
loader: "seldom_pixel::filter::PxFilterLoader",
settings: (
format: FromExtension,
is_srgb: true,
sampler: Default,
asset_usage: ("MAIN_WORLD | RENDER_WORLD"),
),
),
)
8 changes: 4 additions & 4 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "seldom_pixel_macros"
version = "0.1.0"
version = "0.2.0-dev"
edition = "2021"
categories = [ "game-development" ]
categories = ["game-development"]
description = "Macros for `seldom_pixel`"
keywords = [ "gamedev", "bevy", "graphics", "gui", "2d" ]
keywords = ["gamedev", "bevy", "graphics", "gui", "2d"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/Seldom-SE/seldom_pixel"

Expand All @@ -13,4 +13,4 @@ proc-macro = true

[dependencies]
quote = "1"
syn = "1"
syn = "1"
13 changes: 7 additions & 6 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use quote::quote;
use syn::{Error, Meta};

/// Derives required traits for a layer. Use as `#[px_layer]` on an item. Equivalent to
/// `#[derive(Clone, Component, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]`.
/// `#[derive(ExtractComponent, Component, Ord, PartialOrd, Eq, PartialEq, Clone, Default, Debug)]`.
#[proc_macro_attribute]
pub fn px_layer(args: TokenStream, input: TokenStream) -> TokenStream {
let mut output = TokenStream::from(if !args.is_empty() {
Expand All @@ -23,14 +23,15 @@ pub fn px_layer(args: TokenStream, input: TokenStream) -> TokenStream {
} else {
quote! {
#[derive(
::std::clone::Clone,
::bevy::render::extract_component::ExtractComponent,
::bevy::prelude::Component,
::std::fmt::Debug,
::std::default::Default,
::std::cmp::Eq,
::std::cmp::Ord,
::std::cmp::PartialOrd,
::std::cmp::Eq,
::std::cmp::PartialEq,
::std::cmp::PartialOrd
::std::clone::Clone,
::std::default::Default,
::std::fmt::Debug,
)]
}
});
Expand Down
14 changes: 8 additions & 6 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ use crate::{

pub(crate) fn plug(app: &mut App) {
app.add_plugins(ExtractResourcePlugin::<LastUpdate>::default())
.configure_sets(
PostUpdate,
PxSet::FinishAnimations
.after(PxSet::LoadAssets)
.before(PxSet::Draw),
)
.add_systems(
PostUpdate,
(
Expand Down Expand Up @@ -145,6 +139,14 @@ pub(crate) trait AnimationAsset: Asset {
fn max_frame_count(&self) -> usize;
}

pub(crate) type AnimationComponents = (
&'static PxAnimationDirection,
&'static PxAnimationDuration,
&'static PxAnimationFinishBehavior,
&'static PxAnimationFrameTransition,
&'static PxAnimationStart,
);

static DITHERING: &[u16] = &[
0b0000_0000_0000_0000,
0b1000_0000_0000_0000,
Expand Down
4 changes: 1 addition & 3 deletions src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ pub(crate) fn plug(app: &mut App) {
apply_deferred
.after(PxSet::AddButtonAssets)
.before(PxSet::UpdateButtonAssets),
(update_button_sprites, update_button_filters)
.before(PxSet::Draw)
.in_set(PxSet::UpdateButtonAssets),
(update_button_sprites, update_button_filters).in_set(PxSet::UpdateButtonAssets),
disable_buttons
.run_if(resource_changed::<PxEnableButtons>)
.run_if(resource_equals(PxEnableButtons(false))),
Expand Down
7 changes: 5 additions & 2 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use bevy::render::extract_resource::{ExtractResource, ExtractResourcePlugin};
use bevy::render::{
extract_component::ExtractComponent,
extract_resource::{ExtractResource, ExtractResourcePlugin},
};

use crate::prelude::*;

Expand All @@ -12,7 +15,7 @@ pub(crate) fn plug(app: &mut App) {
pub struct PxCamera(pub IVec2);

/// Determines whether the entity is locked to the camera
#[derive(Clone, Component, Copy, Debug, Default)]
#[derive(ExtractComponent, Component, Clone, Copy, Default, Debug)]
pub enum PxCanvas {
/// The entity is drawn relative to the world, like terrain
#[default]
Expand Down
112 changes: 52 additions & 60 deletions src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
//! Cursor
use bevy::window::PrimaryWindow;
use bevy::{
render::extract_resource::{ExtractResource, ExtractResourcePlugin},
window::PrimaryWindow,
};

use crate::{
filter::PxFilter,
image::PxImageSliceMut,
prelude::*,
screen::{Screen, ScreenMarker},
screen::{screen_scale, Screen},
set::PxSet,
};

pub(crate) fn plug(app: &mut App) {
app.init_resource::<PxCursor>()
.init_resource::<PxCursorPosition>()
.add_systems(
PreUpdate,
update_cursor_position.in_set(PxSet::UpdateCursorPosition),
)
.configure_sets(PostUpdate, PxSet::DrawCursor.after(PxSet::Draw))
.add_systems(
PostUpdate,
(
change_cursor.before(PxSet::DrawCursor),
// draw_cursor.in_set(PxSet::DrawCursor),
),
);
app.add_plugins((
ExtractResourcePlugin::<PxCursor>::default(),
ExtractResourcePlugin::<PxCursorPosition>::default(),
ExtractResourcePlugin::<CursorState>::default(),
))
.init_resource::<PxCursor>()
.init_resource::<PxCursorPosition>()
.add_systems(
PreUpdate,
update_cursor_position.in_set(PxSet::UpdateCursorPosition),
)
.add_systems(PostUpdate, change_cursor);
}

/// Resource that defines whether to use an in-game cursor
#[derive(Debug, Default, Resource)]
#[derive(ExtractResource, Resource, Clone, Default, Debug)]
pub enum PxCursor {
/// Use the operating system's cursor
#[default]
Expand All @@ -48,38 +48,45 @@ pub enum PxCursor {
/// Resource marking the cursor's position. Measured in pixels from the bottom-left of the screen.
/// Contains [`None`] if the cursor is off-screen. The cursor's world position
/// is the contained value plus [`PxCamera`]'s contained value.
#[derive(Debug, Default, Deref, DerefMut, Resource)]
#[derive(ExtractResource, Resource, Deref, DerefMut, Clone, Default, Debug)]
pub struct PxCursorPosition(pub Option<UVec2>);

fn update_cursor_position(
mut move_events: EventReader<CursorMoved>,
mut leave_events: EventReader<CursorLeft>,
screens: Query<&Transform, With<ScreenMarker>>,
cameras: Query<(&Camera, &GlobalTransform)>,
screen: Res<Screen>,
mut position: ResMut<PxCursorPosition>,
windows: Query<&Window>,
) {
let Ok(screen_tf) = screens.get_single() else {
return;
};

if leave_events.read().next().is_some() {
if leave_events.read().last().is_some() {
**position = None;
return;
}

let Some(event) = move_events.read().last() else {
return;
};

let Ok((camera, tf)) = cameras.get_single() else {
return;
};

let Ok(window) = windows.get_single() else {
return;
};

let Some(new_position) = camera.viewport_to_world_2d(tf, event.position) else {
**position = None;
return;
};
let new_position = new_position / screen_tf.scale.truncate() * screen.computed_size.as_vec2()

let new_position = new_position
/ screen_scale(
screen.computed_size,
Vec2::new(window.width(), window.height()),
)
* screen.computed_size.as_vec2()
+ screen.computed_size.as_vec2() / 2.;

**position = (new_position.cmpge(Vec2::ZERO).all()
Expand Down Expand Up @@ -107,40 +114,25 @@ fn change_cursor(
};
}

fn draw_cursor(
screen: Res<Screen>,
cursor: Res<PxCursor>,
cursor_pos: Res<PxCursorPosition>,
filters: Res<Assets<PxFilter>>,
mouse: Res<ButtonInput<MouseButton>>,
mut images: ResMut<Assets<Image>>,
) {
if let PxCursor::Filter {
idle,
left_click,
right_click,
} = &*cursor
{
if let Some(cursor_pos) = **cursor_pos {
if let Some(PxFilter(filter)) = filters.get(if mouse.pressed(MouseButton::Left) {
left_click
} else if mouse.pressed(MouseButton::Right) {
right_click
} else {
idle
}) {
let mut image =
PxImageSliceMut::from_image_mut(images.get_mut(&screen.image).unwrap());

if let Some(pixel) = image.get_pixel_mut(IVec2::new(
cursor_pos.x as i32,
image.height() as i32 - 1 - cursor_pos.y as i32,
)) {
*pixel = filter
.get_pixel(IVec2::new(*pixel as i32, 0))
.expect("filter is incorrect size");
}
}
#[derive(Resource)]
pub(crate) enum CursorState {
Idle,
Left,
Right,
}

impl ExtractResource for CursorState {
type Source = ButtonInput<MouseButton>;

fn extract_resource(source: &ButtonInput<MouseButton>) -> Self {
use CursorState::*;

if source.pressed(MouseButton::Left) {
Left
} else if source.pressed(MouseButton::Right) {
Right
} else {
Idle
}
}
}
Loading

0 comments on commit 9872fbe

Please sign in to comment.