Skip to content

Commit

Permalink
Rename ElementState to ButtonState (#4314)
Browse files Browse the repository at this point in the history
# 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>
  • Loading branch information
KDecay and KDecay committed Apr 24, 2022
1 parent dd57a94 commit 00c6acc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions crates/bevy_input/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ElementState, Input};
use crate::{ButtonState, Input};
use bevy_ecs::event::EventReader;
use bevy_ecs::system::ResMut;

Expand All @@ -7,7 +7,7 @@ use bevy_ecs::system::ResMut;
pub struct KeyboardInput {
pub scan_code: u32,
pub key_code: Option<KeyCode>,
pub state: ElementState,
pub state: ButtonState,
}

/// Updates the `Input<KeyCode>` resource with the latest `KeyboardInput` events
Expand All @@ -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),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 4 additions & 4 deletions crates/bevy_input/src/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{ElementState, Input};
use crate::{ButtonState, Input};
use bevy_ecs::{event::EventReader, system::ResMut};
use bevy_math::Vec2;

/// A mouse button input event
#[derive(Debug, Clone)]
pub struct MouseButtonInput {
pub button: MouseButton,
pub state: ElementState,
pub state: ButtonState,
}

/// A button on a mouse device
Expand Down Expand Up @@ -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),
}
}
}
4 changes: 2 additions & 2 deletions crates/bevy_input/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ElementState, KeyCode, KeyboardInput};
use crate::{ButtonState, KeyCode, KeyboardInput};
use bevy_app::AppExit;
use bevy_ecs::prelude::{EventReader, EventWriter};

Expand All @@ -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();
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_winit/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
}
}

Expand Down

0 comments on commit 00c6acc

Please sign in to comment.