refactor, fix

This commit is contained in:
Gavin Nishizawa 2023-09-26 17:38:06 -07:00
parent 69405167be
commit d3721fd4f0
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
2 changed files with 47 additions and 37 deletions

View file

@ -1,6 +1,7 @@
package d2graph
import (
"math"
"sort"
"strings"
@ -285,6 +286,49 @@ func (obj *Object) GetModifierElementAdjustments() (dx, dy float64) {
return dx, dy
}
func (obj *Object) GetMargin() geo.Spacing {
margin := geo.Spacing{}
if obj.HasLabel() && obj.LabelPosition != nil {
position := label.Position(*obj.LabelPosition)
labelWidth := float64(obj.LabelDimensions.Width + label.PADDING)
labelHeight := float64(obj.LabelDimensions.Height + label.PADDING)
switch position {
case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight:
margin.Top = labelHeight
case label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight:
margin.Bottom = labelHeight
case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom:
margin.Left = labelWidth
case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom:
margin.Right = labelWidth
}
}
if obj.Icon != nil && obj.IconPosition != nil && obj.Shape.Value != d2target.ShapeImage {
position := label.Position(*obj.IconPosition)
iconSize := float64(d2target.MAX_ICON_SIZE + label.PADDING)
switch position {
case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight:
margin.Top = math.Max(margin.Top, iconSize)
case label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight:
margin.Bottom = math.Max(margin.Bottom, iconSize)
case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom:
margin.Left = math.Max(margin.Left, iconSize)
case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom:
margin.Right = math.Max(margin.Right, iconSize)
}
}
dx, dy := obj.GetModifierElementAdjustments()
margin.Right += dx
margin.Top += dy
return margin
}
func (obj *Object) ToShape() shape.Shape {
tl := obj.TopLeft
if tl == nil {

View file

@ -845,41 +845,7 @@ func (gd *gridDiagram) sizeForOutsideLabels() (revertTemp func()) {
margins := make(map[*d2graph.Object]geo.Spacing)
for _, o := range gd.objects {
margin := geo.Spacing{}
if o.HasLabel() && o.LabelPosition != nil {
position := label.Position(*o.LabelPosition)
labelWidth := float64(o.LabelDimensions.Width + 2*label.PADDING)
labelHeight := float64(o.LabelDimensions.Height + 2*label.PADDING)
switch position {
case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight:
margin.Top = labelHeight
case label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight:
margin.Bottom = labelHeight
case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom:
margin.Left = labelWidth
case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom:
margin.Right = labelWidth
}
}
if o.Icon != nil && o.IconPosition != nil && o.Shape.Value != d2target.ShapeImage {
position := label.Position(*o.IconPosition)
iconSize := float64(d2target.MAX_ICON_SIZE + 2*label.PADDING)
switch position {
case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight:
margin.Top = math.Max(margin.Top, iconSize)
case label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight:
margin.Bottom = math.Max(margin.Bottom, iconSize)
case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom:
margin.Left = math.Max(margin.Left, iconSize)
case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom:
margin.Right = math.Max(margin.Right, iconSize)
}
}
margin := o.GetMargin()
if margin.Top > 0 {
o.Height += margin.Top
@ -907,8 +873,8 @@ func (gd *gridDiagram) sizeForOutsideLabels() (revertTemp func()) {
o.Height -= margin.Top + margin.Bottom
o.Width -= margin.Left + margin.Right
if margin.Top > 0 || margin.Left > 0 {
o.MoveWithDescendants(margin.Top, margin.Left)
if margin.Left > 0 || margin.Top > 0 {
o.MoveWithDescendants(margin.Left, margin.Top)
}
}
}