2013-12-28 04:36:26 +08:00
|
|
|
package gocui
|
|
|
|
|
|
|
|
import (
|
2014-01-01 04:23:28 +08:00
|
|
|
"github.com/nsf/termbox-go"
|
2013-12-28 04:36:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type View struct {
|
2013-12-30 04:29:29 +08:00
|
|
|
Name string
|
2014-01-03 01:34:58 +08:00
|
|
|
X, Y, W, H float32
|
2013-12-30 04:29:29 +08:00
|
|
|
X0, Y0, X1, Y1 int
|
2013-12-28 04:36:26 +08:00
|
|
|
cx, cy int
|
|
|
|
BgColor, FgColor termbox.Attribute
|
|
|
|
SelBgColor, SelFgColor termbox.Attribute
|
|
|
|
}
|
|
|
|
|
2014-01-03 01:34:58 +08:00
|
|
|
func NewView(name string, x, y, w, h float32, maxX, maxY int) (v *View) {
|
2013-12-29 01:49:01 +08:00
|
|
|
v = &View{
|
2013-12-30 04:29:29 +08:00
|
|
|
Name: name,
|
2014-01-03 01:34:58 +08:00
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
W: w,
|
|
|
|
H: h,
|
2013-12-29 01:49:01 +08:00
|
|
|
BgColor: termbox.ColorBlack,
|
|
|
|
FgColor: termbox.ColorWhite,
|
|
|
|
SelBgColor: termbox.ColorBlack,
|
|
|
|
SelFgColor: termbox.ColorWhite,
|
|
|
|
}
|
2014-01-03 01:34:58 +08:00
|
|
|
v.Resize(maxX, maxY)
|
2013-12-29 01:49:01 +08:00
|
|
|
return v
|
2013-12-28 04:36:26 +08:00
|
|
|
}
|
2014-01-03 01:34:58 +08:00
|
|
|
|
|
|
|
func (v *View) Resize(maxX, maxY int) {
|
|
|
|
switch {
|
|
|
|
case v.X0 < 0:
|
|
|
|
v.X0 = -1
|
|
|
|
case v.X0 < 1:
|
|
|
|
v.X0 = int(v.X*float32(maxX) + 0.5)
|
|
|
|
default:
|
|
|
|
v.X0 = int(v.X + 0.5)
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case v.W < 0:
|
|
|
|
v.X1 = maxX
|
|
|
|
case v.W < 1:
|
|
|
|
v.X1 = v.X0 + int(v.W*float32(maxX)+0.5)
|
|
|
|
default:
|
|
|
|
v.X1 = v.X0 + int(v.W+0.5)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case v.Y < 0:
|
|
|
|
v.Y0 = -1
|
|
|
|
case v.Y < 1:
|
|
|
|
v.Y0 = int(v.Y*float32(maxY) + 0.5)
|
|
|
|
default:
|
|
|
|
v.Y0 = int(v.Y + 0.5)
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case v.H < 0:
|
|
|
|
v.X1 = maxY
|
|
|
|
case v.H < 1:
|
|
|
|
v.Y1 = v.Y0 + int(v.H*float32(maxY)+0.5)
|
|
|
|
default:
|
|
|
|
v.Y1 = v.Y0 + int(v.H+0.5)
|
|
|
|
}
|
|
|
|
}
|