support ratio layouts
This commit is contained in:
63
ui/ui.go
63
ui/ui.go
@@ -12,10 +12,11 @@ type Drawable interface {
|
||||
}
|
||||
|
||||
type Offsets struct {
|
||||
Top int
|
||||
Bottom int
|
||||
Left int
|
||||
Right int
|
||||
Top int
|
||||
Bottom int
|
||||
Left int
|
||||
Right int
|
||||
Percent int
|
||||
}
|
||||
|
||||
type Contents []struct {
|
||||
@@ -27,6 +28,8 @@ const (
|
||||
LayoutUnmanaged = iota
|
||||
LayoutHorizontalEven
|
||||
LayoutVerticalEven
|
||||
LayoutHorizontalPercent
|
||||
LayoutVerticalPercent
|
||||
)
|
||||
|
||||
// A Container has no visible UI of its own, but arranges sub-components on the screen.
|
||||
@@ -87,6 +90,58 @@ func (c *Container) SetSize(x, y, h, w int) {
|
||||
}
|
||||
c.contents[r].Container.SetSize(x, y, h, w)
|
||||
}
|
||||
} else if c.layoutMethod == LayoutHorizontalPercent {
|
||||
// first, work out overall distribution
|
||||
total := 0
|
||||
for r := range c.contents {
|
||||
// `0` or negatives are set as minimum
|
||||
if c.contents[r].Offsets.Percent < 1 {
|
||||
total += 1
|
||||
} else {
|
||||
total += c.contents[r].Offsets.Percent
|
||||
}
|
||||
}
|
||||
carry := 0
|
||||
// push around containers
|
||||
for r := range c.contents {
|
||||
ratio := (float64(c.contents[r].Offsets.Percent) / float64(total))
|
||||
w := int(float64(c.w) * ratio)
|
||||
h := c.h
|
||||
x := c.x + carry
|
||||
y := c.y
|
||||
carry += w
|
||||
// and add any remaining space to the last container
|
||||
if r == len(c.contents)-1 {
|
||||
w += (c.w - carry)
|
||||
}
|
||||
c.contents[r].Container.SetSize(x, y, h, w)
|
||||
}
|
||||
} else if c.layoutMethod == LayoutVerticalPercent {
|
||||
// first, work out overall distribution
|
||||
total := 0
|
||||
for r := range c.contents {
|
||||
// `0` or negatives are set as minimum
|
||||
if c.contents[r].Offsets.Percent < 1 {
|
||||
total += 1
|
||||
} else {
|
||||
total += c.contents[r].Offsets.Percent
|
||||
}
|
||||
}
|
||||
carry := 0
|
||||
// push around containers
|
||||
for r := range c.contents {
|
||||
ratio := (float64(c.contents[r].Offsets.Percent) / float64(total))
|
||||
w := c.w
|
||||
h := int(float64(c.h) * ratio)
|
||||
x := c.x
|
||||
y := c.y + carry
|
||||
carry += h
|
||||
// and add any remaining space to the last container
|
||||
if r == len(c.contents)-1 {
|
||||
h += (c.h - carry)
|
||||
}
|
||||
c.contents[r].Container.SetSize(x, y, h, w)
|
||||
}
|
||||
} else {
|
||||
for r := range c.contents {
|
||||
x := c.x + c.contents[r].Offsets.Left
|
||||
|
Reference in New Issue
Block a user