2022-11-03 13:54:49 +00:00
|
|
|
package shape
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-25 01:32:42 +00:00
|
|
|
"math"
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/svg"
|
2023-02-10 19:03:07 +00:00
|
|
|
"oss.terrastruct.com/util-go/go2"
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type shapeQueue struct {
|
|
|
|
|
*baseShape
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewQueue(box *geo.Box) Shape {
|
2023-02-10 19:03:07 +00:00
|
|
|
shape := shapeQueue{
|
2022-11-03 13:54:49 +00:00
|
|
|
baseShape: &baseShape{
|
|
|
|
|
Type: QUEUE_TYPE,
|
|
|
|
|
Box: box,
|
|
|
|
|
},
|
|
|
|
|
}
|
2023-02-10 19:03:07 +00:00
|
|
|
shape.FullShape = go2.Pointer(Shape(shape))
|
|
|
|
|
return shape
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
func getArcWidth(box *geo.Box) float64 {
|
|
|
|
|
arcWidth := defaultArcDepth
|
|
|
|
|
// Note: box width should always be larger than 3*default
|
|
|
|
|
// this just handles after collaping into an oval
|
|
|
|
|
if box.Width < arcWidth*2 {
|
|
|
|
|
arcWidth = box.Width / 2.0
|
|
|
|
|
}
|
|
|
|
|
return arcWidth
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func (s shapeQueue) GetInnerBox() *geo.Box {
|
|
|
|
|
width := s.Box.Width
|
|
|
|
|
tl := s.Box.TopLeft.Copy()
|
2023-01-21 04:04:59 +00:00
|
|
|
arcWidth := getArcWidth(s.Box)
|
|
|
|
|
width -= 3 * arcWidth
|
|
|
|
|
tl.X += arcWidth
|
2022-11-03 13:54:49 +00:00
|
|
|
return geo.NewBox(tl, width, s.Box.Height)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func queueOuterPath(box *geo.Box) *svg.SvgPathContext {
|
2023-01-21 04:04:59 +00:00
|
|
|
arcWidth := getArcWidth(box)
|
2022-11-03 13:54:49 +00:00
|
|
|
multiplier := 0.45
|
|
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.StartAt(pc.Absolute(arcWidth, 0))
|
|
|
|
|
pc.H(true, box.Width-2*arcWidth)
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.C(false, box.Width, 0, box.Width, box.Height*multiplier, box.Width, box.Height/2.0)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.C(false, box.Width, box.Height-box.Height*multiplier, box.Width, box.Height, box.Width-arcWidth, box.Height)
|
|
|
|
|
pc.H(true, -1*(box.Width-2*arcWidth))
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.C(false, 0, box.Height, 0, box.Height-box.Height*multiplier, 0, box.Height/2.0)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.C(false, 0, box.Height*multiplier, 0, 0, arcWidth, 0)
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.Z()
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func queueInnerPath(box *geo.Box) *svg.SvgPathContext {
|
2023-01-21 04:04:59 +00:00
|
|
|
arcWidth := getArcWidth(box)
|
2022-11-03 13:54:49 +00:00
|
|
|
multiplier := 0.45
|
|
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.StartAt(pc.Absolute(box.Width-arcWidth, 0))
|
|
|
|
|
pc.C(false, box.Width-2*arcWidth, 0, box.Width-2*arcWidth, box.Height*multiplier, box.Width-2*arcWidth, box.Height/2.0)
|
|
|
|
|
pc.C(false, box.Width-2*arcWidth, box.Height-box.Height*multiplier, box.Width-2*arcWidth, box.Height, box.Width-arcWidth, box.Height)
|
2022-11-03 13:54:49 +00:00
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeQueue) Perimeter() []geo.Intersectable {
|
|
|
|
|
return queueOuterPath(s.Box).Path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeQueue) GetSVGPathData() []string {
|
|
|
|
|
return []string{
|
|
|
|
|
queueOuterPath(s.Box).PathData(),
|
|
|
|
|
queueInnerPath(s.Box).PathData(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-21 04:04:59 +00:00
|
|
|
|
2023-01-23 18:32:12 +00:00
|
|
|
func (s shapeQueue) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
|
2023-01-21 04:04:59 +00:00
|
|
|
// 1 arc left, width+ padding, 2 arcs right
|
2023-01-23 18:32:12 +00:00
|
|
|
totalWidth := 3*defaultArcDepth + width + paddingX
|
2023-01-25 01:32:42 +00:00
|
|
|
return math.Ceil(totalWidth), math.Ceil(height + paddingY)
|
2023-01-21 04:04:59 +00:00
|
|
|
}
|
2023-01-24 03:10:31 +00:00
|
|
|
|
|
|
|
|
func (s shapeQueue) GetDefaultPadding() (paddingX, paddingY float64) {
|
|
|
|
|
return defaultPadding / 2, defaultPadding
|
|
|
|
|
}
|