-
Notifications
You must be signed in to change notification settings - Fork 0
/
typing_draw.go
135 lines (113 loc) · 3.01 KB
/
typing_draw.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
package main
import (
"github.com/nsf/termbox-go"
)
const baseColor = termbox.ColorDefault
const baseBackColor = termbox.ColorDefault
const cursorForeColor = termbox.ColorBlack
const cursorBackColor = termbox.ColorYellow
const untypedColor = termbox.ColorDefault
const typedColor = termbox.ColorRed
const errorCountColor = termbox.ColorRed
const lineLength = 78
/*
const progressOffset = 37
const speedOffset = 20
const errorOffset = 34
const accuracyOffset = 48
const timeOffset = 65
*/
type drawInfo struct {
siteName string
title []rune
desc []rune
trans []rune
totalStageNumber int
currentStageNumber int
errorCount int
elapsedTime int
speed int
titlePos int
descPos int
isGameOver bool
}
func drawHeader(info *drawInfo, lineLength int, lineOffset int, rowOffset int) int {
foreColor := baseColor
line := lineOffset
row := rowOffset
// draw site info (max 32 characters)
runes := []rune(info.siteName)
for i, c := range runes {
row = i + rowOffset
termbox.SetCell(row, line, c, foreColor, termbox.ColorDefault)
}
// draw progress (current / total)
// draw error count (or accuracy)
// draw speed
// draw elasped time
return line
}
func drawTitle(info *drawInfo, lineLength int, lineOffset int, rowOffset int) int {
foreColor := typedColor
backColor := baseBackColor
line := lineOffset
row := rowOffset
for i, ch := range info.title {
if i == info.titlePos {
foreColor = cursorForeColor
backColor = cursorBackColor
} else if i > info.titlePos {
foreColor = untypedColor
backColor = baseBackColor
}
line = (i / lineLength) + lineOffset
row = (i % lineLength) + rowOffset
termbox.SetCell(row, line, ch, foreColor, backColor)
}
return line
}
func drawDesc(info *drawInfo, lineLength int, lineOffset int, rowOffset int) int {
foreColor := typedColor
backColor := baseBackColor
line := lineOffset
row := rowOffset
for i, ch := range info.desc {
if i == info.descPos {
foreColor = cursorForeColor
backColor = cursorBackColor
} else if i > info.descPos {
foreColor = untypedColor
backColor = baseBackColor
}
line = (i / lineLength) + lineOffset
row = (i % lineLength) + rowOffset
termbox.SetCell(row, line, ch, foreColor, backColor)
}
return line
}
func drawTranslation(info *drawInfo, lineLength int, lineOffset int, rowOffset int) int {
foreColor := baseColor
line := lineOffset
row := rowOffset
for i, ch := range info.trans {
line = ((i*2) / lineLength) + lineOffset
row = ((i*2) % lineLength) + rowOffset
termbox.SetCell(row, line, ch, foreColor, termbox.ColorDefault)
}
return line
}
func drawLoop(drawInfoChan chan drawInfo) {
for {
currentLine := 0
info := <-drawInfoChan
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
if info.isGameOver {
break
}
currentLine = drawHeader(&info, lineLength, 0, 1)
currentLine = drawTitle(&info, lineLength, currentLine + 2, 1)
currentLine = drawDesc(&info, lineLength, currentLine + 2, 1)
currentLine = drawTranslation(&info, lineLength, currentLine + 2, 1)
termbox.Flush()
}
}