Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the option for image to take its original size #815

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions ImageWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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},
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -374,6 +391,8 @@ func (i *ImageWithURLWidget) Build() {
}()
}

imgState = GetState[imageState](Context, i.id)

switch {
case imgState.failure:
i.whenFailure.Build()
Expand Down
Loading