This commit is contained in:
2018-11-19 15:30:57 +01:00
parent a8f928f488
commit f3643ba31b
11 changed files with 126 additions and 225 deletions
+15 -46
View File
@@ -1,16 +1,7 @@
use sfml::system::Vector2;
use sfml::window::joystick::{
Axis,
axis_position,
is_connected,
is_button_pressed,
COUNT,
};
use sfml::window::joystick::{axis_position, is_button_pressed, is_connected, Axis, COUNT};
use sfml::window::{
Key,
Event,
};
use sfml::window::{Event, Key};
/// The different actions that a user can do.
pub enum Action {
@@ -32,7 +23,6 @@ pub enum Controls {
}
impl Controls {
/// Returns the default keyboard controls.
pub fn default_keyboard() -> Controls {
Controls::Keyboard(KeyboardMap::default())
@@ -81,13 +71,11 @@ impl Controls {
Controls::Gamepad(ref map) => map.is_running(),
}
}
}
/// A map between keyboard keys and actions.
#[derive(Copy, Clone)]
pub struct KeyboardMap {
/// The key corresponding to the jump button.
jump_key: Key,
@@ -99,11 +87,9 @@ pub struct KeyboardMap {
/// The key corresponding to the right button.
right_key: Key,
}
impl KeyboardMap {
/// Creates the default keyboard config.
pub fn default() -> KeyboardMap {
KeyboardMap {
@@ -116,22 +102,17 @@ impl KeyboardMap {
/// Converts an event and depending on the config, returns the corresponding action.
pub fn convert(&self, event: &Event) -> Option<Action> {
match event {
Event::KeyPressed { code, .. } if *code == self.jump_key =>
Some(Action::Jump(true)),
Event::KeyPressed { code, .. } if *code == self.jump_key => Some(Action::Jump(true)),
Event::KeyReleased { code, .. } if *code == self.jump_key =>
Some(Action::Jump(false)),
Event::KeyReleased { code, .. } if *code == self.jump_key => Some(Action::Jump(false)),
_ => None,
}
}
/// Returns the direction of the keys.
pub fn direction(&self) -> Vector2<f32> {
let mut ret = Vector2::new(0.0, 0.0);
const RIGHT: Vector2<f32> = Vector2 { x: 1.0, y: 0.0 };
@@ -145,20 +126,17 @@ impl KeyboardMap {
}
ret
}
/// Returns whether the running button is held down.
pub fn is_running(&self) -> bool {
self.run_key.is_pressed()
}
}
/// A map between gamepad buttons and actions.
#[derive(Copy, Clone)]
pub struct GamepadMap {
/// Id of the gamepad.
id: u32,
@@ -170,17 +148,14 @@ pub struct GamepadMap {
/// Left / Right axis.
left_right_axis: Axis,
}
impl GamepadMap {
/// Creates the default gamepad from an id.
///
/// Returns None if the gamepad corresponding to the id is not connected.
pub fn from_id(id: u32) -> Option<GamepadMap> {
if ! is_connected(id){
if !is_connected(id) {
return None;
}
@@ -196,7 +171,7 @@ impl GamepadMap {
pub fn all() -> Vec<GamepadMap> {
let mut gamepads = vec![];
for id in 0 .. COUNT {
for id in 0..COUNT {
if let Some(gamepad) = GamepadMap::from_id(id) {
gamepads.push(gamepad);
}
@@ -207,21 +182,18 @@ impl GamepadMap {
/// Converts an event and depending on the config, returns the corresponding action.
pub fn convert(&self, event: &Event) -> Option<Action> {
match event {
Event::JoystickButtonPressed { joystickid, button } if
*joystickid == self.id && *button == self.jump_button => {
Event::JoystickButtonPressed { joystickid, button }
if *joystickid == self.id && *button == self.jump_button =>
{
Some(Action::Jump(true))
}
},
Event::JoystickButtonReleased { joystickid, button } if
*joystickid == self.id && *button == self.jump_button => {
Event::JoystickButtonReleased { joystickid, button }
if *joystickid == self.id && *button == self.jump_button =>
{
Some(Action::Jump(false))
},
}
_ => None,
}
@@ -229,10 +201,7 @@ impl GamepadMap {
/// Returns the direction of the directionnal buttons of the gamepad.
pub fn direction(&self) -> Vector2<f32> {
Vector2::new(
axis_position(self.id, self.left_right_axis) / 100.0,
0.0
)
Vector2::new(axis_position(self.id, self.left_right_axis) / 100.0, 0.0)
}
/// Returns whether the run button is held down.