-
Notifications
You must be signed in to change notification settings - Fork 36
/
space.go
73 lines (61 loc) · 1.63 KB
/
space.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
package duit
import (
"image"
)
// Space represents the padding or margin on a UI element.
// In duit functions, these are typically in lowDPI pixels.
type Space struct {
Top, Right, Bottom, Left int
}
// Dx returns the total horizontal space.
func (s Space) Dx() int {
return s.Left + s.Right
}
// Dy returns the total vertical space.
func (s Space) Dy() int {
return s.Top + s.Bottom
}
// Size returns the total horizontal and vertical size.
func (s Space) Size() image.Point {
return image.Pt(s.Dx(), s.Dy())
}
// Mul returns a this space multiplied by n.
func (s Space) Mul(n int) Space {
s.Top *= n
s.Right *= n
s.Bottom *= n
s.Left *= n
return s
}
// Topleft returns a point containing the topleft space.
func (s Space) Topleft() image.Point {
return image.Pt(s.Left, s.Top)
}
// Inset returns a rectangle that is r inset with this space.
func (s Space) Inset(r image.Rectangle) image.Rectangle {
return image.Rect(r.Min.X+s.Left, r.Min.Y+s.Top, r.Max.X-s.Right, r.Max.Y-s.Bottom)
}
// SpaceXY returns a space with x for left/right and y for top/bottom.
func SpaceXY(x, y int) Space {
return Space{y, x, y, x}
}
// SpacePt returns a space with p.X for left/right and p.Y for top/bottom.
func SpacePt(p image.Point) Space {
return Space{p.Y, p.X, p.Y, p.X}
}
// NSpace is a convenience function to create N identical spaces.
func NSpace(n int, space Space) []Space {
l := make([]Space, n)
for i := 0; i < n; i++ {
l[i] = space
}
return l
}
// NSpaceXY is a convenience function to create N identical SpaceXY's.
func NSpaceXY(n, x, y int) []Space {
l := make([]Space, n)
for i := 0; i < n; i++ {
l[i] = SpaceXY(x, y)
}
return l
}