2016-01-26 21:33:25 +00:00
|
|
|
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
|
2016-01-26 21:33:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:33:25 +00:00
|
|
|
// 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 }
|
2016-01-26 21:33:25 +00:00
|
|
|
|
|
|
|
// 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
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
2016-01-26 21:33:25 +00:00
|
|
|
|
|
|
|
// 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
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
2016-01-26 21:33:25 +00:00
|
|
|
|
|
|
|
// 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
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
2016-01-26 21:33:25 +00:00
|
|
|
|
|
|
|
// 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
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
2016-01-26 21:33:25 +00:00
|
|
|
|
|
|
|
// 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
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetScrollX returns the x distance scrolled
|
2016-02-09 16:21:57 +00:00
|
|
|
func (i *ScrollFrame) GetScrollX() int {
|
|
|
|
return i.scrollX
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetScrollY returns the y distance scrolled
|
2016-02-09 16:21:57 +00:00
|
|
|
func (i *ScrollFrame) GetScrollY() int {
|
|
|
|
return i.scrollY
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollDown scrolls the frame down
|
2016-02-09 16:21:57 +00:00
|
|
|
func (i *ScrollFrame) ScrollDown() {
|
|
|
|
i.scrollY++
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollUp scrolls the frame up
|
2016-02-09 16:21:57 +00:00
|
|
|
func (i *ScrollFrame) ScrollUp() {
|
|
|
|
if i.scrollY > 0 {
|
|
|
|
i.scrollY--
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollLeft scrolls the frame left
|
2016-02-09 16:21:57 +00:00
|
|
|
func (i *ScrollFrame) ScrollLeft() {
|
|
|
|
if i.scrollX > 0 {
|
|
|
|
i.scrollX--
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollRight scrolls the frame right
|
2016-02-09 16:21:57 +00:00
|
|
|
func (i *ScrollFrame) ScrollRight() {
|
|
|
|
i.scrollX++
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2016-01-26 21:33:25 +00:00
|
|
|
ctlX, ctlY := t.GetX(), t.GetY()
|
2016-02-09 16:21:57 +00:00
|
|
|
t.SetX((i.GetX() + ctlX))
|
|
|
|
t.SetY((i.GetY() + ctlY))
|
2016-01-26 21:33:25 +00:00
|
|
|
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 {
|
2016-01-26 21:33:25 +00:00
|
|
|
// 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
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
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 {
|
2016-01-26 21:33:25 +00:00
|
|
|
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 {
|
2016-01-26 21:33:25 +00:00
|
|
|
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)
|
2016-01-26 21:33:25 +00:00
|
|
|
maxWidth--
|
|
|
|
maxHeight--
|
|
|
|
x++
|
|
|
|
y++
|
|
|
|
startX++
|
|
|
|
startY++
|
|
|
|
}
|
2016-02-09 16:21:57 +00:00
|
|
|
for idx := range i.controls {
|
|
|
|
i.DrawControl(i.controls[idx])
|
2016-01-26 21:33:25 +00:00
|
|
|
}
|
|
|
|
}
|