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 <KDecayMusic@protonmail.com>
This commit is contained in:
KDecay 2022-04-24 22:57:02 +00:00
parent dd57a94155
commit 00c6acc0cc
5 changed files with 17 additions and 17 deletions

View File

@ -1,4 +1,4 @@
use crate::{ElementState, Input}; use crate::{ButtonState, Input};
use bevy_ecs::event::EventReader; use bevy_ecs::event::EventReader;
use bevy_ecs::system::ResMut; use bevy_ecs::system::ResMut;
@ -7,7 +7,7 @@ use bevy_ecs::system::ResMut;
pub struct KeyboardInput { pub struct KeyboardInput {
pub scan_code: u32, pub scan_code: u32,
pub key_code: Option<KeyCode>, pub key_code: Option<KeyCode>,
pub state: ElementState, pub state: ButtonState,
} }
/// Updates the `Input<KeyCode>` resource with the latest `KeyboardInput` events /// Updates the `Input<KeyCode>` resource with the latest `KeyboardInput` events
@ -24,8 +24,8 @@ pub fn keyboard_input_system(
} = event } = event
{ {
match state { match state {
ElementState::Pressed => keyboard_input.press(*key_code), ButtonState::Pressed => keyboard_input.press(*key_code),
ElementState::Released => keyboard_input.release(*key_code), ButtonState::Released => keyboard_input.release(*key_code),
} }
} }
} }

View File

@ -90,13 +90,13 @@ impl Plugin for InputPlugin {
/// The current "press" state of an element /// The current "press" state of an element
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum ElementState { pub enum ButtonState {
Pressed, Pressed,
Released, Released,
} }
impl ElementState { impl ButtonState {
pub fn is_pressed(&self) -> bool { pub fn is_pressed(&self) -> bool {
matches!(self, ElementState::Pressed) matches!(self, ButtonState::Pressed)
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{ElementState, Input}; use crate::{ButtonState, Input};
use bevy_ecs::{event::EventReader, system::ResMut}; use bevy_ecs::{event::EventReader, system::ResMut};
use bevy_math::Vec2; use bevy_math::Vec2;
@ -6,7 +6,7 @@ use bevy_math::Vec2;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MouseButtonInput { pub struct MouseButtonInput {
pub button: MouseButton, pub button: MouseButton,
pub state: ElementState, pub state: ButtonState,
} }
/// A button on a mouse device /// A button on a mouse device
@ -49,8 +49,8 @@ pub fn mouse_button_input_system(
mouse_button_input.clear(); mouse_button_input.clear();
for event in mouse_button_input_events.iter() { for event in mouse_button_input_events.iter() {
match event.state { match event.state {
ElementState::Pressed => mouse_button_input.press(event.button), ButtonState::Pressed => mouse_button_input.press(event.button),
ElementState::Released => mouse_button_input.release(event.button), ButtonState::Released => mouse_button_input.release(event.button),
} }
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{ElementState, KeyCode, KeyboardInput}; use crate::{ButtonState, KeyCode, KeyboardInput};
use bevy_app::AppExit; use bevy_app::AppExit;
use bevy_ecs::prelude::{EventReader, EventWriter}; use bevy_ecs::prelude::{EventReader, EventWriter};
@ -14,7 +14,7 @@ pub fn exit_on_esc_system(
) { ) {
for event in keyboard_input_events.iter() { for event in keyboard_input_events.iter() {
if let Some(key_code) = event.key_code { 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(); app_exit_events.send_default();
} }
} }

View File

@ -2,7 +2,7 @@ use bevy_input::{
keyboard::{KeyCode, KeyboardInput}, keyboard::{KeyCode, KeyboardInput},
mouse::MouseButton, mouse::MouseButton,
touch::{ForceTouch, TouchInput, TouchPhase}, touch::{ForceTouch, TouchInput, TouchPhase},
ElementState, ButtonState,
}; };
use bevy_math::Vec2; use bevy_math::Vec2;
use bevy_window::CursorIcon; 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 { match element_state {
winit::event::ElementState::Pressed => ElementState::Pressed, winit::event::ElementState::Pressed => ButtonState::Pressed,
winit::event::ElementState::Released => ElementState::Released, winit::event::ElementState::Released => ButtonState::Released,
} }
} }