-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.go
72 lines (63 loc) · 1.65 KB
/
board.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//go:generate fyne bundle -o bundled-board.go board
package main
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"github.com/notnil/chess"
)
type boardLayout struct{}
func (b *boardLayout) Layout(cells []fyne.CanvasObject, s fyne.Size) {
smallEdge := s.Width
if s.Height < s.Width {
smallEdge = s.Height
}
leftInsert := (s.Width - smallEdge) / 2
cellEdge := smallEdge / 8
cellSize := fyne.NewSize(cellEdge, cellEdge)
i := 0
for y := 0; y < 8; y++ {
for x := 0; x < 8; x++ {
cells[i].Resize(cellSize)
cells[i].Move(fyne.NewPos(
leftInsert+(float32(x)*cellEdge), float32(y)*cellEdge))
i++
}
}
}
func (b *boardLayout) MinSize(_ []fyne.CanvasObject) fyne.Size {
edge := theme.IconInlineSize() * 8
return fyne.NewSize(edge, edge)
}
func createGrid(g *chess.Game) *fyne.Container {
var cells []fyne.CanvasObject
for y := 7; y >= 0; y-- {
for x := 0; x < 8; x++ {
bg := canvas.NewRectangle(color.NRGBA{0xF4, 0xE2, 0xB6, 0xFF})
effect := canvas.NewImageFromResource(resourceDarkWoodPng)
if x%2 == y%2 {
bg.FillColor = color.RGBA{0x73, 0x50, 0x32, 0xFF}
effect.Resource = resourceLightWoodPng
}
p := newPiece(g, chess.Square(x+y*8))
cells = append(cells, container.NewMax(bg, effect, p))
}
}
return container.New(&boardLayout{}, cells...)
}
func refreshGrid(grid *fyne.Container, board *chess.Board) {
y, x := 7, 0
for _, cell := range grid.Objects {
p := board.Piece(chess.Square(x + y*8))
img := cell.(*fyne.Container).Objects[2].(*piece)
img.Resource = resourceForPiece(p)
img.Refresh()
x++
if x == 8 {
x = 0
y--
}
}
}