use sdl2::video::FullscreenType;
use crate::graphics::{self, ScreenScaling};
use crate::{Context, Result, TetraError};
pub fn quit(ctx: &mut Context) {
ctx.running = false;
}
pub fn get_title(ctx: &Context) -> &str {
ctx.window.title()
}
pub fn set_title(ctx: &mut Context, title: &str) {
ctx.window.set_title(title).unwrap();
}
pub fn get_width(ctx: &Context) -> i32 {
ctx.window_width
}
pub fn set_width(ctx: &mut Context, width: i32) {
set_size_ex(ctx, width, ctx.window_height, false);
}
pub fn get_height(ctx: &Context) -> i32 {
ctx.window_height
}
pub fn set_height(ctx: &mut Context, height: i32) {
set_size_ex(ctx, ctx.window_width, height, false);
}
pub fn get_size(ctx: &Context) -> (i32, i32) {
(ctx.window_width, ctx.window_height)
}
pub fn set_size(ctx: &mut Context, width: i32, height: i32) {
set_size_ex(ctx, width, height, false);
}
pub(crate) fn set_size_ex(ctx: &mut Context, width: i32, height: i32, from_sdl: bool) {
ctx.window_width = width;
ctx.window_height = height;
graphics::set_window_projection(ctx, width, height);
if let ScreenScaling::Resize = graphics::get_scaling(ctx) {
graphics::set_backbuffer_size(ctx, width, height);
}
graphics::update_screen_rect(ctx);
if !from_sdl {
ctx.window.set_size(width as u32, height as u32).unwrap();
}
}
pub fn toggle_fullscreen(ctx: &mut Context) -> Result {
if ctx.fullscreen {
disable_fullscreen(ctx)
} else {
enable_fullscreen(ctx)
}
}
pub fn enable_fullscreen(ctx: &mut Context) -> Result {
if !ctx.fullscreen {
ctx.window
.display_mode()
.and_then(|m| {
set_size_ex(ctx, m.w, m.h, false);
ctx.window.set_fullscreen(FullscreenType::Desktop)
})
.map(|_| ())
.map_err(TetraError::Sdl)
} else {
Ok(())
}
}
pub fn disable_fullscreen(ctx: &mut Context) -> Result {
if ctx.fullscreen {
ctx.window
.set_fullscreen(FullscreenType::Off)
.map(|_| {
let size = ctx.window.drawable_size();
set_size_ex(ctx, size.0 as i32, size.1 as i32, false);
})
.map_err(TetraError::Sdl)
} else {
Ok(())
}
}
pub fn is_fullscreen(ctx: &Context) -> bool {
ctx.fullscreen
}
pub fn show_mouse(ctx: &mut Context) {
ctx.sdl.mouse().show_cursor(true);
}
pub fn hide_mouse(ctx: &mut Context) {
ctx.sdl.mouse().show_cursor(false);
}
pub fn is_mouse_visible(ctx: &mut Context) {
ctx.sdl.mouse().is_cursor_showing();
}