Skip to content

Commit

Permalink
Update turdgl
Browse files Browse the repository at this point in the history
  • Loading branch information
z-riley committed Nov 9, 2024
1 parent d21e9cc commit 8f677c8
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 44 deletions.
9 changes: 5 additions & 4 deletions common/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ type tile struct {

// newTile constructs a new tile with the correct style.
func newTile(sizePx float64, pos turdgl.Vec, val int, posIdx coord) *tile {
r := turdgl.NewCurvedRect(sizePx, sizePx, TileCornerRadius, pos)
tile := tile{
tb: turdgl.NewTextBox(
turdgl.NewCurvedRect(sizePx, sizePx, TileCornerRadius, pos),
r,
fmt.Sprint(val),
tileFont,
).
Expand All @@ -50,7 +51,7 @@ func newTile(sizePx float64, pos turdgl.Vec, val int, posIdx coord) *tile {
pos: posIdx,
}
tile.tb.Text.SetOffset(turdgl.Vec{X: 0, Y: 15})
tile.tb.Shape.SetStyle(turdgl.Style{Colour: tileColour(val)})
r.SetStyle(turdgl.Style{Colour: tileColour(val)})

return &tile
}
Expand Down Expand Up @@ -371,7 +372,7 @@ func (a *Arena) animateSpawn(animation spawnAnimation, errCh chan error, wg *syn
steps = 10
stepSize = growPx / steps
)
shape := newTile.tb.Shape
shape := newTile.tb.Shape.(*turdgl.CurvedRect)
originalPos := shape.GetPos() // position of shape before animation starts
for i := float64(0); i <= growPx; i += stepSize {
shape.SetPos(turdgl.Sub(originalPos, turdgl.Vec{X: i, Y: i}))
Expand Down Expand Up @@ -407,7 +408,7 @@ func (a *Arena) animateNewFromCombine(animation newFromCombineAnimation, errCh c

// Animate tile growing and shrinking back to normal size
const expandPx = 5
shape := newTile.tb.Shape
shape := newTile.tb.Shape.(*turdgl.CurvedRect)
originalPos := shape.GetPos() // position of shape before animation starts
for i := float64(1); i <= expandPx; i++ {
shape.SetPos(turdgl.Sub(originalPos, turdgl.Vec{X: i, Y: i}))
Expand Down
28 changes: 11 additions & 17 deletions common/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ var (

// 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.NewCurvedRect(width, height, 6, pos.Round(),
turdgl.WithStyle(ButtonStyleUnpressed)),
FontPathMedium,
).
r := turdgl.NewCurvedRect(width, height, 6, pos.Round()).SetStyle(ButtonStyleUnpressed)
b := turdgl.NewButton(r, FontPathMedium).
SetLabelText("SET ME").
SetLabelSize(36).
SetLabelColour(WhiteFontColour).
Expand All @@ -41,19 +38,19 @@ func NewMenuButton(width, height float64, pos turdgl.Vec, cb func()) *turdgl.But
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnHold},
func() {
b.Label.SetColour(WhiteFontColour)
b.Shape.SetStyle(ButtonStyleHovering)
r.SetStyle(ButtonStyleHovering)
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnRelease},
func() {
b.Label.SetColour(WhiteFontColour)
b.Shape.SetStyle(ButtonStyleUnpressed)
r.SetStyle(ButtonStyleUnpressed)
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.LeftClick, Behaviour: turdgl.OnPress},
func() {
b.Label.SetColour(GreyTextColour)
b.Shape.SetStyle(ButtonStylePressed)
r.SetStyle(ButtonStylePressed)
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.LeftClick, Behaviour: turdgl.OnRelease},
Expand All @@ -71,12 +68,9 @@ var gameButtonStyleHovering = turdgl.Style{

// NewGameButton constructs a new game button with sensible defaults.
func NewGameButton(width, height float64, pos turdgl.Vec, cb func()) *turdgl.Button {
b := turdgl.NewButton(
turdgl.NewCurvedRect(
width, height, TileCornerRadius, pos.Round(),
turdgl.WithStyle(turdgl.Style{Colour: buttonOrangeColour})),
FontPathBold,
).
r := turdgl.NewCurvedRect(width, height, TileCornerRadius, pos.Round()).
SetStyle(turdgl.Style{Colour: buttonOrangeColour})
b := turdgl.NewButton(r, FontPathBold).
SetLabelText("BUTTON").
SetLabelSize(14).
SetLabelColour(WhiteFontColour).
Expand All @@ -87,19 +81,19 @@ func NewGameButton(width, height float64, pos turdgl.Vec, cb func()) *turdgl.But
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnHold},
func() {
b.Label.SetColour(WhiteFontColour)
b.Shape.SetStyle(gameButtonStyleHovering)
r.SetStyle(gameButtonStyleHovering)
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnRelease},
func() {
b.Label.SetColour(WhiteFontColour)
b.Shape.SetStyle(ButtonStyleUnpressed)
r.SetStyle(ButtonStyleUnpressed)
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.LeftClick, Behaviour: turdgl.OnPress},
func() {
b.Label.SetColour(GreyTextColour)
b.Shape.SetStyle(ButtonStylePressed)
r.SetStyle(ButtonStylePressed)
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.LeftClick, Behaviour: turdgl.OnRelease},
Expand Down
14 changes: 5 additions & 9 deletions common/textbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewEntryBox(width, height float64, pos turdgl.Vec, txt string) *EntryBox {
}
)

bloom := turdgl.NewCurvedRect(width, height, 6, pos, turdgl.WithStyle(styleUnselected))
bloom := turdgl.NewCurvedRect(width, height, 6, pos).SetStyle(styleUnselected)

tb := NewTextBox(width, height, pos, txt)
tb.SetSelectedCB(func() {
Expand Down Expand Up @@ -71,11 +71,8 @@ func (e *EntryBox) SetModifiedCB(callback func()) *EntryBox {
}

func NewTextBox(width, height float64, pos turdgl.Vec, txt string) *turdgl.TextBox {
r := turdgl.NewCurvedRect(
width, height, 6, pos,
turdgl.WithStyle(turdgl.Style{Colour: color.RGBA{90, 65, 48, 255}, Thickness: 0}),
)
r.SetStyle(turdgl.Style{Colour: buttonColourUnpressed})
r := turdgl.NewCurvedRect(width, height, 6, pos).
SetStyle(turdgl.Style{Colour: buttonColourUnpressed})

tb := turdgl.NewTextBox(r, txt, FontPathMedium).
SetTextOffset(turdgl.Vec{X: 0, Y: 15}).
Expand Down Expand Up @@ -105,8 +102,7 @@ func NewScoreBox(width, height float64, pos turdgl.Vec, colour color.RGBA) *Scor
r := turdgl.NewCurvedRect(
width, height, 3,
pos,
turdgl.WithStyle(turdgl.Style{Colour: colour}),
)
).SetStyle(turdgl.Style{Colour: colour})

body := turdgl.NewTextBox(r, "", FontPathBold).
SetTextOffset(turdgl.Vec{X: 0, Y: 18}).
Expand Down Expand Up @@ -152,7 +148,7 @@ func NewLogoBox(size float64, pos turdgl.Vec) *turdgl.TextBox {
SetTextColour(WhiteFontColour)

logo.Text.SetAlignment(turdgl.AlignCustom).SetOffset(turdgl.Vec{Y: 15})
logo.Shape.SetStyle(turdgl.Style{Colour: Tile2048Colour})
logo.Shape.(*turdgl.CurvedRect).SetStyle(turdgl.Style{Colour: Tile2048Colour})

return logo
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/brunoga/deep v1.2.4
github.com/google/uuid v1.6.0
github.com/moby/moby v27.3.1+incompatible
github.com/z-riley/turdgl v0.0.0-20241103202229-f47b8b5d8d90
github.com/z-riley/turdserve v0.0.0-20241103172943-200ed8c9c094
github.com/z-riley/turdgl v0.0.0-20241109150126-d7ad58ec364c
github.com/z-riley/turdserve v0.0.0-20241109095301-66eab11d38af
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ 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-20241103202229-f47b8b5d8d90 h1:m8g+TQSdHx2iv8Eggix74ze3/YfsoYtHT0NdT1xycpY=
github.com/z-riley/turdgl v0.0.0-20241103202229-f47b8b5d8d90/go.mod h1:PUy0rnENvtQrZRghi9dYinsUUE7gvC9wOz8C/gcwJ8A=
github.com/z-riley/turdgl v0.0.0-20241109150126-d7ad58ec364c h1:8eEADvOzL+DlwZzHkCB/TYNKMMrrdK5/2+cZSPpTL04=
github.com/z-riley/turdgl v0.0.0-20241109150126-d7ad58ec364c/go.mod h1:PUy0rnENvtQrZRghi9dYinsUUE7gvC9wOz8C/gcwJ8A=
github.com/z-riley/turdserve v0.0.0-20241103172943-200ed8c9c094 h1:9ldkpBMZ5+BV8GOGH+4QhrBgevotAprFgMVCasm+td0=
github.com/z-riley/turdserve v0.0.0-20241103172943-200ed8c9c094/go.mod h1:D5VroZVlQvt0vUtZ2xHL8aQOlCumE/9LFEH1BBLE468=
github.com/z-riley/turdserve v0.0.0-20241109095301-66eab11d38af h1:X0XCfUgoGvZaOZtSRcB6UkuEkP4B6EzwvDUkkZkA7p0=
github.com/z-riley/turdserve v0.0.0-20241109095301-66eab11d38af/go.mod h1:D5VroZVlQvt0vUtZ2xHL8aQOlCumE/9LFEH1BBLE468=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ=
Expand Down
12 changes: 6 additions & 6 deletions screen/multiplayer_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func (s *MultiplayerMenuScreen) Enter(_ InitData) {
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnHold},
func() {
s.join.Label.SetColour(common.WhiteFontColour)
s.join.Shape.SetStyle(common.ButtonStyleHovering)
s.join.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleHovering)
s.hint.SetText("Join a LAN game")
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnRelease},
func() {
s.join.Label.SetColour(common.WhiteFontColour)
s.join.Shape.SetStyle(common.ButtonStyleUnpressed)
s.join.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleUnpressed)
s.hint.SetText("")
},
)
Expand All @@ -85,14 +85,14 @@ func (s *MultiplayerMenuScreen) Enter(_ InitData) {
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnHold},
func() {
s.host.Label.SetColour(common.WhiteFontColour)
s.host.Shape.SetStyle(common.ButtonStyleHovering)
s.host.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleHovering)
s.hint.SetText("Host a LAN game")
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnRelease},
func() {
s.host.Label.SetColour(common.WhiteFontColour)
s.host.Shape.SetStyle(common.ButtonStyleUnpressed)
s.host.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleUnpressed)
s.hint.SetText("")
},
)
Expand All @@ -109,14 +109,14 @@ func (s *MultiplayerMenuScreen) Enter(_ InitData) {
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnHold},
func() {
s.back.Label.SetColour(common.WhiteFontColour)
s.back.Shape.SetStyle(common.ButtonStyleHovering)
s.back.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleHovering)
s.hint.SetText("Go back to main menu")
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnRelease},
func() {
s.back.Label.SetColour(common.WhiteFontColour)
s.back.Shape.SetStyle(common.ButtonStyleUnpressed)
s.back.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleUnpressed)
s.hint.SetText("")
},
)
Expand Down
7 changes: 7 additions & 0 deletions screen/screens.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package screen

import (
"fmt"
"sync"
"time"

"github.com/z-riley/go-2048-battle/config"
"github.com/z-riley/turdgl"
)

Expand Down Expand Up @@ -81,7 +84,11 @@ func Update() {
CurrentScreen().Enter(screen.data)

default:
now := time.Now()
CurrentScreen().Update()
if config.Debug {
fmt.Println("Updated in", time.Since(now))
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions screen/title.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func (s *TitleScreen) Enter(_ InitData) {
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnHold},
func() {
s.singleplayer.Label.SetColour(common.WhiteFontColour)
s.singleplayer.Shape.SetStyle(common.ButtonStyleHovering)
s.singleplayer.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleHovering)
s.hint.SetText("Play alone")
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnRelease},
func() {
s.singleplayer.Label.SetColour(common.WhiteFontColour)
s.singleplayer.Shape.SetStyle(common.ButtonStyleUnpressed)
s.singleplayer.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleUnpressed)
s.hint.SetText("")
},
)
Expand All @@ -91,14 +91,14 @@ func (s *TitleScreen) Enter(_ InitData) {
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnHold},
func() {
s.multiplayer.Label.SetColour(common.WhiteFontColour)
s.multiplayer.Shape.SetStyle(common.ButtonStyleHovering)
s.multiplayer.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleHovering)
s.hint.SetText("Play against a friend")
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnRelease},
func() {
s.multiplayer.Label.SetColour(common.WhiteFontColour)
s.multiplayer.Shape.SetStyle(common.ButtonStyleUnpressed)
s.multiplayer.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleUnpressed)
s.hint.SetText("")
},
)
Expand All @@ -117,14 +117,14 @@ func (s *TitleScreen) Enter(_ InitData) {
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnHold},
func() {
s.quit.Label.SetColour(common.WhiteFontColour)
s.quit.Shape.SetStyle(common.ButtonStyleHovering)
s.quit.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleHovering)
s.hint.SetText("Exit to desktop")
},
).SetCallback(
turdgl.ButtonTrigger{State: turdgl.NoClick, Behaviour: turdgl.OnRelease},
func() {
s.quit.Label.SetColour(common.WhiteFontColour)
s.quit.Shape.SetStyle(common.ButtonStyleUnpressed)
s.quit.Shape.(*turdgl.CurvedRect).SetStyle(common.ButtonStyleUnpressed)
s.hint.SetText("")
},
)
Expand Down

0 comments on commit 8f677c8

Please sign in to comment.