2015-03-21 04:21:50 +08:00
|
|
|
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-02-04 03:13:51 +08:00
|
|
|
package termui
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// Block is a base struct for all other upper level widgets,
|
|
|
|
// consider it as css: display:block.
|
|
|
|
// Normally you do not need to create it manually.
|
2015-02-04 09:56:49 +08:00
|
|
|
type Block struct {
|
2015-02-05 05:57:24 +08:00
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
Border labeledBorder
|
|
|
|
IsDisplay bool
|
|
|
|
HasBorder bool
|
|
|
|
BgColor Attribute
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
innerWidth int
|
|
|
|
innerHeight int
|
|
|
|
innerX int
|
|
|
|
innerY int
|
|
|
|
PaddingTop int
|
|
|
|
PaddingBottom int
|
|
|
|
PaddingLeft int
|
|
|
|
PaddingRight int
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// NewBlock returns a *Block which inherits styles from current theme.
|
2015-02-04 09:56:49 +08:00
|
|
|
func NewBlock() *Block {
|
|
|
|
d := Block{}
|
2015-02-04 03:13:51 +08:00
|
|
|
d.IsDisplay = true
|
2015-03-12 04:15:59 +08:00
|
|
|
d.HasBorder = theme.HasBorder
|
|
|
|
d.Border.BgColor = theme.BorderBg
|
|
|
|
d.Border.FgColor = theme.BorderFg
|
|
|
|
d.Border.LabelBgColor = theme.BorderLabelTextBg
|
|
|
|
d.Border.LabelFgColor = theme.BorderLabelTextFg
|
|
|
|
d.BgColor = theme.BlockBg
|
2015-02-04 03:13:51 +08:00
|
|
|
d.Width = 2
|
|
|
|
d.Height = 2
|
2015-02-04 09:56:49 +08:00
|
|
|
return &d
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-04 02:28:09 +08:00
|
|
|
// compute box model
|
2015-02-04 09:56:49 +08:00
|
|
|
func (d *Block) align() {
|
2015-02-05 05:57:24 +08:00
|
|
|
d.innerWidth = d.Width - d.PaddingLeft - d.PaddingRight
|
|
|
|
d.innerHeight = d.Height - d.PaddingTop - d.PaddingBottom
|
|
|
|
d.innerX = d.X + d.PaddingLeft
|
|
|
|
d.innerY = d.Y + d.PaddingTop
|
2015-02-04 03:13:51 +08:00
|
|
|
|
|
|
|
if d.HasBorder {
|
|
|
|
d.innerHeight -= 2
|
|
|
|
d.innerWidth -= 2
|
|
|
|
d.Border.X = d.X
|
|
|
|
d.Border.Y = d.Y
|
|
|
|
d.Border.Width = d.Width
|
|
|
|
d.Border.Height = d.Height
|
2015-03-25 05:16:43 +08:00
|
|
|
d.innerX++
|
|
|
|
d.innerY++
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
2015-03-29 00:58:23 +08:00
|
|
|
|
|
|
|
if d.innerHeight < 0 {
|
|
|
|
d.innerHeight = 0
|
|
|
|
}
|
|
|
|
if d.innerWidth < 0 {
|
|
|
|
d.innerWidth = 0
|
|
|
|
}
|
|
|
|
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// Buffer implements Bufferer interface.
|
|
|
|
// Draw background and border (if any).
|
2015-02-04 09:56:49 +08:00
|
|
|
func (d *Block) Buffer() []Point {
|
|
|
|
d.align()
|
2015-02-04 03:13:51 +08:00
|
|
|
|
|
|
|
ps := []Point{}
|
|
|
|
if !d.IsDisplay {
|
|
|
|
return ps
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasBorder {
|
|
|
|
ps = d.Border.Buffer()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < d.innerWidth; i++ {
|
|
|
|
for j := 0; j < d.innerHeight; j++ {
|
|
|
|
p := Point{}
|
|
|
|
p.X = d.X + 1 + i
|
|
|
|
p.Y = d.Y + 1 + j
|
2015-03-04 02:28:09 +08:00
|
|
|
p.Ch = ' '
|
|
|
|
p.Bg = d.BgColor
|
2015-02-04 03:13:51 +08:00
|
|
|
ps = append(ps, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ps
|
|
|
|
}
|
2015-03-16 03:56:38 +08:00
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// GetHeight implements GridBufferer.
|
|
|
|
// It returns current height of the block.
|
2015-03-16 03:56:38 +08:00
|
|
|
func (d Block) GetHeight() int {
|
|
|
|
return d.Height
|
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// SetX implements GridBufferer interface, which sets block's x position.
|
2015-03-16 03:56:38 +08:00
|
|
|
func (d *Block) SetX(x int) {
|
|
|
|
d.X = x
|
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// SetY implements GridBufferer interface, it sets y position for block.
|
2015-03-16 03:56:38 +08:00
|
|
|
func (d *Block) SetY(y int) {
|
|
|
|
d.Y = y
|
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// SetWidth implements GridBuffer interface, it sets block's width.
|
2015-03-16 03:56:38 +08:00
|
|
|
func (d *Block) SetWidth(w int) {
|
|
|
|
d.Width = w
|
|
|
|
}
|