Piccolo Snippets
From eqqon
(Difference between revisions)
(New page: = Piccolo Nodes = == PComposite == The PComposite node and all of it's Children act like a single PNode. All user interaction, even if it happened on a child node, is handled by the compo...) |
|||
Line 1: | Line 1: | ||
- | = Piccolo | + | = Useful Piccolo Extensions = |
+ | == MouseWheelScrollController == | ||
+ | This class replaces Piccolo's default zoom event handler which is not very attractive with a mouse wheel zoom event handler. Just instanciate it with your canvas' camera as parameter and mouse wheel zooming just works! | ||
- | == | + | <pre> |
- | + | public class MouseWheelZoomController | |
+ | { | ||
+ | public static float MIN_SCALE = .0001f; | ||
+ | public static float MAX_SCALE = 2500; | ||
+ | PCamera camera; | ||
+ | |||
+ | public MouseWheelZoomController(PCamera camera) | ||
+ | { | ||
+ | this.camera = camera; | ||
+ | camera.Canvas.ZoomEventHandler = null; | ||
+ | camera.MouseWheel += OnMouseWheel; | ||
+ | } | ||
+ | |||
+ | public void OnMouseWheel(object o, PInputEventArgs ea) | ||
+ | { | ||
+ | float currentScale = camera.ViewScale; | ||
+ | float scaleDelta = (1.0f + (0.001f * ea.WheelDelta)); | ||
+ | float newScale = currentScale * scaleDelta; | ||
+ | if (newScale < MIN_SCALE) | ||
+ | { | ||
+ | camera.ViewScale = MIN_SCALE; | ||
+ | return; | ||
+ | } | ||
+ | if ((MAX_SCALE > 0) && (newScale > MAX_SCALE)) | ||
+ | { | ||
+ | camera.ViewScale = MAX_SCALE; | ||
+ | return; | ||
+ | } | ||
+ | PointF pos = ea.Position; | ||
+ | camera.ScaleViewBy(scaleDelta, pos.X, pos.Y); | ||
+ | } | ||
+ | } | ||
+ | </pre> |
Revision as of 21:31, 28 October 2007
Useful Piccolo Extensions
MouseWheelScrollController
This class replaces Piccolo's default zoom event handler which is not very attractive with a mouse wheel zoom event handler. Just instanciate it with your canvas' camera as parameter and mouse wheel zooming just works!
public class MouseWheelZoomController { public static float MIN_SCALE = .0001f; public static float MAX_SCALE = 2500; PCamera camera; public MouseWheelZoomController(PCamera camera) { this.camera = camera; camera.Canvas.ZoomEventHandler = null; camera.MouseWheel += OnMouseWheel; } public void OnMouseWheel(object o, PInputEventArgs ea) { float currentScale = camera.ViewScale; float scaleDelta = (1.0f + (0.001f * ea.WheelDelta)); float newScale = currentScale * scaleDelta; if (newScale < MIN_SCALE) { camera.ViewScale = MIN_SCALE; return; } if ((MAX_SCALE > 0) && (newScale > MAX_SCALE)) { camera.ViewScale = MAX_SCALE; return; } PointF pos = ea.Position; camera.ScaleViewBy(scaleDelta, pos.X, pos.Y); } }