refactor, fix
This commit is contained in:
parent
69405167be
commit
d3721fd4f0
2 changed files with 47 additions and 37 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package d2graph
|
package d2graph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -285,6 +286,49 @@ func (obj *Object) GetModifierElementAdjustments() (dx, dy float64) {
|
||||||
return dx, dy
|
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 {
|
func (obj *Object) ToShape() shape.Shape {
|
||||||
tl := obj.TopLeft
|
tl := obj.TopLeft
|
||||||
if tl == nil {
|
if tl == nil {
|
||||||
|
|
|
||||||
|
|
@ -845,41 +845,7 @@ func (gd *gridDiagram) sizeForOutsideLabels() (revertTemp func()) {
|
||||||
margins := make(map[*d2graph.Object]geo.Spacing)
|
margins := make(map[*d2graph.Object]geo.Spacing)
|
||||||
|
|
||||||
for _, o := range gd.objects {
|
for _, o := range gd.objects {
|
||||||
margin := geo.Spacing{}
|
margin := o.GetMargin()
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if margin.Top > 0 {
|
if margin.Top > 0 {
|
||||||
o.Height += margin.Top
|
o.Height += margin.Top
|
||||||
|
|
@ -907,8 +873,8 @@ func (gd *gridDiagram) sizeForOutsideLabels() (revertTemp func()) {
|
||||||
o.Height -= margin.Top + margin.Bottom
|
o.Height -= margin.Top + margin.Bottom
|
||||||
o.Width -= margin.Left + margin.Right
|
o.Width -= margin.Left + margin.Right
|
||||||
|
|
||||||
if margin.Top > 0 || margin.Left > 0 {
|
if margin.Left > 0 || margin.Top > 0 {
|
||||||
o.MoveWithDescendants(margin.Top, margin.Left)
|
o.MoveWithDescendants(margin.Left, margin.Top)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue