Skip to content

Commit

Permalink
add: display overlap and touch check logic
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed Apr 29, 2024
1 parent bf53335 commit 2d615ed
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn set_display_position(server_disp: Vec<Display>) -> Vec<Display> {
let system_disp: Vec<Display> = DisplayInfo::all()
.expect("[ERR] failed to get system displays")
.into_iter()
.map(Display::from)
.map(|x| Display::from(x, 0))
.collect();

// TODO
Expand Down
55 changes: 52 additions & 3 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ pub struct Display {
pub owner: Cid,
}

impl From<DisplayInfo> for Display {
fn from(item: DisplayInfo) -> Self {
impl Display {
pub fn from(item: DisplayInfo, cid: Cid) -> Self {
Display {
name: item.name,
id: rand::random(),
Expand All @@ -71,7 +71,56 @@ impl From<DisplayInfo> for Display {
frequency: item.frequency,
is_primary: item.is_primary,
warpzones: Vec::new(),
owner: 0,
owner: cid,
}
}

pub fn is_overlap(&self, target: Display) -> bool {
let self_right = self.x + self.width;
let self_bottom = self.y + self.height;
let target_right = target.x + target.width;
let target_bottom = target.y + target.height;

self.x < target_right
&& self_right > target.x
&& self.y < target_bottom
&& self_bottom > target.y
}

pub fn is_touch(&self, target: Display) -> Option<(i32, i32, ZoneDirection)> {
let self_right = self.x + self.width;
let self_bottom = self.y + self.height;
let target_right = target.x + target.width;
let target_bottom = target.y + target.height;

let horizontal_touch = (self_right == target.x || self.x == target_right)
&& (self.y < target_bottom && self_bottom > target.y);

let vertical_touch = (self_bottom == target.y || self.y == target_bottom)
&& (self.x < target_right && self_right > target.x);

if horizontal_touch {
return Some((
i32::max(self.y, target.y),
i32::min(self_bottom, target_bottom),
if self_right == target.x {
ZoneDirection::HorizontalRight
} else {
ZoneDirection::HorizontalLeft
},
));
} else if vertical_touch {
return Some((
i32::max(self.x, target.x),
i32::min(self_right, target_right),
if self_bottom == target.y {
ZoneDirection::VerticalDown
} else {
ZoneDirection::VerticalUp
},
));
} else {
None
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ pub use server::*;
pub use utils::*;

pub const PORT: u16 = 2426;
pub const MAX_DISPLAYS: u32 = 128;
pub const SERVER_CID: Cid = 0;
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Server {
let disp: Vec<Display> = DisplayInfo::all()
.expect("[ERR] failed to get system displays")
.into_iter()
.map(Display::from)
.map(|x| Display::from(x, SERVER_CID))
.collect();

if disp.len() == 0 {
Expand Down

0 comments on commit 2d615ed

Please sign in to comment.