-
Notifications
You must be signed in to change notification settings - Fork 1
/
rectangle.go
101 lines (82 loc) · 2.19 KB
/
rectangle.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
package tooey
import (
"image"
"sync"
)
func NewRectangle(padding *Padding) Rectangle {
if padding == nil {
padding = NewDefaultPadding()
}
return Rectangle{
Padding: padding,
}
}
// Rectangle is a convenience extension of the stdlib
// image.Rectangle
type Rectangle struct {
image.Rectangle
// Padding should only affect objects within a rectangle
// not the outer bounds of the rectangle
Padding *Padding
sync.Mutex
}
// SetRect defines the boundaries of the rectangle
func (r *Rectangle) SetRect(x1 int, y1 int, x2 int, y2 int) {
r.Rectangle = image.Rect(x1, y1, x2, y2)
}
// GetRect returns the current underlying image.Rectangle
func (r *Rectangle) GetRect() image.Rectangle {
return r.Rectangle
}
// GetInnerRect returns the bounds of the inner padded rectangle
func (r *Rectangle) GetInnerRect() image.Rectangle {
return image.Rect(r.InnerX1(), r.InnerY1(), r.InnerX2(), r.InnerY2())
}
// X1 returns the rectangle's Min.X point
func (r *Rectangle) X1() int {
return r.Min.X
}
// X2 returns the rectangle's Max.X point
func (r *Rectangle) X2() int {
return r.Max.X
}
// Y1 returns the rectangle's Min.Y point
func (r *Rectangle) Y1() int {
return r.Min.Y
}
// Y2 returns the rectangle's Max.Y point
func (r *Rectangle) Y2() int {
return r.Max.Y
}
// InnerX1 returns X1 with padding
func (r *Rectangle) InnerX1() int {
return r.X1() + r.Padding.Left
}
// InnerX2 returns X2 with padding
func (r *Rectangle) InnerX2() int {
return r.X2() - r.Padding.Right
}
// InnerY1 returns Y1 with padding
func (r *Rectangle) InnerY1() int {
return r.Y1() + r.Padding.Top
}
// InnerY2 returns Y2 with padding
func (r *Rectangle) InnerY2() int {
return r.Y2() - r.Padding.Bottom
}
// DrawableWidth returns the max width of the rectangle minus padding
func (r *Rectangle) DrawableWidth() int {
return r.X2() - r.X1() - r.Padding.Left - r.Padding.Right
}
// DrawableHeight returns the max height of the rectangle minux padding
func (r *Rectangle) DrawableHeight() int {
return r.Y2() - r.Y1() - r.Padding.Top - r.Padding.Bottom
}
// ZeroSize returns true if the rectangle has no size
func (r *Rectangle) ZeroSize() bool {
if r.Y1() == r.Y2() && r.X1() == r.X2() {
return true
} else {
return false
}
}