Skip to content

Commit

Permalink
Use updated button API for menus
Browse files Browse the repository at this point in the history
  • Loading branch information
z-riley committed Oct 21, 2024
1 parent e30fb2f commit 467617e
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 99 deletions.
70 changes: 37 additions & 33 deletions common/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,44 @@ var buttonStylePressed = turdgl.Style{
Bloom: 5,
}

// MenuButton is a commonly used button for navigating menus.
type MenuButton struct{ *turdgl.Button }

// NewMenuButton constructs a new button with suitable defaults for a menu button.
func NewMenuButton(width, height float64, pos turdgl.Vec, cb func()) *MenuButton {
r := turdgl.NewRect(width, height, pos, turdgl.WithStyle(buttonStyleUnpressed))
b := turdgl.NewButton(r, FontPathMedium).
// NewMenuButton constructs a new menu button with sensible defaults.
func NewMenuButton(width, height float64, pos turdgl.Vec, cb func()) *turdgl.Button {
b := turdgl.NewButton(
turdgl.NewRect(width, height, pos, turdgl.WithStyle(buttonStyleUnpressed)),
FontPathMedium,
).
SetLabelSize(36).
SetLabelColour(LightFontColour)
b.Behaviour = turdgl.OnRelease
b.SetCallback(func(m turdgl.MouseState) { cb() })

return &MenuButton{b}
}
SetLabelText("SET ME").
SetLabelColour(LightFontColour).
SetLabelAlignment(turdgl.AlignCustom).
SetLabelOffset(turdgl.Vec{X: 0, Y: 32})

func (b *MenuButton) Update(win *turdgl.Window) {
// Adjust style if cursor hovering or button is pressed
if b.Shape.IsWithin(win.MouseLocation()) {
if win.MouseButtonState() == turdgl.LeftClick {
b.Label.SetColour(LighterFontColour)
b.Shape.SetStyle(buttonStylePressed)
} else {
b.SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnHold},
func() {
b.Label.SetColour(LighterFontColour)
b.Shape.SetStyle(buttonStyleHovering)
}
} else {
b.Label.SetColour(LightFontColour)
b.Shape.SetStyle(buttonStyleUnpressed)
}
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnRelease},
func() {
b.Label.SetColour(LightFontColour)
b.Shape.SetStyle(buttonStyleUnpressed)
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.LeftClick, Behaviour: turdgl.OnPress},
func() {
b.Label.SetColour(LighterFontColour)
b.Shape.SetStyle(buttonStylePressed)
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.LeftClick, Behaviour: turdgl.OnRelease},
cb,
)

// Call underlying button update function
b.Button.Update(win)
return b
}

// GameButton is a button that's used in the main game's UI.
type GameButton turdgl.Button

// NewGameButton constructs a new game button with sensible defaults.
func NewGameButton(width, height float64, pos turdgl.Vec, cb func()) *turdgl.Button {
r := turdgl.NewCurvedRect(width, height, 3, pos)
Expand All @@ -69,9 +70,12 @@ func NewGameButton(width, height float64, pos turdgl.Vec, cb func()) *turdgl.But
SetLabelSize(14).
SetLabelColour(WhiteFontColour).
SetLabelAlignment(turdgl.AlignCustom).
SetCallback(func(turdgl.MouseState) { cb() })
b.Behaviour = turdgl.OnRelease
b.Label.SetOffset(turdgl.Vec{X: width / 2, Y: height / 1.2})
SetLabelOffset(turdgl.Vec{X: width / 2, Y: height / 1.2}).
// TODO: add hover and press animations
SetCallback(
turdgl.ButtonTrigger{State: turdgl.LeftClick, Behaviour: turdgl.OnRelease},
cb,
)

return b
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.0
require (
github.com/brunoga/deep v1.2.4
github.com/google/uuid v1.6.0
github.com/z-riley/turdgl v0.0.0-20241010200905-70b7776935f4
github.com/z-riley/turdgl v0.0.0-20241021073636-5639f5bbb473
github.com/z-riley/turdserve v0.0.0-20241013151207-e4418fc464c6
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
)
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ github.com/veandco/go-sdl2 v0.4.40 h1:fZv6wC3zz1Xt167P09gazawnpa0KY5LM7JAvKpX9d/
github.com/veandco/go-sdl2 v0.4.40/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY=
github.com/z-riley/turdgl v0.0.0-20241010200905-70b7776935f4 h1:0zb+jC6JuP3uBneCjuAEkEDTBTLocxrpblm4unaIcXU=
github.com/z-riley/turdgl v0.0.0-20241010200905-70b7776935f4/go.mod h1:PUy0rnENvtQrZRghi9dYinsUUE7gvC9wOz8C/gcwJ8A=
github.com/z-riley/turdgl v0.0.0-20241020141606-fbf610fdec06 h1:TAXyiYGguJj8IHDOw9otkkKVRPFr2uNY9ajK99o8cmQ=
github.com/z-riley/turdgl v0.0.0-20241020141606-fbf610fdec06/go.mod h1:PUy0rnENvtQrZRghi9dYinsUUE7gvC9wOz8C/gcwJ8A=
github.com/z-riley/turdgl v0.0.0-20241021065243-0b2b1ab95537 h1:SlB89Jp1cXa8fvcVrH8rIIPTHo6UEuMX7RRPBex+qds=
github.com/z-riley/turdgl v0.0.0-20241021065243-0b2b1ab95537/go.mod h1:PUy0rnENvtQrZRghi9dYinsUUE7gvC9wOz8C/gcwJ8A=
github.com/z-riley/turdgl v0.0.0-20241021072845-236a4005db3d h1:v1DfH/iEqo2qtIzw975YnPNe+Cd0rIaT7K/KQpxePWE=
github.com/z-riley/turdgl v0.0.0-20241021072845-236a4005db3d/go.mod h1:PUy0rnENvtQrZRghi9dYinsUUE7gvC9wOz8C/gcwJ8A=
github.com/z-riley/turdgl v0.0.0-20241021073636-5639f5bbb473 h1:msavOiRMqt58S06zI/gAOLdbYwTkuW8AuGN0aY6IFaA=
github.com/z-riley/turdgl v0.0.0-20241021073636-5639f5bbb473/go.mod h1:PUy0rnENvtQrZRghi9dYinsUUE7gvC9wOz8C/gcwJ8A=
github.com/z-riley/turdserve v0.0.0-20241012214724-ac0a7bd77e0a h1:osSGToKmun666CuIm/2clKw/3F5KpThk9diQJ9h6IgE=
github.com/z-riley/turdserve v0.0.0-20241012214724-ac0a7bd77e0a/go.mod h1:i9Dlhid4XYNAFdTC4L94dEpXujy8FsfEGlkV7VH4Wmk=
github.com/z-riley/turdserve v0.0.0-20241013071742-f4b41cfd97b0 h1:lKSuct1fi5SW3Rgny2H5K/uLxrKjbtm3kGX2PZlrYyI=
Expand Down
34 changes: 17 additions & 17 deletions screen/multiplayer_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ type MultiplayerHostScreen struct {
title *turdgl.Text
ipHeading *turdgl.TextBox
ipBody *turdgl.TextBox
nameHeading *common.MenuButton
nameHeading *turdgl.Button
nameEntry *turdgl.TextBox
start *common.MenuButton
back *common.MenuButton
start *turdgl.Button
back *turdgl.Button
playerCards []*playerCard

server *turdserve.Server
Expand All @@ -50,21 +50,21 @@ func (s *MultiplayerHostScreen) Enter(_ InitData) {
SetTextOffset(turdgl.Vec{X: 0, Y: 32}).SetText(s.getIPAddr())

s.nameHeading = common.NewMenuButton(400, 60, turdgl.Vec{X: 200 - 20, Y: 200}, func() {})
s.nameHeading.SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Your name:")
s.nameHeading.SetLabelText("Your name:")
s.nameEntry = common.NewEntryBox(400, 60, turdgl.Vec{X: 600 + 20, Y: 200})

s.start = common.NewMenuButton(400, 60, turdgl.Vec{X: 200 - 20, Y: 650}, func() {})
s.start.SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Start game")
s.start.SetCallback(func(_ turdgl.MouseState) {
if err := s.startGame(); err != nil {
fmt.Println("Failed to start game:", err)
}
})

s.back = common.NewMenuButton(400, 60, turdgl.Vec{X: 600 + 20, Y: 650},
s.start = common.NewMenuButton(
400, 60, turdgl.Vec{X: 200 - 20, Y: 650},
func() {
if err := s.startGame(); err != nil {
fmt.Println("Failed to start game:", err)
}
},
).SetLabelText("Start game")
s.back = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 600 + 20, Y: 650},
func() { SetScreen(MultiplayerMenu, nil) },
)
s.back.SetLabelAlignment(turdgl.AlignCustom).SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Back")
).SetLabelText("Back")

s.playerCards = make([]*playerCard, maxPlayers-1)
for i := range s.playerCards {
Expand Down Expand Up @@ -122,7 +122,7 @@ func (s *MultiplayerHostScreen) Update() {
s.win.Draw(l)
}

for _, b := range []*common.MenuButton{
for _, b := range []*turdgl.Button{
s.nameHeading,
s.start,
s.back,
Expand Down
46 changes: 25 additions & 21 deletions screen/multiplayer_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type MultiplayerJoinScreen struct {
win *turdgl.Window

title *turdgl.Text
ipHeading *common.MenuButton
ipHeading *turdgl.Button
ipEntry *turdgl.TextBox
nameHeading *common.MenuButton
nameHeading *turdgl.Button
nameEntry *turdgl.TextBox
join *common.MenuButton
back *common.MenuButton
join *turdgl.Button
back *turdgl.Button

hostIsReady chan bool
client *turdserve.Client
Expand All @@ -39,18 +39,25 @@ func (s *MultiplayerJoinScreen) Enter(_ InitData) {
SetAlignment(turdgl.AlignCentre).
SetSize(40)

s.ipHeading = common.NewMenuButton(400, 60, turdgl.Vec{X: 200 - 20, Y: 200}, func() {})
s.ipHeading.SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Host IP:")
s.ipHeading = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 200 - 20, Y: 200},
func() {},
).SetLabelText("Host IP:")

s.ipEntry = common.NewEntryBox(400, 60, turdgl.Vec{X: 600 + 20, Y: 200})
s.ipEntry.SetText("127.0.0.1") // temporary for local testing

s.nameHeading = common.NewMenuButton(400, 60, turdgl.Vec{X: 200 - 20, Y: 300}, func() {})
s.nameHeading.SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Your name:")
s.nameHeading = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 200 - 20, Y: 300}, func() {},
).SetLabelText("Your name:")

s.nameEntry = common.NewEntryBox(400, 60, turdgl.Vec{X: 600 + 20, Y: 300})
s.hostIsReady = make(chan bool)
s.join = common.NewMenuButton(400, 60, turdgl.Vec{X: 400, Y: 400},
s.join = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 400, Y: 400},
func() {
if err := s.joinGame(); err != nil {
fmt.Println("Failed to join game:", err)
Expand All @@ -60,7 +67,7 @@ func (s *MultiplayerJoinScreen) Enter(_ InitData) {
s.join.SetLabelText("Waiting for host")

// Disable the button so user can't connect again
s.join.SetCallback(func(_ turdgl.MouseState) {})
s.join.Disable()

go func() {
if <-s.hostIsReady {
Expand All @@ -69,19 +76,16 @@ func (s *MultiplayerJoinScreen) Enter(_ InitData) {
}
}()
},
)
s.join.SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Join")

s.back = common.NewMenuButton(400, 60, turdgl.Vec{X: 400, Y: 500}, func() {})
s.back.SetLabelAlignment(turdgl.AlignCustom).
SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Back")
s.back.SetCallback(
func(_ turdgl.MouseState) {
).SetLabelText("Join")

s.back = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 400, Y: 500},
func() {
s.join.SetLabelText("Join")
s.client.Destroy()
SetScreen(MultiplayerMenu, nil)
},
)
}).SetLabelText("Back")

s.client = turdserve.NewClient()

Expand All @@ -101,7 +105,7 @@ func (s *MultiplayerJoinScreen) Update() {

s.win.Draw(s.title)

for _, b := range []*common.MenuButton{
for _, b := range []*turdgl.Button{
s.ipHeading,
s.nameHeading,
s.join,
Expand Down
31 changes: 20 additions & 11 deletions screen/multiplayer_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ type MultiplayerMenuScreen struct {
win *turdgl.Window

title *turdgl.Text
join *common.MenuButton
host *common.MenuButton
back *common.MenuButton
join *turdgl.Button
host *turdgl.Button
back *turdgl.Button
}

// NewTitle Screen constructs a new multiplayer menu screen for the given window.
Expand All @@ -26,14 +26,23 @@ func (s *MultiplayerMenuScreen) Enter(_ InitData) {
SetAlignment(turdgl.AlignCentre).
SetSize(40)

s.join = common.NewMenuButton(400, 60, turdgl.Vec{X: 400, Y: 300}, func() { SetScreen(MultiplayerJoin, nil) })
s.join.SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Join game")
s.join = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 400, Y: 300},
func() { SetScreen(MultiplayerJoin, nil) },
).SetLabelText("Join game")

s.host = common.NewMenuButton(400, 60, turdgl.Vec{X: 400, Y: 400}, func() { SetScreen(MultiplayerHost, nil) })
s.host.SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Host game")
s.host = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 400, Y: 400},
func() { SetScreen(MultiplayerHost, nil) },
).SetLabelText("Host game")

s.back = common.NewMenuButton(400, 60, turdgl.Vec{X: 400, Y: 500}, func() { SetScreen(Title, nil) })
s.back.SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Back")
s.back = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 400, Y: 500},
func() { SetScreen(Title, nil) },
).SetLabelText("Back")

s.win.RegisterKeybind(turdgl.Key1, turdgl.KeyRelease, func() {
SetScreen(MultiplayerJoin, nil)
Expand Down Expand Up @@ -63,12 +72,12 @@ func (s *MultiplayerMenuScreen) Update() {

s.win.Draw(s.title)

for _, b := range []*common.MenuButton{
for _, b := range []*turdgl.Button{
s.join,
s.host,
s.back,
} {
s.win.Draw(b)
b.Update(s.win)
s.win.Draw(b)
}
}
33 changes: 19 additions & 14 deletions screen/title.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ type TitleScreen struct {
win *turdgl.Window

title *turdgl.Text
singleplayer *common.MenuButton
multiplayer *common.MenuButton
quit *common.MenuButton
singleplayer *turdgl.Button
multiplayer *turdgl.Button
quit *turdgl.Button
}

// NewTitle Screen constructs a new title screen for the given window.
Expand All @@ -28,15 +28,20 @@ func (s *TitleScreen) Enter(_ InitData) {
SetSize(40)

// Menu buttons
s.singleplayer = common.NewMenuButton(400, 60, turdgl.Vec{X: 400, Y: 300}, func() { SetScreen(Singleplayer, nil) })
s.singleplayer.SetLabelAlignment(turdgl.AlignCustom).
SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Singleplayer")
s.multiplayer = common.NewMenuButton(400, 60, turdgl.Vec{X: 400, Y: 400}, func() { SetScreen(MultiplayerMenu, nil) })
s.multiplayer.SetLabelAlignment(turdgl.AlignCustom).
SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Multiplayer")
s.quit = common.NewMenuButton(400, 60, turdgl.Vec{X: 400, Y: 500}, s.win.Quit)
s.quit.SetLabelAlignment(turdgl.AlignCustom).
SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Quit")
s.singleplayer = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 400, Y: 300},
func() { SetScreen(Singleplayer, nil) },
).SetLabelText("Singleplayer")
s.multiplayer = common.NewMenuButton(
400, 60,
turdgl.Vec{X: 400, Y: 400},
func() { SetScreen(MultiplayerMenu, nil) },
).SetLabelText("Multiplayer")
s.quit = common.NewMenuButton(
400, 60, turdgl.Vec{X: 400, Y: 500},
s.win.Quit,
).SetLabelText("Quit")

// Keybinds
s.win.RegisterKeybind(turdgl.Key1, turdgl.KeyRelease, func() {
Expand All @@ -62,12 +67,12 @@ func (s *TitleScreen) Update() {

s.win.Draw(s.title)

for _, b := range []*common.MenuButton{
for _, b := range []*turdgl.Button{
s.singleplayer,
s.multiplayer,
s.quit,
} {
s.win.Draw(b)
b.Update(s.win)
s.win.Draw(b)
}
}
4 changes: 2 additions & 2 deletions update_deps.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash
# Script for updating dependencies to the latest commit

cd ~/repo/turdgl
cd ~/r/turdgl
latest_commit=$(git rev-parse HEAD)
cd -
echo "Getting turdgl"
go get "github.com/z-riley/turdgl"@$latest_commit

cd ~/repo/turdserve
cd ~/r/turdserve
latest_commit=$(git rev-parse HEAD)
cd -
echo "Getting turdserve"
Expand Down

0 comments on commit 467617e

Please sign in to comment.