Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more methods for MouseState #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ impl MouseState {
!self.flags.contains(MouseFlags::RIGHT_BUTTON)
}

/// Returns true if the middle mouse button is currently down.
pub fn middle_button_down(&self) -> bool {
self.flags.contains(MouseFlags::MIDDLE_BUTTON)
}

/// Returns true if the middle mouse button is currently up.
pub fn middle_button_up(&self) -> bool {
!self.flags.contains(MouseFlags::MIDDLE_BUTTON)
}

/// Returns true if the x axis has moved.
pub fn x_moved(&self) -> bool {
self.x != 0
Expand All @@ -118,6 +128,11 @@ impl MouseState {
self.x_moved() || self.y_moved()
}

/// Returns the flags of the mouse state.
pub fn get_flags(&self) -> MouseFlags {
self.flags
}

/// Returns the x delta of the mouse state.
pub fn get_x(&self) -> i16 {
self.x
Expand Down Expand Up @@ -281,6 +296,8 @@ mod test {
MouseFlags::ALWAYS_ONE.bits() | MouseFlags::LEFT_BUTTON.bits();
const RIGHT_MOUSE_BUTTON_DOWN_PACKET: u8 =
MouseFlags::ALWAYS_ONE.bits() | MouseFlags::RIGHT_BUTTON.bits();
const MIDDLE_MOUSE_BUTTON_DOWN_PACKET: u8 =
MouseFlags::ALWAYS_ONE.bits() | MouseFlags::MIDDLE_BUTTON.bits();
const POSITIVE_X_PACKET: u8 = 0x5;
const POSITIVE_Y_PACKET: u8 = 0x8;
const NEGATIVE_X_PACKET: u8 = 0xD8;
Expand Down Expand Up @@ -379,4 +396,18 @@ mod test {
mouse.process_packet(VALID_PACKET);
assert_eq!(mouse.current_state.right_button_up(), true);
}

#[test]
fn middle_mouse_button_down() {
let mut mouse = Mouse::new();
mouse.process_packet(MIDDLE_MOUSE_BUTTON_DOWN_PACKET);
assert_eq!(mouse.current_state.middle_button_down(), true);
}

#[test]
fn middle_mouse_button_up() {
let mut mouse = Mouse::new();
mouse.process_packet(VALID_PACKET);
assert_eq!(mouse.current_state.middle_button_up(), true);
}
}