use std::str::FromStr;
use strum::EnumCount;
#[ derive( Debug, Clone, Copy, PartialEq, Eq, Hash, EnumCount ) ]
pub enum MouseButton
{
Main,
Auxiliary,
Secondary,
Back,
Forward,
Unknown,
}
impl MouseButton
{
pub const fn from_button( button : i16 ) -> Self
{
match button
{
0 => MouseButton::Main,
1 => MouseButton::Auxiliary,
2 => MouseButton::Secondary,
3 => MouseButton::Back,
4 => MouseButton::Forward,
_ => MouseButton::Unknown,
}
}
pub fn from_name( name : &str ) -> Self
{
MouseButton::from_str( name ).unwrap_or( MouseButton::Unknown )
}
pub fn button_value( &self ) -> i16
{
match self
{
MouseButton::Main => 0,
MouseButton::Auxiliary => 1,
MouseButton::Secondary => 2,
MouseButton::Back => 3,
MouseButton::Forward => 4,
MouseButton::Unknown => 5,
}
}
pub const fn name( &self ) -> &'static str
{
match self
{
MouseButton::Main => "Left",
MouseButton::Auxiliary => "Middle",
MouseButton::Secondary => "Right",
MouseButton::Back => "Back",
MouseButton::Forward => "Forward",
MouseButton::Unknown => "Unknown",
}
}
pub fn technical_name( &self ) -> &'static str
{
match self
{
MouseButton::Main => "Main",
MouseButton::Auxiliary => "Auxiliary",
MouseButton::Secondary => "Secondary",
MouseButton::Back => "Back",
MouseButton::Forward => "Forward",
MouseButton::Unknown => "Unknown",
}
}
pub const fn is_main( &self ) -> bool
{
matches!( self, MouseButton::Main )
}
pub const fn is_secondary( &self ) -> bool
{
matches!( self, MouseButton::Secondary )
}
pub const fn is_auxiliary( &self ) -> bool
{
matches!( self, MouseButton::Auxiliary )
}
pub const fn is_navigation( &self ) -> bool
{
matches!( self, MouseButton::Back | MouseButton::Forward )
}
}
impl FromStr for MouseButton
{
type Err = ();
fn from_str( s : &str ) -> Result< Self, Self::Err >
{
match s.to_lowercase().as_str()
{
"main" | "left" | "primary" => Ok( MouseButton::Main ),
"auxiliary" | "middle" | "wheel" => Ok( MouseButton::Auxiliary ),
"secondary" | "right" | "context" => Ok( MouseButton::Secondary ),
"back" => Ok( MouseButton::Back ),
"forward" => Ok( MouseButton::Forward ),
_ => Err( () ),
}
}
}
impl From< i16 > for MouseButton
{
fn from( value : i16 ) -> Self
{
MouseButton::from_button( value )
}
}