forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageWidgets.go
391 lines (327 loc) · 9.24 KB
/
ImageWidgets.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package giu
import (
ctx "context"
"fmt"
"image"
"image/color"
"net/http"
"time"
"github.com/AllenDang/imgui-go"
)
var _ Widget = &ImageWidget{}
// ImageWidget adds an image.
// NOTE: ImageWidget is going to be deprecated. ImageWithRGBAWidget
// should be used instead, however, because it is a native
// imgui's solution it is still there.
type ImageWidget struct {
texture *Texture
width float32
height float32
uv0, uv1 imgui.Vec2
tintColor, borderColor color.Color
onClick func()
}
// Image adds an image from giu.Texture.
func Image(texture *Texture) *ImageWidget {
return &ImageWidget{
texture: texture,
width: 100,
height: 100,
uv0: imgui.Vec2{X: 0, Y: 0},
uv1: imgui.Vec2{X: 1, Y: 1},
tintColor: color.RGBA{255, 255, 255, 255},
borderColor: color.RGBA{0, 0, 0, 0},
}
}
// Uv allows to specify uv parameters.
func (i *ImageWidget) Uv(uv0X, uv0Y, uv1X, uv1Y float32) *ImageWidget {
i.uv0.X, i.uv0.Y, i.uv1.X, i.uv1.Y = uv0X, uv0Y, uv1X, uv1Y
return i
}
// TintColor sets image's tint color.
func (i *ImageWidget) TintColor(tintColor color.Color) *ImageWidget {
i.tintColor = tintColor
return i
}
// BorderCol sets color of the border.
func (i *ImageWidget) BorderCol(borderColor color.Color) *ImageWidget {
i.borderColor = borderColor
return i
}
// OnClick adds on-click-callback.
func (i *ImageWidget) OnClick(cb func()) *ImageWidget {
i.onClick = cb
return i
}
// Size sets image size.
func (i *ImageWidget) Size(width, height float32) *ImageWidget {
// Size image with DPI scaling
factor := Context.GetPlatform().GetContentScale()
i.width, i.height = width*factor, height*factor
return i
}
// Build implements Widget interface.
func (i *ImageWidget) Build() {
size := imgui.Vec2{X: i.width, Y: i.height}
rect := imgui.ContentRegionAvail()
if size.X == -1 {
size.X = rect.X
}
if size.Y == -1 {
size.Y = rect.Y
}
if i.texture == nil || i.texture.id == 0 {
Dummy(size.X, size.Y).Build()
return
}
// trick: detect click event
if i.onClick != nil && IsMouseClicked(MouseButtonLeft) && IsWindowFocused(0) {
cursorPos := GetCursorScreenPos()
mousePos := GetMousePos()
mousePos = mousePos.Add(cursorPos)
if cursorPos.X <= mousePos.X && cursorPos.Y <= mousePos.Y &&
cursorPos.X+int(i.width) >= mousePos.X && cursorPos.Y+int(i.height) >= mousePos.Y {
i.onClick()
}
}
imgui.ImageV(i.texture.id, size, i.uv0, i.uv1, ToVec4Color(i.tintColor), ToVec4Color(i.borderColor))
}
type imageState struct {
loading bool
failure bool
cancel ctx.CancelFunc
texture *Texture
}
// Dispose cleans imageState (implements Disposable interface).
func (is *imageState) Dispose() {
is.texture = nil
// Cancel ongoing image downloading
if is.loading && is.cancel != nil {
is.cancel()
}
}
var _ Widget = &ImageWithRgbaWidget{}
// ImageWithRgbaWidget wraps ImageWidget.
// It is more useful because it doesn't make you to care about
// imgui textures. You can just pass golang-native image.Image and
// display it in giu.
type ImageWithRgbaWidget struct {
id string
Rgba image.Image
img *ImageWidget
}
// ImageWithRgba creates ImageWithRgbaWidget.
func ImageWithRgba(rgba image.Image) *ImageWithRgbaWidget {
return &ImageWithRgbaWidget{
id: GenAutoID("ImageWithRgba"),
Rgba: rgba,
img: Image(nil),
}
}
// ID sets the interval id of ImageWithRgba widgets.
func (i *ImageWithRgbaWidget) ID(id string) *ImageWithRgbaWidget {
i.id = id
return i
}
// Size sets image's size.
func (i *ImageWithRgbaWidget) Size(width, height float32) *ImageWithRgbaWidget {
i.img.Size(width, height)
return i
}
// OnClick sets click callback.
func (i *ImageWithRgbaWidget) OnClick(cb func()) *ImageWithRgbaWidget {
i.img.OnClick(cb)
return i
}
// Build implements Widget interface.
func (i *ImageWithRgbaWidget) Build() {
if i.Rgba != nil {
var imgState *imageState
if imgState = GetState[imageState](Context, i.id); imgState == nil {
imgState = &imageState{}
SetState(Context, i.id, imgState)
NewTextureFromRgba(i.Rgba, func(tex *Texture) {
imgState.texture = tex
})
} else {
NewTextureFromRgba(i.Rgba, func(tex *Texture) {
imgState.texture = tex
})
}
i.img.texture = imgState.texture
}
i.img.Build()
}
var _ Widget = &ImageWithFileWidget{}
// ImageWithFileWidget allows to display an image directly
// from .png file.
// NOTE: Be aware that project using this solution may not be portable
// because files are not included in executable binaries!
// You may want to use "embed" package and ImageWithRgba instead.
type ImageWithFileWidget struct {
id string
imgPath string
img *ImageWidget
}
// ImageWithFile constructs a new ImageWithFileWidget.
func ImageWithFile(imgPath string) *ImageWithFileWidget {
return &ImageWithFileWidget{
id: fmt.Sprintf("ImageWithFile_%s", imgPath),
imgPath: imgPath,
img: Image(nil),
}
}
// ID sets the interval id of ImageWithFile widgets.
func (i *ImageWithFileWidget) ID(id string) *ImageWithFileWidget {
i.id = id
return i
}
// Size sets image's size.
func (i *ImageWithFileWidget) Size(width, height float32) *ImageWithFileWidget {
i.img.Size(width, height)
return i
}
// OnClick sets click callback.
func (i *ImageWithFileWidget) OnClick(cb func()) *ImageWithFileWidget {
i.img.OnClick(cb)
return i
}
// Build implements Widget interface.
func (i *ImageWithFileWidget) Build() {
var imgState *imageState
if imgState = GetState[imageState](Context, i.id); imgState == nil {
// Prevent multiple invocation to LoadImage.
imgState = &imageState{}
SetState(Context, i.id, imgState)
img, err := LoadImage(i.imgPath)
if err == nil {
NewTextureFromRgba(img, func(tex *Texture) {
imgState.texture = tex
})
}
}
i.img.texture = imgState.texture
i.img.Build()
}
var _ Widget = &ImageWithURLWidget{}
// ImageWithURLWidget allows to display an image using
// an URL as image source.
type ImageWithURLWidget struct {
id string
imgURL string
downloadTimeout time.Duration
whenLoading Layout
whenFailure Layout
onReady func()
onFailure func(error)
img *ImageWidget
}
// ImageWithURL creates ImageWithURLWidget.
func ImageWithURL(url string) *ImageWithURLWidget {
return &ImageWithURLWidget{
id: fmt.Sprintf("ImageWithURL_%s", url),
imgURL: url,
downloadTimeout: 10 * time.Second,
whenLoading: Layout{Dummy(100, 100)},
whenFailure: Layout{Dummy(100, 100)},
img: Image(nil),
}
}
// OnReady sets event trigger when image is downloaded and ready to display.
func (i *ImageWithURLWidget) OnReady(onReady func()) *ImageWithURLWidget {
i.onReady = onReady
return i
}
// OnFailure sets event trigger when image failed to download/load.
func (i *ImageWithURLWidget) OnFailure(onFailure func(error)) *ImageWithURLWidget {
i.onFailure = onFailure
return i
}
// OnClick sets click callback.
func (i *ImageWithURLWidget) OnClick(cb func()) *ImageWithURLWidget {
i.img.OnClick(cb)
return i
}
// Timeout sets download timeout.
func (i *ImageWithURLWidget) Timeout(downloadTimeout time.Duration) *ImageWithURLWidget {
i.downloadTimeout = downloadTimeout
return i
}
// Size sets image's size.
func (i *ImageWithURLWidget) Size(width, height float32) *ImageWithURLWidget {
i.img.Size(width, height)
return i
}
// LayoutForLoading allows to set layout rendered while loading an image.
func (i *ImageWithURLWidget) LayoutForLoading(widgets ...Widget) *ImageWithURLWidget {
i.whenLoading = Layout(widgets)
return i
}
// LayoutForFailure allows to specify layout when image failed to download.
func (i *ImageWithURLWidget) LayoutForFailure(widgets ...Widget) *ImageWithURLWidget {
i.whenFailure = Layout(widgets)
return i
}
// Build implements Widget interface.
func (i *ImageWithURLWidget) Build() {
var imgState *imageState
if imgState = GetState[imageState](Context, i.id); imgState == nil {
imgState = &imageState{}
SetState(Context, i.id, imgState)
// Prevent multiple invocation to download image.
downloadContext, cancelFunc := ctx.WithCancel(ctx.Background())
SetState(Context, i.id, &imageState{loading: true, cancel: cancelFunc})
errorFn := func(err error) {
SetState(Context, i.id, &imageState{failure: true})
// Trigger onFailure event
if i.onFailure != nil {
i.onFailure(err)
}
}
go func() {
// Load image from url
client := &http.Client{Timeout: i.downloadTimeout}
req, err := http.NewRequestWithContext(downloadContext, "GET", i.imgURL, http.NoBody)
if err != nil {
errorFn(err)
return
}
resp, err := client.Do(req)
if err != nil {
errorFn(err)
return
}
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
errorFn(closeErr)
}
}()
img, _, err := image.Decode(resp.Body)
if err != nil {
errorFn(err)
return
}
rgba := ImageToRgba(img)
NewTextureFromRgba(rgba, func(tex *Texture) {
SetState(Context, i.id, &imageState{
loading: false,
failure: false,
texture: tex,
})
})
// Trigger onReady event
if i.onReady != nil {
i.onReady()
}
}()
}
switch {
case imgState.failure:
i.whenFailure.Build()
case imgState.loading:
i.whenLoading.Build()
default:
i.img.texture = imgState.texture
i.img.Build()
}
}