 d70595b667
			
		
	
	
		d70595b667
		
			
		
	
	
	
	
		
			
			# Objective - Fixes #6370 - Closes #6581 ## Solution - Added the following lints to the workspace: - `std_instead_of_core` - `std_instead_of_alloc` - `alloc_instead_of_core` - Used `cargo +nightly fmt` with [item level use formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Item%5C%3A) to split all `use` statements into single items. - Used `cargo clippy --workspace --all-targets --all-features --fix --allow-dirty` to _attempt_ to resolve the new linting issues, and intervened where the lint was unable to resolve the issue automatically (usually due to needing an `extern crate alloc;` statement in a crate root). - Manually removed certain uses of `std` where negative feature gating prevented `--all-features` from finding the offending uses. - Used `cargo +nightly fmt` with [crate level use formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Crate%5C%3A) to re-merge all `use` statements matching Bevy's previous styling. - Manually fixed cases where the `fmt` tool could not re-merge `use` statements due to conditional compilation attributes. ## Testing - Ran CI locally ## Migration Guide The MSRV is now 1.81. Please update to this version or higher. ## Notes - This is a _massive_ change to try and push through, which is why I've outlined the semi-automatic steps I used to create this PR, in case this fails and someone else tries again in the future. - Making this change has no impact on user code, but does mean Bevy contributors will be warned to use `core` and `alloc` instead of `std` where possible. - This lint is a critical first step towards investigating `no_std` options for Bevy. --------- Co-authored-by: François Mockers <francois.mockers@vleue.com>
		
			
				
	
	
		
			252 lines
		
	
	
		
			8.3 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			252 lines
		
	
	
		
			8.3 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! The mouse input functionality.
 | |
| 
 | |
| use crate::{ButtonInput, ButtonState};
 | |
| use bevy_ecs::{
 | |
|     change_detection::DetectChangesMut,
 | |
|     entity::Entity,
 | |
|     event::{Event, EventReader},
 | |
|     system::{ResMut, Resource},
 | |
| };
 | |
| use bevy_math::Vec2;
 | |
| #[cfg(feature = "bevy_reflect")]
 | |
| use {
 | |
|     bevy_ecs::reflect::ReflectResource,
 | |
|     bevy_reflect::{std_traits::ReflectDefault, Reflect},
 | |
| };
 | |
| 
 | |
| #[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
 | |
| use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
 | |
| 
 | |
| /// A mouse button input event.
 | |
| ///
 | |
| /// This event is the translated version of the `WindowEvent::MouseInput` from the `winit` crate.
 | |
| ///
 | |
| /// ## Usage
 | |
| ///
 | |
| /// The event is read inside of the [`mouse_button_input_system`]
 | |
| /// to update the [`ButtonInput<MouseButton>`] resource.
 | |
| #[derive(Event, Debug, Clone, Copy, PartialEq, Eq)]
 | |
| #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
 | |
| #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
 | |
| #[cfg_attr(
 | |
|     all(feature = "serialize", feature = "bevy_reflect"),
 | |
|     reflect(Serialize, Deserialize)
 | |
| )]
 | |
| pub struct MouseButtonInput {
 | |
|     /// The mouse button assigned to the event.
 | |
|     pub button: MouseButton,
 | |
|     /// The pressed state of the button.
 | |
|     pub state: ButtonState,
 | |
|     /// Window that received the input.
 | |
|     pub window: Entity,
 | |
| }
 | |
| 
 | |
| /// A button on a mouse device.
 | |
| ///
 | |
| /// ## Usage
 | |
| ///
 | |
| /// It is used as the generic `T` value of an [`ButtonInput`] to create a `bevy`
 | |
| /// resource.
 | |
| ///
 | |
| /// ## Updating
 | |
| ///
 | |
| /// The resource is updated inside of the [`mouse_button_input_system`].
 | |
| #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
 | |
| #[cfg_attr(
 | |
|     feature = "bevy_reflect",
 | |
|     derive(Reflect),
 | |
|     reflect(Debug, Hash, PartialEq)
 | |
| )]
 | |
| #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
 | |
| #[cfg_attr(
 | |
|     all(feature = "serialize", feature = "bevy_reflect"),
 | |
|     reflect(Serialize, Deserialize)
 | |
| )]
 | |
| pub enum MouseButton {
 | |
|     /// The left mouse button.
 | |
|     Left,
 | |
|     /// The right mouse button.
 | |
|     Right,
 | |
|     /// The middle mouse button.
 | |
|     Middle,
 | |
|     /// The back mouse button.
 | |
|     Back,
 | |
|     /// The forward mouse button.
 | |
|     Forward,
 | |
|     /// Another mouse button with the associated number.
 | |
|     Other(u16),
 | |
| }
 | |
| 
 | |
| /// An event reporting the change in physical position of a pointing device.
 | |
| ///
 | |
| /// This represents raw, unfiltered physical motion.
 | |
| /// It is the translated version of [`DeviceEvent::MouseMotion`] from the `winit` crate.
 | |
| ///
 | |
| /// All pointing devices connected to a single machine at the same time can emit the event independently.
 | |
| /// However, the event data does not make it possible to distinguish which device it is referring to.
 | |
| ///
 | |
| /// [`DeviceEvent::MouseMotion`]: https://docs.rs/winit/latest/winit/event/enum.DeviceEvent.html#variant.MouseMotion
 | |
| #[derive(Event, Debug, Clone, Copy, PartialEq)]
 | |
| #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
 | |
| #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
 | |
| #[cfg_attr(
 | |
|     all(feature = "serialize", feature = "bevy_reflect"),
 | |
|     reflect(Serialize, Deserialize)
 | |
| )]
 | |
| pub struct MouseMotion {
 | |
|     /// The change in the position of the pointing device since the last event was sent.
 | |
|     pub delta: Vec2,
 | |
| }
 | |
| 
 | |
| /// The scroll unit.
 | |
| ///
 | |
| /// Describes how a value of a [`MouseWheel`] event has to be interpreted.
 | |
| ///
 | |
| /// The value of the event can either be interpreted as the amount of lines or the amount of pixels
 | |
| /// to scroll.
 | |
| #[derive(Debug, Clone, Copy, Eq, PartialEq)]
 | |
| #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
 | |
| #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
 | |
| #[cfg_attr(
 | |
|     all(feature = "serialize", feature = "bevy_reflect"),
 | |
|     reflect(Serialize, Deserialize)
 | |
| )]
 | |
| pub enum MouseScrollUnit {
 | |
|     /// The line scroll unit.
 | |
|     ///
 | |
|     /// The delta of the associated [`MouseWheel`] event corresponds
 | |
|     /// to the amount of lines or rows to scroll.
 | |
|     Line,
 | |
|     /// The pixel scroll unit.
 | |
|     ///
 | |
|     /// The delta of the associated [`MouseWheel`] event corresponds
 | |
|     /// to the amount of pixels to scroll.
 | |
|     Pixel,
 | |
| }
 | |
| 
 | |
| /// A mouse wheel event.
 | |
| ///
 | |
| /// This event is the translated version of the `WindowEvent::MouseWheel` from the `winit` crate.
 | |
| #[derive(Event, Debug, Clone, Copy, PartialEq)]
 | |
| #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
 | |
| #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
 | |
| #[cfg_attr(
 | |
|     all(feature = "serialize", feature = "bevy_reflect"),
 | |
|     reflect(Serialize, Deserialize)
 | |
| )]
 | |
| pub struct MouseWheel {
 | |
|     /// The mouse scroll unit.
 | |
|     pub unit: MouseScrollUnit,
 | |
|     /// The horizontal scroll value.
 | |
|     pub x: f32,
 | |
|     /// The vertical scroll value.
 | |
|     pub y: f32,
 | |
|     /// Window that received the input.
 | |
|     pub window: Entity,
 | |
| }
 | |
| 
 | |
| /// Updates the [`ButtonInput<MouseButton>`] resource with the latest [`MouseButtonInput`] events.
 | |
| ///
 | |
| /// ## Differences
 | |
| ///
 | |
| /// The main difference between the [`MouseButtonInput`] event and the [`ButtonInput<MouseButton>`] resource is that
 | |
| /// the latter has convenient functions like [`ButtonInput::pressed`], [`ButtonInput::just_pressed`] and [`ButtonInput::just_released`].
 | |
| pub fn mouse_button_input_system(
 | |
|     mut mouse_button_input: ResMut<ButtonInput<MouseButton>>,
 | |
|     mut mouse_button_input_events: EventReader<MouseButtonInput>,
 | |
| ) {
 | |
|     mouse_button_input.bypass_change_detection().clear();
 | |
|     for event in mouse_button_input_events.read() {
 | |
|         match event.state {
 | |
|             ButtonState::Pressed => mouse_button_input.press(event.button),
 | |
|             ButtonState::Released => mouse_button_input.release(event.button),
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// Tracks how much the mouse has moved every frame.
 | |
| ///
 | |
| /// This resource is reset to zero every frame.
 | |
| ///
 | |
| /// This resource sums the total [`MouseMotion`] events received this frame.
 | |
| #[derive(Resource, Debug, Clone, Copy, PartialEq, Default)]
 | |
| #[cfg_attr(
 | |
|     feature = "bevy_reflect",
 | |
|     derive(Reflect),
 | |
|     reflect(Debug, Default, Resource, PartialEq)
 | |
| )]
 | |
| #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
 | |
| #[cfg_attr(
 | |
|     all(feature = "serialize", feature = "bevy_reflect"),
 | |
|     reflect(Serialize, Deserialize)
 | |
| )]
 | |
| pub struct AccumulatedMouseMotion {
 | |
|     /// The change in mouse position.
 | |
|     pub delta: Vec2,
 | |
| }
 | |
| 
 | |
| /// Tracks how much the mouse has scrolled every frame.
 | |
| ///
 | |
| /// This resource is reset to zero every frame.
 | |
| ///
 | |
| /// This resource sums the total [`MouseWheel`] events received this frame.
 | |
| #[derive(Resource, Debug, Clone, Copy, PartialEq)]
 | |
| #[cfg_attr(
 | |
|     feature = "bevy_reflect",
 | |
|     derive(Reflect),
 | |
|     reflect(Debug, Default, Resource, PartialEq)
 | |
| )]
 | |
| #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
 | |
| #[cfg_attr(
 | |
|     all(feature = "serialize", feature = "bevy_reflect"),
 | |
|     reflect(Serialize, Deserialize)
 | |
| )]
 | |
| pub struct AccumulatedMouseScroll {
 | |
|     /// The mouse scroll unit.
 | |
|     /// If this value changes while scrolling, then the
 | |
|     /// result of the accumulation could be incorrect
 | |
|     pub unit: MouseScrollUnit,
 | |
|     /// The change in scroll position.
 | |
|     pub delta: Vec2,
 | |
| }
 | |
| 
 | |
| impl Default for AccumulatedMouseScroll {
 | |
|     fn default() -> Self {
 | |
|         Self {
 | |
|             unit: MouseScrollUnit::Line,
 | |
|             delta: Vec2::ZERO,
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// Updates the [`AccumulatedMouseMotion`] resource using the [`MouseMotion`] event.
 | |
| /// The value of [`AccumulatedMouseMotion`] is reset to zero every frame
 | |
| pub fn accumulate_mouse_motion_system(
 | |
|     mut mouse_motion_event: EventReader<MouseMotion>,
 | |
|     mut accumulated_mouse_motion: ResMut<AccumulatedMouseMotion>,
 | |
| ) {
 | |
|     let mut delta = Vec2::ZERO;
 | |
|     for event in mouse_motion_event.read() {
 | |
|         delta += event.delta;
 | |
|     }
 | |
|     accumulated_mouse_motion.delta = delta;
 | |
| }
 | |
| 
 | |
| /// Updates the [`AccumulatedMouseScroll`] resource using the [`MouseWheel`] event.
 | |
| /// The value of [`AccumulatedMouseScroll`] is reset to zero every frame
 | |
| pub fn accumulate_mouse_scroll_system(
 | |
|     mut mouse_scroll_event: EventReader<MouseWheel>,
 | |
|     mut accumulated_mouse_scroll: ResMut<AccumulatedMouseScroll>,
 | |
| ) {
 | |
|     let mut delta = Vec2::ZERO;
 | |
|     let mut unit = MouseScrollUnit::Line;
 | |
|     for event in mouse_scroll_event.read() {
 | |
|         if event.unit != unit {
 | |
|             unit = event.unit;
 | |
|         }
 | |
|         delta += Vec2::new(event.x, event.y);
 | |
|     }
 | |
|     accumulated_mouse_scroll.delta = delta;
 | |
|     accumulated_mouse_scroll.unit = unit;
 | |
| }
 |