use context::Context;
use error::GameResult;
use graphics;
use graphics::Point2;
#[derive(Clone, Debug)]
pub struct MouseContext {
last_position: Point2,
}
impl MouseContext {
pub fn new() -> Self {
Self {
last_position: Point2::origin(),
}
}
pub(crate) fn set_last_position(&mut self, p: Point2) {
self.last_position = p;
}
}
impl Default for MouseContext {
fn default() -> Self {
Self::new()
}
}
pub fn get_grabbed(ctx: &Context) -> bool {
graphics::get_window(ctx).grab()
}
pub fn set_grabbed(ctx: &mut Context, grabbed: bool) {
graphics::get_window_mut(ctx).set_grab(grabbed)
}
pub fn get_relative_mode(ctx: &Context) -> bool {
ctx.sdl_context.mouse().relative_mouse_mode()
}
pub fn set_relative_mode(ctx: &Context, mode: bool) {
ctx.sdl_context.mouse().set_relative_mouse_mode(mode)
}
pub fn get_position(ctx: &Context) -> GameResult<Point2> {
Ok(ctx.mouse_context.last_position)
}
pub fn set_position(ctx: &Context, point: Point2) {
let window = graphics::get_window(ctx);
ctx.sdl_context
.mouse()
.warp_mouse_in_window(window, point.x as i32, point.y as i32)
}
pub fn set_cursor_visible(ctx: &Context, visible: bool) {
ctx.sdl_context.mouse().show_cursor(visible)
}
pub fn get_cursor_visible(ctx: &Context) -> bool {
ctx.sdl_context.mouse().is_cursor_showing()
}