termbox-util/termbox_scrollframe.go

186 lines
4.5 KiB
Go
Raw Permalink 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
2019-03-25 15:10:25 +00:00
activeFg, activeBg termbox.Attribute
bordered bool
controls []termboxControl
2019-03-25 15:10:25 +00:00
active bool
}
// CreateScrollFrame creates Scrolling Frame at x, y that is w by h
func CreateScrollFrame(x, y, w, h int, fg, bg termbox.Attribute) *ScrollFrame {
2019-03-25 15:10:25 +00:00
c := ScrollFrame{
x: x, y: y, width: w, height: h,
fg: fg, bg: bg, activeFg: fg, activeBg: bg,
}
return &c
}
2016-02-09 16:21:57 +00:00
// GetID returns this control's ID
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) GetID() string { return c.id }
2016-02-09 16:21:57 +00:00
// SetID sets this control's ID
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) SetID(newID string) {
c.id = newID
2016-02-09 16:21:57 +00:00
}
// GetX returns the x position of the scroll frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) GetX() int { return c.x }
// SetX sets the x position of the scroll frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) SetX(x int) {
c.x = x
}
// GetY returns the y position of the scroll frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) GetY() int { return c.y }
// SetY sets the y position of the scroll frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) SetY(y int) {
c.y = y
}
// GetWidth returns the current width of the scroll frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) GetWidth() int { return c.width }
// SetWidth sets the current width of the scroll frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) SetWidth(w int) {
c.width = w
}
// GetHeight returns the current height of the scroll frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) GetHeight() int { return c.height }
// SetHeight sets the current height of the scroll frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) SetHeight(h int) {
c.height = h
2016-02-09 16:21:57 +00:00
}
// GetFgColor returns the foreground color
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) GetFgColor() termbox.Attribute { return c.fg }
2016-02-09 16:21:57 +00:00
// SetFgColor sets the foreground color
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) SetFgColor(fg termbox.Attribute) {
c.fg = fg
2016-02-09 16:21:57 +00:00
}
// GetBgColor returns the background color
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) GetBgColor() termbox.Attribute { return c.bg }
2016-02-09 16:21:57 +00:00
// SetBgColor sets the current background color
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) SetBgColor(bg termbox.Attribute) {
c.bg = bg
}
// IsBordered returns true or false if this scroll frame has a border
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) IsBordered() bool { return c.bordered }
// SetBordered sets whether we render a border around the scroll frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) SetBordered(b bool) {
c.bordered = b
}
// GetScrollX returns the x distance scrolled
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) GetScrollX() int {
return c.scrollX
}
// GetScrollY returns the y distance scrolled
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) GetScrollY() int {
return c.scrollY
}
// ScrollDown scrolls the frame down
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) ScrollDown() {
c.scrollY++
}
// ScrollUp scrolls the frame up
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) ScrollUp() {
if c.scrollY > 0 {
c.scrollY--
}
}
// ScrollLeft scrolls the frame left
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) ScrollLeft() {
if c.scrollX > 0 {
c.scrollX--
}
}
// ScrollRight scrolls the frame right
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) ScrollRight() {
c.scrollX++
}
// AddControl adds a control to the frame
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) AddControl(t termboxControl) {
c.controls = append(c.controls, t)
}
// DrawControl figures out the relative position of the control,
// sets it, draws it, then resets it.
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) DrawControl(t termboxControl) {
if c.IsVisible(t) {
ctlX, ctlY := t.GetX(), t.GetY()
2019-03-25 15:10:25 +00:00
t.SetX((c.GetX() + ctlX))
t.SetY((c.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
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) IsVisible(t termboxControl) bool {
// Check if any part of t should be visible
cX, cY := t.GetX(), t.GetY()
2019-03-25 15:10:25 +00:00
if cX+t.GetWidth() >= c.scrollX && cX <= c.scrollX+c.width {
return cY+t.GetHeight() >= c.scrollY && cY <= c.scrollY+c.height
}
return false
}
2016-02-09 16:21:57 +00:00
// HandleEvent accepts the termbox event and returns whether it was consumed
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) HandleEvent(event termbox.Event) bool {
return false
}
// DrawToStrings generates a slice of strings with what should
// be drawn to the screen
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) DrawToStrings() []string {
return []string{}
}
// Draw outputs the Scoll Frame on the screen
2019-03-25 15:10:25 +00:00
func (c *ScrollFrame) Draw() {
maxWidth := c.width
maxHeight := c.height
x, y := c.x, c.y
startX := c.x
startY := c.y
if c.bordered {
DrawBorder(c.x, c.y, c.x+c.width, c.y+c.height, c.fg, c.bg)
maxWidth--
maxHeight--
x++
y++
startX++
startY++
}
2019-03-25 15:10:25 +00:00
for idx := range c.controls {
c.DrawControl(c.controls[idx])
}
}