-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (146 loc) · 3.18 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"github.com/mitsutoshi/golife/golife"
"time"
"github.com/nsf/termbox-go"
)
const (
backgroundColor = termbox.ColorBlack
cellColor = termbox.ColorGreen
textColor = termbox.ColorWhite
cellWidth = 2
size = 40
marginTop = 2
marginLeft = 4
ptnTextStartY = 14
)
var (
drawInterval = 100 * time.Millisecond
cells = golife.NewCells(size)
pattern = map[rune][][]bool{
'A': golife.Glider,
'B': golife.GliderGun,
'C': golife.Galaxy,
}
pause = true
)
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
eventQueue := make(chan termbox.Event)
go func() {
for {
eventQueue <- termbox.PollEvent()
}
}()
termbox.Clear(backgroundColor, backgroundColor)
cells.Reset()
draw()
drawKeyOperationsText()
drawPatternText()
termbox.Flush()
loop:
for {
select {
case event := <-eventQueue:
if event.Type == termbox.EventKey {
if event.Key == termbox.KeyEsc {
// Exit
break loop
} else if event.Key == termbox.KeySpace {
// Pause cells
pause = !pause
} else if event.Key == termbox.KeyCtrlR {
// Reset cells
pause = true
cells.Reset()
draw()
} else if event.Key == termbox.KeyArrowLeft {
// Speed down
drawInterval += 50 * time.Millisecond
} else if event.Key == termbox.KeyArrowRight {
// Speed up
if drawInterval > 50*time.Millisecond {
drawInterval -= 50 * time.Millisecond
}
} else if _, ok := pattern[event.Ch]; ok {
// Plot cells pattern
pause = true
cells.Clear()
cells.UpdateCells(pattern[event.Ch])
draw()
}
}
default:
if !pause {
termbox.Clear(backgroundColor, backgroundColor)
cells.Next()
draw()
drawKeyOperationsText()
drawPatternText()
}
}
termbox.Flush()
time.Sleep(drawInterval)
}
}
func draw() {
for row := 0; row < len(cells.Cells); row++ {
for col := 0; col < len(cells.Cells[row]); col++ {
color := backgroundColor
if cells.Cells[row][col] {
color = cellColor
}
for i := 0; i < cellWidth; i++ {
termbox.SetCell(marginLeft+col*cellWidth+i, marginTop+row, ' ', color, color)
}
}
}
}
func drawKeyOperationsText() {
y := marginTop
drawText(size+8, y, "Control")
y++
y++
drawText(size+8, y, "Key Function ")
y++
drawText(size+8, y, "---------+----------------")
y++
drawText(size+8, y, "Space | Run or Pause ")
y++
drawText(size+8, y, "Ctrl + R | Reset cells ")
y++
drawText(size+8, y, "-> | Speed up ")
y++
drawText(size+8, y, "<- | Speed down ")
y++
drawText(size+8, y, "Escape | Exit ")
y++
y++
}
func drawPatternText() {
y := ptnTextStartY
drawText(size+8, y, "Pattern")
y++
y++
drawText(size+8, y, "Key | Name ")
y++
drawText(size+8, y, "----------+----------------")
y++
drawText(size+8, y, "Shift + A | Glider ")
y++
drawText(size+8, y, "Shift + B | Glider Gun ")
y++
drawText(size+8, y, "Shift + C | Galaxy ")
y++
y++
}
func drawText(x, y int, text string) {
for _, c := range text {
termbox.SetCell(size+4+x, y, c, textColor, backgroundColor)
x++
}
}