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

Add image::Viewer widget #319

Merged
merged 25 commits into from
Dec 18, 2020
Merged

Add image::Viewer widget #319

merged 25 commits into from
Dec 18, 2020

Conversation

tarkah
Copy link
Member

@tarkah tarkah commented Apr 23, 2020

This isn't complete, but the zooming and panning features "work". If an image handle's dimensions are larger than the dimensions of the ImagePane, it is scaled down to fit inside the pane. Images smaller than the pane won't get rescaled until the mouse wheel is scrolled.

Some improvements needed before merging:

  • Image should be centered inside the ImagePane. Can't implement this until negative offsets are allowed inside Clip primitive
  • Clipping offset should get updated when scaling the image to "zoom" around wherever the cursor is placed. Right now offset doesn't change on scaling, so image just zooms out from top left corner.
  • Add builder methods for State to allow specifying the min / max scale factor and step size

Let me know what you think and if there are any other features / defaults that should be added!

resolves #306

@hecrj hecrj added the feature New feature or request label Apr 24, 2020
@hecrj hecrj added this to the 0.2.0 milestone Apr 24, 2020
@hecrj
Copy link
Member

hecrj commented May 4, 2020

This looks great! Thank you.

I think we could put this new widget inside the current image module, as it seems to be an Image variation of some sort. Maybe we could call it Viewer or something similar. That way, users would use it as image::Viewer.

Image should be centered inside the ImagePane. Can't implement this until negative offsets are allowed inside Clip primitive

I will try to get this ready soon!

Clipping offset should get updated when scaling the image to "zoom" around wherever the cursor is placed. Right now offset doesn't change on scaling, so image just zooms out from top left corner.

I implemented some logic to do this in the new game_of_life example. We probably shouldn't copy the behavior verbatim, but it may be useful for inspiration.

Add builder methods for State to allow specifying the min / max scale factor and step size

I'd probably put these in ImagePane itself. This way, we let users decide how to derive these values in view logic.

@tarkah
Copy link
Member Author

tarkah commented May 8, 2020

Thanks for taking a look! I'll definitely move this into the image module and look to incorporate your other suggestions.

@tarkah
Copy link
Member Author

tarkah commented May 15, 2020

I went ahead and renamed it ImageViewer since it's being re-exported to the root of the Iced crate, and Viewer seems a little generic for it there. Unless you don't want it re-exported to the root.

I'm going to hold off on implementing logic for zooming to the cursor until we can center the image inside the Clipping primitive, as to not have to rewrite anything. I'll implement the builder methods then as well.

@hecrj
Copy link
Member

hecrj commented May 26, 2020

Unless you don't want it re-exported to the root.

Yeah, I tend to only re-export the types that have the same name as the module.

I'm going to hold off on implementing logic for zooming to the cursor until we can center the image inside the Clipping primitive

Since #325, there is a new Translate primitive that we should be able to use here. I forgot about this! It's very likely we will end up dropping the offset from Clipping and just use this new primitive instead.

@tarkah
Copy link
Member Author

tarkah commented May 27, 2020

Yeah, I tend to only re-export the types that have the same name as the module.

Ok, I renamed to image::Viewer and removed the re-export.

Since #325, there is a new Translate primitive that we should be able to use here. I forgot about this! It's very likely we will end up dropping the offset from Clipping and just use this new primitive instead.

Awesome! Looking forward to this change

@hecrj
Copy link
Member

hecrj commented May 27, 2020

Awesome! Looking forward to this change

The new primitive already landed in master. We should be able to use to move the Image primitive even with a clipping offset of (0, 0).

@tarkah
Copy link
Member Author

tarkah commented May 27, 2020

Nice, I wrapped the Image primitive in a Translate and applied the inverse of my original offset to this, then left the Clip primitive offset as (0, 0) and it worked! I'll get this PR hopefully wrapped up by end of week. Thanks!

@tarkah
Copy link
Member Author

tarkah commented May 27, 2020

Ok, everything should be implemented! After performing mental gymnastics all morning, I finally got the calculations worked out to translate the image so it's centred with the proper adjustments made for smooth scaling in / out from cursor location.

Note that currently image panning is bounded to the edges of the image, so you cannot infinitely pan past the image and therefore panning is only enabled when the image becomes clipped by it's width and/or height.

@hecrj
Copy link
Member

hecrj commented Jun 2, 2020

Awesome! Thanks. I will review the changes soon.

Merging #354 seems to have caused some conflicts. But don't worry about it, I should be able to fix them.

@hecrj hecrj modified the milestones: 0.2.0, 0.3.0 Nov 26, 2020
@hecrj hecrj changed the base branch from master to 0.2 December 18, 2020 09:16
@hecrj hecrj changed the base branch from 0.2 to master December 18, 2020 09:16
Copy link
Member

@hecrj hecrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've brought this up to speed! 🎉

I have replaced the old Image with this new widget in the pokedex example. I tried to describe all the other changes in the commit history.

I think this should be ready to merge. Hopefully I didn't break anything. Let me know!

@tarkah
Copy link
Member Author

tarkah commented Dec 18, 2020

This is awesome! I'll test it out today and let you know :)

@tarkah
Copy link
Member Author

tarkah commented Dec 18, 2020

@hecrj I've tested on my old project and it works great. Thanks for getting master back-merged into this properly!

@hecrj
Copy link
Member

hecrj commented Dec 18, 2020

Awesome! We can finally merge this then!

Thank you again 🥂

@hecrj hecrj merged commit 0e9f649 into iced-rs:master Dec 18, 2020
@tarkah tarkah deleted the image-pane branch December 18, 2020 22:36
@hecrj hecrj changed the title Add ImagePane widget Add image::Viewer widget Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Image Zoom & Pan
2 participants