-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_window.go
101 lines (88 loc) · 2.23 KB
/
game_window.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package app
import (
"fmt"
"fyne.io/fyne"
"fyne.io/fyne/dialog"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
)
const (
MaxRows = 5
MaxCell int = 15
ALIVE = ""
DEAD = ""
)
type triangleLayout struct {
parent fyne.CanvasObject
}
func (t *triangleLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
//fmt.Println("minSize:", t.parent.Canvas().Size())
//all button of same size
size := objects[0].MinSize()
return fyne.NewSize(size.Width*MaxRows, size.Height*MaxRows)
}
func (t *triangleLayout) Layout(objects []fyne.CanvasObject, containerSize fyne.Size) {
parentSize := t.parent.Size()
totalSize := t.MinSize(objects)
leftX := parentSize.Width - t.MinSize(objects).Width
topY := parentSize.Height - t.MinSize(objects).Height
centerX := t.MinSize(objects).Width/2 + leftX
size := objects[0].MinSize()
c := 0
fmt.Println(parentSize, totalSize)
for i := 0; i < MaxRows; i++ {
x := centerX - ((i+1)*size.Width)/2
for j := 0; j <= i; j++ {
pos := fyne.NewPos(x+j*size.Width, i*size.Height+topY)
objects[c].Resize(size)
objects[c].Move(pos)
c++
}
}
}
type gameWindow struct {
cells map[int]*widget.Button
window fyne.Window
app fyne.App
}
func newGameWindow(app fyne.App) *gameWindow {
return &gameWindow{
app: app,
window: app.NewWindow("Play"),
cells: make(map[int]*widget.Button),
}
}
func (gw *gameWindow) CreateAndShow(onClose func()) {
gw.window.SetOnClosed(onClose)
click := func(i int) func() {
return func() {
if gw.cells[i].Text == ALIVE {
gw.cells[i].SetText(DEAD)
gw.cells[i].SetIcon(nil)
} else {
dialog.ShowInformation("Error", "Already Dead !!", gw.window)
}
}
}
var cells []fyne.CanvasObject
for i := 0; i < MaxCell; i++ {
btn := widget.NewButton(ALIVE, click(i))
btn.SetIcon(theme.FyneLogo())
gw.cells[i] = btn
cells = append(cells, btn)
}
gw.window.Resize(fyne.Size{150, 200})
container := fyne.NewContainer()
container.AddObject(fyne.NewContainerWithLayout(&triangleLayout{parent: container}, cells...))
gw.window.SetContent(widget.NewVBox(
fyne.NewContainerWithLayout(
layout.NewGridLayout(1),
container,
widget.NewButton("Logout", func() {
gw.window.Close()
},
),
)))
gw.window.Show()
}