use super::input_handler::InputHandler;
use crate::Context;
use crate::graphics::Point2;
pub use crate::input::input_handler::MouseButton;
pub struct MouseContext {
pub(crate) input_handler: InputHandler,
last_position: Point2,
delta: Point2,
cursor_grabbed: bool,
cursor_hidden: bool,
cursor_type: miniquad::CursorIcon,
}
impl MouseContext {
pub fn new(input_handler: InputHandler) -> Self {
MouseContext {
input_handler,
last_position: Point2::new(0., 0.),
delta: Point2::new(0., 0.),
cursor_grabbed: false,
cursor_hidden: false,
cursor_type: miniquad::CursorIcon::Default,
}
}
pub(crate) fn set_last_position(&mut self, p: Point2) {
self.last_position = p;
}
pub fn reset_delta(&mut self) {
self.delta = Point2::new(0., 0.);
}
pub(crate) fn set_delta(&mut self, p: Point2) {
self.delta = p;
}
pub fn mouse_position(&self) -> cgmath::Point2<f32> {
self.input_handler.mouse_position
}
pub fn button_pressed(&self, button: MouseButton) -> bool {
self.input_handler.is_mouse_key_down(&button)
}
pub fn wheel(&self) -> f32 {
self.input_handler.wheel
}
}
pub fn position(ctx: &Context) -> mint::Point2<f32> {
ctx.mouse_context.mouse_position().into()
}
pub fn button_pressed(ctx: &Context, button: MouseButton) -> bool {
ctx.mouse_context.button_pressed(button)
}
pub fn wheel(ctx: &Context) -> f32 {
ctx.mouse_context.wheel()
}
pub fn delta(ctx: &Context) -> mint::Point2<f32> {
ctx.mouse_context.delta.into()
}
pub fn last_position(ctx: &Context) -> mint::Point2<f32> {
ctx.mouse_context.last_position.into()
}
pub fn cursor_grabbed(ctx: &Context) -> bool {
ctx.mouse_context.cursor_grabbed
}
pub fn set_cursor_grabbed(
ctx: &mut Context,
quad_ctx: &mut miniquad::GraphicsContext,
grabbed: bool,
) {
ctx.mouse_context.cursor_grabbed = grabbed;
quad_ctx.set_cursor_grab(grabbed);
}
pub fn cursor_type(ctx: &Context) -> miniquad::CursorIcon {
ctx.mouse_context.cursor_type
}
pub fn set_cursor_type(
ctx: &mut Context,
quad_ctx: &mut miniquad::graphics::GraphicsContext,
cursor_type: miniquad::CursorIcon,
) {
ctx.mouse_context.cursor_type = cursor_type;
quad_ctx.set_mouse_cursor(cursor_type);
}
pub fn cursor_hidden(ctx: &Context) -> bool {
ctx.mouse_context.cursor_hidden
}
pub fn set_cursor_hidden(
ctx: &mut Context,
quad_ctx: &mut miniquad::graphics::GraphicsContext,
hidden: bool,
) {
ctx.mouse_context.cursor_hidden = hidden;
quad_ctx.show_mouse(!hidden);
}