From 467617e8b8ef749428a379b62d0e08330e935a7d Mon Sep 17 00:00:00 2001 From: Zac Date: Mon, 21 Oct 2024 08:43:40 +0100 Subject: [PATCH] Use updated button API for menus --- common/button.go | 70 ++++++++++++++++++++------------------ go.mod | 2 +- go.sum | 8 +++++ screen/multiplayer_host.go | 34 +++++++++--------- screen/multiplayer_join.go | 46 +++++++++++++------------ screen/multiplayer_menu.go | 31 +++++++++++------ screen/title.go | 33 ++++++++++-------- update_deps.sh | 4 +-- 8 files changed, 129 insertions(+), 99 deletions(-) diff --git a/common/button.go b/common/button.go index 315585f..dac6051 100644 --- a/common/button.go +++ b/common/button.go @@ -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) @@ -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 } diff --git a/go.mod b/go.mod index ad33315..4bd2bad 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 552a8f8..c32e662 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/screen/multiplayer_host.go b/screen/multiplayer_host.go index 813ae9f..9b690e1 100644 --- a/screen/multiplayer_host.go +++ b/screen/multiplayer_host.go @@ -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 @@ -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 { @@ -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, diff --git a/screen/multiplayer_join.go b/screen/multiplayer_join.go index 0d2132e..6a17a30 100644 --- a/screen/multiplayer_join.go +++ b/screen/multiplayer_join.go @@ -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 @@ -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) @@ -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 { @@ -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() @@ -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, diff --git a/screen/multiplayer_menu.go b/screen/multiplayer_menu.go index 88ce045..787e6d1 100644 --- a/screen/multiplayer_menu.go +++ b/screen/multiplayer_menu.go @@ -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. @@ -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) @@ -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) } } diff --git a/screen/title.go b/screen/title.go index bcdf3b1..71238fe 100644 --- a/screen/title.go +++ b/screen/title.go @@ -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. @@ -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() { @@ -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) } } diff --git a/update_deps.sh b/update_deps.sh index e9af5ea..fb50b6c 100755 --- a/update_deps.sh +++ b/update_deps.sh @@ -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"