zntg/ui/length.go

78 lines
1.4 KiB
Go

package ui
import "opslag.de/schobers/geom"
type Length struct {
float32
}
func (l *Length) Value() float32 {
if l == nil {
return 0
}
return l.float32
}
func (l *Length) Zero(value float32) float32 {
if l == nil {
return value
}
return l.Value()
}
func Fixed(l float32) *Length { return &Length{l} }
func Infinite() *Length { return &Length{geom.NaN32()} }
func ZL() *Length { return &Length{0} }
type SideLengths struct {
Left *Length
Top *Length
Right *Length
Bottom *Length
}
func (l SideLengths) InsetRect(r geom.RectangleF32) geom.RectangleF32 {
return Sides{
Left: l.Left.Value(),
Top: l.Top.Value(),
Right: l.Right.Value(),
Bottom: l.Bottom.Value(),
}.InsetRect(r)
}
func (l SideLengths) Zero(value float32) Sides {
return Sides{
Left: l.Left.Zero(value),
Top: l.Top.Zero(value),
Right: l.Right.Zero(value),
Bottom: l.Bottom.Zero(value),
}
}
type Sides struct {
Left float32
Top float32
Right float32
Bottom float32
}
func (s Sides) InsetRect(r geom.RectangleF32) geom.RectangleF32 {
if r.Dx() < (s.Left + s.Right) {
r.Min.X += r.Dx() * s.Left / (s.Left + s.Right)
r.Max.X = r.Min.X
} else {
r.Min.X += s.Left
r.Max.X -= s.Right
}
if r.Dy() < (s.Top + s.Bottom) {
r.Min.Y += r.Dy() * s.Top / (s.Top + s.Bottom)
r.Max.Y = r.Min.Y
} else {
r.Min.Y += s.Top
r.Max.Y -= s.Bottom
}
return r
}