cinder / Cinder

Cinder is a community-developed, free and open source library for professional-quality creative coding in C++.
http://libcinder.org
Other
5.28k stars 939 forks source link

Added Canvas UI #2326

Open paulhoux opened 5 months ago

paulhoux commented 5 months ago

This is a 2D equivalent of CameraUi. It allows users to easily add pan and zoom to their 2D applications. CanvasUi is header-only.

andrewfb commented 4 months ago

Thanks for this - I think it could be a really nice addition to Cinder. Any thoughts on a mode - maybe a constructor parameter - that lets me opt-in to left / right / middle buttons serving as drag? My thought being, for many applications, I'd want the left and potentially right mouse buttons to be free for scene interaction, and the middle button for panning.

Also, thoughts on zoomIn( float percent ) and zoomOut( float percent ) function that a user could easily map to keyboard hotkeys or similar?

paulhoux commented 3 months ago

Those are good suggestions. Will make changes as soon as time permits.

paulhoux commented 3 months ago

Actually, while the suggestions are good, they are not straightforward to implement. I based this implementation upon the CameraUi, which also doesn't support custom mouse/keyboard mappings. If I am going to add that to CanvasUi, we should also add it to CameraUi and come up with a struct of some kind which handles the mapping.

Regarding zoomIn and zoomOut: scaling is done relative to the last recorded mouse position. Keeping track of this position is the most important part of CanvasUi, as it requires conversion to and from window position. Adding keyboard shortcuts to zoom in or out would not be sufficient: to use this intuitively would also require us to reposition the scale origin to the center of the window (I think). At the very least, we would have to think about the desired behavior.

Again, the CameraUi does not have support for assigning keys and mouse buttons. Can't we accept CanvasUi as-is and add those features later if needed?