/
frame.go
224 lines (204 loc) · 4.92 KB
/
frame.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package cgame
import (
"bufio"
"errors"
"fmt"
"io"
"math"
"os"
"strings"
"time"
"github.com/jf-tech/console/cterm"
"github.com/jf-tech/console/cwin"
"github.com/jf-tech/go-corelib/ios"
"github.com/jf-tech/go-corelib/maths"
)
type Cell struct {
X, Y int
Chx cwin.Chx
}
type Frame []Cell
func CopyFrame(src Frame) Frame {
dst := make(Frame, len(src))
copy(dst, src)
return dst
}
func SetAttrInFrame(f Frame, attr cwin.Attr) Frame {
for i := 0; i < len(f); i++ {
f[i].Chx.Attr = attr
}
return f
}
func FrameFromStringEx(s string, attr cwin.Attr, spaceEqualsTransparency bool) Frame {
s = strings.Trim(s, "\n")
rect := cwin.TextDimension(s)
var f Frame
rs := []rune(s)
rsLen := len(rs)
for i, x, y := 0, 0, 0; i < rsLen; i++ {
if rs[i] == '\n' {
if !spaceEqualsTransparency {
for ; x < rect.W; x++ {
f = append(f, Cell{X: x, Y: y, Chx: cwin.Chx{Ch: ' ', Attr: attr}})
}
}
x = 0
y++
continue
}
if rs[i] != ' ' || !spaceEqualsTransparency {
f = append(f, Cell{X: x, Y: y, Chx: cwin.Chx{Ch: rs[i], Attr: attr}})
}
x++
}
return f
}
func FrameFromString(s string, attr cwin.Attr) Frame {
return FrameFromStringEx(s, attr, true)
}
var (
colorMap = map[rune]cterm.Attribute{
'K': cterm.ColorBlack,
'R': cterm.ColorRed,
'G': cterm.ColorGreen,
'Y': cterm.ColorYellow,
'B': cterm.ColorBlue,
'M': cterm.ColorMagenta,
'C': cterm.ColorCyan,
'W': cterm.ColorWhite,
'A': cterm.ColorDarkGray,
'r': cterm.ColorLightRed,
'g': cterm.ColorLightGreen,
'y': cterm.ColorLightYellow,
'b': cterm.ColorLightBlue,
'm': cterm.ColorLightMagenta,
'c': cterm.ColorLightCyan,
'a': cterm.ColorLightGray,
}
)
func MultiColorFrameFromString(s string, fg string, bg ...string) Frame {
if len(bg) > 1 {
panic("background color frame must be 1")
}
f := FrameFromString(s, cwin.TransparentChx().Attr)
fgF := FrameFromString(fg, cwin.TransparentChx().Attr)
if len(f) != len(fgF) {
panic(fmt.Sprintf(
"frame and its color frame not matching: len(f)=%d, len(fgF)=%d", len(f), len(fgF)))
}
for i := 0; i < len(f); i++ {
if color, ok := colorMap[fgF[i].Chx.Ch]; ok {
f[i].Chx.Attr.Fg = color
}
}
return f
}
func MultiColorFrameFromFile(filepath string, n int) (Frame, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
br := bufio.NewReader(f)
var lines []string
for {
line, err := ios.ReadLine(br)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
lines = append(lines, line)
}
if len(lines) < 2*n {
return nil, errors.New("not enough lines in file")
}
s := strings.Join(lines[:n], "\n")
fg := strings.Join(lines[n:2*n], "\n")
var bg []string
if len(lines) >= 3*n {
bg = append(bg, strings.Join(lines[2*n:3*n], "\n"))
}
return MultiColorFrameFromString(s, fg, bg...), nil
}
func FrameRect(f Frame) cwin.Rect {
maxX, maxY := math.MinInt32, math.MinInt32
for i := 0; i < len(f); i++ {
maxX = maths.MaxInt(maxX, f[i].X)
maxY = maths.MaxInt(maxY, f[i].Y)
}
return cwin.Rect{X: 0, Y: 0, W: maxX + 1, H: maxY + 1}
}
func FrameFromWin(w cwin.Win) Frame {
var f Frame
for y := 0; y < w.ClientRect().H; y++ {
for x := 0; x < w.ClientRect().W; x++ {
chx := w.GetClient(x, y)
if chx != cwin.TransparentChx() {
f = append(f, Cell{X: x, Y: y, Chx: chx})
}
}
}
return f
}
func FrameToWin(f Frame, w cwin.Win) {
w.FillClient(w.ClientRect().ToOrigin(), cwin.TransparentChx())
for i := 0; i < len(f); i++ {
w.PutClient(f[i].X, f[i].Y, f[i].Chx)
}
}
type Frames []Frame
func FramesFromString(ss []string, attr cwin.Attr) (Frames, cwin.Rect) {
// Unlike a single frame func FrameFromString where the frame hosting rect
// can be implied from the frame content, multiple frames can have different
// sizes with which we need to do some normalization:
// - compute the bounding rect large enough for all frames.
// - use that rect as a container, to "put" all frames in the center of it -
// which means adjusting each frame cell's coordinates.
var fs Frames
var maxR cwin.Rect
for _, s := range ss {
f := FrameFromString(s, attr)
fsR := FrameRect(f)
maxR.W = maths.MaxInt(maxR.W, fsR.W)
maxR.H = maths.MaxInt(maxR.H, fsR.H)
fs = append(fs, f)
}
for i := 0; i < len(fs); i++ {
fsR := FrameRect(fs[i])
dx := (maxR.W - fsR.W) / 2
dy := (maxR.H - fsR.H) / 2
for j := 0; j < len(fs[i]); j++ {
fs[i][j].X += dx
fs[i][j].Y += dy
}
}
return fs, maxR
}
type FrameProvider interface {
Next() (Frame, time.Duration, bool)
}
type simpleFrameProvider struct {
frames Frames
t time.Duration
loop bool
idx int
}
func (sfp *simpleFrameProvider) Next() (Frame, time.Duration, bool) {
if sfp.idx >= len(sfp.frames) {
if !sfp.loop {
return nil, 0, false
}
sfp.idx = 0
}
f := sfp.frames[sfp.idx]
sfp.idx++
return f, sfp.t, true
}
func NewSimpleFrameProvider(frames Frames, t time.Duration, loop bool) *simpleFrameProvider {
return &simpleFrameProvider{
frames: frames,
t: t,
loop: loop,
}
}