//! The mouse handling every survival and container screen shares //! (`AbstractContainerScreen.mouseClicked` / `mouseReleased`): which slot id //! a click targets (-999 outside the window, `hasClickedOutside` with the //! recipe book's own rule) and which `ContainerInput` it sends. Shift is //! the event's own modifiers (`InputWithModifiers.hasShiftDown`: the //! physical Shift keys, never the remapped or toggled sneak key). use crate::menu::Input; /// The screen's window: `leftPos`, `topPos`, `imageWidth`, `imageHeight`. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct Image { pub left: i32, pub top: i32, pub width: i32, pub height: i32, } /// The recipe book of an `AbstractRecipeBookScreen`. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct Book { /// `RecipeBookComponent.isVisible`. pub visible: bool, /// `selectedTab.isHoveredOrFocused()`. pub selected_tab_hovered: bool, } /// `RecipeBookComponent.IMAGE_WIDTH`. const BOOK_WIDTH: i32 = 147; fn outside(mx: f64, my: f64, image: Image) -> bool { mx < f64::from(image.left) || my < f64::from(image.top) || mx >= f64::from(image.left + image.width) || my >= f64::from(image.top + image.height) } /// `AbstractContainerScreen.hasClickedOutside`, or /// `AbstractRecipeBookScreen.hasClickedOutside` when the screen has a book /// (`RecipeBookComponent.hasClickedOutside`: a click on the book, or with /// its selected tab hovered, is not outside). pub fn clicked_outside(mx: f64, my: f64, image: Image, book: Option) -> bool { let out = outside(mx, my, image); let Some(book) = book else { return out; }; let book_outside = !book.visible || { let on_book = f64::from(image.left - BOOK_WIDTH) < mx && mx < f64::from(image.left) && f64::from(image.top) < my && my < f64::from(image.top + image.height); out && !on_book && !book.selected_tab_hovered }; book_outside && out } /// What a click points at: a slot, outside the window (slot id -999), or /// neither (slot id -1). #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Target { Slot(S), Outside, Nothing, } impl Target { /// Outside wins over a hovered slot, as in `mouseClicked`. pub fn of(slot: Option, outside: bool) -> Self { match slot { _ if outside => Self::Outside, Some(s) => Self::Slot(s), None => Self::Nothing, } } } /// The left, right and middle buttons (SDL's 1, 3 and 2). #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Button { Left, Right, Middle, } impl Button { /// `getContainerClickButton`: the button a menu click carries. pub fn menu_button(self) -> i32 { match self { Self::Left => 0, Self::Right => 1, Self::Middle => 2, } } } /// The input state `mouseClicked`/`mouseReleased` keep between events. #[derive(Clone, Debug, Default)] pub struct Clicks { /// `isQuickCrafting` with `quickCraftingButton`. quick_crafting: Option