diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-01-19 23:37:45 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-01-19 23:37:45 -0500 |
| commit | 4f6384f449b50a95255837b1fc394b4ebe14caaf (patch) | |
| tree | 7e17524b87d11f921d1e8d370480fe5634f00537 | |
| parent | cb4343f838b008e3f4da2bf85937ed86cd1805e1 (diff) | |
| download | volute-4f6384f449b50a95255837b1fc394b4ebe14caaf.zip | |
2d navigation
| -rw-r--r-- | focus.go | 59 | ||||
| -rw-r--r-- | main.go | 23 |
2 files changed, 56 insertions, 26 deletions
@@ -1,33 +1,58 @@ package main type Focus struct { - widgets []chan bool - i int // index of focused widget + widgets [][]chan bool + p Point // currently focused widget } -func NewFocus(nWidgets int) Focus { - f := Focus{make([]chan bool, nWidgets), 0} +func NewFocus(rows []int) Focus { + f := Focus{ + make([][]chan bool, len(rows)), + Point{}, + } for i := range f.widgets { - f.widgets[i] = make(chan bool) + f.widgets[i] = make([]chan bool, rows[i]) + for j := range f.widgets[i] { + f.widgets[i][j] = make(chan bool) + } } return f } -func (f *Focus) Next() { - f.widgets[f.i] <- false - f.i = (f.i + 1) % len(f.widgets) - f.widgets[f.i] <- true +func (f *Focus) Left() { + f.widgets[f.p.Y][f.p.X] <- false + if f.p.X <= 0 { + f.p.X = len(f.widgets[f.p.Y]) - 1 + } else { + f.p.X-- + } + f.widgets[f.p.Y][f.p.X] <- true } -func (f *Focus) Prev() { - f.widgets[f.i] <- false - f.i = abs(f.i-1) % len(f.widgets) - f.widgets[f.i] <- true +func (f *Focus) Right() { + f.widgets[f.p.Y][f.p.X] <- false + f.p.X = (f.p.X + 1) % len(f.widgets[f.p.Y]) + f.widgets[f.p.Y][f.p.X] <- true } -func abs(n int) int { - if n < 0 { - return -n +func (f *Focus) Up() { + f.widgets[f.p.Y][f.p.X] <- false + if f.p.Y <= 0 { + f.p.Y = len(f.widgets) - 1 + } else { + f.p.Y-- } - return n + f.p.X = min(f.p.X, len(f.widgets[f.p.Y])-1) + f.widgets[f.p.Y][f.p.X] <- true +} + +func (f *Focus) Down() { + f.widgets[f.p.Y][f.p.X] <- false + f.p.Y = (f.p.Y + 1) % len(f.widgets) + f.p.X = min(f.p.X, len(f.widgets[f.p.Y])-1) + f.widgets[f.p.Y][f.p.X] <- true +} + +type Point struct { + X, Y int } @@ -29,7 +29,7 @@ func run() { displacementChan = make(chan uint) rpmChan = make([]chan uint, POINTS) veChan = make([]chan uint, POINTS) - focus = NewFocus(1 + 2*POINTS) + focus = NewFocus([]int{1, POINTS, POINTS}) ) for i := 0; i < POINTS; i++ { rpmChan[i] = make(chan uint) @@ -52,7 +52,7 @@ func run() { go widget.Input( displacementChan, bounds[1], - focus.widgets[0], + focus.widgets[0][0], mux.MakeEnv(), ) go widget.Label("speed (rpm)", bounds[2], mux.MakeEnv()) @@ -61,24 +61,25 @@ func run() { go widget.Input( rpmChan[i], bounds[3+i], - focus.widgets[1+i], + focus.widgets[1][i], mux.MakeEnv(), ) go widget.Input( veChan[i], bounds[3+POINTS+1+i], - focus.widgets[1+POINTS+i], + focus.widgets[2][i], mux.MakeEnv(), ) } - focus.widgets[focus.i] <- true + focus.widgets[focus.p.Y][focus.p.X] <- true Loop: for { select { case _ = <-displacementChan: case _ = <-rpmChan[0]: + case _ = <-veChan[0]: case event, ok := <-env.Events(): if !ok { // channel closed break Loop @@ -90,10 +91,14 @@ Loop: switch event.Rune { case 'q': break Loop - case 'j', 'l': - focus.Next() - case 'k', 'h': - focus.Prev() + case 'h': + focus.Left() + case 'j': + focus.Down() + case 'k': + focus.Up() + case 'l': + focus.Right() } } } |