forked from aarzilli/nucular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
238 lines (198 loc) · 4.14 KB
/
util.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
package nucular
import (
"image"
"strings"
nstyle "github.com/aarzilli/nucular/style"
"golang.org/x/image/font"
"golang.org/x/mobile/event/mouse"
"github.com/aarzilli/nucular/rect"
"github.com/hashicorp/golang-lru"
)
type Heading int
const (
Up Heading = iota
Right
Down
Left
)
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func assert(cond bool) {
if !cond {
panic("assert!")
}
}
func assert2(cond bool, reason string) {
if !cond {
panic(reason)
}
}
func triangleFromDirection(r rect.Rect, pad_x, pad_y int, direction Heading) (result [3]image.Point) {
var w_half int
var h_half int
r.W = max(2*pad_x, r.W)
r.H = max(2*pad_y, r.H)
r.W = r.W - 2*pad_x
r.H = r.H - 2*pad_y
r.X = r.X + pad_x
r.Y = r.Y + pad_y
w_half = r.W / 2.0
h_half = r.H / 2.0
if direction == Up {
result[0] = image.Point{r.X + w_half, r.Y}
result[1] = image.Point{r.X + r.W, r.Y + r.H}
result[2] = image.Point{r.X, r.Y + r.H}
} else if direction == Right {
result[0] = image.Point{r.X, r.Y}
result[1] = image.Point{r.X + r.W, r.Y + h_half}
result[2] = image.Point{r.X, r.Y + r.H}
} else if direction == Down {
result[0] = image.Point{r.X, r.Y}
result[1] = image.Point{r.X + r.W, r.Y}
result[2] = image.Point{r.X + w_half, r.Y + r.H}
} else {
result[0] = image.Point{r.X, r.Y + h_half}
result[1] = image.Point{r.X + r.W, r.Y}
result[2] = image.Point{r.X + r.W, r.Y + r.H}
}
return
}
func minFloat(x, y float64) float64 {
if x < y {
return x
}
return y
}
func maxFloat(x, y float64) float64 {
if x > y {
return x
}
return y
}
func clampFloat(i, v, x float64) float64 {
if v < i {
v = i
}
if v > x {
v = x
}
return v
}
func clampInt(i, v, x int) int {
if v < i {
v = i
}
if v > x {
v = x
}
return v
}
func saturateFloat(x float64) float64 {
return maxFloat(0.0, minFloat(1.0, x))
}
func basicWidgetStateControl(state *nstyle.WidgetStates, in *Input, bounds rect.Rect) nstyle.WidgetStates {
if in == nil {
*state = nstyle.WidgetStateInactive
return nstyle.WidgetStateInactive
}
hovering := in.Mouse.HoveringRect(bounds)
if *state == nstyle.WidgetStateInactive && hovering {
*state = nstyle.WidgetStateHovered
}
if *state == nstyle.WidgetStateHovered && !hovering {
*state = nstyle.WidgetStateInactive
}
if *state == nstyle.WidgetStateHovered && in.Mouse.HasClickInRect(mouse.ButtonLeft, bounds) {
*state = nstyle.WidgetStateActive
}
if hovering {
return nstyle.WidgetStateHovered
} else {
return nstyle.WidgetStateInactive
}
}
func shrinkRect(r rect.Rect, amount int) rect.Rect {
var res rect.Rect
r.W = max(r.W, 2*amount)
r.H = max(r.H, 2*amount)
res.X = r.X + amount
res.Y = r.Y + amount
res.W = r.W - 2*amount
res.H = r.H - 2*amount
return res
}
func FontHeight(f font.Face) int {
return f.Metrics().Ascent.Ceil() + f.Metrics().Descent.Ceil()
}
var fontWidthCache *lru.Cache
var fontWidthCacheSize int
func init() {
fontWidthCacheSize = 256
fontWidthCache, _ = lru.New(256)
}
func ChangeFontWidthCache(size int) {
if size > fontWidthCacheSize {
fontWidthCacheSize = size
fontWidthCache, _ = lru.New(fontWidthCacheSize)
}
}
type fontWidthCacheKey struct {
f font.Face
string string
}
func FontWidth(f font.Face, str string) int {
maxw := 0
for {
newline := strings.Index(str, "\n")
line := str
if newline >= 0 {
line = str[:newline]
}
k := fontWidthCacheKey{f, line}
var w int
if val, ok := fontWidthCache.Get(k); ok {
w = val.(int)
} else {
d := font.Drawer{Face: f}
w = d.MeasureString(line).Ceil()
fontWidthCache.Add(k, w)
}
if w > maxw {
maxw = w
}
if newline >= 0 {
str = str[newline+1:]
} else {
break
}
}
return maxw
}
func unify(a rect.Rect, b rect.Rect) (clip rect.Rect) {
clip.X = max(a.X, b.X)
clip.Y = max(a.Y, b.Y)
clip.W = min(a.X+a.W, b.X+b.W) - clip.X
clip.H = min(a.Y+a.H, b.Y+b.H) - clip.Y
clip.W = max(0.0, clip.W)
clip.H = max(0.0, clip.H)
return
}
func padRect(r rect.Rect, pad image.Point) rect.Rect {
r.W = max(r.W, 2*pad.X)
r.H = max(r.H, 2*pad.Y)
r.X += pad.X
r.Y += pad.Y
r.W -= 2 * pad.X
r.H -= 2 * pad.Y
return r
}