2017-01-13 23:07:43 -07:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2022-08-28 10:46:41 -06:00
|
|
|
// Use of this source code is governed by a GPLv3 license that can
|
2015-03-20 14:21:50 -06:00
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-03-15 13:56:38 -06:00
|
|
|
package termui
|
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
type gridItemType uint
|
2015-03-19 10:00:38 -06:00
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
const (
|
|
|
|
col gridItemType = 0
|
|
|
|
row gridItemType = 1
|
|
|
|
)
|
2015-03-19 10:00:38 -06:00
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
type Grid struct {
|
|
|
|
Block
|
|
|
|
Items []*GridItem
|
|
|
|
}
|
|
|
|
|
2019-02-01 23:16:02 -07:00
|
|
|
// GridItem represents either a Row or Column in a grid.
|
|
|
|
// Holds sizing information and either an []GridItems or a widget.
|
2019-01-23 21:12:10 -07:00
|
|
|
type GridItem struct {
|
|
|
|
Type gridItemType
|
|
|
|
XRatio float64
|
|
|
|
YRatio float64
|
|
|
|
WidthRatio float64
|
|
|
|
HeightRatio float64
|
|
|
|
Entry interface{} // Entry.type == GridBufferer if IsLeaf else []GridItem
|
|
|
|
IsLeaf bool
|
|
|
|
ratio float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGrid() *Grid {
|
|
|
|
g := &Grid{
|
|
|
|
Block: *NewBlock(),
|
|
|
|
}
|
|
|
|
g.Border = false
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCol takes a height percentage and either a widget or a Row or Column
|
|
|
|
func NewCol(ratio float64, i ...interface{}) GridItem {
|
|
|
|
_, ok := i[0].(Drawable)
|
|
|
|
entry := i[0]
|
|
|
|
if !ok {
|
|
|
|
entry = i
|
|
|
|
}
|
|
|
|
return GridItem{
|
|
|
|
Type: col,
|
|
|
|
Entry: entry,
|
|
|
|
IsLeaf: ok,
|
|
|
|
ratio: ratio,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRow takes a width percentage and either a widget or a Row or Column
|
|
|
|
func NewRow(ratio float64, i ...interface{}) GridItem {
|
|
|
|
_, ok := i[0].(Drawable)
|
|
|
|
entry := i[0]
|
|
|
|
if !ok {
|
|
|
|
entry = i
|
|
|
|
}
|
|
|
|
return GridItem{
|
|
|
|
Type: row,
|
|
|
|
Entry: entry,
|
|
|
|
IsLeaf: ok,
|
|
|
|
ratio: ratio,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set is used to add Columns and Rows to the grid.
|
|
|
|
// It recursively searches the GridItems, adding leaves to the grid and calculating the dimensions of the leaves.
|
|
|
|
func (self *Grid) Set(entries ...interface{}) {
|
|
|
|
entry := GridItem{
|
|
|
|
Type: row,
|
|
|
|
Entry: entries,
|
|
|
|
IsLeaf: false,
|
|
|
|
ratio: 1.0,
|
|
|
|
}
|
|
|
|
self.setHelper(entry, 1.0, 1.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Grid) setHelper(item GridItem, parentWidthRatio, parentHeightRatio float64) {
|
|
|
|
var HeightRatio float64
|
|
|
|
var WidthRatio float64
|
|
|
|
switch item.Type {
|
|
|
|
case col:
|
|
|
|
HeightRatio = 1.0
|
|
|
|
WidthRatio = item.ratio
|
|
|
|
case row:
|
|
|
|
HeightRatio = item.ratio
|
|
|
|
WidthRatio = 1.0
|
|
|
|
}
|
|
|
|
item.WidthRatio = parentWidthRatio * WidthRatio
|
|
|
|
item.HeightRatio = parentHeightRatio * HeightRatio
|
|
|
|
|
|
|
|
if item.IsLeaf {
|
|
|
|
self.Items = append(self.Items, &item)
|
|
|
|
} else {
|
|
|
|
XRatio := 0.0
|
|
|
|
YRatio := 0.0
|
|
|
|
cols := false
|
|
|
|
rows := false
|
|
|
|
|
|
|
|
children := InterfaceSlice(item.Entry)
|
|
|
|
|
|
|
|
for i := 0; i < len(children); i++ {
|
|
|
|
if children[i] == nil {
|
|
|
|
continue
|
2015-03-19 10:00:38 -06:00
|
|
|
}
|
2019-01-23 21:12:10 -07:00
|
|
|
child, _ := children[i].(GridItem)
|
|
|
|
|
|
|
|
child.XRatio = item.XRatio + (item.WidthRatio * XRatio)
|
|
|
|
child.YRatio = item.YRatio + (item.HeightRatio * YRatio)
|
|
|
|
|
|
|
|
switch child.Type {
|
|
|
|
case col:
|
|
|
|
cols = true
|
|
|
|
XRatio += child.ratio
|
|
|
|
if rows {
|
|
|
|
item.HeightRatio /= 2
|
|
|
|
}
|
|
|
|
case row:
|
|
|
|
rows = true
|
|
|
|
YRatio += child.ratio
|
|
|
|
if cols {
|
|
|
|
item.WidthRatio /= 2
|
|
|
|
}
|
2015-03-19 10:00:38 -06:00
|
|
|
}
|
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
self.setHelper(child, item.WidthRatio, item.HeightRatio)
|
2015-03-19 10:00:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
func (self *Grid) Draw(buf *Buffer) {
|
|
|
|
width := float64(self.Dx()) + 1
|
|
|
|
height := float64(self.Dy()) + 1
|
2015-03-19 10:00:38 -06:00
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
for _, item := range self.Items {
|
|
|
|
entry, _ := item.Entry.(Drawable)
|
2015-03-19 10:00:38 -06:00
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
x := int(width*item.XRatio) + self.Min.X
|
|
|
|
y := int(height*item.YRatio) + self.Min.Y
|
|
|
|
w := int(width * item.WidthRatio)
|
|
|
|
h := int(height * item.HeightRatio)
|
2015-03-20 06:24:48 -06:00
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
if x+w > self.Dx() {
|
|
|
|
w--
|
2015-03-19 10:00:38 -06:00
|
|
|
}
|
2019-01-23 21:12:10 -07:00
|
|
|
if y+h > self.Dy() {
|
|
|
|
h--
|
2015-03-20 06:24:48 -06:00
|
|
|
}
|
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
entry.SetRect(x, y, x+w, y+h)
|
2015-03-20 06:24:48 -06:00
|
|
|
|
2019-03-07 03:00:36 -07:00
|
|
|
entry.Lock()
|
2019-01-23 21:12:10 -07:00
|
|
|
entry.Draw(buf)
|
2019-03-07 03:00:36 -07:00
|
|
|
entry.Unlock()
|
2015-03-20 06:24:48 -06:00
|
|
|
}
|
|
|
|
}
|