2023-02-01 23:47:31 -07:00
|
|
|
package basicLayouts
|
2023-01-10 21:20:42 -07:00
|
|
|
|
|
|
|
import "image"
|
2023-03-16 18:55:11 -06:00
|
|
|
import "golang.org/x/image/math/fixed"
|
2023-03-03 23:42:14 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-02-01 23:47:31 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/layouts"
|
2023-03-16 18:55:11 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
|
2023-01-10 21:20:42 -07:00
|
|
|
|
|
|
|
// Horizontal arranges elements horizontally. Elements at the start of the entry
|
|
|
|
// list will be positioned on the left, and elements at the end of the entry
|
|
|
|
// list will positioned on the right. All elements have the same height.
|
|
|
|
type Horizontal struct {
|
|
|
|
// If Gap is true, a gap will be placed between each element.
|
|
|
|
Gap bool
|
|
|
|
|
|
|
|
// If Pad is true, there will be padding running along the inside of the
|
|
|
|
// layout's border.
|
|
|
|
Pad bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arrange arranges a list of entries horizontally.
|
2023-02-01 23:47:31 -07:00
|
|
|
func (layout Horizontal) Arrange (
|
|
|
|
entries []layouts.LayoutEntry,
|
2023-03-03 23:42:14 -07:00
|
|
|
margin image.Point,
|
|
|
|
padding artist.Inset,
|
2023-02-01 23:47:31 -07:00
|
|
|
bounds image.Rectangle,
|
|
|
|
) {
|
2023-03-03 23:42:14 -07:00
|
|
|
if layout.Pad { bounds = padding.Apply(bounds) }
|
2023-01-31 16:06:55 -07:00
|
|
|
|
2023-01-17 13:31:59 -07:00
|
|
|
// get width of expanding elements
|
2023-02-01 23:47:31 -07:00
|
|
|
expandingElementWidth := layout.expandingElementWidth (
|
2023-03-02 15:58:42 -07:00
|
|
|
entries, margin, padding, bounds.Dx())
|
2023-01-10 21:20:42 -07:00
|
|
|
|
|
|
|
// set the size and position of each element
|
2023-03-16 18:55:11 -06:00
|
|
|
dot := fixedutil.Pt(bounds.Min)
|
2023-01-10 21:20:42 -07:00
|
|
|
for index, entry := range entries {
|
2023-03-16 18:55:11 -06:00
|
|
|
if index > 0 && layout.Gap { dot.X += fixed.I(margin.X) }
|
2023-01-10 21:20:42 -07:00
|
|
|
|
2023-03-16 18:55:11 -06:00
|
|
|
entry.Bounds.Min = fixedutil.FloorPt(dot)
|
|
|
|
entryWidth := fixed.Int26_6(0)
|
2023-01-10 21:20:42 -07:00
|
|
|
if entry.Expand {
|
|
|
|
entryWidth = expandingElementWidth
|
|
|
|
} else {
|
2023-03-16 18:55:11 -06:00
|
|
|
min, _ := entry.MinimumSize()
|
|
|
|
entryWidth = fixed.I(min)
|
2023-01-10 21:20:42 -07:00
|
|
|
}
|
2023-01-31 16:06:55 -07:00
|
|
|
dot.X += entryWidth
|
2023-03-16 18:55:11 -06:00
|
|
|
entry.Bounds.Max = entry.Bounds.Min.Add (
|
|
|
|
image.Pt(entryWidth.Floor(), bounds.Dy()))
|
2023-01-31 16:06:55 -07:00
|
|
|
|
|
|
|
entries[index] = entry
|
2023-01-10 21:20:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MinimumSize returns the minimum width and height that will be needed to
|
|
|
|
// arrange the given list of entries.
|
2023-01-15 22:09:01 -07:00
|
|
|
func (layout Horizontal) MinimumSize (
|
2023-02-01 23:47:31 -07:00
|
|
|
entries []layouts.LayoutEntry,
|
2023-03-03 23:42:14 -07:00
|
|
|
margin image.Point,
|
|
|
|
padding artist.Inset,
|
2023-01-15 22:09:01 -07:00
|
|
|
) (
|
|
|
|
width, height int,
|
|
|
|
) {
|
2023-01-10 21:20:42 -07:00
|
|
|
for index, entry := range entries {
|
|
|
|
entryWidth, entryHeight := entry.MinimumSize()
|
|
|
|
if entryHeight > height {
|
|
|
|
height = entryHeight
|
|
|
|
}
|
|
|
|
width += entryWidth
|
|
|
|
if layout.Gap && index > 0 {
|
2023-03-03 23:42:14 -07:00
|
|
|
width += margin.X
|
2023-01-10 21:20:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if layout.Pad {
|
2023-03-03 23:42:14 -07:00
|
|
|
width += padding.Horizontal()
|
|
|
|
height += padding.Vertical()
|
2023-01-10 21:20:42 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-01-16 21:58:20 -07:00
|
|
|
|
2023-01-17 13:31:59 -07:00
|
|
|
func (layout Horizontal) expandingElementWidth (
|
2023-02-01 23:47:31 -07:00
|
|
|
entries []layouts.LayoutEntry,
|
2023-03-03 23:42:14 -07:00
|
|
|
margin image.Point,
|
|
|
|
padding artist.Inset,
|
2023-01-17 13:31:59 -07:00
|
|
|
freeSpace int,
|
|
|
|
) (
|
2023-03-16 18:55:11 -06:00
|
|
|
width fixed.Int26_6,
|
2023-01-17 13:31:59 -07:00
|
|
|
) {
|
|
|
|
expandingElements := 0
|
|
|
|
|
|
|
|
// count the number of expanding elements and the amount of free space
|
|
|
|
// for them to collectively occupy
|
|
|
|
for index, entry := range entries {
|
|
|
|
if entry.Expand {
|
|
|
|
expandingElements ++
|
|
|
|
} else {
|
|
|
|
entryMinWidth, _ := entry.MinimumSize()
|
|
|
|
freeSpace -= entryMinWidth
|
|
|
|
}
|
|
|
|
if index > 0 && layout.Gap {
|
2023-03-03 23:42:14 -07:00
|
|
|
freeSpace -= margin.X
|
2023-01-17 13:31:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if expandingElements > 0 {
|
2023-03-16 18:55:11 -06:00
|
|
|
width = fixed.I(freeSpace) / fixed.Int26_6(expandingElements)
|
2023-01-17 13:31:59 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|