component selection using mouse click

This commit is contained in:
sqshq 2019-04-13 20:24:24 -04:00
parent c8ba8e9491
commit 646fd4f67d
4 changed files with 53 additions and 3 deletions

View File

@ -66,7 +66,20 @@ func (l *Layout) changeMode(m Mode) {
l.ChangeModeEvents <- m
}
func (l *Layout) HandleConsoleEvent(e string) {
func (l *Layout) HandleMouseClick(x int, y int) {
l.getSelection().CommandChannel <- &data.Command{Type: runchart.CommandMoveSelection, Value: 0}
l.menu.Idle()
selected, i := l.findComponentAtPoint(image.Point{X: x, Y: y})
if selected == nil {
l.changeMode(ModeDefault)
} else {
l.selection = i
l.menu.Highlight(selected)
l.changeMode(ModeComponentSelect)
}
}
func (l *Layout) HandleKeyboardEvent(e string) {
selected := l.getSelection()
@ -289,6 +302,39 @@ func (l *Layout) Draw(buffer *ui.Buffer) {
l.menu.Draw(buffer)
}
func (l *Layout) findComponentAtPoint(point image.Point) (*component.Component, int) {
columnWidth := float64(l.GetRect().Dx()) / float64(columnsCount)
rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(rowsCount)
for i, c := range l.Components {
x1 := math.Floor(float64(c.Location.X) * columnWidth)
y1 := math.Floor(float64(c.Location.Y) * rowHeight)
x2 := x1 + math.Floor(float64(c.Size.X))*columnWidth
y2 := y1 + math.Floor(float64(c.Size.Y))*rowHeight
if x2-x1 < minDimension {
x2 = x1 + minDimension
}
if y2-y1 < minDimension {
y2 = y1 + minDimension
}
rectangle := image.Rectangle{Min: image.Point{
X: int(x1), Y: int(y1)},
Max: image.Point{X: int(x2), Y: int(y2)},
}
if point.In(rectangle) {
return c, i
}
}
return nil, -1
}
func (l *Layout) resetAlerts() {
for _, c := range l.Components {
c.AlertChannel <- nil

View File

@ -122,7 +122,7 @@ func (m *Menu) Draw(buffer *ui.Buffer) {
func (m *Menu) renderHighlight(buffer *ui.Buffer) {
arrowsText := "Use arrows for selection"
arrowsText := "Use mouse or arrows for selection"
optionsText := "<ENTER> to view options"
resumeText := "<ESC> to resume"

View File

@ -2,6 +2,7 @@ package console
const (
SignalResize = "<Resize>"
SignalClick = "<MouseLeft>"
)
const (

View File

@ -44,6 +44,9 @@ func (h *Handler) HandleEvents() {
ui.Render(h.layout)
case e := <-h.consoleEvents:
switch e.ID {
case console.SignalClick:
payload := e.Payload.(ui.Mouse)
h.layout.HandleMouseClick(payload.X, payload.Y)
case console.KeyQuit, console.KeyExit:
h.handleExit()
return
@ -51,7 +54,7 @@ func (h *Handler) HandleEvents() {
payload := e.Payload.(ui.Resize)
h.layout.ChangeDimensions(payload.Width, payload.Height)
default:
h.layout.HandleConsoleEvent(e.ID)
h.layout.HandleKeyboardEvent(e.ID)
}
}
}