Skip to content

Commit

Permalink
Add custom pin drawing functionality to PinInfo struct
Browse files Browse the repository at this point in the history
  • Loading branch information
oligamiq committed Dec 28, 2024
1 parent b17ce75 commit c5aa1ed
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use self::{

pub use self::{
background_pattern::{BackgroundPattern, Grid, Viewport},
pin::{AnyPins, PinInfo, PinShape},
pin::{AnyPins, CustomPinDrawer, PinInfo, PinShape},
viewer::SnarlViewer,
wire::{WireLayer, WireStyle},
};
Expand Down
47 changes: 46 additions & 1 deletion src/ui/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ pub struct PinInfo {

/// Style of the wire connected to the pin.
pub wire_style: Option<WireStyle>,

/// Custom pin information.
pub custom: Option<Box<dyn CustomPinDrawer>>,
}

mod tests {
/// This test checks if the PinInfo struct is Sync and Send.
#[test]
fn test_pin_info_sync_send() {
fn assert_sync_send<T: Sync + Send>() {}
assert_sync_send::<super::PinInfo>();
}
}

/// Customization for the pin.
/// This trait is used to customize the appearance of the pin.
pub trait CustomPinDrawer: Sync + Send {
/// Draws the pin.
fn draw(
&self,
painter: &Painter,
shape: PinShape,
fill: Color32,
stroke: Stroke,
pos: Pos2,
size: f32,
);
}

impl PinInfo {
Expand Down Expand Up @@ -133,6 +160,19 @@ impl PinInfo {
}
}

/// Sets the custom pin information.
/// This will override all other pin information.
/// The custom pin information will be used to draw the pin.
/// The custom pin information must implement the `CustomPinDrawer` trait.
/// The custom pin information must be `Sync` and `Send`.
#[must_use]
pub fn custom(custom: impl CustomPinDrawer + 'static) -> Self {
PinInfo {
custom: Some(Box::new(custom)),
..Default::default()
}
}

/// Returns the shape of the pin.
#[must_use]
pub fn get_shape(&self, snarl_style: &SnarlStyle) -> PinShape {
Expand Down Expand Up @@ -170,7 +210,12 @@ impl PinInfo {
let fill = self.get_fill(snarl_style, style);
let stroke = self.get_stroke(snarl_style, style, scale);
let size = self.size.zoomed(scale).unwrap_or(size);
draw_pin(painter, shape, fill, stroke, pos, size);

if let Some(custom) = &self.custom {
custom.draw(painter, shape, fill, stroke, pos, size);
} else {
draw_pin(painter, shape, fill, stroke, pos, size);
}

fill
}
Expand Down

0 comments on commit c5aa1ed

Please sign in to comment.