Skip to content
Snippets Groups Projects
Verified Commit 3ce73ff9 authored by Louis's avatar Louis :fire:
Browse files

Update to bevy 0.10

parent 39418ff4
No related branches found
No related tags found
No related merge requests found
#[cfg(target_arch = "wasm32")]
mod __plugin {
use bevy::app::{App, Plugin, CoreStage};
use bevy::prelude::*;
pub fn handle_startup_fullscreen() {
if micro_bevy_web_utils::bindings::is_touch_device() {
micro_bevy_web_utils::bindings::make_selector_fullscreen("canvas".to_string());
......@@ -12,12 +12,11 @@ mod __plugin {
pub struct WebPlugin;
impl Plugin for WebPlugin {
fn build(&self, app: &mut App) {
app.add_startup_system(handle_startup_fullscreen)
.add_system_to_stage(
CoreStage::First,
micro_bevy_web_utils::bevy::emit_touch_events,
);
}
app.add_startup_system(handle_startup_fullscreen)
.add_system(
micro_bevy_web_utils::bevy::emit_touch_events.in_base_set(CoreSet::First),
);
}
}
}
......@@ -30,4 +29,4 @@ mod __plugin {
}
}
pub use __plugin::WebPlugin;
\ No newline at end of file
pub use __plugin::WebPlugin;
use bevy::ecs::system::SystemParam;
use bevy::math::Vec4Swizzles;
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
// use advent_ui::prelude::Units;
// use num_traits::AsPrimitive;
use crate::system::camera::GameCamera;
#[derive(PartialEq, Default, Debug, Copy, Clone, Resource)]
pub struct WindowUnits {
pub inner_width: f32,
pub inner_height: f32,
pub vw: f32,
pub vh: f32,
}
// TODO: Enable if using advent_ui
// impl WindowUnits {
// pub fn vw_units<T: AsPrimitive<f32>>(&self, units: T) -> Units {
// Units::Pixels(self.vw * units.as_())
// }
// pub fn vh_units<T: AsPrimitive<f32>>(&self, units: T) -> Units {
// Units::Pixels(self.vh * units.as_())
// }
// }
pub fn update_window_units(window: WindowManager, mut units: ResMut<WindowUnits>) {
let next_units = window.get_units();
if next_units != *units {
*units = next_units;
}
}
pub struct WindowSyncPlugin;
impl Plugin for WindowSyncPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<WindowUnits>()
.add_system(update_window_units.in_base_set(CoreSet::First));
}
}
/// A struct that provides several convenience methods for getting mouse and
/// window related information
#[derive(SystemParam)]
pub struct WindowManager<'w, 's> {
mouse: Res<'w, Input<MouseButton>>,
windows: Res<'w, Windows>,
cam_query: ParamSet<'w, 's, (Query<'w, 's, &'static Transform, With<GameCamera>>,)>,
windows: Query<'w, 's, &'static Window, With<PrimaryWindow>>,
camera: Query<'w, 's, (&'static OrthographicProjection, &'static Transform), With<GameCamera>>,
}
impl<'w, 's> WindowManager<'w, 's> {
/// Conditionally run a function with the primary window. The function will not
/// run if the primary window does not exist - typically this is the desired behaviour.
///
/// ## Arguments
/// - `func`: an `FnOnce` callback that is given a [`bevy::prelude::Window`]
pub fn with_primary_window<Func: FnOnce(&Window)>(&self, func: Func) {
match self.windows.get_primary() {
Some(window) => func(window),
None => {}
}
}
pub fn get_primary_window(&self) -> Option<&Window> {
self.windows.get_primary()
self.windows.get_single().ok()
}
pub fn get_mouse_press(&mut self) -> Option<Vec2> {
if self.mouse.just_pressed(MouseButton::Left) {
if let Some(window) = self.windows.get_primary() {
if let Some(window) = self.get_primary_window() {
if let Some(position) = window.cursor_position() {
let window_size = Vec2::new(window.width() as f32, window.height() as f32);
let adjusted_position = position - window_size / 2.0;
if let Ok(camera_transform) = self.cam_query.p0().get_single() {
if let Ok((projection, camera_transform)) = self.camera.get_single() {
let window_size = Vec2::new(window.width() as f32, window.height() as f32);
let projection_size = Vec2::new(
projection.area.max.x - projection.area.min.x,
projection.area.max.y - projection.area.min.y,
);
let adjusted_position = position - window_size / 2.0;
let scale = window_size / projection_size;
let adjusted_position = adjusted_position / scale;
let world_position = camera_transform.compute_matrix()
* adjusted_position.extend(0.0).extend(1.0);
return Some(Vec2::new(world_position.x, world_position.y));
return Some(world_position.xy());
}
}
}
}
None
}
pub fn get_units(&self) -> WindowUnits {
let window = self.get_primary_window();
if let Some(window) = window {
let inner_width = window.width();
let real_width = window.physical_width();
let inner_height = window.height();
WindowUnits {
inner_width,
inner_height,
vh: inner_height / 100.0,
vw: inner_width / 100.0,
}
} else {
WindowUnits {
inner_height: 0.0,
inner_width: 0.0,
vw: 0.0,
vh: 0.0,
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment