Implement connection method for grid

This commit is contained in:
hodasemi 2024-05-25 09:42:05 +02:00
parent acfe67ee8c
commit 67ad8bf665
2 changed files with 90 additions and 3 deletions

View file

@ -15,9 +15,12 @@ use super::{
}; };
use cgmath::{vec2, vec4}; use cgmath::{vec2, vec4};
use std::sync::{ use std::{
atomic::{AtomicBool, Ordering::SeqCst}, ops::Deref,
Arc, Mutex, sync::{
atomic::{AtomicBool, Ordering::SeqCst},
Arc, Mutex,
},
}; };
pub struct ButtonBuilder { pub struct ButtonBuilder {
@ -694,3 +697,11 @@ impl Button {
Ok(()) Ok(())
} }
} }
impl Deref for Button {
type Target = Arc<Selectable>;
fn deref(&self) -> &Self::Target {
&self.selectable
}
}

View file

@ -11,6 +11,13 @@ use std::{ops::Deref, sync::Weak};
use super::fill_type::FillType; use super::fill_type::FillType;
pub enum ConnectDirection {
East,
West,
South,
North,
}
#[derive(Clone)] #[derive(Clone)]
enum ChildState { enum ChildState {
Some { Some {
@ -315,6 +322,75 @@ impl Grid {
self.framable.allow_size_scale(false) self.framable.allow_size_scale(false)
} }
pub fn connect<'a>(
&self,
direction: ConnectDirection,
elements: impl IntoIterator<Item = (usize, &'a Arc<Selectable>)>,
) -> Result<()> {
for (index, selectable) in elements {
match direction {
ConnectDirection::East => {
let y = index;
for x in (0..self.dim_x).rev() {
if let Some(child) = self.child_at(x, y)? {
if let Some(gridable) = child.gridable() {
if let Some(grid_selectable) = gridable.selectable() {
Selectable::connect_horizontally(grid_selectable, selectable);
break;
}
}
}
}
}
ConnectDirection::West => {
let y = index;
for x in 0..self.dim_x {
if let Some(child) = self.child_at(x, y)? {
if let Some(gridable) = child.gridable() {
if let Some(grid_selectable) = gridable.selectable() {
Selectable::connect_horizontally(selectable, grid_selectable);
break;
}
}
}
}
}
ConnectDirection::South => {
let x = index;
for y in (0..self.dim_y).rev() {
if let Some(child) = self.child_at(x, y)? {
if let Some(gridable) = child.gridable() {
if let Some(grid_selectable) = gridable.selectable() {
Selectable::connect_vertically(grid_selectable, selectable);
break;
}
}
}
}
}
ConnectDirection::North => {
let x = index;
for y in (0..self.dim_y).rev() {
if let Some(child) = self.child_at(x, y)? {
if let Some(gridable) = child.gridable() {
if let Some(grid_selectable) = gridable.selectable() {
Selectable::connect_vertically(grid_selectable, selectable);
break;
}
}
}
}
}
}
}
Ok(())
}
pub fn attach( pub fn attach(
&self, &self,
child: Arc<dyn GuiElementTraits>, child: Arc<dyn GuiElementTraits>,