commit 988286bee002dc6ff74378e57a626298bd8ac5ad
Author: Thomas Vigouroux <me@vigoux.eu>
Date: Thu, 25 Apr 2024 16:23:02 +0200
Initial commit
Diffstat:
A | go.mod | | | 19 | +++++++++++++++++++ |
A | go.sum | | | 49 | +++++++++++++++++++++++++++++++++++++++++++++++++ |
A | main.go | | | 155 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 223 insertions(+), 0 deletions(-)
diff --git a/go.mod b/go.mod
@@ -0,0 +1,19 @@
+module igoui
+
+go 1.22.0
+
+require (
+ gioui.org v0.6.0
+ github.com/rooklift/sgf v0.0.0-20230511200928-fc09b7bd0bfe
+)
+
+require (
+ gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2 // indirect
+ gioui.org/shader v1.0.8 // indirect
+ github.com/go-text/typesetting v0.1.1 // indirect
+ golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 // indirect
+ golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91 // indirect
+ golang.org/x/image v0.5.0 // indirect
+ golang.org/x/sys v0.5.0 // indirect
+ golang.org/x/text v0.9.0 // indirect
+)
diff --git a/go.sum b/go.sum
@@ -0,0 +1,49 @@
+eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d h1:ARo7NCVvN2NdhLlJE9xAbKweuI9L6UgfTbYb0YwPacY=
+eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d/go.mod h1:OYVuxibdk9OSLX8vAqydtRPP87PyTFcT9uH3MlEGBQA=
+gioui.org v0.6.0 h1:ZSXO/AbpFZJ2L9NU69uFQfDI3BKIH+YEJElrn0B+aZI=
+gioui.org v0.6.0/go.mod h1:eUvGo6FAzA7jUqeSu5a+M1W03yc9r1nanIBS8A5+Nng=
+gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ=
+gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2 h1:AGDDxsJE1RpcXTAxPG2B4jrwVUJGFDjINIPi1jtO6pc=
+gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ=
+gioui.org/shader v1.0.8 h1:6ks0o/A+b0ne7RzEqRZK5f4Gboz2CfG+mVliciy6+qA=
+gioui.org/shader v1.0.8/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM=
+github.com/go-text/typesetting v0.1.1 h1:bGAesCuo85nXnEN5LmFMVGAGpGkCPtHrZLi//qD7EJo=
+github.com/go-text/typesetting v0.1.1/go.mod h1:d22AnmeKq/on0HNv73UFriMKc4Ez6EqZAofLhAzpSzI=
+github.com/go-text/typesetting-utils v0.0.0-20231211103740-d9332ae51f04 h1:zBx+p/W2aQYtNuyZNcTfinWvXBQwYtDfme051PR/lAY=
+github.com/go-text/typesetting-utils v0.0.0-20231211103740-d9332ae51f04/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
+github.com/rooklift/sgf v0.0.0-20230511200928-fc09b7bd0bfe h1:d9oKy6cd0TK4pmkkd0roK9latPGsPJVDiT7wi/oTyk8=
+github.com/rooklift/sgf v0.0.0-20230511200928-fc09b7bd0bfe/go.mod h1:NxkdyQii9LvyameqofPnGDNj/BNvaMUUCoxvreS1YKc=
+github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
+golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 h1:sBdrWpxhGDdTAYNqbgBLAR+ULAPPhfgncLr1X0lyWtg=
+golang.org/x/exp v0.0.0-20221012211006-4de253d81b95/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
+golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91 h1:ryT6Nf0R83ZgD8WnFFdfI8wCeyqgdXWN4+CkFVNPAT0=
+golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91/go.mod h1:VjAR7z0ngyATZTELrBSkxOOHhhlnVUxDye4mcjx5h/8=
+golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
+golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
+golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
+golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
+golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
+golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
diff --git a/main.go b/main.go
@@ -0,0 +1,155 @@
+package main
+
+import (
+ "image"
+ "image/color"
+ // "image/png"
+ "log"
+ "os"
+
+ // UI generation
+ "gioui.org/app"
+ "gioui.org/f32"
+ "gioui.org/io/event"
+ "gioui.org/io/pointer"
+ "gioui.org/layout"
+ "gioui.org/op"
+ "gioui.org/op/clip"
+ "gioui.org/op/paint"
+ "gioui.org/unit"
+ // "gioui.org/text"
+ // "gioui.org/widget/material"
+
+ // SGF manipulation
+ "github.com/rooklift/sgf"
+)
+
+type Goban struct {
+ Board *sgf.Board
+}
+
+func (g *Goban) Layout(gtx layout.Context) layout.Dimensions {
+ size := min(gtx.Constraints.Max.X, gtx.Constraints.Max.Y)
+ black := color.NRGBA{A: 0xFF}
+ white := color.NRGBA{A: 0xFF, R: 0xFF, G: 0xFF, B: 0xFF}
+ offset := float32(size) / float32(g.Board.Size)
+
+ // Handle inputs within the goban area
+ {
+ defer clip.Rect(image.Rect(0, 0, size, size)).Push(gtx.Ops).Pop()
+
+ event.Op(gtx.Ops, g)
+
+ for {
+ ev, ok := gtx.Event(pointer.Filter{
+ Target: g,
+ Kinds: pointer.Press,
+ })
+ if !ok {
+ break
+ }
+
+ e, ok := ev.(pointer.Event)
+ if !ok {
+ continue
+ }
+
+ switch e.Kind {
+ case pointer.Press:
+ xpos := int(e.Position.X / offset)
+ ypos := int(e.Position.Y / offset)
+ g.Board.Play(sgf.Point(xpos, ypos))
+ }
+ }
+ }
+
+ // Draw the frame
+ var path clip.Path
+ path.Begin(gtx.Ops)
+ foffset := offset / 2
+ for i := 0; i < g.Board.Size; i++ {
+ ioffset := float32(offset)*float32(i) + foffset
+ // Line from left to right
+ path.MoveTo(f32.Pt(ioffset, foffset))
+ path.LineTo(f32.Pt(ioffset, float32(size)-foffset))
+
+ // Line from top to bottom
+ path.MoveTo(f32.Pt(foffset, ioffset))
+ path.LineTo(f32.Pt(float32(size)-foffset, ioffset))
+ }
+ path.Close()
+ paint.FillShape(gtx.Ops, black, clip.Stroke{
+ Path: path.End(),
+ Width: 4,
+ }.Op())
+
+ // Now draw the stones
+ for x := 0; x < g.Board.Size; x++ {
+ for y := 0; y < g.Board.Size; y++ {
+ if g.Board.State[x][y] != sgf.EMPTY {
+ scolor := g.Board.State[x][y]
+ fx := float32(x)
+ fy := float32(y)
+ rect := image.Rect(int(fx*offset), int(fy*offset), int((fx+1)*offset), int((fy+1)*offset))
+ elipse := clip.Ellipse(rect)
+
+ var strokecolor color.NRGBA
+ var fillcolor color.NRGBA
+
+ if scolor == sgf.BLACK {
+ strokecolor = white
+ fillcolor = black
+ } else {
+ strokecolor = black
+ fillcolor = white
+ }
+
+ // Now that we have the rectangle, put the stone
+ paint.FillShape(gtx.Ops, fillcolor, elipse.Op(gtx.Ops))
+ paint.FillShape(gtx.Ops, strokecolor, clip.Stroke{
+ Path: elipse.Path(gtx.Ops),
+ Width: 2,
+ }.Op())
+ }
+ }
+ }
+ // paint.PaintOp{}.Add(gtx.Ops)
+ return layout.Dimensions{Size: gtx.Constraints.Max}
+}
+
+func run(window *app.Window) error {
+ // theme := material.NewTheme()
+ var ops op.Ops
+
+ goban := Goban{
+ Board: sgf.NewBoard(19),
+ }
+
+ for {
+ switch e := window.Event().(type) {
+ case app.DestroyEvent:
+ return e.Err
+ case app.FrameEvent:
+ // This graphics context is used for managing the rendering state.
+ gtx := app.NewContext(&ops, e)
+
+ // goban.Layout(gtx)
+ layout.UniformInset(unit.Dp(30)).Layout(gtx, goban.Layout)
+
+ // Pass the drawing operations to the GPU.
+ e.Frame(gtx.Ops)
+ }
+ }
+}
+
+func main() {
+ go func() {
+ window := new(app.Window)
+ err := run(window)
+ if err != nil {
+ log.Fatal(err)
+ }
+ os.Exit(0)
+ }()
+ app.Main()
+}