Add Float prop

This commit is contained in:
gizak 2015-10-13 01:00:15 -04:00
parent 52f9777092
commit 86950762b0
6 changed files with 144 additions and 22 deletions

View File

@ -120,6 +120,7 @@ type Block struct {
PaddingLeft int PaddingLeft int
PaddingRight int PaddingRight int
id string id string
Float Align
} }
// NewBlock returns a *Block which inherits styles from current theme. // NewBlock returns a *Block which inherits styles from current theme.
@ -139,6 +140,7 @@ func NewBlock() *Block {
b.Width = 2 b.Width = 2
b.Height = 2 b.Height = 2
b.id = GenId() b.id = GenId()
b.Float = AlignNone
return &b return &b
} }
@ -148,13 +150,19 @@ func (b Block) Id() string {
// Align computes box model // Align computes box model
func (b *Block) Align() { func (b *Block) Align() {
b.area.Min.X = b.X // outer
b.area.Min.Y = b.Y b.area.Min.X = 0
b.area.Max.X = b.X + b.Width b.area.Min.Y = 0
b.area.Max.Y = b.Y + b.Height b.area.Max.X = b.Width
b.area.Max.Y = b.Height
b.innerArea.Min.X = b.X + b.PaddingLeft // float
b.innerArea.Min.Y = b.Y + b.PaddingTop b.area = AlignArea(TermRect(), b.area, b.Float)
b.area = MoveArea(b.area, b.X, b.Y)
// inner
b.innerArea.Min.X = b.area.Min.X + b.PaddingLeft
b.innerArea.Min.Y = b.area.Min.Y + b.PaddingTop
b.innerArea.Max.X = b.area.Max.X - b.PaddingRight b.innerArea.Max.X = b.area.Max.X - b.PaddingRight
b.innerArea.Max.Y = b.area.Max.Y - b.PaddingBottom b.innerArea.Max.Y = b.area.Max.Y - b.PaddingBottom

View File

@ -1,8 +1,25 @@
package termui package termui
import "testing" import (
"testing"
)
func TestBlockFloat(t *testing.T) {
Init()
defer Close()
b := NewBlock()
b.X = 10
b.Y = 20
b.Float = AlignCenter
b.Align()
}
func TestBlockInnerBounds(t *testing.T) {
Init()
defer Close()
func TestBlock_InnerBounds(t *testing.T) {
b := NewBlock() b := NewBlock()
b.X = 10 b.X = 10
b.Y = 11 b.Y = 11
@ -16,11 +33,12 @@ func TestBlock_InnerBounds(t *testing.T) {
cy := area.Min.Y cy := area.Min.Y
cw := area.Dx() cw := area.Dx()
ch := area.Dy() ch := area.Dy()
if cx != x { if cx != x {
t.Errorf("expected x to be %d but got %d", x, cx) t.Errorf("expected x to be %d but got %d", x, cx)
} }
if cy != y { if cy != y {
t.Errorf("expected y to be %d but got %d", y, cy) t.Errorf("expected y to be %d but got %d\n%+v", y, cy, area)
} }
if cw != w { if cw != w {
t.Errorf("expected width to be %d but got %d", w, cw) t.Errorf("expected width to be %d but got %d", w, cw)

View File

@ -21,16 +21,6 @@ import (
g.PercentColor = termui.ColorBlue g.PercentColor = termui.ColorBlue
*/ */
// Align is the position of the gauge's label.
type Align int
// All supported positions.
const (
AlignLeft Align = iota
AlignCenter
AlignRight
)
type Gauge struct { type Gauge struct {
Block Block
Percent int Percent int

71
pos.go Normal file
View File

@ -0,0 +1,71 @@
package termui
import "image"
// Align is the position of the gauge's label.
type Align uint
// All supported positions.
const (
AlignNone Align = 0
AlignLeft Align = 1 << iota
AlignRight
AlignBottom
AlignTop
AlignCenterVertical
AlignCenterHorizontal
AlignCenter = AlignCenterVertical | AlignCenterHorizontal
)
func AlignArea(parent, child image.Rectangle, a Align) image.Rectangle {
w, h := child.Dx(), child.Dy()
// parent center
pcx, pcy := parent.Min.X+parent.Dx()/2, parent.Min.Y+parent.Dy()/2
// child center
ccx, ccy := child.Min.X+child.Dx()/2, child.Min.Y+child.Dy()/2
if a&AlignLeft == AlignLeft {
child.Min.X = parent.Min.X
child.Max.X = child.Min.X + w
}
if a&AlignRight == AlignRight {
child.Max.X = parent.Max.X
child.Min.X = child.Max.X - w
}
if a&AlignBottom == AlignBottom {
child.Max.Y = parent.Max.Y
child.Min.Y = child.Max.Y - h
}
if a&AlignTop == AlignRight {
child.Min.Y = parent.Min.Y
child.Max.Y = child.Min.Y + h
}
if a&AlignCenterHorizontal == AlignCenterHorizontal {
child.Min.X += pcx - ccx
child.Max.X = child.Min.X + w
}
if a&AlignCenterVertical == AlignCenterVertical {
child.Min.Y += pcy - ccy
child.Max.Y = child.Min.Y + h
}
return child
}
func MoveArea(a image.Rectangle, dx, dy int) image.Rectangle {
a.Min.X += dx
a.Max.X += dx
a.Min.Y += dy
a.Max.Y += dy
return a
}
func TermRect() image.Rectangle {
return image.Rect(0, 0, TermWidth(), TermHeight())
}

34
pos_test.go Normal file
View File

@ -0,0 +1,34 @@
package termui
import (
"image"
"testing"
)
func TestAlignArea(t *testing.T) {
p := image.Rect(0, 0, 100, 100)
c := image.Rect(10, 10, 20, 20)
nc := AlignArea(p, c, AlignLeft)
if nc.Min.X != 0 || nc.Max.Y != 20 {
t.Errorf("AlignLeft failed:\n%+v", nc)
}
nc = AlignArea(p, c, AlignCenter)
if nc.Min.X != 45 || nc.Max.Y != 55 {
t.Error("AlignCenter failed")
}
nc = AlignArea(p, c, AlignBottom|AlignRight)
if nc.Min.X != 90 || nc.Max.Y != 100 {
t.Errorf("AlignBottom|AlignRight failed\n%+v", nc)
}
}
func TestMoveArea(t *testing.T) {
a := image.Rect(10, 10, 20, 20)
a = MoveArea(a, 5, 10)
if a.Min.X != 15 || a.Min.Y != 20 || a.Max.X != 25 || a.Max.Y != 30 {
t.Error("MoveArea failed")
}
}

View File

@ -26,10 +26,11 @@ func main() {
//termui.UseTheme("helloworld") //termui.UseTheme("helloworld")
b := termui.NewBlock() b := termui.NewBlock()
b.Width = 20 b.Width = 20
b.Height = 30 b.Height = 20
b.Float = termui.AlignCenter
b.BorderLabel = "[HELLO](fg-red,bg-white) [WORLD](fg-blue,bg-green)" b.BorderLabel = "[HELLO](fg-red,bg-white) [WORLD](fg-blue,bg-green)"
termui.SendBufferToRender(b) termui.Render(b)
termui.Handle("/sys", func(e termui.Event) { termui.Handle("/sys", func(e termui.Event) {
k, ok := e.Data.(termui.EvtKbd) k, ok := e.Data.(termui.EvtKbd)
@ -48,7 +49,7 @@ func main() {
b.BorderLabel = "[HELLO](fg-blue,bg-white) [WORLD](fg-red,bg-green)" b.BorderLabel = "[HELLO](fg-blue,bg-white) [WORLD](fg-red,bg-green)"
} }
termui.SendBufferToRender(b) termui.Render(b)
}) })
termui.Loop() termui.Loop()