Implement horizontal scrolling and pinch-to-zoom

This commit is contained in:
Patrick Walton 2013-06-08 18:15:37 -07:00
parent 0bbf2fc30a
commit 1fbfd7d45e
3 changed files with 21 additions and 12 deletions

View file

@ -290,12 +290,11 @@ fn run_main_loop(port: Port<Msg>,
// When the user pinch-zooms, scale the layer // When the user pinch-zooms, scale the layer
do window.set_zoom_callback |delta| { do window.set_zoom_callback |magnification| {
let zoom_const = 0.01;
let old_world_zoom = *world_zoom; let old_world_zoom = *world_zoom;
// Determine zoom amount // Determine zoom amount
*world_zoom = (*world_zoom + delta.y * zoom_const).max(&1.0); *world_zoom = (*world_zoom * magnification).max(&1.0);
// Update world offset // Update world offset
let corner_to_center_x = world_offset.x + window_size.width as f32 / 2f32; let corner_to_center_x = world_offset.x + window_size.width as f32 / 2f32;

View file

@ -94,16 +94,18 @@ impl WindowMethods<Application> for Window {
window.handle_mouse(button, state, x, y); window.handle_mouse(button, state, x, y);
} }
} }
do glut::mouse_wheel_func |button, direction, x, y| { do glut::mouse_wheel_func |wheel, direction, x, y| {
let delta = if HAVE_PRECISE_MOUSE_WHEEL { let delta = if HAVE_PRECISE_MOUSE_WHEEL {
(direction as f32) / 10000.0 (direction as f32) / 10000.0
} else { } else {
(direction as f32) * 30.0 (direction as f32) * 30.0
}; };
println(fmt!("delta is %f", delta as float)); match wheel {
1 => window.handle_scroll(Point2D(delta, 0.0)),
window.handle_scroll(delta); 2 => window.handle_zoom(delta),
_ => window.handle_scroll(Point2D(0.0, delta)),
}
} }
machack::perform_scroll_wheel_hack(); machack::perform_scroll_wheel_hack();
@ -170,12 +172,12 @@ impl Window {
12 => self.load_url(), // Ctrl+L 12 => self.load_url(), // Ctrl+L
k if k == ('=' as u8) && (glut::get_modifiers() & ACTIVE_CTRL) != 0 => { // Ctrl++ k if k == ('=' as u8) && (glut::get_modifiers() & ACTIVE_CTRL) != 0 => { // Ctrl++
for self.zoom_callback.each |&callback| { for self.zoom_callback.each |&callback| {
callback(Point2D(0.0, 20.0)); callback(0.1);
} }
} }
k if k == 31 && (glut::get_modifiers() & ACTIVE_CTRL) != 0 => { // Ctrl+- k if k == 31 && (glut::get_modifiers() & ACTIVE_CTRL) != 0 => { // Ctrl+-
for self.zoom_callback.each |&callback| { for self.zoom_callback.each |&callback| {
callback(Point2D(0.0, -20.0)); callback(-0.1);
} }
} }
_ => {} _ => {}
@ -217,10 +219,18 @@ impl Window {
} }
/// Helper function to handle a scroll. /// Helper function to handle a scroll.
fn handle_scroll(&mut self, delta: f32) { fn handle_scroll(&mut self, delta: Point2D<f32>) {
match self.scroll_callback { match self.scroll_callback {
None => {} None => {}
Some(callback) => callback(Point2D(0.0, delta)), Some(callback) => callback(delta),
}
}
/// Helper function to handle a zoom.
fn handle_zoom(&mut self, magnification: f32) {
match self.zoom_callback {
None => {}
Some(callback) => callback(magnification),
} }
} }

View file

@ -29,7 +29,7 @@ pub type MouseCallback = @fn(WindowMouseEvent);
pub type ScrollCallback = @fn(Point2D<f32>); pub type ScrollCallback = @fn(Point2D<f32>);
///Type of the function that is called when the user zooms. ///Type of the function that is called when the user zooms.
pub type ZoomCallback = @fn(Point2D<f32>); pub type ZoomCallback = @fn(f32);
/// Methods for an abstract Application. /// Methods for an abstract Application.
pub trait ApplicationMethods { pub trait ApplicationMethods {