From 00c6acc0cc115ffc9d2dc1fbc910083f217d83e2 Mon Sep 17 00:00:00 2001 From: KDecay Date: Sun, 24 Apr 2022 22:57:02 +0000 Subject: [PATCH] Rename `ElementState` to `ButtonState` (#4314) # Objective - Part of the splitting process of #3692. ## Solution - Rename `ElementState` to `ButtonState` ## Reasons - The old name was too generic. - If something can be pressed it is automatically button-like (thanks to @alice-i-cecile for bringing it up in #3692). - The reason it is called `ElementState` is because that's how `winit` calls it. - It is used to define if a keyboard or mouse **button** is pressed or not. - Discussion in #3692. ## Changelog ### Changed - The `ElementState` type received a rename and is now called `ButtonState`. ## Migration Guide - The `ElementState` type received a rename and is now called `ButtonState`. To migrate you just have to change every occurrence of `ElementState` to `ButtonState`. Co-authored-by: KDecay --- crates/bevy_input/src/keyboard.rs | 8 ++++---- crates/bevy_input/src/lib.rs | 6 +++--- crates/bevy_input/src/mouse.rs | 8 ++++---- crates/bevy_input/src/system.rs | 4 ++-- crates/bevy_winit/src/converters.rs | 8 ++++---- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 3a652330fc6ca..e829b90e478dc 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -1,4 +1,4 @@ -use crate::{ElementState, Input}; +use crate::{ButtonState, Input}; use bevy_ecs::event::EventReader; use bevy_ecs::system::ResMut; @@ -7,7 +7,7 @@ use bevy_ecs::system::ResMut; pub struct KeyboardInput { pub scan_code: u32, pub key_code: Option, - pub state: ElementState, + pub state: ButtonState, } /// Updates the `Input` resource with the latest `KeyboardInput` events @@ -24,8 +24,8 @@ pub fn keyboard_input_system( } = event { match state { - ElementState::Pressed => keyboard_input.press(*key_code), - ElementState::Released => keyboard_input.release(*key_code), + ButtonState::Pressed => keyboard_input.press(*key_code), + ButtonState::Released => keyboard_input.release(*key_code), } } } diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index af4d1dd461429..142f1883550cc 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -90,13 +90,13 @@ impl Plugin for InputPlugin { /// The current "press" state of an element #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -pub enum ElementState { +pub enum ButtonState { Pressed, Released, } -impl ElementState { +impl ButtonState { pub fn is_pressed(&self) -> bool { - matches!(self, ElementState::Pressed) + matches!(self, ButtonState::Pressed) } } diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index 10e32932faeec..68f1540bb27fb 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -1,4 +1,4 @@ -use crate::{ElementState, Input}; +use crate::{ButtonState, Input}; use bevy_ecs::{event::EventReader, system::ResMut}; use bevy_math::Vec2; @@ -6,7 +6,7 @@ use bevy_math::Vec2; #[derive(Debug, Clone)] pub struct MouseButtonInput { pub button: MouseButton, - pub state: ElementState, + pub state: ButtonState, } /// A button on a mouse device @@ -49,8 +49,8 @@ pub fn mouse_button_input_system( mouse_button_input.clear(); for event in mouse_button_input_events.iter() { match event.state { - ElementState::Pressed => mouse_button_input.press(event.button), - ElementState::Released => mouse_button_input.release(event.button), + ButtonState::Pressed => mouse_button_input.press(event.button), + ButtonState::Released => mouse_button_input.release(event.button), } } } diff --git a/crates/bevy_input/src/system.rs b/crates/bevy_input/src/system.rs index 753115bdebcdb..d21fd70ee390f 100644 --- a/crates/bevy_input/src/system.rs +++ b/crates/bevy_input/src/system.rs @@ -1,4 +1,4 @@ -use crate::{ElementState, KeyCode, KeyboardInput}; +use crate::{ButtonState, KeyCode, KeyboardInput}; use bevy_app::AppExit; use bevy_ecs::prelude::{EventReader, EventWriter}; @@ -14,7 +14,7 @@ pub fn exit_on_esc_system( ) { for event in keyboard_input_events.iter() { if let Some(key_code) = event.key_code { - if event.state == ElementState::Pressed && key_code == KeyCode::Escape { + if event.state == ButtonState::Pressed && key_code == KeyCode::Escape { app_exit_events.send_default(); } } diff --git a/crates/bevy_winit/src/converters.rs b/crates/bevy_winit/src/converters.rs index e1e7451d07dd9..3c131ad470c9f 100644 --- a/crates/bevy_winit/src/converters.rs +++ b/crates/bevy_winit/src/converters.rs @@ -2,7 +2,7 @@ use bevy_input::{ keyboard::{KeyCode, KeyboardInput}, mouse::MouseButton, touch::{ForceTouch, TouchInput, TouchPhase}, - ElementState, + ButtonState, }; use bevy_math::Vec2; use bevy_window::CursorIcon; @@ -15,10 +15,10 @@ pub fn convert_keyboard_input(keyboard_input: &winit::event::KeyboardInput) -> K } } -pub fn convert_element_state(element_state: winit::event::ElementState) -> ElementState { +pub fn convert_element_state(element_state: winit::event::ElementState) -> ButtonState { match element_state { - winit::event::ElementState::Pressed => ElementState::Pressed, - winit::event::ElementState::Released => ElementState::Released, + winit::event::ElementState::Pressed => ButtonState::Pressed, + winit::event::ElementState::Released => ButtonState::Released, } }