sampler-fork/widgets/layout.go

215 lines
5.1 KiB
Go
Raw Normal View History

2019-02-01 05:07:25 +00:00
package widgets
2019-01-31 00:02:38 +00:00
import (
2019-02-11 02:51:55 +00:00
"github.com/sqshq/sampler/console"
2019-02-15 04:34:45 +00:00
"github.com/sqshq/sampler/widgets/runchart"
2019-02-11 02:51:55 +00:00
ui "github.com/sqshq/termui"
2019-01-31 00:02:38 +00:00
)
type Layout struct {
2019-02-11 02:51:55 +00:00
ui.Block
2019-01-31 01:41:51 +00:00
components []Component
2019-02-11 02:51:55 +00:00
menu *Menu
mode Mode
selection int
2019-01-31 00:02:38 +00:00
}
2019-02-11 02:51:55 +00:00
type Mode rune
const (
ModeDefault Mode = 0
ModeComponentSelect Mode = 1
ModeMenuOptionSelect Mode = 2
ModeComponentMove Mode = 3
ModeComponentResize Mode = 4
ModeChartPinpoint Mode = 5
)
2019-01-31 00:02:38 +00:00
const (
columnsCount = 30
rowsCount = 30
)
2019-02-11 02:51:55 +00:00
func NewLayout(width, height int, menu *Menu) *Layout {
2019-01-31 00:02:38 +00:00
2019-02-11 02:51:55 +00:00
block := *ui.NewBlock()
2019-01-31 00:02:38 +00:00
block.SetRect(0, 0, width, height)
return &Layout{
2019-01-31 01:41:51 +00:00
Block: block,
components: make([]Component, 0),
2019-02-11 02:51:55 +00:00
menu: menu,
mode: ModeDefault,
selection: 0,
2019-01-31 00:02:38 +00:00
}
}
2019-02-11 02:51:55 +00:00
func (l *Layout) AddComponent(drawable ui.Drawable, title string, position Position, size Size, Type ComponentType) {
l.components = append(l.components, Component{drawable, title, position, size, Type})
}
2019-02-11 02:51:55 +00:00
func (l *Layout) GetComponents(Type ComponentType) []ui.Drawable {
2019-02-11 02:51:55 +00:00
var components []ui.Drawable
2019-02-11 02:51:55 +00:00
for _, component := range l.components {
if component.Type == Type {
components = append(components, component.Drawable)
}
}
return components
2019-01-31 00:02:38 +00:00
}
2019-02-11 02:51:55 +00:00
func (l *Layout) HandleConsoleEvent(e string) {
switch e {
case console.KeyEnter:
switch l.mode {
case ModeComponentSelect:
l.menu.choose()
l.mode = ModeMenuOptionSelect
case ModeMenuOptionSelect:
option := l.menu.getSelectedOption()
switch option {
case MenuOptionMove:
l.mode = ModeComponentMove
l.menu.moveOrResize()
case MenuOptionResize:
l.mode = ModeComponentResize
l.menu.moveOrResize()
case MenuOptionPinpoint:
l.mode = ModeChartPinpoint
l.menu.idle()
2019-02-15 04:34:45 +00:00
chart := l.getSelectedComponent().Drawable.(*runchart.RunChart)
2019-02-11 02:51:55 +00:00
chart.MoveSelection(0)
2019-02-11 04:06:48 +00:00
case MenuOptionResume:
2019-02-11 02:51:55 +00:00
l.mode = ModeDefault
l.menu.idle()
}
case ModeComponentMove:
fallthrough
case ModeComponentResize:
l.menu.idle()
l.mode = ModeDefault
}
case console.KeyEsc:
switch l.mode {
case ModeChartPinpoint:
2019-02-15 04:34:45 +00:00
chart := l.getSelectedComponent().Drawable.(*runchart.RunChart)
2019-02-11 02:51:55 +00:00
chart.DisableSelection()
fallthrough
case ModeComponentSelect:
fallthrough
case ModeMenuOptionSelect:
l.menu.idle()
l.mode = ModeDefault
}
case console.KeyLeft:
switch l.mode {
case ModeDefault:
l.mode = ModeComponentSelect
l.selection = 0
l.menu.highlight(l.getComponent(l.selection))
case ModeChartPinpoint:
2019-02-15 04:34:45 +00:00
chart := l.getSelectedComponent().Drawable.(*runchart.RunChart)
2019-02-11 02:51:55 +00:00
chart.MoveSelection(-1)
case ModeComponentSelect:
if l.selection > 0 {
l.selection--
}
l.menu.highlight(l.getComponent(l.selection))
case ModeComponentMove:
l.getSelectedComponent().Move(-1, 0)
case ModeComponentResize:
l.getSelectedComponent().Resize(-1, 0)
}
case console.KeyRight:
switch l.mode {
case ModeDefault:
l.mode = ModeComponentSelect
l.selection = 0
l.menu.highlight(l.getComponent(l.selection))
case ModeChartPinpoint:
2019-02-15 04:34:45 +00:00
chart := l.getSelectedComponent().Drawable.(*runchart.RunChart)
2019-02-11 02:51:55 +00:00
chart.MoveSelection(1)
case ModeComponentSelect:
if l.selection < len(l.components)-1 {
l.selection++
}
l.menu.highlight(l.getComponent(l.selection))
case ModeComponentMove:
l.getSelectedComponent().Move(1, 0)
case ModeComponentResize:
l.getSelectedComponent().Resize(1, 0)
}
case console.KeyUp:
switch l.mode {
2019-02-16 03:46:03 +00:00
case ModeDefault:
l.mode = ModeComponentSelect
l.selection = 0
l.menu.highlight(l.getComponent(l.selection))
case ModeComponentSelect:
if l.selection > 0 {
l.selection--
}
l.menu.highlight(l.getComponent(l.selection))
2019-02-11 02:51:55 +00:00
case ModeMenuOptionSelect:
l.menu.up()
case ModeComponentMove:
l.getSelectedComponent().Move(0, -1)
case ModeComponentResize:
l.getSelectedComponent().Resize(0, -1)
}
case console.KeyDown:
switch l.mode {
2019-02-16 03:46:03 +00:00
case ModeDefault:
l.mode = ModeComponentSelect
l.selection = 0
l.menu.highlight(l.getComponent(l.selection))
case ModeComponentSelect:
if l.selection < len(l.components)-1 {
l.selection++
}
l.menu.highlight(l.getComponent(l.selection))
2019-02-11 02:51:55 +00:00
case ModeMenuOptionSelect:
l.menu.down()
case ModeComponentMove:
l.getSelectedComponent().Move(0, 1)
case ModeComponentResize:
l.getSelectedComponent().Resize(0, 1)
}
}
}
2019-02-11 02:51:55 +00:00
func (l *Layout) ChangeDimensions(width, height int) {
l.SetRect(0, 0, width, height)
2019-01-31 00:02:38 +00:00
}
// TODO func to get prev/next component navigating left/right/top/bottom
func (l *Layout) getComponent(i int) Component {
return l.components[i]
}
func (l *Layout) getSelectedComponent() *Component {
return &l.components[l.selection]
}
2019-02-11 02:51:55 +00:00
func (l *Layout) Draw(buffer *ui.Buffer) {
2019-01-31 00:02:38 +00:00
2019-02-11 02:51:55 +00:00
columnWidth := float64(l.GetRect().Dx()) / columnsCount
rowHeight := float64(l.GetRect().Dy()) / rowsCount
2019-01-31 00:02:38 +00:00
2019-02-11 02:51:55 +00:00
for _, component := range l.components {
2019-01-31 00:02:38 +00:00
2019-01-31 01:41:51 +00:00
x1 := float64(component.Position.X) * columnWidth
y1 := float64(component.Position.Y) * rowHeight
x2 := x1 + float64(component.Size.X)*columnWidth
y2 := y1 + float64(component.Size.Y)*rowHeight
2019-01-31 00:02:38 +00:00
2019-01-31 01:41:51 +00:00
component.Drawable.SetRect(int(x1), int(y1), int(x2), int(y2))
2019-02-03 03:30:45 +00:00
component.Drawable.Draw(buffer)
2019-01-31 00:02:38 +00:00
}
2019-02-11 02:51:55 +00:00
l.menu.Draw(buffer)
2019-01-31 00:02:38 +00:00
}