fixed extender ports + added align-out param

This commit is contained in:
Louis Heredero 2024-05-18 16:46:18 +02:00
parent e4194d0925
commit 7da2bb2b64
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7

View File

@ -1,14 +1,23 @@
#import "@preview/cetz:0.2.2": draw #import "@preview/cetz:0.2.2": draw
#import "element.typ" #import "element.typ"
#import "ports.typ": add-port
#let draw-shape(id, tl, tr, br, bl, fill, stroke) = { #let draw-shape(id, tl, tr, br, bl, fill, stroke, h-ratio: 75%, align-out: true) = {
let (x, y) = bl let (x, y) = bl
let (width, height) = (tr.at(0) - x, tr.at(1) - y) let (width, height) = (tr.at(0) - x, tr.at(1) - y)
tl = (x, y + height * 0.75) let ratio = h-ratio / 100%
let tr2 = (x + width, y + height * 0.75)
tl = (x, y + height * ratio)
let tr2 = (x + width, y + height * ratio)
let br = (x + width, y) let br = (x + width, y)
if align-out {
(tr, tr2) = (tr2, tr) (tr, tr2) = (tr2, tr)
} else {
(tr, tr2) = (tr, tr)
}
let f = draw.group(name: id, { let f = draw.group(name: id, {
draw.merge-path( draw.merge-path(
inset: 0.5em, inset: 0.5em,
@ -28,7 +37,9 @@
/// Draws a bit extender /// Draws a bit extender
/// ///
/// #examples.extender /// #examples.extender
/// For parameters description, see #doc-ref("element.elmt") /// For other parameters description, see #doc-ref("element.elmt")
/// - h-ratio (ratio): The height ratio of the left side relative to the full height
/// - align-out (bool): If true, the output and input ports are aligned, otherwise, the output port is centered on the right side
#let extender( #let extender(
x: none, x: none,
y: none, y: none,
@ -39,6 +50,8 @@
fill: none, fill: none,
stroke: black + 1pt, stroke: black + 1pt,
id: "", id: "",
h-ratio: 75%,
align-out: true,
debug: ( debug: (
ports: false ports: false
) )
@ -51,9 +64,14 @@
(id: "out"), (id: "out"),
) )
) )
let out-pct = if align-out {h-ratio / 2} else {50%}
let ports-y = (
"in": (h) => {h - h * (h-ratio / 200%)},
"out": (h) => {h * (out-pct / 100%)}
)
element.elmt( element.elmt(
draw-shape: draw-shape, draw-shape: draw-shape.with(h-ratio: h-ratio, align-out: align-out),
x: x, x: x,
y: y, y: y,
w: w, w: w,
@ -61,9 +79,16 @@
name: name, name: name,
name-anchor: name-anchor, name-anchor: name-anchor,
ports: ports, ports: ports,
auto-ports: false,
ports-y: ports-y,
fill: fill, fill: fill,
stroke: stroke, stroke: stroke,
id: id, id: id,
debug: debug debug: debug
) )
let in-pos = (rel: (0, h * (h-ratio / 200%)), to: id+".south-west")
let out-pos = (id+".south-east", out-pct, id+".north-east")
add-port(id, "west", ports.west.first(), in-pos)
add-port(id, "east", ports.east.first(), out-pos)
} }