Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
4.9 kB · 204 lines
Rust
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205use std::ops::{Add, RangeInclusive};
/// font size in pixel. font is a squarepub const FONT_SIZE: u8 = 8;pub const FONT_SIZE16: u16 = FONT_SIZE as u16;/// window size in characterspub const WINDOW_SIZE_CHARS: (u8, u8) = (80, 50);/// window size in pixelpub const WINDOW_SIZE: (u16, u16) = ( // TODO: use from once const traits are available FONT_SIZE16 * WINDOW_SIZE_CHARS.0 as u16, FONT_SIZE16 * WINDOW_SIZE_CHARS.1 as u16,);
/// CharRect as well as PixelRect uses all values inclusive, meaning the borders are included#[derive(Debug, Clone, Copy)]pub struct CharRect { top: u8, bot: u8, left: u8, right: u8,}
impl CharRect { /// row 11 is reserved for the page title. The page shouldn't draw on it pub const PAGE_AREA: Self = Self::new(12, WINDOW_SIZE_CHARS.1 - 1, 0, WINDOW_SIZE_CHARS.0 - 1); pub const HEADER_AREA: Self = Self::new(0, 10, 0, WINDOW_SIZE_CHARS.0 - 1);
pub const fn new(top: u8, bot: u8, left: u8, right: u8) -> Self { assert!(top <= bot, "top needs to be smaller than bot"); assert!(left <= right, "left needs to be smaller than right"); assert!(bot < WINDOW_SIZE_CHARS.1, "lower than window bounds"); assert!(right < WINDOW_SIZE_CHARS.0, "right out of window bounds");
Self { top, bot, left, right, } }
pub const fn top(self) -> u8 { self.top } pub const fn bot(self) -> u8 { self.bot } pub const fn right(self) -> u8 { self.right } pub const fn left(self) -> u8 { self.left }
pub const fn top_left(self) -> CharPosition { CharPosition { x: self.left, y: self.top, } }
pub const fn width(self) -> u8 { self.right - self.left }
pub const fn height(self) -> u8 { self.bot - self.top }}
impl From<CharPosition> for CharRect { fn from(value: CharPosition) -> Self { Self::new(value.y, value.y, value.x, value.x) }}
#[cfg(feature = "accesskit")]impl From<CharRect> for accesskit::Rect { fn from(value: CharRect) -> Self { accesskit::Rect { x0: value.left as f64, y0: value.top as f64, x1: value.right as f64, y1: value.bot as f64, } }}
/// PixelRect as well as CharRect uses all values inclusive, meaning the borders are included#[derive(Debug, Clone, Copy)]pub struct PixelRect { top: u16, bot: u16, right: u16, left: u16,}
impl PixelRect { pub const fn new(top: u16, bot: u16, right: u16, left: u16) -> Self { assert!(top <= bot, "top needs to be smaller than bot"); assert!(left <= right, "left needs to be smaller than right"); assert!(bot < WINDOW_SIZE.1, "lower than window bounds"); assert!(right < WINDOW_SIZE.0, "right out of window bounds");
Self { top, bot, right, left, } }
pub const fn vertical_range(&self) -> RangeInclusive<u16> { RangeInclusive::new(self.top, self.bot) }
pub const fn horizontal_range(&self) -> RangeInclusive<u16> { RangeInclusive::new(self.left, self.right) }
pub const fn top(&self) -> u16 { self.top }
pub const fn bot(&self) -> u16 { self.bot }
pub const fn right(&self) -> u16 { self.right }
pub const fn left(&self) -> u16 { self.left }}
impl From<CharRect> for PixelRect { fn from(value: CharRect) -> Self { Self::new( u16::from(value.top) * FONT_SIZE16, u16::from(value.bot) * FONT_SIZE16 + FONT_SIZE16 - 1, u16::from(value.right) * FONT_SIZE16 + FONT_SIZE16 - 1, u16::from(value.left) * FONT_SIZE16, ) }}
impl From<CharPosition> for PixelRect { fn from(value: CharPosition) -> Self { Self::from(CharRect::from(value)) }}
#[derive(Debug, Clone, Copy)]pub struct CharPosition { x: u8, y: u8,}
impl CharPosition { #[track_caller] pub const fn new(x: u8, y: u8) -> Self { assert!(y < WINDOW_SIZE_CHARS.1); assert!(x < WINDOW_SIZE_CHARS.0);
Self { x, y } }
pub const fn x(&self) -> u8 { self.x }
pub const fn y(&self) -> u8 { self.y }}
impl Add for CharPosition { type Output = Self;
#[track_caller] fn add(self, rhs: Self) -> Self::Output { Self::new(self.x + rhs.x, self.y + rhs.y) }}
impl Add<(u8, u8)> for CharPosition { type Output = Self;
#[track_caller] fn add(self, rhs: (u8, u8)) -> Self::Output { Self::new(self.x + rhs.0, self.y + rhs.1) }}
// impl Mul<usize> for CharPosition {// type Output = Self;
// fn mul(self, rhs: usize) -> Self::Output {// Self::new(self.x * rhs, self.y * rhs)// }// }