This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathpainter.go
186 lines (155 loc) · 4.16 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
package tui
import (
"image"
termbox "github.com/nsf/termbox-go"
)
// Surface defines a surface that can be painted on.
type Surface interface {
SetCell(x, y int, ch rune, fg, bg Color)
SetCursor(x, y int)
Begin()
End()
Size() image.Point
}
type termboxSurface struct{}
// NewTermboxSurface returns the default paint surface.
func NewTermboxSurface() Surface {
return termboxSurface{}
}
func (s termboxSurface) SetCell(x, y int, ch rune, fg, bg Color) {
termbox.SetCell(x, y, ch, termbox.Attribute(fg), termbox.Attribute(bg))
}
func (s termboxSurface) SetCursor(x, y int) {
termbox.SetCursor(x, y)
}
func (s termboxSurface) Begin() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
}
func (s termboxSurface) End() {
termbox.Flush()
}
func (s termboxSurface) Size() image.Point {
w, h := termbox.Size()
return image.Point{w, h}
}
// Painter provides operations to paint on a surface.
type Painter struct {
// Surface to paint on.
surface Surface
palette *Palette
// Current brush.
fg, bg Color
// Transform stack
transforms []image.Point
}
// NewPainter returns a new instance of Painter.
func NewPainter(s Surface, p *Palette) *Painter {
return &Painter{
surface: s,
palette: p,
fg: p.Item("normal").Fg,
bg: p.Item("normal").Bg,
}
}
// 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.Begin()
w.Resize(p.surface.Size())
w.Draw(p)
p.End()
}
// 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)
}
}
// DrawRune paints a rune at the given coordinate.
func (p *Painter) DrawRune(x, y int, r rune) {
wp := p.mapLocalToWorld(image.Point{x, y})
p.surface.SetCell(wp.X, wp.Y, r, p.fg, p.bg)
}
func (p *Painter) DrawHorizontalLine(x1, x2, y int) {
for x := x1; x < x2; x++ {
wp := p.mapLocalToWorld(image.Point{x, y})
p.surface.SetCell(wp.X, wp.Y, '─', p.fg, p.bg)
}
}
func (p *Painter) DrawVerticalLine(x, y1, y2 int) {
for y := y1; y < y2; y++ {
wp := p.mapLocalToWorld(image.Point{x, y})
p.surface.SetCell(wp.X, wp.Y, '│', p.fg, p.bg)
}
}
// DrawRect paints a rectangle.
func (p *Painter) DrawRect(x, y, w, h int) {
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
wp := p.mapLocalToWorld(image.Point{i + x, j + y})
switch {
case i == 0 && j == 0:
p.surface.SetCell(wp.X, wp.Y, '┌', p.fg, p.bg)
case i == w-1 && j == 0:
p.surface.SetCell(wp.X, wp.Y, '┐', p.fg, p.bg)
case i == 0 && j == h-1:
p.surface.SetCell(wp.X, wp.Y, '└', p.fg, p.bg)
case i == w-1 && j == h-1:
p.surface.SetCell(wp.X, wp.Y, '┘', p.fg, p.bg)
case i == 0 || i == w-1:
p.surface.SetCell(wp.X, wp.Y, '│', p.fg, p.bg)
case j == 0 || j == h-1:
p.surface.SetCell(wp.X, wp.Y, '─', p.fg, p.bg)
}
}
}
}
func (p *Painter) FillRect(x, y, w, h int) {
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
wp := p.mapLocalToWorld(image.Point{i + x, j + y})
p.surface.SetCell(wp.X, wp.Y, ' ', p.fg, p.bg)
}
}
}
func (p *Painter) DrawCursor(x, y int) {
wp := p.mapLocalToWorld(image.Point{x, y})
p.surface.SetCursor(wp.X, wp.Y)
}
func (p *Painter) SetBrush(fg, bg Color) {
p.fg = fg
p.bg = bg
}
func (p *Painter) RestoreBrush() {
p.SetBrush(p.palette.Item("normal").Fg, p.palette.Item("normal").Bg)
}
func (p *Painter) WithStyledBrush(n string, fn func(*Painter)) {
p.SetBrush(p.palette.Item(n).Fg, p.palette.Item(n).Bg)
fn(p)
p.RestoreBrush()
}
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)
}