Add refresh button to lobby

This commit is contained in:
Trevor Slocum 2021-10-14 19:13:30 -07:00
parent 32ab7da9af
commit ff6e3e5147
6 changed files with 77 additions and 22 deletions

View File

@ -1,3 +1,4 @@
//go:build js && wasm
// +build js,wasm
package main

View File

@ -1,3 +1,4 @@
//go:build !js || !wasm
// +build !js !wasm
package main

View File

@ -1,3 +1,4 @@
//go:build js && wasm
// +build js,wasm
package main

View File

@ -218,12 +218,12 @@ func (g *Game) handleEvents() {
for e := range g.Client.Event {
switch event := e.(type) {
case *fibs.EventWho:
empty := len(g.lobby.who) == 0
if viewBoard || empty {
if viewBoard || g.lobby.refresh {
g.lobby.setWhoInfo(event.Who)
if empty {
if g.lobby.refresh {
ebiten.ScheduleFrame()
g.lobby.refresh = false
}
} else {
g.pendingWho = event.Who

View File

@ -4,7 +4,8 @@ import (
"fmt"
"image/color"
"math"
"os"
"sort"
"strings"
"github.com/hajimehoshi/ebiten/v2/inpututil"
@ -19,17 +20,17 @@ type lobbyButton struct {
}
var mainButtons = []*lobbyButton{
{"Invite...", func() {}},
{"Join", func() {}},
{"Refresh", func() {}},
{"Watch", func() {}},
{"Quit", func() {}},
{"Invite", func() {}},
{"Join", func() {}},
}
var inviteButtons = []*lobbyButton{
{"Send", func() {}},
{"Cancel", func() {}},
{"- Point", func() {}},
{"+ Point", func() {}},
{"Cancel", func() {}},
{"Send", func() {}},
}
type lobby struct {
@ -60,11 +61,14 @@ type lobby struct {
inviteUser *fibs.WhoInfo
invitePoints int
refresh bool
}
func NewLobby() *lobby {
l := &lobby{
op: &ebiten.DrawImageOptions{},
refresh: true,
op: &ebiten.DrawImageOptions{},
}
return l
}
@ -72,6 +76,19 @@ func NewLobby() *lobby {
func (l *lobby) setWhoInfo(who []*fibs.WhoInfo) {
l.who = who
sort.Slice(l.who, func(i, j int) bool {
if (l.who[i].Opponent != "") != (l.who[j].Opponent != "") {
return l.who[i].Opponent != ""
}
if l.who[i].Ready != l.who[j].Ready {
return l.who[i].Ready
}
if l.who[i].Rating != l.who[j].Rating {
return l.who[i].Rating > l.who[j].Rating
}
return strings.ToLower(l.who[i].Username) < strings.ToLower(l.who[j].Username)
})
l.bufferDirty = true
}
@ -199,6 +216,8 @@ func (l *lobby) drawBuffer() {
status := "In the lobby"
if who.Opponent != "" {
status = fmt.Sprintf("Playing versus %s", who.Opponent)
} else if who.Ready {
status = "Ready to play"
}
drawEntry(cx+l.padding, cy+l.padding, who.Username, details, status, i == l.selected)
@ -287,13 +306,9 @@ func (l *lobby) click(x, y int) {
if l.inviteUser != nil {
switch buttonIndex {
case 0:
l.c.Out <- []byte(fmt.Sprintf("invite %s %d", l.inviteUser.Username, l.invitePoints))
l.inviteUser = nil
l.bufferDirty = true
l.bufferButtonsDirty = true
viewBoard = true
case 1:
l.invitePoints--
if l.invitePoints < 1 {
@ -304,27 +319,32 @@ func (l *lobby) click(x, y int) {
l.invitePoints++
l.bufferDirty = true
case 3:
l.c.Out <- []byte(fmt.Sprintf("invite %s %d", l.inviteUser.Username, l.invitePoints))
l.inviteUser = nil
l.bufferDirty = true
l.bufferButtonsDirty = true
viewBoard = true
}
return
}
switch buttonIndex {
case 0:
l.refresh = true
l.c.Out <- []byte("rawwho")
case 1:
l.c.Out <- []byte(fmt.Sprintf("watch %s", l.who[l.selected].Username))
viewBoard = true
case 2:
l.inviteUser = l.who[l.selected]
l.invitePoints = 1
l.bufferDirty = true
l.bufferButtonsDirty = true
case 1:
case 3:
l.c.Out <- []byte(fmt.Sprintf("join %s", l.who[l.selected].Username))
viewBoard = true
case 2:
l.c.Out <- []byte(fmt.Sprintf("watch %s", l.who[l.selected].Username))
viewBoard = true
case 3:
os.Exit(0)
}
return
}
@ -338,10 +358,16 @@ func (l *lobby) click(x, y int) {
}
func (l *lobby) update() {
scrollLength := 2
scrollLength := 3
if _, y := ebiten.Wheel(); y != 0 {
l.offset -= int(math.Ceil(y)) * scrollLength
scroll := int(math.Ceil(y))
if scroll < -1 {
scroll = -1
} else if scroll > 1 {
scroll = 1
}
l.offset -= scroll * scrollLength
l.bufferDirty = true
}

View File

@ -2,6 +2,7 @@ package game
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const (
@ -29,6 +30,8 @@ type tabbedBuffers struct {
state int
focused bool
touchIDs []ebiten.TouchID
}
func newTabbedBuffers() *tabbedBuffers {
@ -79,3 +82,26 @@ func (t *tabbedBuffers) draw(target *ebiten.Image) {
t.op.ColorM.Scale(1, 1, 1, alpha)
target.DrawImage(t.buffer, t.op)
}
func (t *tabbedBuffers) click(x, y int) {
}
func (t *tabbedBuffers) update() {
// TODO accept keyboard input
// TODO switch tabs
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
t.click(x, y)
}
t.touchIDs = inpututil.AppendJustPressedTouchIDs(t.touchIDs[:0])
for _, id := range t.touchIDs {
x, y := ebiten.TouchPosition(id)
t.click(x, y)
}
// TODO add show virtual keyboard button
}