sampler-fork/component/layout/layout.go

297 lines
8.2 KiB
Go
Raw Normal View History

2019-03-08 04:04:46 +00:00
package layout
2019-01-31 00:02:38 +00:00
import (
2019-03-14 03:01:44 +00:00
ui "github.com/gizak/termui/v3"
2019-03-08 04:04:46 +00:00
"github.com/sqshq/sampler/component"
2019-02-24 04:49:09 +00:00
"github.com/sqshq/sampler/component/runchart"
"github.com/sqshq/sampler/config"
2019-02-11 02:51:55 +00:00
"github.com/sqshq/sampler/console"
"image"
2019-03-01 03:44:01 +00:00
"math"
2019-01-31 00:02:38 +00:00
)
type Layout struct {
2019-02-11 02:51:55 +00:00
ui.Block
2019-03-08 04:04:46 +00:00
Components []component.Component
ChangeModeEvents chan Mode
2019-03-08 04:04:46 +00:00
statusbar *component.StatusBar
menu *component.Menu
2019-02-25 00:08:36 +00:00
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
2019-03-03 01:04:02 +00:00
ModePause Mode = 1
ModeComponentSelect Mode = 2
ModeMenuOptionSelect Mode = 3
ModeComponentMove Mode = 4
ModeComponentResize Mode = 5
ModeChartPinpoint Mode = 6
2019-02-11 02:51:55 +00:00
)
2019-01-31 00:02:38 +00:00
const (
2019-02-26 04:36:23 +00:00
columnsCount = 80
2019-03-01 03:44:01 +00:00
rowsCount = 40
2019-03-02 21:38:36 +00:00
minDimension = 3
2019-02-25 00:37:57 +00:00
statusbarHeight = 1
2019-01-31 00:02:38 +00:00
)
2019-03-08 04:04:46 +00:00
func NewLayout(width, height int, statusline *component.StatusBar, menu *component.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)
2019-02-25 00:37:57 +00:00
statusline.SetRect(0, height-statusbarHeight, width, height)
2019-01-31 00:02:38 +00:00
return &Layout{
Block: block,
2019-03-08 04:04:46 +00:00
Components: make([]component.Component, 0),
2019-02-25 00:08:36 +00:00
statusbar: statusline,
menu: menu,
mode: ModeDefault,
selection: 0,
ChangeModeEvents: make(chan Mode, 10),
2019-01-31 00:02:38 +00:00
}
}
func (l *Layout) AddComponent(Type config.ComponentType, drawable ui.Drawable, title string, position config.Position, size config.Size, refreshRateMs int) {
2019-03-08 04:04:46 +00:00
l.Components = append(l.Components, component.Component{
Type: Type,
Drawable: drawable,
Title: title,
Position: position,
Size: size,
RefreshRateMs: refreshRateMs,
})
}
2019-03-08 04:04:46 +00:00
func (l *Layout) GetComponents(Type config.ComponentType) []component.Component {
2019-03-08 04:04:46 +00:00
var components []component.Component
2019-03-08 04:04:46 +00:00
for _, c := range l.Components {
if c.Type == Type {
components = append(components, c)
}
}
return components
2019-01-31 00:02:38 +00:00
}
func (l *Layout) changeMode(m Mode) {
l.mode = m
l.ChangeModeEvents <- m
}
2019-02-11 02:51:55 +00:00
func (l *Layout) HandleConsoleEvent(e string) {
switch e {
2019-03-03 01:04:02 +00:00
case console.KeyPause:
if l.mode == ModePause {
l.changeMode(ModeDefault)
} else {
if l.getSelectedComponent().Type == config.TypeRunChart {
chart := l.getSelectedComponent().Drawable.(*runchart.RunChart)
chart.DisableSelection()
}
2019-03-08 04:04:46 +00:00
l.menu.Idle()
2019-03-03 01:04:02 +00:00
l.changeMode(ModePause)
}
2019-02-11 02:51:55 +00:00
case console.KeyEnter:
switch l.mode {
case ModeComponentSelect:
2019-03-08 04:04:46 +00:00
l.menu.Choose()
l.changeMode(ModeMenuOptionSelect)
2019-02-11 02:51:55 +00:00
case ModeMenuOptionSelect:
2019-03-08 04:04:46 +00:00
option := l.menu.GetSelectedOption()
2019-02-11 02:51:55 +00:00
switch option {
2019-03-08 04:04:46 +00:00
case component.MenuOptionMove:
l.changeMode(ModeComponentMove)
2019-03-08 04:04:46 +00:00
l.menu.MoveOrResize()
case component.MenuOptionResize:
l.changeMode(ModeComponentResize)
2019-03-08 04:04:46 +00:00
l.menu.MoveOrResize()
case component.MenuOptionPinpoint:
l.changeMode(ModeChartPinpoint)
2019-03-08 04:04:46 +00:00
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-03-08 04:04:46 +00:00
case component.MenuOptionResume:
l.changeMode(ModeDefault)
2019-03-08 04:04:46 +00:00
l.menu.Idle()
2019-02-11 02:51:55 +00:00
}
case ModeComponentMove:
fallthrough
case ModeComponentResize:
2019-03-08 04:04:46 +00:00
l.menu.Idle()
l.changeMode(ModeDefault)
2019-02-11 02:51:55 +00:00
}
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:
2019-03-08 04:04:46 +00:00
l.menu.Idle()
l.changeMode(ModeDefault)
2019-02-11 02:51:55 +00:00
}
case console.KeyLeft:
switch l.mode {
case ModeDefault:
l.changeMode(ModeComponentSelect)
2019-03-08 04:04:46 +00:00
l.menu.Highlight(l.getComponent(l.selection))
2019-02-11 02:51:55 +00:00
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:
l.moveSelection(e)
2019-03-08 04:04:46 +00:00
l.menu.Highlight(l.getComponent(l.selection))
2019-02-11 02:51:55 +00:00
case ModeComponentMove:
l.getSelectedComponent().Move(-1, 0)
case ModeComponentResize:
l.getSelectedComponent().Resize(-1, 0)
}
case console.KeyRight:
switch l.mode {
case ModeDefault:
l.changeMode(ModeComponentSelect)
2019-03-08 04:04:46 +00:00
l.menu.Highlight(l.getComponent(l.selection))
2019-02-11 02:51:55 +00:00
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:
l.moveSelection(e)
2019-03-08 04:04:46 +00:00
l.menu.Highlight(l.getComponent(l.selection))
2019-02-11 02:51:55 +00:00
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.changeMode(ModeComponentSelect)
2019-03-08 04:04:46 +00:00
l.menu.Highlight(l.getComponent(l.selection))
2019-02-16 03:46:03 +00:00
case ModeComponentSelect:
l.moveSelection(e)
2019-03-08 04:04:46 +00:00
l.menu.Highlight(l.getComponent(l.selection))
2019-02-11 02:51:55 +00:00
case ModeMenuOptionSelect:
2019-03-08 04:04:46 +00:00
l.menu.Up()
2019-02-11 02:51:55 +00:00
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.changeMode(ModeComponentSelect)
2019-03-08 04:04:46 +00:00
l.menu.Highlight(l.getComponent(l.selection))
2019-02-16 03:46:03 +00:00
case ModeComponentSelect:
l.moveSelection(e)
2019-03-08 04:04:46 +00:00
l.menu.Highlight(l.getComponent(l.selection))
2019-02-11 02:51:55 +00:00
case ModeMenuOptionSelect:
2019-03-08 04:04:46 +00:00
l.menu.Down()
2019-02-11 02:51:55 +00:00
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
}
2019-03-08 04:04:46 +00:00
func (l *Layout) getComponent(i int) *component.Component {
return &l.Components[i]
}
2019-03-08 04:04:46 +00:00
func (l *Layout) getSelectedComponent() *component.Component {
return &l.Components[l.selection]
}
func (l *Layout) moveSelection(direction string) {
previouslySelected := *l.getSelectedComponent()
newlySelectedIndex := l.selection
for i, current := range l.Components {
if current == previouslySelected {
continue
}
if newlySelectedIndex < 0 {
newlySelectedIndex = i
}
var previouslySelectedCornerPoint image.Point
var newlySelectedCornerPoint image.Point
var currentCornerPoint image.Point
switch direction {
case console.KeyLeft:
2019-03-08 04:04:46 +00:00
previouslySelectedCornerPoint = component.GetRectLeftAgeCenter(previouslySelected.Drawable.GetRect())
newlySelectedCornerPoint = component.GetRectRightAgeCenter(l.getComponent(newlySelectedIndex).Drawable.GetRect())
currentCornerPoint = component.GetRectRightAgeCenter(current.Drawable.GetRect())
case console.KeyRight:
2019-03-08 04:04:46 +00:00
previouslySelectedCornerPoint = component.GetRectRightAgeCenter(previouslySelected.Drawable.GetRect())
newlySelectedCornerPoint = component.GetRectLeftAgeCenter(l.getComponent(newlySelectedIndex).Drawable.GetRect())
currentCornerPoint = component.GetRectLeftAgeCenter(current.Drawable.GetRect())
case console.KeyUp:
2019-03-08 04:04:46 +00:00
previouslySelectedCornerPoint = component.GetRectTopAgeCenter(previouslySelected.Drawable.GetRect())
newlySelectedCornerPoint = component.GetRectBottomAgeCenter(l.getComponent(newlySelectedIndex).Drawable.GetRect())
currentCornerPoint = component.GetRectBottomAgeCenter(current.Drawable.GetRect())
case console.KeyDown:
2019-03-08 04:04:46 +00:00
previouslySelectedCornerPoint = component.GetRectBottomAgeCenter(previouslySelected.Drawable.GetRect())
newlySelectedCornerPoint = component.GetRectTopAgeCenter(l.getComponent(newlySelectedIndex).Drawable.GetRect())
currentCornerPoint = component.GetRectTopAgeCenter(current.Drawable.GetRect())
}
2019-03-08 04:04:46 +00:00
if component.GetDistance(previouslySelectedCornerPoint, currentCornerPoint) <
component.GetDistance(previouslySelectedCornerPoint, newlySelectedCornerPoint) {
newlySelectedIndex = i
}
}
l.selection = newlySelectedIndex
}
2019-02-11 02:51:55 +00:00
func (l *Layout) Draw(buffer *ui.Buffer) {
2019-01-31 00:02:38 +00:00
2019-03-01 03:44:01 +00:00
columnWidth := float64(l.GetRect().Dx()) / float64(columnsCount)
rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(rowsCount)
2019-01-31 00:02:38 +00:00
2019-03-08 04:04:46 +00:00
for _, c := range l.Components {
2019-01-31 00:02:38 +00:00
2019-03-08 04:04:46 +00:00
x1 := math.Floor(float64(c.Position.X) * columnWidth)
y1 := math.Floor(float64(c.Position.Y) * rowHeight)
x2 := x1 + math.Floor(float64(c.Size.X))*columnWidth
y2 := y1 + math.Floor(float64(c.Size.Y))*rowHeight
2019-01-31 00:02:38 +00:00
2019-03-02 21:38:36 +00:00
if x2-x1 < minDimension {
x2 = x1 + minDimension
}
if y2-y1 < minDimension {
y2 = y1 + minDimension
}
2019-03-08 04:04:46 +00:00
c.Drawable.SetRect(int(x1), int(y1), int(x2), int(y2))
c.Drawable.Draw(buffer)
2019-01-31 00:02:38 +00:00
}
2019-02-11 02:51:55 +00:00
2019-02-25 00:37:57 +00:00
l.statusbar.SetRect(
0, l.GetRect().Dy()-statusbarHeight,
l.GetRect().Dx(), l.GetRect().Dy())
2019-02-25 00:08:36 +00:00
l.statusbar.Draw(buffer)
2019-03-03 01:04:02 +00:00
l.menu.Draw(buffer)
2019-01-31 00:02:38 +00:00
}