use crate::consts::FONT_PATH; use sdl2::{ keyboard::{Keycode, Mod}, pixels::Color, render::Canvas, ttf::Sdl2TtfContext, video::Window, }; #[macro_export] macro_rules! rect( ($x:expr, $y:expr, $w:expr, $h:expr) => ( ::sdl2::rect::Rect::new($x as i32, $y as i32, $w as u32, $h as u32) ) ); pub fn draw_text>( canvas: &mut Canvas, ttf_context: &Sdl2TtfContext, text: S, color: Color, (x, y): (u32, u32), ) -> u32 { let text = text.as_ref(); let texture_creator = canvas.texture_creator(); let mut font = ttf_context.load_font(FONT_PATH, 17).unwrap(); font.set_style(sdl2::ttf::FontStyle::NORMAL); font.set_hinting(sdl2::ttf::Hinting::Mono); let surface = font.render(text.as_ref()).blended(color).unwrap(); let texture = texture_creator .create_texture_from_surface(&surface) .unwrap(); let (width, height) = font.size_of_latin1(text.as_bytes()).unwrap(); let area = rect!(x, y, width, height); canvas.copy(&texture, None, area).unwrap(); width } pub fn is_copy_event(keycode: Option, keymod: Mod) -> bool { keycode == Some(Keycode::C) && (keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD) } pub fn is_paste_event(keycode: Option, keymod: Mod) -> bool { keycode == Some(Keycode::V) && (keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD) }