From a0f274e6a74b296c0c4cc65c93e26040d74c2028 Mon Sep 17 00:00:00 2001 From: Ivan Misustin Date: Mon, 8 Jul 2024 18:08:08 +0200 Subject: [PATCH 1/5] Added the option for image to take it original size --- ImageWidgets.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ImageWidgets.go b/ImageWidgets.go index 9d4e2187..cb9b09d1 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -105,11 +105,13 @@ func (i *ImageWidget) Build() { } type imageState struct { - loading bool - failure bool - cancel ctx.CancelFunc - texture *Texture - img *image.RGBA + loading bool + failure bool + cancel ctx.CancelFunc + texture *Texture + img *image.RGBA + default_width float32 + default_height float32 } // Dispose cleans imageState (implements Disposable interface). @@ -292,6 +294,14 @@ func (i *ImageWithURLWidget) Timeout(downloadTimeout time.Duration) *ImageWithUR // Size sets image's size. func (i *ImageWithURLWidget) Size(width, height float32) *ImageWithURLWidget { + + if width == 0 && height == 0 { + imgState := GetState[imageState](Context, i.id) + if imgState != nil { + width, height = imgState.default_width, imgState.default_height + } + } + i.img.Size(width, height) return i } @@ -361,9 +371,11 @@ func (i *ImageWithURLWidget) Build() { EnqueueNewTextureFromRgba(rgba, func(tex *Texture) { SetState(Context, i.id, &imageState{ - loading: false, - failure: false, - texture: tex, + loading: false, + failure: false, + texture: tex, + default_width: float32(rgba.Bounds().Dx()), + default_height: float32(rgba.Bounds().Dy()), }) }) @@ -374,6 +386,8 @@ func (i *ImageWithURLWidget) Build() { }() } + imgState = GetState[imageState](Context, i.id) + switch { case imgState.failure: i.whenFailure.Build() From 196770aead305bd82c1b0ed0e90ffac2dc39dcbe Mon Sep 17 00:00:00 2001 From: Ivan Misustin Date: Tue, 9 Jul 2024 16:10:16 +0200 Subject: [PATCH 2/5] Make behavior from previous commit default --- ImageWidgets.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ImageWidgets.go b/ImageWidgets.go index cb9b09d1..8b3f3073 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -31,8 +31,8 @@ type ImageWidget struct { func Image(texture *Texture) *ImageWidget { return &ImageWidget{ texture: texture, - width: 100, - height: 100, + width: 0, + height: 0, uv0: imgui.Vec2{X: 0, Y: 0}, uv1: imgui.Vec2{X: 1, Y: 1}, tintColor: color.RGBA{255, 255, 255, 255}, @@ -74,14 +74,24 @@ func (i *ImageWidget) Size(width, height float32) *ImageWidget { // 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} - rect := imgui.ContentRegionAvail() if size.X == -1 { + rect := imgui.ContentRegionAvail() size.X = rect.X } if size.Y == -1 { + rect := imgui.ContentRegionAvail() size.Y = rect.Y } From a6d7dfea81fab12896c6280d64168573eec5649c Mon Sep 17 00:00:00 2001 From: Ivan Misustin Date: Tue, 9 Jul 2024 16:18:25 +0200 Subject: [PATCH 3/5] Cleaned up some leftover things which arent needed anymore --- ImageWidgets.go | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/ImageWidgets.go b/ImageWidgets.go index 8b3f3073..f323d81a 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -115,13 +115,11 @@ func (i *ImageWidget) Build() { } type imageState struct { - loading bool - failure bool - cancel ctx.CancelFunc - texture *Texture - img *image.RGBA - default_width float32 - default_height float32 + loading bool + failure bool + cancel ctx.CancelFunc + texture *Texture + img *image.RGBA } // Dispose cleans imageState (implements Disposable interface). @@ -304,14 +302,6 @@ func (i *ImageWithURLWidget) Timeout(downloadTimeout time.Duration) *ImageWithUR // Size sets image's size. func (i *ImageWithURLWidget) Size(width, height float32) *ImageWithURLWidget { - - if width == 0 && height == 0 { - imgState := GetState[imageState](Context, i.id) - if imgState != nil { - width, height = imgState.default_width, imgState.default_height - } - } - i.img.Size(width, height) return i } @@ -381,11 +371,9 @@ func (i *ImageWithURLWidget) Build() { EnqueueNewTextureFromRgba(rgba, func(tex *Texture) { SetState(Context, i.id, &imageState{ - loading: false, - failure: false, - texture: tex, - default_width: float32(rgba.Bounds().Dx()), - default_height: float32(rgba.Bounds().Dy()), + loading: false, + failure: false, + texture: tex, }) }) From e4a58ce9afca881f334f451fb44dcd5e3610a69a Mon Sep 17 00:00:00 2001 From: Ivan Misustin Date: Wed, 10 Jul 2024 10:22:28 +0200 Subject: [PATCH 4/5] Added comments and fixed linter complaint --- ImageWidgets.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ImageWidgets.go b/ImageWidgets.go index f323d81a..f9257f45 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -15,6 +15,8 @@ import ( 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. @@ -74,7 +76,6 @@ func (i *ImageWidget) Size(width, height float32) *ImageWidget { // 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) @@ -144,6 +145,8 @@ type ImageWithRgbaWidget struct { } // 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"), @@ -203,6 +206,8 @@ type ImageWithFileWidget struct { } // 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: fmt.Sprintf("ImageWithFile_%s", imgPath), @@ -265,6 +270,8 @@ type ImageWithURLWidget struct { } // 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: fmt.Sprintf("ImageWithURL_%s", url), From 746f6eeac2877d45252fe727119690e09dd783c7 Mon Sep 17 00:00:00 2001 From: Ivan Misustin Date: Wed, 10 Jul 2024 10:24:53 +0200 Subject: [PATCH 5/5] Fixed more linter complaints... --- ImageWidgets.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ImageWidgets.go b/ImageWidgets.go index f9257f45..2b553795 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -16,7 +16,7 @@ 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) +// 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. @@ -146,7 +146,7 @@ type ImageWithRgbaWidget struct { // ImageWithRgba creates ImageWithRgbaWidget. // The default size is the size of the image, -// to set a specific size, use .Size(width, height) +// to set a specific size, use .Size(width, height). func ImageWithRgba(rgba image.Image) *ImageWithRgbaWidget { return &ImageWithRgbaWidget{ id: GenAutoID("ImageWithRgba"), @@ -207,7 +207,7 @@ type ImageWithFileWidget struct { // ImageWithFile constructs a new ImageWithFileWidget. // The default size is the size of the image, -// to set a specific size, use .Size(width, height) +// to set a specific size, use .Size(width, height). func ImageWithFile(imgPath string) *ImageWithFileWidget { return &ImageWithFileWidget{ id: fmt.Sprintf("ImageWithFile_%s", imgPath), @@ -271,7 +271,7 @@ type ImageWithURLWidget struct { // ImageWithURL creates ImageWithURLWidget. // The default size is the size of the image, -// to set a specific size, use .Size(width, height) +// to set a specific size, use .Size(width, height). func ImageWithURL(url string) *ImageWithURLWidget { return &ImageWithURLWidget{ id: fmt.Sprintf("ImageWithURL_%s", url),