Accept user input in chat window

This commit is contained in:
Trevor Slocum 2021-11-08 17:11:51 -08:00
parent 8e5d87bbcc
commit a061c032c6
4 changed files with 60 additions and 27 deletions

View File

@ -259,6 +259,7 @@ func (g *Game) Connect() {
g.Client = fibs.NewClient(address, g.Username, g.Password)
g.lobby.c = g.Client
g.Board.Client = g.Client
g.buffers.client = g.Client
go g.handleEvents()
@ -380,7 +381,7 @@ func (g *Game) Update() error { // Called by ebiten only when input occurs
f()
}
if inpututil.IsKeyJustPressed(ebiten.KeyD) {
if ebiten.IsKeyPressed(ebiten.KeyControl) && inpututil.IsKeyJustPressed(ebiten.KeyD) {
g.Debug++
if g.Debug == 3 {
g.Debug = 0
@ -392,12 +393,12 @@ func (g *Game) Update() error { // Called by ebiten only when input occurs
viewBoard = !viewBoard
}
g.buffers.update()
if !viewBoard {
g.lobby.update()
} else {
g.Board.update()
g.buffers.update()
}
return nil
@ -449,7 +450,7 @@ http://www.fibs.com/help.html#register`
if g.Debug > 0 {
g.drawBuffer.Reset()
g.drawBuffer.Write([]byte(fmt.Sprintf("FPS %0.0f %c\nTPS %0.0f", ebiten.CurrentFPS(), spinner[g.spinnerIndex], ebiten.CurrentTPS())))
g.drawBuffer.Write([]byte(fmt.Sprintf("FPS %c %0.0f", spinner[g.spinnerIndex], ebiten.CurrentFPS())))
g.spinnerIndex++
if g.spinnerIndex == 4 {

View File

@ -3,6 +3,7 @@ package game
import (
"image"
"code.rocketnine.space/tslocum/fibs"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
@ -40,6 +41,12 @@ type tabbedBuffers struct {
focused bool
touchIDs []ebiten.TouchID
incomingBuffer []rune
inputBuffer []byte
client *fibs.Client
}
func newTabbedBuffers() *tabbedBuffers {
@ -63,6 +70,8 @@ func (t *tabbedBuffers) setRect(x, y, w, h int) {
return
}
// TODO dynamic padding
if t.w != w || t.h != h {
t.buffer = ebiten.NewImage(w, h)
t.bufferDirty = true
@ -90,9 +99,7 @@ func (t *tabbedBuffers) drawBuffer() {
lineHeight := 16
showLines := t.h / lineHeight
if showLines > 1 {
showLines--
}
// Leave space for the input buffer.
if showLines > 1 {
showLines--
}
@ -108,7 +115,7 @@ func (t *tabbedBuffers) drawBuffer() {
text.Draw(t.buffer, line, monoFont, 0, (lineHeight * (i + 1)), colornames.White)
}
text.Draw(t.buffer, "Say: Input buffer test", monoFont, 0, t.h-lineHeight, colornames.White)
text.Draw(t.buffer, "> "+string(t.inputBuffer), monoFont, 0, t.h, colornames.White)
}
func (t *tabbedBuffers) draw(target *ebiten.Image) {
@ -151,17 +158,6 @@ func (t *tabbedBuffers) click(x, y int) {
}
func (t *tabbedBuffers) update() {
// TODO accept keyboard input
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
if t.state == windowMinimized {
t.state = windowNormal
} else {
t.state = windowMinimized
}
t.bufferDirty = true
}
// Enter brings up keyboard and hides it when there is no input
// TODO switch tabs
@ -177,5 +173,40 @@ func (t *tabbedBuffers) update() {
t.click(x, y)
}
// Read user input.
t.incomingBuffer = ebiten.AppendInputChars(t.incomingBuffer[:0])
if len(t.incomingBuffer) > 0 {
t.inputBuffer = append(t.inputBuffer, []byte(string(t.incomingBuffer))...)
t.bufferDirty = true
}
if inpututil.IsKeyJustPressed(ebiten.KeyBackspace) && len(t.inputBuffer) > 0 {
b := string(t.inputBuffer)
if len(b) > 1 {
t.inputBuffer = []byte(b[:len(b)-1])
} else {
t.inputBuffer = nil
}
t.bufferDirty = true
}
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
if len(t.inputBuffer) == 0 {
if t.state == windowMinimized {
t.state = windowNormal
} else {
t.state = windowMinimized
}
} else {
if t.client != nil {
t.client.Out <- t.inputBuffer
} else {
fibs.StatusWriter.Write([]byte("* You have not connected to a server yet"))
}
t.inputBuffer = nil
}
t.bufferDirty = true
}
// TODO add show virtual keyboard button
}

6
go.mod
View File

@ -5,10 +5,10 @@ go 1.17
require (
code.rocketnine.space/tslocum/fibs v0.0.0-20211104004539-6041d6487dd3
code.rocketnine.space/tslocum/kibodo v0.0.0-20211027223129-7b870790d865
github.com/hajimehoshi/ebiten/v2 v2.2.1
github.com/hajimehoshi/ebiten/v2 v2.2.2
github.com/llgcode/draw2d v0.0.0-20210904075650-80aa0a2a901d
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
golang.org/x/exp v0.0.0-20211103171733-83d51122435b
golang.org/x/exp v0.0.0-20211105205138-14c72366447f
golang.org/x/image v0.0.0-20211028202545-6944b10bf410
)
@ -23,7 +23,7 @@ require (
github.com/reiver/go-telnet v0.0.0-20180421082511-9ff0b2ab096e // indirect
golang.org/x/mobile v0.0.0-20211103151657-e68c98865fb2 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
golang.org/x/sys v0.0.0-20211108224332-cbcd623f202e // indirect
golang.org/x/text v0.3.7 // indirect
nhooyr.io/websocket v1.8.7 // indirect
)

11
go.sum
View File

@ -132,8 +132,9 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hajimehoshi/bitmapfont/v2 v2.1.3 h1:JefUkL0M4nrdVwVq7MMZxSTh6mSxOylm+C4Anoucbb0=
github.com/hajimehoshi/bitmapfont/v2 v2.1.3/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs=
github.com/hajimehoshi/ebiten/v2 v2.2.1 h1:YhITMaBQmnwb4kzAXCCfSkSZmeQX7pfQ7BnC32cnPjc=
github.com/hajimehoshi/ebiten/v2 v2.2.1/go.mod h1:olKl/qqhMBBAm2oI7Zy292nCtE+nitlmYKNF3UpbFn0=
github.com/hajimehoshi/ebiten/v2 v2.2.2 h1:92E+ogdNyH1P/LlvMQ7vonbFDh6bl+O7Ak+H1HX0RX8=
github.com/hajimehoshi/ebiten/v2 v2.2.2/go.mod h1:olKl/qqhMBBAm2oI7Zy292nCtE+nitlmYKNF3UpbFn0=
github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
github.com/hajimehoshi/go-mp3 v0.3.2/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
@ -350,8 +351,8 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/exp v0.0.0-20211012155715-ffe10e552389/go.mod h1:a3o/VtDNHN+dCVLEpzjjUHOzR+Ln3DHX056ZPzoZGGA=
golang.org/x/exp v0.0.0-20211103171733-83d51122435b h1:e+Rv1NiVC9bG9xDgJ2avTRqmdMJ6X5q1f8QQKr5uFww=
golang.org/x/exp v0.0.0-20211103171733-83d51122435b/go.mod h1:OyI624f2tQ/aU3IMa7GB16Hk54CHURAfHfj6tMqtyhA=
golang.org/x/exp v0.0.0-20211105205138-14c72366447f h1:LOrKZCSwatuEIzc+tzZRm7m5pmv+xxDXjGqElpd2LGA=
golang.org/x/exp v0.0.0-20211105205138-14c72366447f/go.mod h1:OyI624f2tQ/aU3IMa7GB16Hk54CHURAfHfj6tMqtyhA=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
@ -441,8 +442,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211108224332-cbcd623f202e h1:9nbuBbpiqktwdlzHKUohsD5+y2a0QvX98gIWK2ARkqc=
golang.org/x/sys v0.0.0-20211108224332-cbcd623f202e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=