use sdl2;
pub use sdl2::keyboard::Keycode;
pub use sdl2::keyboard::Mod;
pub use sdl2::mouse::MouseButton;
pub use sdl2::mouse::MouseState;
pub use sdl2::controller::Button;
pub use sdl2::controller::Axis;
pub use sdl2::event::EventPollIterator;
pub use sdl2::event::Event;
use sdl2::event::Event::*;
use sdl2::event;
use sdl2::mouse;
use sdl2::keyboard;
use context::Context;
use GameResult;
pub use sdl2::keyboard::{CAPSMOD, LALTMOD, LCTRLMOD, LGUIMOD, LSHIFTMOD, MODEMOD, NOMOD, NUMMOD,
RALTMOD, RCTRLMOD, RESERVEDMOD, RGUIMOD, RSHIFTMOD};
pub trait EventHandler {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()>;
fn draw(&mut self, _ctx: &mut Context) -> GameResult<()>;
fn mouse_button_down_event(&mut self,
_ctx: &mut Context,
_button: mouse::MouseButton,
_x: i32,
_y: i32) {
}
fn mouse_button_up_event(&mut self,
_ctx: &mut Context,
_button: mouse::MouseButton,
_x: i32,
_y: i32) {
}
fn mouse_motion_event(&mut self,
_ctx: &mut Context,
_state: mouse::MouseState,
_x: i32,
_y: i32,
_xrel: i32,
_yrel: i32) {
}
fn mouse_wheel_event(&mut self, _ctx: &mut Context, _x: i32, _y: i32) {}
fn key_down_event(&mut self,
ctx: &mut Context,
keycode: Keycode,
_keymod: Mod,
_repeat: bool) {
if keycode == keyboard::Keycode::Escape {
ctx.quit().expect("Should never fail");
}
}
fn key_up_event(&mut self,
_ctx: &mut Context,
_keycode: Keycode,
_keymod: Mod,
_repeat: bool) {
}
fn controller_button_down_event(&mut self,
_ctx: &mut Context,
_btn: Button,
_instance_id: i32) {
}
fn controller_button_up_event(&mut self, _ctx: &mut Context, _btn: Button, _instance_id: i32) {}
fn controller_axis_event(&mut self,
_ctx: &mut Context,
_axis: Axis,
_value: i16,
_instance_id: i32) {
}
fn focus_event(&mut self, _ctx: &mut Context, _gained: bool) {}
fn quit_event(&mut self, _ctx: &mut Context) -> bool {
println!("quit_event() callback called, quitting...");
false
}
fn resize_event(&mut self, _ctx: &mut Context, _width: u32, _height: u32) {}
}
pub struct Events(sdl2::EventPump);
use std::fmt;
impl fmt::Debug for Events {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<Events: {:p}>", self)
}
}
impl Events {
pub fn new(ctx: &Context) -> GameResult<Events> {
let e = ctx.sdl_context.event_pump()?;
Ok(Events(e))
}
pub fn poll(&mut self) -> EventPollIterator {
self.0.poll_iter()
}
}
pub fn run<S>(ctx: &mut Context, state: &mut S) -> GameResult<()>
where S: EventHandler
{
let mut event_pump = ctx.sdl_context.event_pump()?;
let mut continuing = true;
while continuing {
ctx.timer_context.tick();
for event in event_pump.poll_iter() {
match event {
Quit { .. } => {
continuing = state.quit_event(ctx);
}
KeyDown {
keycode,
keymod,
repeat,
..
} => {
if let Some(key) = keycode {
state.key_down_event(ctx, key, keymod, repeat)
}
}
KeyUp {
keycode,
keymod,
repeat,
..
} => {
if let Some(key) = keycode {
state.key_up_event(ctx, key, keymod, repeat)
}
}
MouseButtonDown { mouse_btn, x, y, .. } => {
state.mouse_button_down_event(ctx, mouse_btn, x, y)
}
MouseButtonUp { mouse_btn, x, y, .. } => {
state.mouse_button_up_event(ctx, mouse_btn, x, y)
}
MouseMotion {
mousestate,
x,
y,
xrel,
yrel,
..
} => state.mouse_motion_event(ctx, mousestate, x, y, xrel, yrel),
MouseWheel { x, y, .. } => state.mouse_wheel_event(ctx, x, y),
ControllerButtonDown { button, which, .. } => {
state.controller_button_down_event(ctx, button, which)
}
ControllerButtonUp { button, which, .. } => {
state.controller_button_up_event(ctx, button, which)
}
ControllerAxisMotion { axis, value, which, .. } => {
state.controller_axis_event(ctx, axis, value, which)
}
Window { win_event: event::WindowEvent::FocusGained, .. } => {
state.focus_event(ctx, true)
}
Window { win_event: event::WindowEvent::FocusLost, .. } => {
state.focus_event(ctx, false)
}
Window { win_event: event::WindowEvent::Resized(w, h), .. } => {
ctx.gfx_context.resize_viewport();
state.resize_event(ctx, w as u32, h as u32);
}
_ => {}
}
}
state.update(ctx)?;
state.draw(ctx)?;
}
Ok(())
}