added arrangement into empty space, if available
This commit is contained in:
parent
7334efa9ff
commit
3321e80a56
|
@ -48,7 +48,7 @@ func (t *TextBox) Draw(buffer *ui.Buffer) {
|
|||
rows := ui.SplitCells(cells, '\n')
|
||||
|
||||
for y, row := range rows {
|
||||
if y+t.Inner.Min.Y >= t.Inner.Max.Y {
|
||||
if y+t.Inner.Min.Y >= t.Inner.Max.Y-1 {
|
||||
break
|
||||
}
|
||||
row = ui.TrimCells(row, t.Inner.Dx()-2)
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
ui "github.com/gizak/termui/v3"
|
||||
"github.com/sqshq/sampler/console"
|
||||
"image"
|
||||
)
|
||||
|
||||
func (c *Config) setDefaultArrangement() {
|
||||
|
@ -13,11 +15,23 @@ func (c *Config) setDefaultArrangement() {
|
|||
}
|
||||
|
||||
for _, component := range components {
|
||||
if component.Position == nil {
|
||||
if component.Position == nil || len(component.Position) == 0 {
|
||||
|
||||
lc := getLargestComponent(components)
|
||||
lr := lc.GetRectangle()
|
||||
le := getLargestEmptySpaceRectangle(components)
|
||||
|
||||
if getSquare(lc) > le.Dx()*le.Dy()*2 {
|
||||
arrangeIntoLargestComponent(component, lc)
|
||||
} else {
|
||||
arrangeIntoLargestEmptySpace(component, le)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func arrangeIntoLargestComponent(component *ComponentConfig, lc *ComponentConfig) {
|
||||
|
||||
lr := lc.GetRectangle()
|
||||
var w, h, ws, hs int
|
||||
|
||||
if lr.Dx()/2 > lr.Dy() {
|
||||
|
@ -31,17 +45,20 @@ func (c *Config) setDefaultArrangement() {
|
|||
ws = 0
|
||||
hs = h
|
||||
}
|
||||
|
||||
component.Position = [][]int{
|
||||
{lr.Min.X + ws, lr.Min.Y + hs},
|
||||
{w, h},
|
||||
}
|
||||
|
||||
lc.Position = [][]int{
|
||||
{lr.Min.X, lr.Min.Y},
|
||||
{w, h},
|
||||
}
|
||||
}
|
||||
|
||||
func arrangeIntoLargestEmptySpace(component *ComponentConfig, le image.Rectangle) {
|
||||
component.Position = [][]int{
|
||||
{le.Min.X, le.Min.Y},
|
||||
{le.Dx(), le.Dy()},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -99,10 +116,82 @@ func getComponents(c *Config) []*ComponentConfig {
|
|||
}
|
||||
|
||||
func setSingleComponentPosition(c *ComponentConfig) {
|
||||
w := int(console.ColumnsCount * 0.8)
|
||||
h := int(console.RowsCount * 0.7)
|
||||
w := int(console.ColumnsCount)
|
||||
h := int(console.RowsCount * 0.6)
|
||||
c.Position = [][]int{
|
||||
{(console.ColumnsCount - w) / 2, (console.RowsCount - h) / 2},
|
||||
{w, h},
|
||||
}
|
||||
}
|
||||
|
||||
func getLargestEmptySpaceRectangle(components []*ComponentConfig) image.Rectangle {
|
||||
|
||||
grid := [console.RowsCount][console.ColumnsCount]int{}
|
||||
|
||||
for _, component := range components {
|
||||
rect := component.GetRectangle()
|
||||
for r := ui.MinInt(console.RowsCount, rect.Min.Y); r < ui.MinInt(console.RowsCount, rect.Max.Y); r++ {
|
||||
for c := ui.MinInt(console.ColumnsCount, rect.Min.X); c < ui.MinInt(console.ColumnsCount, rect.Max.X); c++ {
|
||||
grid[r][c] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mr := image.ZR
|
||||
|
||||
for row := 0; row < console.RowsCount; row++ {
|
||||
histogram := createHistogram(grid, row)
|
||||
r := calcMaxRectangle(histogram, row)
|
||||
if r.Dx()*r.Dy() > mr.Dx()*mr.Dy() {
|
||||
mr = r
|
||||
}
|
||||
}
|
||||
|
||||
return mr
|
||||
}
|
||||
|
||||
func calcMaxRectangle(histogram [console.ColumnsCount]int, row int) image.Rectangle {
|
||||
|
||||
maxRectangle := image.ZR
|
||||
maxArea := 0
|
||||
|
||||
for i := 0; i < len(histogram); i++ {
|
||||
|
||||
height := histogram[i]
|
||||
if height > maxArea {
|
||||
maxArea = height
|
||||
maxRectangle = image.Rect(i, row, i, row+height)
|
||||
}
|
||||
|
||||
for j := i - 1; j >= 0; j-- {
|
||||
width := i - j + 1
|
||||
height = ui.MinInt(height, histogram[j])
|
||||
if width*height > maxArea {
|
||||
maxArea = width * height
|
||||
maxRectangle = image.Rect(j, row, j+width, row+height)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxRectangle
|
||||
}
|
||||
|
||||
func createHistogram(grid [console.RowsCount][console.ColumnsCount]int, row int) [console.ColumnsCount]int {
|
||||
histogram := [console.ColumnsCount]int{}
|
||||
for column := 0; column < console.ColumnsCount; column++ {
|
||||
histogram[column] = countEmptyCellsBelow(grid, row, column)
|
||||
}
|
||||
return histogram
|
||||
}
|
||||
|
||||
func countEmptyCellsBelow(grid [console.RowsCount][console.ColumnsCount]int, row int, column int) int {
|
||||
count := 0
|
||||
for r := row; r < console.RowsCount; r++ {
|
||||
if grid[r][column] == 1 {
|
||||
return count
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ func (c *ComponentConfig) GetSize() Size {
|
|||
}
|
||||
|
||||
func (c *ComponentConfig) GetRectangle() image.Rectangle {
|
||||
if c.Position == nil {
|
||||
if c.Position == nil || len(c.Position) == 0 {
|
||||
return image.ZR
|
||||
} else {
|
||||
return image.Rect(c.Position[0][0], c.Position[0][1], c.Position[0][0]+c.Position[1][0], c.Position[0][1]+c.Position[1][1])
|
||||
|
|
Loading…
Reference in New Issue