From 2d615ed735f44baff751b850ddb060223ca97503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=98=A4=EB=B3=91=EC=A4=80?= Date: Mon, 29 Apr 2024 19:57:18 +0900 Subject: [PATCH] add: display overlap and touch check logic --- src/client.rs | 2 +- src/display.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++--- src/lib.rs | 2 +- src/server.rs | 2 +- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/client.rs b/src/client.rs index 52fa6f6..f139d81 100644 --- a/src/client.rs +++ b/src/client.rs @@ -69,7 +69,7 @@ fn set_display_position(server_disp: Vec) -> Vec { let system_disp: Vec = DisplayInfo::all() .expect("[ERR] failed to get system displays") .into_iter() - .map(Display::from) + .map(|x| Display::from(x, 0)) .collect(); // TODO diff --git a/src/display.rs b/src/display.rs index 503c549..46b4917 100644 --- a/src/display.rs +++ b/src/display.rs @@ -56,8 +56,8 @@ pub struct Display { pub owner: Cid, } -impl From for Display { - fn from(item: DisplayInfo) -> Self { +impl Display { + pub fn from(item: DisplayInfo, cid: Cid) -> Self { Display { name: item.name, id: rand::random(), @@ -71,7 +71,56 @@ impl From 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 } } } diff --git a/src/lib.rs b/src/lib.rs index f39722e..b248272 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/server.rs b/src/server.rs index be2127a..1aec1eb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -28,7 +28,7 @@ impl Server { let disp: Vec = 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 {