forked from aarzilli/nucular
/
command.go
201 lines (171 loc) · 3.38 KB
/
command.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
package command
import (
"image"
"image/color"
"github.com/Varunram/nucular/rect"
"golang.org/x/image/font"
)
// CommandBuffer is a list of drawing directives.
type Buffer struct {
UseClipping bool
Clip rect.Rect
Commands []Command
}
var nk_null_rect = rect.Rect{-8192.0, -8192.0, 16384.0, 16384.0}
func (buffer *Buffer) Reset() {
buffer.UseClipping = true
buffer.Clip = nk_null_rect
buffer.Commands = buffer.Commands[:0]
}
// Represents one drawing directive.
type Command struct {
rect.Rect
Kind CommandKind
Scissor Scissor
Line Line
RectFilled RectFilled
TriangleFilled TriangleFilled
CircleFilled CircleFilled
Image Image
Text Text
}
type CommandKind uint8
const (
ScissorCmd CommandKind = iota
LineCmd
RectFilledCmd
TriangleFilledCmd
CircleFilledCmd
ImageCmd
TextCmd
)
type Scissor struct {
rect.Rect
}
type Line struct {
LineThickness uint16
Begin image.Point
End image.Point
Color color.RGBA
}
type RectFilled struct {
//rect.Rect
Rounding uint16
Color color.RGBA
}
type TriangleFilled struct {
A image.Point
B image.Point
C image.Point
Color color.RGBA
}
type CircleFilled struct {
//rect.Rect
Color color.RGBA
}
type Image struct {
//rect.Rect
Img *image.RGBA
}
type Text struct {
//rect.Rect
Face font.Face
Foreground color.RGBA
String string
}
func (b *Buffer) PushScissor(r rect.Rect) {
b.Clip = r
var cmd Command
cmd.Kind = ScissorCmd
cmd.Rect = r
b.Commands = append(b.Commands, cmd)
}
func (b *Buffer) StrokeLine(p0, p1 image.Point, line_thickness int, c color.RGBA) {
var cmd Command
cmd.Kind = LineCmd
cmd.Line.LineThickness = uint16(line_thickness)
cmd.Line.Begin = p0
cmd.Line.End = p1
cmd.Line.Color = c
b.Commands = append(b.Commands, cmd)
}
func (b *Buffer) FillRect(rect rect.Rect, rounding uint16, c color.RGBA) {
if c.A == 0 {
return
}
if b.UseClipping {
if !rect.Intersect(&b.Clip) {
return
}
}
var cmd Command
cmd.Kind = RectFilledCmd
cmd.RectFilled.Rounding = rounding
cmd.Rect = rect
cmd.RectFilled.Color = c
b.Commands = append(b.Commands, cmd)
}
func (b *Buffer) FillCircle(r rect.Rect, c color.RGBA) {
if c.A == 0 {
return
}
if b.UseClipping {
if !r.Intersect(&b.Clip) {
return
}
}
var cmd Command
cmd.Kind = CircleFilledCmd
cmd.Rect = r
cmd.CircleFilled.Color = c
b.Commands = append(b.Commands, cmd)
}
func (b *Buffer) FillTriangle(p0, p1, p2 image.Point, c color.RGBA) {
if c.A == 0 {
return
}
if b.UseClipping {
if !b.Clip.Contains(p0) || !b.Clip.Contains(p1) || !b.Clip.Contains(p2) {
return
}
}
var cmd Command
cmd.Kind = TriangleFilledCmd
cmd.TriangleFilled.A = p0
cmd.TriangleFilled.B = p1
cmd.TriangleFilled.C = p2
cmd.TriangleFilled.Color = c
b.Commands = append(b.Commands, cmd)
}
func (b *Buffer) DrawImage(r rect.Rect, img *image.RGBA) {
if b.UseClipping {
if !r.Intersect(&b.Clip) {
return
}
}
var cmd Command
cmd.Kind = ImageCmd
cmd.Rect = r
cmd.Image.Img = img
b.Commands = append(b.Commands, cmd)
}
func (b *Buffer) DrawText(r rect.Rect, str string, face font.Face, fg color.RGBA) {
if len(str) == 0 || (fg.A == 0) {
return
}
if b.UseClipping {
if !r.Intersect(&b.Clip) {
return
}
}
if len(str) == 0 {
return
}
var cmd Command
cmd.Kind = TextCmd
cmd.Rect = r
cmd.Text.Foreground = fg
cmd.Text.Face = face
cmd.Text.String = str
b.Commands = append(b.Commands, cmd)
}