-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
ImageWidgets.go
419 lines (351 loc) · 10.1 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package giu
import (
ctx "context"
"fmt"
"image"
"image/color"
"net/http"
"reflect"
"time"
"github.com/AllenDang/cimgui-go/imgui"
)
var _ Widget = &ImageWidget{}
// ImageWidget adds an image.
// The default size is the size of the image,
// to set a specific size, use .Size(width, height).
// 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
scale imgui.Vec2
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: 0,
height: 0,
scale: imgui.Vec2{X: 1, Y: 1},
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
i.width, i.height = width, height
return i
}
// Scale multiply dimensions after size.
func (i *ImageWidget) Scale(scaleX, scaleY float32) *ImageWidget {
// Size image with DPI scaling
i.scale = imgui.Vec2{X: scaleX, Y: scaleY}
return i
}
// Build implements Widget interface.
func (i *ImageWidget) Build() {
if i.width == 0 && i.height == 0 {
if i.texture != nil {
i.width, i.height = float32(i.texture.tex.Width), float32(i.texture.tex.Height)
} else {
i.width, i.height = 100, 100
}
}
size := imgui.Vec2{X: i.width, Y: i.height}
if size.X == -1 {
rect := imgui.ContentRegionAvail()
size.X = rect.X
}
if size.Y == -1 {
rect := imgui.ContentRegionAvail()
size.Y = rect.Y
}
size.X *= i.scale.X
size.Y *= i.scale.Y
if i.texture == nil || i.texture.tex == nil {
Dummy(size.X, size.Y).Build()
return
}
// trick: detect click event
if i.onClick != nil && IsMouseClicked(MouseButtonLeft) && IsWindowHovered(0) {
cursorPos := GetCursorScreenPos()
mousePos := GetMousePos()
if cursorPos.X <= mousePos.X && cursorPos.Y <= mousePos.Y &&
cursorPos.X+int(size.X) >= mousePos.X && cursorPos.Y+int(size.Y) >= mousePos.Y {
i.onClick()
}
}
imgui.ImageV(i.texture.tex.ID, size, i.uv0, i.uv1, ToVec4Color(i.tintColor), ToVec4Color(i.borderColor))
}
type imageState struct {
loading bool
failure bool
cancel ctx.CancelFunc
texture *Texture
img *image.RGBA
}
// 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 ID
rgba *image.RGBA
img *ImageWidget
}
// ImageWithRgba creates ImageWithRgbaWidget.
// The default size is the size of the image,
// to set a specific size, use .Size(width, height).
func ImageWithRgba(rgba image.Image) *ImageWithRgbaWidget {
return &ImageWithRgbaWidget{
id: GenAutoID("ImageWithRgba"),
rgba: ImageToRgba(rgba),
img: Image(nil),
}
}
// ID sets the interval id of ImageWithRgba widgets.
func (i *ImageWithRgbaWidget) ID(id ID) *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 || !reflect.DeepEqual(i.rgba, imgState.img) {
imgState = &imageState{}
imgState.img = i.rgba
SetState(Context, i.id, imgState)
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 ID
imgPath string
img *ImageWidget
}
// ImageWithFile constructs a new ImageWithFileWidget.
// The default size is the size of the image,
// to set a specific size, use .Size(width, height).
func ImageWithFile(imgPath string) *ImageWithFileWidget {
return &ImageWithFileWidget{
id: ID(fmt.Sprintf("ImageWithFile_%s", imgPath)),
imgPath: imgPath,
img: Image(nil),
}
}
// ID sets the interval id of ImageWithFile widgets.
func (i *ImageWithFileWidget) ID(id ID) *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 ID
imgURL string
downloadTimeout time.Duration
whenLoading Layout
whenFailure Layout
onReady func()
onFailure func(error)
img *ImageWidget
}
// ImageWithURL creates ImageWithURLWidget.
// The default size is the size of the image,
// to set a specific size, use .Size(width, height).
func ImageWithURL(url string) *ImageWithURLWidget {
return &ImageWithURLWidget{
id: 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 = widgets
return i
}
// LayoutForFailure allows to specify layout when image failed to download.
func (i *ImageWithURLWidget) LayoutForFailure(widgets ...Widget) *ImageWithURLWidget {
i.whenFailure = 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, http.MethodGet, 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)
EnqueueNewTextureFromRgba(rgba, func(tex *Texture) {
SetState(Context, i.id, &imageState{
loading: false,
failure: false,
texture: tex,
})
})
// Trigger onReady event
if i.onReady != nil {
i.onReady()
}
}()
}
imgState = GetState[imageState](Context, i.id)
switch {
case imgState.failure:
i.whenFailure.Build()
case imgState.loading:
i.whenLoading.Build()
default:
i.img.texture = imgState.texture
i.img.Build()
}
}