forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
painter.go
194 lines (163 loc) · 4.13 KB
/
painter.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
package tui
import (
"image"
)
// Surface defines a surface that can be painted on.
type Surface interface {
SetCell(x, y int, ch rune, s Style)
SetCursor(x, y int)
HideCursor()
Begin()
End()
Size() image.Point
}
// Painter provides operations to paint on a surface.
type Painter struct {
theme *Theme
// Surface to paint on.
surface Surface
// Current brush.
style Style
// Transform stack
transforms []image.Point
mask image.Rectangle
}
// NewPainter returns a new instance of Painter.
func NewPainter(s Surface, p *Theme) *Painter {
return &Painter{
theme: p,
surface: s,
style: p.Style("normal"),
mask: image.Rectangle{
Min: image.ZP,
Max: s.Size(),
},
}
}
// Translate pushes a new translation transform to the stack.
func (p *Painter) Translate(x, y int) {
p.transforms = append(p.transforms, image.Point{x, y})
}
// Restore pops the latest transform from the stack.
func (p *Painter) Restore() {
if len(p.transforms) > 0 {
p.transforms = p.transforms[:len(p.transforms)-1]
}
}
// Begin prepares the surface for painting.
func (p *Painter) Begin() {
p.surface.Begin()
}
// End finalizes any painting that has been made.
func (p *Painter) End() {
p.surface.End()
}
// Repaint clears the surface, draws the scene and flushes it.
func (p *Painter) Repaint(w Widget) {
p.mask = image.Rectangle{
Min: image.ZP,
Max: p.surface.Size(),
}
p.surface.HideCursor()
p.Begin()
w.Resize(p.surface.Size())
w.Draw(p)
p.End()
}
// DrawCursor draws the cursor at the given position.
func (p *Painter) DrawCursor(x, y int) {
wp := p.mapLocalToWorld(image.Point{x, y})
p.surface.SetCursor(wp.X, wp.Y)
}
// DrawRune paints a rune at the given coordinate.
func (p *Painter) DrawRune(x, y int, r rune) {
wp := p.mapLocalToWorld(image.Point{x, y})
if (p.mask.Min.X <= wp.X) && (wp.X < p.mask.Max.X) && (p.mask.Min.Y <= wp.Y) && (wp.Y < p.mask.Max.Y) {
p.surface.SetCell(wp.X, wp.Y, r, p.style)
}
}
// DrawText paints a string starting at the given coordinate.
func (p *Painter) DrawText(x, y int, text string) {
for _, r := range text {
p.DrawRune(x, y, r)
x += runeWidth(r)
}
}
// DrawHorizontalLine paints a horizontal line using box characters.
func (p *Painter) DrawHorizontalLine(x1, x2, y int) {
for x := x1; x < x2; x++ {
p.DrawRune(x, y, '─')
}
}
// DrawVerticalLine paints a vertical line using box characters.
func (p *Painter) DrawVerticalLine(x, y1, y2 int) {
for y := y1; y < y2; y++ {
p.DrawRune(x, y, '│')
}
}
// DrawRect paints a rectangle using box characters.
func (p *Painter) DrawRect(x, y, w, h int) {
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
m := i + x
n := j + y
switch {
case i == 0 && j == 0:
p.DrawRune(m, n, '┌')
case i == w-1 && j == 0:
p.DrawRune(m, n, '┐')
case i == 0 && j == h-1:
p.DrawRune(m, n, '└')
case i == w-1 && j == h-1:
p.DrawRune(m, n, '┘')
case i == 0 || i == w-1:
p.DrawRune(m, n, '│')
case j == 0 || j == h-1:
p.DrawRune(m, n, '─')
}
}
}
}
// FillRect clears a rectangular area with whitespace.
func (p *Painter) FillRect(x, y, w, h int) {
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
p.DrawRune(i+x, j+y, ' ')
}
}
}
// SetStyle sets the style used when painting.
func (p *Painter) SetStyle(s Style) {
p.style = s
}
// RestoreStyle clears any style and restores it to the default.
func (p *Painter) RestoreStyle() {
p.SetStyle(p.theme.Style("normal"))
}
// WithStyle decorates the painter with the style associated with an identifier.
func (p *Painter) WithStyle(n string, fn func(*Painter)) {
if !p.theme.HasStyle(n) {
fn(p)
return
}
p.SetStyle(p.theme.Style(n))
fn(p)
p.RestoreStyle()
}
// WithMask masks a painter to restrict painting within the given rectangle.
func (p *Painter) WithMask(r image.Rectangle, fn func(*Painter)) {
tmp := p.mask
defer func() { p.mask = tmp }()
p.mask = p.mask.Intersect(image.Rectangle{
Min: p.mapLocalToWorld(r.Min),
Max: p.mapLocalToWorld(r.Max),
})
fn(p)
}
func (p *Painter) mapLocalToWorld(point image.Point) image.Point {
var offset image.Point
for _, s := range p.transforms {
offset = offset.Add(s)
}
return point.Add(offset)
}