diff --git a/ImageWidgets.go b/ImageWidgets.go index 9d4e2187..2b553795 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. @@ -31,8 +33,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 +76,23 @@ 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 } @@ -134,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"), @@ -193,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), @@ -255,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), @@ -374,6 +391,8 @@ func (i *ImageWithURLWidget) Build() { }() } + imgState = GetState[imageState](Context, i.id) + switch { case imgState.failure: i.whenFailure.Build()