-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect.go
360 lines (306 loc) · 9.96 KB
/
rect.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package gogl
import (
"image/color"
"math"
)
// Rect is a rectangle shape, aligned to the top-left corner.
type Rect struct {
Pos Vec
Direction Vec
w, h float64
style Style
}
var _ Shape = (*Rect)(nil)
var _ hoverable = (*Rect)(nil)
// NewRect constructs a new rectangle shape.
func NewRect(width, height float64, pos Vec) *Rect {
return &Rect{
Pos: pos,
Direction: Vec{},
w: width,
h: height,
style: DefaultStyle,
}
}
// Draw draws the rectangle onto the provided frame buffer.
func (r *Rect) Draw(buf *FrameBuffer) {
if r.style.Thickness == 0 {
for x := 0; x <= int(math.Round(r.w)); x++ {
for y := 0; y <= int(math.Round(r.h)); y++ {
xInt, yInt := int(math.Round(r.Pos.X)), int(math.Round(r.Pos.Y))
buf.SetPixel(xInt+x, yInt+y, NewPixel(r.style.Colour))
}
}
if r.style.Bloom > 0 {
r.drawBloom(buf)
}
} else {
// Draw each edge as its own rectangle
NewRect(r.w, r.style.Thickness, r.Pos).
SetStyle(Style{r.style.Colour, 0, 0}).
Draw(buf)
NewRect(r.w, r.style.Thickness, Vec{r.Pos.X, r.Pos.Y + float64(r.h) - float64(r.style.Thickness)}).
SetStyle(Style{r.style.Colour, 0, 0}).
Draw(buf)
NewRect(r.style.Thickness, r.h, r.Pos).
SetStyle(Style{r.style.Colour, 0, 0}).
Draw(buf)
NewRect(r.style.Thickness, r.h, Vec{r.Pos.X + float64(r.w) - float64(r.style.Thickness), r.Pos.Y}).
SetStyle(Style{r.style.Colour, 0, 0}).
Draw(buf)
if r.style.Bloom > 0 {
r.drawBloom(buf)
}
}
}
// Width returns the pixel width of the rectangle.
func (r *Rect) Width() float64 {
return r.w
}
// SetWidth sets the width of the rectangle.
func (r *Rect) SetWidth(px float64) *Rect {
r.w = px
return r
}
// Height returns the pixel height of the rectangle.
func (r *Rect) Height() float64 {
return r.h
}
// SetHeight sets the height of the rectangle.
func (r *Rect) SetHeight(px float64) *Rect {
r.h = px
return r
}
// GetPos returns the position of the rectangle.
func (r *Rect) GetPos() Vec {
return r.Pos
}
// SetPos sets the position of the rectangle.
func (r *Rect) SetPos(pos Vec) {
r.Pos = pos
}
// GetStyle returns's the rectangle's style.
func (r *Rect) GetStyle() Style {
return r.style
}
// SetStyle sets the style of the rectangle.
func (r *Rect) SetStyle(style Style) *Rect {
r.style = style
return r
}
// Move moves the rectangle by the given vector.
func (r *Rect) Move(px Vec) {
r.Pos = Add(r.Pos, px)
}
// String returns the type of shape as a string.
func (r *Rect) String() string {
return "rectangle"
}
// IsWithin returns whether a position lies within the rectangle's perimeter.
func (r *Rect) IsWithin(pos Vec) bool {
return (pos.X >= r.Pos.X) && (pos.X <= r.Pos.X+r.Width()) &&
(pos.Y >= r.Pos.Y) && (pos.Y <= r.Pos.Y+r.Height())
}
// drawBloom draws a bloom effect around the shape.
func (r *Rect) drawBloom(buf *FrameBuffer) {
// Draw borders around the rectangle of increasing size and decreasing intensity.
for dist := 1; dist <= r.style.Bloom; dist++ {
x, y := int(math.Round(r.Pos.X)), int(math.Round(r.Pos.Y))
topLeftX := x - dist
topLeftY := y - dist
topRightX := x + int(math.Round(r.w)) + dist
bottomLeftY := y + int(math.Round(r.h)) + dist
// Calculate colour from distance away from shape body
brightness := 1 - (float64(dist) / float64(r.style.Bloom))
r, g, b, a := RGBA8(r.style.Colour)
bloomColour := color.RGBA{r, g, b, uint8(brightness * float64(a))}
// Draw top and bottom bloom
for i := topLeftX + 1; i < topRightX; i++ {
buf.SetPixel(i, topLeftY, NewPixel(bloomColour))
buf.SetPixel(i, bottomLeftY, NewPixel(bloomColour))
}
// Draw left and right bloom
for i := topLeftY; i <= bottomLeftY; i++ {
buf.SetPixel(topLeftX, i, NewPixel(bloomColour))
buf.SetPixel(topRightX, i, NewPixel(bloomColour))
}
}
}
// CurvedRect is a rectangle with rounded corners, aligned to the top-left.
type CurvedRect struct {
Pos Vec
Direction Vec
w, h float64
style Style
radius float64
}
var _ Shape = (*CurvedRect)(nil)
var _ hoverable = (*Rect)(nil)
// NewCurvedRect constructs a new curved rectangle.
func NewCurvedRect(width, height, radius float64, pos Vec) *CurvedRect {
return &CurvedRect{
Pos: pos,
Direction: Vec{},
w: width,
h: height,
style: DefaultStyle,
radius: radius,
}
}
// IsWithin returns whether a position lies within the curved rectangle's perimeter.
func (r *CurvedRect) IsWithin(pos Vec) bool {
// Note: this doesn't account for the rounded corners
return (pos.X >= r.Pos.X) && (pos.X <= r.Pos.X+r.Width()) &&
(pos.Y >= r.Pos.Y) && (pos.Y <= r.Pos.Y+r.Height())
}
// Draw draws the curved rectangle onto the provided frame buffer.
func (r *CurvedRect) Draw(buf *FrameBuffer) {
subRectHeight := r.style.Thickness
subRectWidth := r.style.Thickness
if r.style.Thickness == 0 {
// For filled shape
subRectWidth = r.w / 2
subRectHeight = r.h / 2
}
// Draw each edge as its own rectangle
NewRect(r.w-2*(r.radius), subRectHeight, Vec{r.Pos.X + r.radius, r.Pos.Y}).
SetStyle(Style{r.style.Colour, 0, 0}).
Draw(buf)
NewRect(r.w-2*(r.radius), subRectHeight, Vec{r.Pos.X + r.radius, r.Pos.Y + r.h - subRectHeight}).
SetStyle(Style{r.style.Colour, 0, 0}).
Draw(buf)
NewRect(subRectWidth, r.h-2*r.radius, Vec{r.Pos.X, r.Pos.Y + r.radius}).
SetStyle(Style{r.style.Colour, 0, 0}).
Draw(buf)
NewRect(subRectWidth, r.h-2*r.radius, Vec{r.Pos.X + r.w - subRectWidth, r.Pos.Y + r.radius}).
SetStyle(Style{r.style.Colour, 0, 0}).
Draw(buf)
// Draw rounded corners
drawCorner := func(bbox *Rect, origin Vec) {
// Iterate over every pixel in the bounding box
for x := bbox.Pos.X; x <= bbox.Pos.X+bbox.w; x++ {
for y := bbox.Pos.Y; y <= bbox.Pos.Y+bbox.h; y++ {
dist := Dist(origin, Vec{x, y})
withinCircle := func() bool {
if r.style.Thickness == 0 {
return dist <= r.radius
}
return dist <= r.radius && dist > r.radius-r.style.Thickness
}()
if withinCircle {
buf.SetPixel(int(math.Round(x)), int(math.Round(y)), NewPixel(r.style.Colour))
}
}
}
}
// Top left
bboxSize := r.radius
bbox := NewRect(bboxSize, bboxSize, Vec{r.Pos.X, r.Pos.Y})
drawCorner(bbox, Vec{bbox.Pos.X + bbox.w, bbox.Pos.Y + bbox.h})
// Top right
bbox = NewRect(bboxSize, bboxSize, Vec{r.Pos.X + r.w - r.radius, r.Pos.Y})
drawCorner(bbox, Vec{bbox.Pos.X, bbox.Pos.Y + bbox.h})
// Bottom left
bbox = NewRect(bboxSize, bboxSize, Vec{r.Pos.X, r.Pos.Y + r.h - r.radius})
drawCorner(bbox, Vec{bbox.Pos.X + bbox.w, bbox.Pos.Y})
// Bottom right
bbox = NewRect(bboxSize, bboxSize, Vec{r.Pos.X + r.w - r.radius, r.Pos.Y + r.h - r.radius})
drawCorner(bbox, Vec{bbox.Pos.X, bbox.Pos.Y})
if r.style.Bloom > 0 {
r.drawBloom(buf)
}
}
// Width returns the pixel width of the curved rectangle.
func (r *CurvedRect) Width() float64 {
return r.w
}
// SetWidth sets the width of the curved rectangle.
func (r *CurvedRect) SetWidth(px float64) *CurvedRect {
r.w = px
return r
}
// Height returns the pixel height of the curved rectangle.
func (r *CurvedRect) Height() float64 {
return r.h
}
// SetHeight sets the height of the curved rectangle.
func (r *CurvedRect) SetHeight(px float64) *CurvedRect {
r.h = px
return r
}
// GetPos returns the position of the curved rectangle.
func (r *CurvedRect) GetPos() Vec {
return r.Pos
}
// SetPos sets the position of the curved rectangle.
func (r *CurvedRect) SetPos(pos Vec) {
r.Pos = pos
}
// GetStyle returns's the curved rectangle's style.
func (r *CurvedRect) GetStyle() Style {
return r.style
}
// SetStyle sets the style of the curved rectangle.
func (r *CurvedRect) SetStyle(style Style) *CurvedRect {
r.style = style
return r
}
// Move moves the curved rectangle by the given vector.
func (r *CurvedRect) Move(px Vec) {
r.Pos = Add(r.Pos, px)
}
// String returns the type of shape as a string.
func (r *CurvedRect) String() string {
return "curved rectangle"
}
// drawBloom draws a bloom effect around the shape.
func (r *CurvedRect) drawBloom(buf *FrameBuffer) {
bloom := float64(r.style.Bloom)
// Draw straight edge bloom
for dist := 1; dist <= r.style.Bloom; dist++ {
// Calculate colour from distance away from shape body
brightness := 1 - (float64(dist) / bloom)
R, G, B, A := RGBA8(r.style.Colour)
bloomColour := color.RGBA{R, G, B, uint8(brightness * float64(A))}
// Top and bottom bloom
for x := r.Pos.X + r.radius + 1; x < r.Pos.X+r.w-r.radius; x++ {
buf.SetPixel(int(x), int(r.Pos.Y)-dist, NewPixel(bloomColour))
buf.SetPixel(int(x), int(r.Pos.Y+r.h)+dist, NewPixel(bloomColour))
}
// Left and right
for y := r.Pos.Y + r.radius + 1; y < r.Pos.Y+r.h-r.radius; y++ {
buf.SetPixel(int(r.Pos.X)-dist, int(y), NewPixel(bloomColour))
buf.SetPixel(int(r.Pos.X+r.w)+dist, int(y), NewPixel(bloomColour))
}
}
// Draw rounded corner bloom
drawCorner := func(bbox *Rect, origin Vec) {
// Iterate over every pixel in the bounding box
for x := bbox.Pos.X; x <= bbox.Pos.X+bbox.w; x++ {
for y := bbox.Pos.Y; y <= bbox.Pos.Y+bbox.h; y++ {
dist := Dist(origin, Vec{x, y})
withinCircle := dist > r.radius && dist <= r.radius+bloom
if withinCircle {
// Calculate colour from distance away from shape body
brightness := 1 - (dist-r.radius)/(bloom)
r, g, b, a := RGBA8(r.style.Colour)
bloomColour := color.RGBA{r, g, b, uint8(brightness * float64(a))}
buf.SetPixel(int(math.Round(x)), int(math.Round(y)), NewPixel(bloomColour))
}
}
}
}
// Top left
bboxSize := bloom + r.radius
bbox := NewRect(bboxSize, bboxSize, Vec{r.Pos.X - bloom, r.Pos.Y - bloom})
drawCorner(bbox, Vec{bbox.Pos.X + bbox.w, bbox.Pos.Y + bbox.h})
// Top right
bbox = NewRect(bboxSize, bboxSize, Vec{r.Pos.X + r.w - r.radius, r.Pos.Y - bloom})
drawCorner(bbox, Vec{bbox.Pos.X, bbox.Pos.Y + bbox.h})
// Bottom left
bbox = NewRect(bboxSize, bboxSize, Vec{r.Pos.X - bloom, r.Pos.Y + r.h - r.radius})
drawCorner(bbox, Vec{bbox.Pos.X + bbox.w, bbox.Pos.Y})
// Bottom right
bbox = NewRect(bboxSize, bboxSize, Vec{r.Pos.X + r.w - r.radius, r.Pos.Y + r.h - r.radius})
drawCorner(bbox, Vec{bbox.Pos.X, bbox.Pos.Y})
}