Browse Source

Support clicking and dragging to move

tableissue
Trevor Slocum 3 months ago
parent
commit
1490f51ce3
  1. 22
      board.go
  2. 2
      go.mod
  3. 4
      go.sum

22
board.go

@ -11,6 +11,8 @@ import (
"github.com/gdamore/tcell/v2"
)
// TODO Add PlayerName, etc
const (
StateLength = iota
StatePlayerScore
@ -78,6 +80,9 @@ type Board struct {
premove [][2]int
premovefrom map[int]int
premoveto map[int]int
dragFromX int
dragFromY int
}
func NewBoard(client *Client) *Board {
@ -699,6 +704,23 @@ func (b *Board) MouseHandler() func(action cview.MouseAction, event *tcell.Event
}
switch action {
case cview.MouseLeftDown:
b.dragFromX, b.dragFromY = x, y
case cview.MouseLeftUp:
if b.dragFromX != x || b.dragFromY != y {
app.QueueUpdateDraw(func() {
// Simulate click event at start of drag
fromEvent := tcell.NewEventMouse(b.dragFromX, b.dragFromY, tcell.ButtonPrimary, event.Modifiers())
b.TextView.MouseHandler()(cview.MouseLeftClick, fromEvent, setFocus)
consumed, _ = b.TextView.MouseHandler()(cview.MouseLeftClick, event, setFocus)
if consumed {
// Succeeded
return
}
// Failed, undo
b.TextView.MouseHandler()(cview.MouseLeftClick, fromEvent, setFocus)
})
}
case cview.MouseRightClick:
b.TextView.SetHighlightedFunc(nil)
h := b.GetHighlights()

2
go.mod

@ -3,7 +3,7 @@ module code.rocketnine.space/tslocum/bgammon
go 1.16
require (
code.rocketnine.space/tslocum/cview v1.5.6
code.rocketnine.space/tslocum/cview v1.5.7-0.20210712202132-f794cceb95a7
github.com/gdamore/tcell/v2 v2.3.11
github.com/reiver/go-oi v1.0.0
github.com/reiver/go-telnet v0.0.0-20180421082511-9ff0b2ab096e

4
go.sum

@ -1,7 +1,7 @@
code.rocketnine.space/tslocum/cbind v0.1.5 h1:i6NkeLLNPNMS4NWNi3302Ay3zSU6MrqOT+yJskiodxE=
code.rocketnine.space/tslocum/cbind v0.1.5/go.mod h1:LtfqJTzM7qhg88nAvNhx+VnTjZ0SXBJtxBObbfBWo/M=
code.rocketnine.space/tslocum/cview v1.5.6 h1:W0HJFIIgly3LzYoTitZIIaYDDqW0u/qmA0B6jWzw6R0=
code.rocketnine.space/tslocum/cview v1.5.6/go.mod h1:RogJMObbKuGiP8+9WsFsHpPeQQqgkCXgvTLxh7IH5eE=
code.rocketnine.space/tslocum/cview v1.5.7-0.20210712202132-f794cceb95a7 h1:7eL6/S/Xef/jZa2oAtbtImAydpF9vzYf1hKyZU8aY/w=
code.rocketnine.space/tslocum/cview v1.5.7-0.20210712202132-f794cceb95a7/go.mod h1:RogJMObbKuGiP8+9WsFsHpPeQQqgkCXgvTLxh7IH5eE=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.2.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=

Loading…
Cancel
Save