ecs #15
@ -1,152 +0,0 @@
|
||||
package layouts
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||
|
||||
// Dialog arranges elements in the form of a dialog box. The first element is
|
||||
// positioned above as the main focus of the dialog, and is set to expand
|
||||
// regardless of whether it is expanding or not. The remaining elements are
|
||||
// arranged at the bottom in a row called the control row, which is aligned to
|
||||
// the right, the last element being the rightmost one.
|
||||
type Dialog struct {
|
||||
// If Mergin is true, a margin 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 into a dialog.
|
||||
func (layout Dialog) Arrange (
|
||||
entries []tomo.LayoutEntry,
|
||||
margin image.Point,
|
||||
padding artist.Inset,
|
||||
bounds image.Rectangle,
|
||||
) {
|
||||
if layout.Pad { bounds = padding.Apply(bounds) }
|
||||
|
||||
controlRowWidth, controlRowHeight := 0, 0
|
||||
if len(entries) > 1 {
|
||||
controlRowWidth,
|
||||
controlRowHeight = layout.minimumSizeOfControlRow (
|
||||
entries[1:], margin, padding)
|
||||
}
|
||||
|
||||
if len(entries) > 0 {
|
||||
main := entries[0]
|
||||
main.Bounds.Min = bounds.Min
|
||||
mainHeight := bounds.Dy() - controlRowHeight
|
||||
if layout.Gap {
|
||||
mainHeight -= margin.Y
|
||||
}
|
||||
main.Bounds.Max = main.Bounds.Min.Add(image.Pt(bounds.Dx(), mainHeight))
|
||||
entries[0] = main
|
||||
}
|
||||
|
||||
if len(entries) > 1 {
|
||||
freeSpace := bounds.Dx()
|
||||
expandingElements := 0
|
||||
|
||||
// count the number of expanding elements and the amount of free
|
||||
// space for them to collectively occupy
|
||||
for index, entry := range entries[1:] {
|
||||
if entry.Expand {
|
||||
expandingElements ++
|
||||
} else {
|
||||
entryMinWidth, _ := entry.MinimumSize()
|
||||
freeSpace -= entryMinWidth
|
||||
}
|
||||
if index > 0 && layout.Gap {
|
||||
freeSpace -= margin.X
|
||||
}
|
||||
}
|
||||
expandingElementWidth := 0
|
||||
if expandingElements > 0 {
|
||||
expandingElementWidth = freeSpace / expandingElements
|
||||
}
|
||||
|
||||
// determine starting position and dimensions for control row
|
||||
dot := image.Pt(bounds.Min.X, bounds.Max.Y - controlRowHeight)
|
||||
if expandingElements == 0 {
|
||||
dot.X = bounds.Max.X - controlRowWidth
|
||||
}
|
||||
|
||||
// set the size and position of each element in the control row
|
||||
for index, entry := range entries[1:] {
|
||||
if index > 0 && layout.Gap { dot.X += margin.X }
|
||||
|
||||
entry.Bounds.Min = dot
|
||||
entryWidth := 0
|
||||
if entry.Expand {
|
||||
entryWidth = expandingElementWidth
|
||||
} else {
|
||||
entryWidth, _ = entry.MinimumSize()
|
||||
}
|
||||
dot.X += entryWidth
|
||||
entryBounds := entry.Bounds
|
||||
if entryBounds.Dy() != controlRowHeight ||
|
||||
entryBounds.Dx() != entryWidth {
|
||||
entry.Bounds.Max = entryBounds.Min.Add (
|
||||
image.Pt(entryWidth, controlRowHeight))
|
||||
}
|
||||
entries[index + 1] = entry
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// MinimumSize returns the minimum width and height that will be needed to
|
||||
// arrange the given list of entries.
|
||||
func (layout Dialog) MinimumSize (
|
||||
entries []tomo.LayoutEntry,
|
||||
margin image.Point,
|
||||
padding artist.Inset,
|
||||
) (
|
||||
width, height int,
|
||||
) {
|
||||
if len(entries) > 0 {
|
||||
mainChildHeight := 0
|
||||
width, mainChildHeight = entries[0].MinimumSize()
|
||||
height += mainChildHeight
|
||||
}
|
||||
|
||||
if len(entries) > 1 {
|
||||
if layout.Gap { height += margin.X }
|
||||
additionalWidth,
|
||||
additionalHeight := layout.minimumSizeOfControlRow (
|
||||
entries[1:], margin, padding)
|
||||
height += additionalHeight
|
||||
if additionalWidth > width {
|
||||
width = additionalWidth
|
||||
}
|
||||
}
|
||||
|
||||
if layout.Pad {
|
||||
width += padding.Horizontal()
|
||||
height += padding.Vertical()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (layout Dialog) minimumSizeOfControlRow (
|
||||
entries []tomo.LayoutEntry,
|
||||
margin image.Point,
|
||||
padding artist.Inset,
|
||||
) (
|
||||
width, height int,
|
||||
) {
|
||||
for index, entry := range entries {
|
||||
entryWidth, entryHeight := entry.MinimumSize()
|
||||
if entryHeight > height {
|
||||
height = entryHeight
|
||||
}
|
||||
width += entryWidth
|
||||
if layout.Gap && index > 0 {
|
||||
width += margin.X
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
// Package layouts provides a set of pre-made layouts.
|
||||
package layouts
|
@ -1,110 +0,0 @@
|
||||
package layouts
|
||||
|
||||
import "image"
|
||||
import "golang.org/x/image/math/fixed"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
|
||||
|
||||
// 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.
|
||||
func (layout Horizontal) Arrange (
|
||||
entries []tomo.LayoutEntry,
|
||||
margin image.Point,
|
||||
padding artist.Inset,
|
||||
bounds image.Rectangle,
|
||||
) {
|
||||
if layout.Pad { bounds = padding.Apply(bounds) }
|
||||
|
||||
// get width of expanding elements
|
||||
expandingElementWidth := layout.expandingElementWidth (
|
||||
entries, margin, padding, bounds.Dx())
|
||||
|
||||
// set the size and position of each element
|
||||
dot := fixedutil.Pt(bounds.Min)
|
||||
for index, entry := range entries {
|
||||
if index > 0 && layout.Gap { dot.X += fixed.I(margin.X) }
|
||||
|
||||
entry.Bounds.Min = fixedutil.FloorPt(dot)
|
||||
entryWidth := fixed.Int26_6(0)
|
||||
if entry.Expand {
|
||||
entryWidth = expandingElementWidth
|
||||
} else {
|
||||
min, _ := entry.MinimumSize()
|
||||
entryWidth = fixed.I(min)
|
||||
}
|
||||
dot.X += entryWidth
|
||||
entry.Bounds.Max = entry.Bounds.Min.Add (
|
||||
image.Pt(entryWidth.Floor(), bounds.Dy()))
|
||||
|
||||
entries[index] = entry
|
||||
}
|
||||
}
|
||||
|
||||
// MinimumSize returns the minimum width and height that will be needed to
|
||||
// arrange the given list of entries.
|
||||
func (layout Horizontal) MinimumSize (
|
||||
entries []tomo.LayoutEntry,
|
||||
margin image.Point,
|
||||
padding artist.Inset,
|
||||
) (
|
||||
width, height int,
|
||||
) {
|
||||
for index, entry := range entries {
|
||||
entryWidth, entryHeight := entry.MinimumSize()
|
||||
if entryHeight > height {
|
||||
height = entryHeight
|
||||
}
|
||||
width += entryWidth
|
||||
if layout.Gap && index > 0 {
|
||||
width += margin.X
|
||||
}
|
||||
}
|
||||
|
||||
if layout.Pad {
|
||||
width += padding.Horizontal()
|
||||
height += padding.Vertical()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (layout Horizontal) expandingElementWidth (
|
||||
entries []tomo.LayoutEntry,
|
||||
margin image.Point,
|
||||
padding artist.Inset,
|
||||
freeSpace int,
|
||||
) (
|
||||
width fixed.Int26_6,
|
||||
) {
|
||||
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 {
|
||||
freeSpace -= margin.X
|
||||
}
|
||||
}
|
||||
|
||||
if expandingElements > 0 {
|
||||
width = fixed.I(freeSpace) / fixed.Int26_6(expandingElements)
|
||||
}
|
||||
return
|
||||
}
|
@ -1,113 +0,0 @@
|
||||
package layouts
|
||||
|
||||
import "image"
|
||||
import "golang.org/x/image/math/fixed"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
|
||||
|
||||
// Vertical arranges elements vertically. Elements at the start of the entry
|
||||
// list will be positioned at the top, and elements at the end of the entry list
|
||||
// will positioned at the bottom. All elements have the same width.
|
||||
type Vertical 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 vertically.
|
||||
func (layout Vertical) Arrange (
|
||||
entries []tomo.LayoutEntry,
|
||||
margin image.Point,
|
||||
padding artist.Inset,
|
||||
bounds image.Rectangle,
|
||||
) {
|
||||
if layout.Pad { bounds = padding.Apply(bounds) }
|
||||
|
||||
// get height of expanding elements
|
||||
expandingElementHeight, minimumHeights := layout.expandingElementHeight (
|
||||
entries, margin, padding, bounds.Dy())
|
||||
|
||||
// set the size and position of each element
|
||||
dot := fixedutil.Pt(bounds.Min)
|
||||
for index, entry := range entries {
|
||||
if index > 0 && layout.Gap { dot.Y += fixed.I(margin.Y) }
|
||||
|
||||
entry.Bounds.Min = fixedutil.FloorPt(dot)
|
||||
entryHeight := fixed.Int26_6(0)
|
||||
if entry.Expand {
|
||||
entryHeight = expandingElementHeight
|
||||
} else {
|
||||
entryHeight = fixed.I(minimumHeights[index])
|
||||
}
|
||||
dot.Y += entryHeight
|
||||
entryBounds := entry.Bounds
|
||||
entry.Bounds.Max = entryBounds.Min.Add (
|
||||
image.Pt(bounds.Dx(),
|
||||
entryHeight.Floor()))
|
||||
entries[index] = entry
|
||||
}
|
||||
}
|
||||
|
||||
// MinimumSize returns the minimum width and height that will be needed to
|
||||
// arrange the given list of entries.
|
||||
func (layout Vertical) MinimumSize (
|
||||
entries []tomo.LayoutEntry,
|
||||
margin image.Point,
|
||||
padding artist.Inset,
|
||||
) (
|
||||
width, height int,
|
||||
) {
|
||||
for index, entry := range entries {
|
||||
entryWidth, entryHeight := entry.MinimumSize()
|
||||
if entryWidth > width {
|
||||
width = entryWidth
|
||||
}
|
||||
height += entryHeight
|
||||
if layout.Gap && index > 0 {
|
||||
height += margin.Y
|
||||
}
|
||||
}
|
||||
|
||||
if layout.Pad {
|
||||
width += padding.Horizontal()
|
||||
height += padding.Vertical()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (layout Vertical) expandingElementHeight (
|
||||
entries []tomo.LayoutEntry,
|
||||
margin image.Point,
|
||||
padding artist.Inset,
|
||||
freeSpace int,
|
||||
) (
|
||||
height fixed.Int26_6,
|
||||
minimumHeights []int,
|
||||
) {
|
||||
// count the number of expanding elements and the amount of free space
|
||||
// for them to collectively occupy, while gathering minimum heights.
|
||||
minimumHeights = make([]int, len(entries))
|
||||
expandingElements := 0
|
||||
for index, entry := range entries {
|
||||
_, entryMinHeight := entry.MinimumSize()
|
||||
minimumHeights[index] = entryMinHeight
|
||||
|
||||
if entry.Expand {
|
||||
expandingElements ++
|
||||
} else {
|
||||
freeSpace -= entryMinHeight
|
||||
}
|
||||
if index > 0 && layout.Gap {
|
||||
freeSpace -= margin.Y
|
||||
}
|
||||
}
|
||||
|
||||
if expandingElements > 0 {
|
||||
height = fixed.I(freeSpace) / fixed.Int26_6(expandingElements)
|
||||
}
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user