added more customization options for wires

This commit is contained in:
Louis Heredero 2024-05-18 12:39:47 +02:00
parent 66ac91af7b
commit 5bd3dd8111
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7

View File

@ -7,8 +7,8 @@
#let signal-width = 1pt #let signal-width = 1pt
#let bus-width = 1.5pt #let bus-width = 1.5pt
#let intersection(pt) = { #let intersection(pt, radius: .2, fill: black) = {
draw.circle(pt, radius: .2, stroke: none, fill: black) draw.circle(pt, radius: radius, stroke: none, fill: fill)
} }
#let get-direct-wire(pts) = { #let get-direct-wire(pts) = {
@ -19,17 +19,26 @@
return (pts, anchors) return (pts, anchors)
} }
#let get-zigzag-wire(pts, ratio) = { #let get-zigzag-wire(pts, ratio, dir) = {
let start = pts.first() let start = pts.first()
let end = pts.last() let end = pts.last()
let mid = (start, ratio, end) let mid = (start, ratio, end)
let points = ( let points = if dir == "vertical" {
start, (
(horizontal: mid, vertical: ()), start,
(horizontal: (), vertical: end), (horizontal: mid, vertical: ()),
end (horizontal: (), vertical: end),
) end
)
} else {
(
start,
(horizontal: (), vertical: mid),
(horizontal: end, vertical: ()),
end
)
}
let anchors = ( let anchors = (
"start": start, "start": start,
"zig": points.at(1), "zig": points.at(1),
@ -102,7 +111,10 @@
/// - dashed (bool): Whether the stroke is dashed or not /// - dashed (bool): Whether the stroke is dashed or not
/// - style (str): The wire's style (see #doc-ref("wire.wire-styles", var: true) for possible values) /// - style (str): The wire's style (see #doc-ref("wire.wire-styles", var: true) for possible values)
/// - reverse (bool): If true, the start and end points will be swapped (useful in cases where the start point depends on the end point, for example with perpendiculars) /// - reverse (bool): If true, the start and end points will be swapped (useful in cases where the start point depends on the end point, for example with perpendiculars)
/// - directed (bool): If true, the wire will be directed, meaning an arrow will be drawn at the endpoint
/// - rotate-name (bool): If true, the name will be rotated according to the wire's slope
/// - zigzag-ratio (ratio): Position of the zigzag vertical relative to the horizontal span (only with style "zigzag") /// - zigzag-ratio (ratio): Position of the zigzag vertical relative to the horizontal span (only with style "zigzag")
/// - zigzag-dir (str): The zigzag's direction. As either "vertical" or "horizontal" (only with dstyle "zigzag")
/// - dodge-y (number): Y position to dodge the wire to (only with style "dodge") /// - dodge-y (number): Y position to dodge the wire to (only with style "dodge")
/// - dodge-sides (array): The start and end sides (going out of the connected element) of the wire (only with style "dodge") /// - dodge-sides (array): The start and end sides (going out of the connected element) of the wire (only with style "dodge")
/// - dodge-margins (array): The start and end margins (i.e. space before dodging) of the wire (only with style "dodge") /// - dodge-margins (array): The start and end margins (i.e. space before dodging) of the wire (only with style "dodge")
@ -116,7 +128,10 @@
dashed: false, dashed: false,
style: "direct", style: "direct",
reverse: false, reverse: false,
directed: false,
rotate-name: true,
zigzag-ratio: 50%, zigzag-ratio: 50%,
zigzag-dir: "vertical",
dodge-y: 0, dodge-y: 0,
dodge-sides: ("east", "west"), dodge-sides: ("east", "west"),
dodge-margins: (5%, 5%) dodge-margins: (5%, 5%)
@ -144,7 +159,7 @@
(points, anchors) = get-direct-wire(pts) (points, anchors) = get-direct-wire(pts)
} else if style == "zigzag" { } else if style == "zigzag" {
(points, anchors) = get-zigzag-wire(pts, zigzag-ratio) (points, anchors) = get-zigzag-wire(pts, zigzag-ratio, zigzag-dir)
} else if style == "dodge" { } else if style == "dodge" {
(points, anchors) = get-dodge-wire( (points, anchors) = get-dodge-wire(
@ -156,8 +171,12 @@
) )
} }
let mark = (fill: color)
if directed {
mark = (end: ">", fill: color)
}
draw.group(name: id, { draw.group(name: id, {
draw.line(..points, stroke: stroke) draw.line(..points, stroke: stroke, mark: mark)
for (anchor-name, anchor-pos) in anchors { for (anchor-name, anchor-pos) in anchors {
draw.anchor(anchor-name, anchor-pos) draw.anchor(anchor-name, anchor-pos)
} }
@ -165,8 +184,20 @@
let first-pt = id + ".start" let first-pt = id + ".start"
let last-pt = id + ".end" let last-pt = id + ".end"
let first-pos = points.first()
let second-pos = points.at(1)
if reverse { if reverse {
(first-pt, last-pt) = (last-pt, first-pt) (first-pt, last-pt) = (last-pt, first-pt)
(first-pos, second-pos) = (second-pos, first-pos)
}
let angle = 0deg
if rotate-name {
(ctx, first-pos) = coordinate.resolve(ctx, first-pos)
(ctx, second-pos) = coordinate.resolve(ctx, second-pos)
let (x1, y1, _) = first-pos
let (x2, y2, _) = second-pos
angle = calc.atan2(x2 - x1, y2 - y1)
} }
if name != none { if name != none {
@ -199,7 +230,7 @@
anchor = "south-east" anchor = "south-east"
} }
draw.content(point, anchor: anchor, padding: 3pt, name) draw.content(point, anchor: anchor, padding: 3pt, angle: angle, name)
} }
} }