termbox-util/termbox_scrollframe.go

181 lines
4.4 KiB
Go
Raw Normal View History

package termboxUtil
import "github.com/nsf/termbox-go"
// ScrollFrame is a frame for holding other elements
// It manages it's own x/y, tab index
type ScrollFrame struct {
2016-02-09 16:21:57 +00:00
id string
x, y, width, height int
scrollX, scrollY int
tabIdx int
fg, bg termbox.Attribute
bordered bool
controls []termboxControl
}
// CreateScrollFrame creates Scrolling Frame at x, y that is w by h
func CreateScrollFrame(x, y, w, h int, fg, bg termbox.Attribute) *ScrollFrame {
s := ScrollFrame{x: x, y: y, width: w, height: h, fg: fg, bg: bg}
return &s
}
2016-02-09 16:21:57 +00:00
// GetID returns this control's ID
func (i *ScrollFrame) GetID() string { return i.id }
// SetID sets this control's ID
func (i *ScrollFrame) SetID(newID string) {
i.id = newID
}
// GetX returns the x position of the scroll frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) GetX() int { return i.x }
// SetX sets the x position of the scroll frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) SetX(x int) {
i.x = x
}
// GetY returns the y position of the scroll frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) GetY() int { return i.y }
// SetY sets the y position of the scroll frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) SetY(y int) {
i.y = y
}
// GetWidth returns the current width of the scroll frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) GetWidth() int { return i.width }
// SetWidth sets the current width of the scroll frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) SetWidth(w int) {
i.width = w
}
// GetHeight returns the current height of the scroll frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) GetHeight() int { return i.height }
// SetHeight sets the current height of the scroll frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) SetHeight(h int) {
i.height = h
}
// GetFgColor returns the foreground color
func (i *ScrollFrame) GetFgColor() termbox.Attribute { return i.fg }
// SetFgColor sets the foreground color
func (i *ScrollFrame) SetFgColor(fg termbox.Attribute) {
i.fg = fg
}
// GetBgColor returns the background color
func (i *ScrollFrame) GetBgColor() termbox.Attribute { return i.bg }
// SetBgColor sets the current background color
func (i *ScrollFrame) SetBgColor(bg termbox.Attribute) {
i.bg = bg
}
// IsBordered returns true or false if this scroll frame has a border
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) IsBordered() bool { return i.bordered }
// SetBordered sets whether we render a border around the scroll frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) SetBordered(b bool) {
i.bordered = b
}
// GetScrollX returns the x distance scrolled
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) GetScrollX() int {
return i.scrollX
}
// GetScrollY returns the y distance scrolled
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) GetScrollY() int {
return i.scrollY
}
// ScrollDown scrolls the frame down
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) ScrollDown() {
i.scrollY++
}
// ScrollUp scrolls the frame up
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) ScrollUp() {
if i.scrollY > 0 {
i.scrollY--
}
}
// ScrollLeft scrolls the frame left
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) ScrollLeft() {
if i.scrollX > 0 {
i.scrollX--
}
}
// ScrollRight scrolls the frame right
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) ScrollRight() {
i.scrollX++
}
// AddControl adds a control to the frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) AddControl(t termboxControl) {
i.controls = append(i.controls, t)
}
// DrawControl figures out the relative position of the control,
// sets it, draws it, then resets it.
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) DrawControl(t termboxControl) {
if i.IsVisible(t) {
ctlX, ctlY := t.GetX(), t.GetY()
2016-02-09 16:21:57 +00:00
t.SetX((i.GetX() + ctlX))
t.SetY((i.GetY() + ctlY))
t.Draw()
t.SetX(ctlX)
t.SetY(ctlY)
}
}
// IsVisible takes a Termbox Control and returns whether
// that control would be visible in the frame
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) IsVisible(t termboxControl) bool {
// Check if any part of t should be visible
cX, cY := t.GetX(), t.GetY()
2016-02-09 16:21:57 +00:00
if cX+t.GetWidth() >= i.scrollX && cX <= i.scrollX+i.width {
return cY+t.GetHeight() >= i.scrollY && cY <= i.scrollY+i.height
}
return false
}
2016-02-09 16:21:57 +00:00
// HandleEvent accepts the termbox event and returns whether it was consumed
func (i *ScrollFrame) HandleEvent(event termbox.Event) bool {
return false
}
// DrawToStrings generates a slice of strings with what should
// be drawn to the screen
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) DrawToStrings() []string {
return []string{}
}
// Draw outputs the Scoll Frame on the screen
2016-02-09 16:21:57 +00:00
func (i *ScrollFrame) Draw() {
maxWidth := i.width
maxHeight := i.height
x, y := i.x, i.y
startX := i.x
startY := i.y
if i.bordered {
DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
maxWidth--
maxHeight--
x++
y++
startX++
startY++
}
2016-02-09 16:21:57 +00:00
for idx := range i.controls {
i.DrawControl(i.controls[idx])
}
}