From 3eb4a093d2f2bde321edc6d36e59ad93cac76e32 Mon Sep 17 00:00:00 2001 From: "Colin J. Brigato" Date: Thu, 3 Oct 2024 12:08:03 +0200 Subject: [PATCH 1/2] ImageWidget: Fix click event detection / Add Scale --- ImageWidgets.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ImageWidgets.go b/ImageWidgets.go index 4e23e36d..5e8ee846 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -24,6 +24,7 @@ type ImageWidget struct { texture *Texture width float32 height float32 + scale imgui.Vec2 uv0, uv1 imgui.Vec2 tintColor, borderColor color.Color onClick func() @@ -35,6 +36,7 @@ func Image(texture *Texture) *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}, @@ -74,6 +76,14 @@ func (i *ImageWidget) Size(width, height float32) *ImageWidget { 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 { @@ -96,6 +106,9 @@ func (i *ImageWidget) Build() { 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 @@ -107,7 +120,7 @@ func (i *ImageWidget) Build() { mousePos := GetMousePos() if cursorPos.X <= mousePos.X && cursorPos.Y <= mousePos.Y && - cursorPos.X+int(i.width) >= mousePos.X && cursorPos.Y+int(i.height) >= mousePos.Y { + cursorPos.X+int(size.X) >= mousePos.X && cursorPos.Y+int(size.Y) >= mousePos.Y { i.onClick() } } From 4fffdcb0fde1d70cbc52a153b6d0068bcc7eceec Mon Sep 17 00:00:00 2001 From: "Colin J. Brigato" Date: Thu, 3 Oct 2024 12:10:22 +0200 Subject: [PATCH 2/2] linter --- ImageWidgets.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ImageWidgets.go b/ImageWidgets.go index 5e8ee846..9a7dfd1f 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -80,7 +80,7 @@ func (i *ImageWidget) Size(width, height float32) *ImageWidget { func (i *ImageWidget) Scale(scaleX, scaleY float32) *ImageWidget { // Size image with DPI scaling i.scale = imgui.Vec2{X: scaleX, Y: scaleY} - + return i }