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

canvas.Image should be Tappable #697

Closed
Nazgand opened this issue Feb 23, 2020 · 7 comments
Closed

canvas.Image should be Tappable #697

Nazgand opened this issue Feb 23, 2020 · 7 comments

Comments

@Nazgand
Copy link

Nazgand commented Feb 23, 2020

Is your feature request related to a problem? Please describe:

canvas.Image.Tapped could be set for each image and this would make games easier to make.

Is it possible to construct a solution with the existing API?

I can see from the Solitaire example that a tappable gameboard is possible yet more complicated than desired.

Describe the solution you'd like to see:

I want code like this to work:
func main() {
a := app.New()
win := a.NewWindow("Blooms")
board := canvas.NewImageFromFile("board.png")
board.Tapped = func(event *fyne.PointEvent) { fmt.Println("tap", event) }
win.SetContent(board)
win.ShowAndRun()
}

@andydotxyz
Copy link
Member

The canvas primitives (things in the canvas package) like Image are just for drawing - interaction is not supported at that level.

To be Tappable an item would need to be a widget. It is possible that we are missing a helper construct like widget.NewSimpleWidget(fyne.CanvasObject) but I don’t know if that would help here?

@Nazgand
Copy link
Author

Nazgand commented Feb 24, 2020

If that works how I think it works, then it can produce exactly what I want (A Tappable image I could update every time it is Tapped). I confess that I still have not figured out what Solitaire is doing to act like a Tappable Image, so this would be extremely helpful.

@andydotxyz
Copy link
Member

I created a new issue and will close this in preference to that feature request: #709

@metal3d
Copy link
Contributor

metal3d commented Mar 18, 2021

I strangely made this (by error) and it worked....

image :=  canvas.NewImageFromImage(i) // this is an image.Image
image.FillMode = canvas.ImageFillContain
// This button will be placed "over" the image using a Padded box
openButton := widget.NewButton("", func(){
    fmt.Println("Image clicked...")
})
// this encapsulate the image and the button
box := container.NewPadded(image, openButton)

I'm not sure to understand why it works, but it works...

@andydotxyz
Copy link
Member

NewPadded lays all elements out inside a container with a padding inset. NewMax would do the same with no gap around the edge. You have created a button with no content above your image.
You will be able to see the button border.

@metal3d
Copy link
Contributor

metal3d commented Mar 21, 2021

Ho ! I Should test !
Anyway, there are workarounds to make image clickable 😉

@fish-hex
Copy link

type ImageButton struct {
	*canvas.Image
	OnTapped func()
}

func NewImageButtonFromFile(file string, onTapped func()) *ImageButton {
	return &ImageButton{canvas.NewImageFromFile(file), onTapped}
}

func (b *ImageButton) CreateRenderer() fyne.WidgetRenderer {
	return widget.NewSimpleRenderer(b.Image)
}

func (b *ImageButton) Tapped(ev *fyne.PointEvent) {
	b.OnTapped()
}

it works!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants