Skip to content

Commit

Permalink
Add GamepadContext and event handling for gamepads
Browse files Browse the repository at this point in the history
  • Loading branch information
svercl committed Jun 14, 2018
1 parent bcaee1d commit 115a363
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ nalgebra = {version = "^0.15.2", features = ["mint"] }
# Has to be the same version of mint that nalgebra uses here.
mint = "0.5"
winit = { version = "0.15", features = ["icon_loading"] }
gilrs = "0.6"

[dev-dependencies]
chrono = "0.4"
Expand Down
14 changes: 7 additions & 7 deletions examples/input_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,30 @@ impl event::EventHandler for MainState {
println!("Text input: {}", ch);
}

fn controller_button_down_event(&mut self, _ctx: &mut Context, btn: Button, instance_id: i32) {
fn controller_button_down_event(&mut self, _ctx: &mut Context, btn: Button, id: usize) {
println!(
"Controller button pressed: {:?} Controller_Id: {}",
btn, instance_id
btn, id
);
}

fn controller_button_up_event(&mut self, _ctx: &mut Context, btn: Button, instance_id: i32) {
fn controller_button_up_event(&mut self, _ctx: &mut Context, btn: Button, id: usize) {
println!(
"Controller button released: {:?} Controller_Id: {}",
btn, instance_id
btn, id
);
}

fn controller_axis_event(
&mut self,
_ctx: &mut Context,
axis: Axis,
value: i16,
instance_id: i32,
value: f32,
id: usize,
) {
println!(
"Axis Event: {:?} Value: {} Controller_Id: {}",
axis, value, instance_id
axis, value, id
);
}

Expand Down
9 changes: 7 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use winit;

use std::fmt;

use GameResult;
use audio;
use conf;
use event::winit_event;
use filesystem::Filesystem;
use gamepad;
use graphics::{self, Point2};
use keyboard;
use mouse;
use timer;
use GameResult;

/// A `Context` is an object that holds on to global resources.
/// It basically tracks hardware state such as the screen, audio
Expand Down Expand Up @@ -39,6 +40,8 @@ pub struct Context {
pub keyboard_context: keyboard::KeyboardContext,
/// Mouse context
pub mouse_context: mouse::MouseContext,
/// Gamepad context
pub gamepad_context: gamepad::GamepadContext,
/// Default font
pub default_font: graphics::Font,

Expand Down Expand Up @@ -73,12 +76,13 @@ impl Context {
)?;
let mouse_context = mouse::MouseContext::new();
let keyboard_context = keyboard::KeyboardContext::new();
let gamepad_context = gamepad::GamepadContext::new()?;

// TODO: Clean up the bytes here a bit.
let font_id = graphics_context.glyph_brush.add_font_bytes(
&include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/resources/DejaVuSerif.ttf"
"/resources/DejaVuSerif.ttf"
))[..]
);
let default_font = graphics::Font::GlyphFont(font_id);
Expand All @@ -91,6 +95,7 @@ impl Context {
timer_context,
audio_context,
keyboard_context,
gamepad_context,
mouse_context,

default_font: default_font,
Expand Down
14 changes: 13 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use glutin;
use winit;

use app_dirs2::AppDirsError;
use gilrs;
use image;
use rodio::decoder::DecoderError;
use toml;
Expand Down Expand Up @@ -40,8 +41,11 @@ pub enum GameError {
FontError(String),
/// Something went wrong applying video settings.
VideoError(String),
/// Something went compiling shaders
/// Something went wrong compiling shaders
ShaderProgramError(gfx::shade::ProgramError),
// TODO: Document this better
/// Something went wrong with Gilrs
GilrsError(gilrs::Error),
/// Something else happened; this is generally a bug.
UnknownError(String),
}
Expand Down Expand Up @@ -77,6 +81,7 @@ impl Error for GameError {
GameError::FontError(_) => "Font error",
GameError::VideoError(_) => "Video error",
GameError::ShaderProgramError(_) => "Shader program error",
GameError::GilrsError(_) => "Gilrs error",
GameError::UnknownError(_) => "Unknown error",
}
}
Expand Down Expand Up @@ -240,3 +245,10 @@ impl From<glutin::ContextError> for GameError {
GameError::RenderError(format!("OpenGL context error: {}", s))
}
}

impl From<gilrs::Error> for GameError {
// TODO: Better error type?
fn from(s: gilrs::Error) -> GameError {
GameError::GilrsError(s)
}
}
34 changes: 18 additions & 16 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//! See the `eventloop` example for an implementation.
use winit;
use gilrs;

/// A key code.
pub use winit::VirtualKeyCode as KeyCode;
Expand All @@ -24,10 +25,9 @@ pub use winit::ModifiersState as KeyMods;
pub use winit::MouseButton;

/// An analog axis of some device (controller, joystick...).
// TODO: verify. (probably useless; investigate `gilrs`)
pub use winit::AxisId as Axis;
pub use gilrs::Axis;
/// A button of some device (controller, joystick...).
pub use winit::ButtonId as Button;
pub use gilrs::Button;

/// `winit` events; nested in a module for re-export neatness.
pub mod winit_event {
Expand Down Expand Up @@ -115,20 +115,20 @@ pub trait EventHandler {
&mut self,
_ctx: &mut Context,
_btn: Button,
_instance_id: i32,
_id: usize,
) {
}

/// A controller button was released.
fn controller_button_up_event(&mut self, _ctx: &mut Context, _btn: Button, _instance_id: i32) {}
fn controller_button_up_event(&mut self, _ctx: &mut Context, _btn: Button, _id: usize) {}

/// A controller axis moved.
fn controller_axis_event(
&mut self,
_ctx: &mut Context,
_axis: Axis,
_value: i16,
_instance_id: i32,
_value: f32,
_id: usize,
) {
}

Expand Down Expand Up @@ -234,18 +234,20 @@ where
Event::Suspended(_) => unimplemented!(),
}
});
/*{ // TODO: Investigate `gilrs` for gamepad support.
ControllerButtonDown { button, which, .. } => {
state.controller_button_down_event(ctx, button, which)
while let Some(gilrs::Event{id, event,..}) = ctx.gamepad_context.gilrs.next_event() {
match event {
gilrs::EventType::ButtonPressed(button, _) => {
state.controller_button_down_event(ctx, button, id);
}
gilrs::EventType::ButtonReleased(button, _) => {
state.controller_button_up_event(ctx, button, id);
}
ControllerButtonUp { button, which, .. } => {
state.controller_button_up_event(ctx, button, which)
gilrs::EventType::AxisChanged(axis, value, _) => {
state.controller_axis_event(ctx, axis, value, id);
}
ControllerAxisMotion {
axis, value, which, ..
} => state.controller_axis_event(ctx, axis, value, which),
_ => {}
}*/
}
}
state.update(ctx)?;
state.draw(ctx)?;
}
Expand Down
32 changes: 32 additions & 0 deletions src/gamepad.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// TODO: Documentation
//! Gamepad utility functions.
use std::fmt;

use gilrs::{Gamepad, Gilrs};

use context::Context;
use GameResult;

/// A structure that contains gamepad state.
pub struct GamepadContext {
pub(crate) gilrs: Gilrs,
}

impl fmt::Debug for GamepadContext {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<GamepadContext: {:p}>", self)
}
}

impl GamepadContext {
pub(crate) fn new() -> GameResult<GamepadContext> {
let gilrs = Gilrs::new()?;
Ok(GamepadContext { gilrs })
}
}

/// returns the `Gamepad` associated with an id.
pub fn get_gamepad(ctx: &Context, id: usize) -> Option<&Gamepad> {
ctx.gamepad_context.gilrs.get(id)
}
Empty file removed src/input.rs
Empty file.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ extern crate rodio;
extern crate serde_derive;
#[macro_use]
extern crate smart_default;
extern crate gilrs;
extern crate toml;
extern crate winit;
extern crate zip;
Expand All @@ -127,6 +128,7 @@ mod context;
pub mod error;
pub mod event;
pub mod filesystem;
pub mod gamepad;
pub mod graphics;
pub mod keyboard;
pub mod mouse;
Expand Down

0 comments on commit 115a363

Please sign in to comment.