-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.go
155 lines (133 loc) · 3.09 KB
/
display.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
package main
import (
"fmt"
termbox "github.com/nsf/termbox-go"
)
const (
BORDER_H = 2
BORDER_V = 1
)
func DrawWindow(title string, footer string, lines []string, colors []termbox.Attribute) {
termw, termh := termbox.Size()
contw, conth := termw-2*BORDER_H, termh-2*BORDER_V
num_lines := len(lines)
max_len := 0
for _, line := range lines {
max_len = Max(max_len, len(line))
}
cols := 1
rows := num_lines
for (cols+1)*max_len+cols < contw && rows > conth {
cols++
rows = (num_lines + cols - 1) / cols
}
contw = cols*max_len + (cols - 1)
conth = rows
if len(title)+4 > contw || len(footer)+4 > contw {
contw = Max(len(title), len(footer)) + 4
}
left := Max((termw-contw-2*BORDER_H)/2, 0)
right := left + contw + BORDER_H + 1
top := Max((termh-conth-2*BORDER_V)/2, 0)
bottom := top + conth + BORDER_V
// draw border
for x := left; x <= right; x += 1 {
print(x, top, "-")
print(x, bottom, "-")
}
for y := top + 1; y <= bottom-1; y += 1 {
print(left, y, "|")
print(right, y, "|")
// clear content area
for x := left + 1; x <= right-1; x += 1 {
print(x, y, " ")
}
}
if title != "" {
print(left+contw/2-len(title)/2, top, " ", title, " ")
}
if footer != "" {
print(left+contw/2-len(footer)/2, bottom, " ", footer, " ")
}
left += BORDER_H
top += BORDER_V
for pos, line := range lines {
color := termbox.ColorDefault
if pos < len(colors) {
color = colors[pos]
}
printC(
left+(pos/rows)*(max_len+1),
top+(pos%rows),
color, Pad(max_len, line, ""))
}
}
func DrawPlanes(game *GameState) bool {
lines := make([]string, 0, len(game.planes))
colors := make([]termbox.Attribute, 0, len(game.planes))
for _, p := range game.planes {
if !game.rules.show_pending_planes && p.state == StatePending {
continue
}
lines = append(lines, p.String())
switch {
case p.IsActive():
colors = append(colors, termbox.ColorDefault)
case p.IsDone():
colors = append(colors, termbox.ColorGreen)
default:
colors = append(colors, termbox.ColorBlue)
}
}
if len(lines) == 0 {
return false
}
DrawWindow("Planes", "", lines, colors)
return true
}
func DrawHelp(screen uint) {
screen = screen % uint(len(HELP))
lines := SplitLines(HELP[screen])
DrawWindow(
fmt.Sprintf("Help (page %d of %d)", screen+1, len(HELP)),
"<- / -> / Space", lines, nil)
}
func DialogKeys(ev termbox.Event, visible *bool, screen *uint) {
switch ev.Ch {
case 0:
switch ev.Key {
case termbox.KeyEsc,
termbox.KeyBackspace, termbox.KeyBackspace2,
termbox.KeyTab:
*visible = false
case termbox.KeySpace,
termbox.KeyEnter:
if screen != nil {
*screen++
}
case termbox.KeyArrowRight:
if screen != nil {
*screen++
}
case termbox.KeyArrowLeft:
if screen != nil {
*screen--
}
}
case 'x', 'X', 'q', 'Q':
*visible = false
}
}
func print(x, y int, strings ...string) int {
return printC(x, y, termbox.ColorDefault, strings...)
}
func printC(x, y int, fg termbox.Attribute, strings ...string) int {
for _, s := range strings {
for _, r := range s {
termbox.SetCell(x, y, r,
fg, termbox.ColorDefault)
x += 1
}
}
return x
}