hex_patch/app/plugins/ui_location/
point.rs

1use mlua::IntoLua;
2use ratatui::layout::Rect;
3
4use super::rect_borders::RectBorders;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub struct Point {
8    pub x: u16,
9    pub y: u16,
10}
11
12impl Point {
13    pub fn new(x: u16, y: u16) -> Self {
14        Self { x, y }
15    }
16
17    /// Returns Some if this point is inside the Rect. Returns None if it's outside
18    pub fn get_relative_location(&self, rect: Option<&Rect>) -> Option<(Point, RectBorders)> {
19        if let Some(rect) = rect {
20            if self.x >= rect.x
21                && self.x < rect.x + rect.width
22                && self.y >= rect.y
23                && self.y < rect.y + rect.height
24            {
25                Some((
26                    Point::new(self.x - rect.x, self.y - rect.y),
27                    RectBorders {
28                        top: self.y == rect.y,
29                        bottom: self.y == rect.y + rect.height.saturating_sub(1),
30                        left: self.x == rect.x,
31                        right: self.x == rect.x + rect.width.saturating_sub(1),
32                    },
33                ))
34            } else {
35                None
36            }
37        } else {
38            None
39        }
40    }
41}
42
43impl IntoLua for Point {
44    fn into_lua(self, lua: &mlua::Lua) -> mlua::Result<mlua::Value> {
45        let ret = lua.create_table()?;
46        ret.set("x", self.x)?;
47        ret.set("y", self.y)?;
48        Ok(mlua::Value::Table(ret))
49    }
50}