Skip to content

Instantly share code, notes, and snippets.

@loloof64
Last active September 2, 2019 15:09
Show Gist options
  • Save loloof64/c97588a6848d66ada3d4641879717625 to your computer and use it in GitHub Desktop.
Save loloof64/c97588a6848d66ada3d4641879717625 to your computer and use it in GitHub Desktop.
Trying to create a chessboard in Fyne
package main
import (
"image/color"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/layout"
"fyne.io/fyne/widget"
)
type ChessBoard struct {
size fyne.Size
position fyne.Position
hidden bool
}
type chessboardRenderer struct {
grid *fyne.Container
board *ChessBoard
}
func (c *ChessBoard) CreateRenderer() fyne.WidgetRenderer {
renderer := &chessboardRenderer{board: c}
var cells = []fyne.CanvasObject{}
for j := 0; j < 8; j++ {
for i := 0; i < 8; i++ {
blackCell := (i+j)%2 > 0
cells = append(cells, &ChessCell{isBlack: blackCell})
}
}
renderer.grid = fyne.NewContainerWithLayout(layout.NewGridLayout(8), cells...)
return renderer
}
func (c *chessboardRenderer) MinSize() fyne.Size {
return fyne.Size{8 * cellSize, 8 * cellSize}
}
func (c *chessboardRenderer) Layout(size fyne.Size) {
c.grid.Layout.Layout(c.grid.Objects, size)
}
func (c *chessboardRenderer) ApplyTheme() {
}
func (c *chessboardRenderer) BackgroundColor() color.Color {
return color.RGBA{234, 135, 239, 1}
}
func (c *chessboardRenderer) Refresh() {
canvas.Refresh(c.grid)
}
func (c *chessboardRenderer) Objects() []fyne.CanvasObject {
return c.grid.Objects
}
func (c *chessboardRenderer) Destroy() {
}
func (c *ChessBoard) Size() fyne.Size {
return c.size
}
func (c *ChessBoard) Resize(size fyne.Size) {
c.size = size
widget.Renderer(c).Layout(size)
}
func (c *ChessBoard) Position() fyne.Position {
return c.position
}
func (c *ChessBoard) Move(pos fyne.Position) {
c.position = pos
widget.Renderer(c).Layout(c.size)
}
func (c *ChessBoard) MinSize() fyne.Size {
return widget.Renderer(c).MinSize()
}
func (c *ChessBoard) Visible() bool {
return !c.hidden
}
func (c *ChessBoard) Show() {
c.hidden = false
}
func (c *ChessBoard) Hide() {
c.hidden = true
}
package main
import (
"image/color"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/widget"
)
const cellSize = 80
type ChessCell struct {
size fyne.Size
position fyne.Position
hidden bool
isBlack bool
objects []fyne.CanvasObject
}
func (c *ChessCell) CreateRenderer() fyne.WidgetRenderer {
renderer := &chesscellRenderer{cellLogic: c}
var color color.Color
if c.isBlack {
color = blackCellColor
} else {
color = whiteCellColor
}
graphicCell := canvas.NewRectangle(color)
graphicCell.Resize(fyne.NewSize(cellSize, cellSize))
renderer.graphic = graphicCell
c.objects = append(c.objects, graphicCell)
return renderer
}
var whiteCellColor = color.RGBA{255, 206, 158, 1}
var blackCellColor = color.RGBA{209, 139, 71, 1}
type chesscellRenderer struct {
graphic *canvas.Rectangle
cellLogic *ChessCell
}
func (c *chesscellRenderer) MinSize() fyne.Size {
return fyne.Size{cellSize, cellSize}
}
func (c *chesscellRenderer) Layout(size fyne.Size) {
}
func (c *chesscellRenderer) ApplyTheme() {
}
func (c *chesscellRenderer) BackgroundColor() color.Color {
return c.graphic.FillColor
}
func (c *chesscellRenderer) Refresh() {
canvas.Refresh(c.graphic)
}
func (c *chesscellRenderer) Objects() []fyne.CanvasObject {
return []fyne.CanvasObject{}
}
func (c *chesscellRenderer) Destroy() {
}
func (c *ChessCell) Size() fyne.Size {
return c.size
}
func (c *ChessCell) Resize(size fyne.Size) {
c.size = size
widget.Renderer(c).Layout(size)
}
func (c *ChessCell) Position() fyne.Position {
return c.position
}
func (c *ChessCell) Move(pos fyne.Position) {
c.position = pos
widget.Renderer(c).Layout(c.size)
}
func (c *ChessCell) MinSize() fyne.Size {
return widget.Renderer(c).MinSize()
}
func (c *ChessCell) Visible() bool {
return !c.hidden
}
func (c *ChessCell) Show() {
c.hidden = false
}
func (c *ChessCell) Hide() {
c.hidden = true
}
package main
import (
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/layout"
"fyne.io/fyne/widget"
)
func main() {
app := app.New()
window := app.NewWindow("Basic chess endgames")
button := widget.NewButton("Quit", func() {
app.Quit()
})
window.SetContent(fyne.NewContainerWithLayout(
layout.NewBorderLayout(nil,
button,
nil,
nil),
&ChessBoard{},
button))
window.ShowAndRun()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment