From bececa4461e76e16b09632b9203a516dfd3bc65d Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Sat, 3 Oct 2015 16:37:03 +0200 Subject: [PATCH] Added visual axis values to "user_input" example See https://github.com/PistonDevelopers/piston-examples/issues/310 --- user_input/Cargo.toml | 4 ++-- user_input/src/main.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/user_input/Cargo.toml b/user_input/Cargo.toml index 57b54b2..76a4c34 100644 --- a/user_input/Cargo.toml +++ b/user_input/Cargo.toml @@ -14,7 +14,7 @@ piston2d-opengl_graphics = "0.14.0" piston2d-graphics = "0.10.0" [dependencies.pistoncore-sdl2_window] -version = "0.15.0" +version = "0.15.1" optional = true [dependencies.pistoncore-glfw_window] @@ -26,7 +26,7 @@ version = "0.14.0" optional = true [features] -default = ["include_glutin"] +default = ["include_sdl2"] include_sdl2 = ["pistoncore-sdl2_window"] include_glfw = ["pistoncore-glfw_window"] include_glutin = ["pistoncore-glutin_window"] diff --git a/user_input/src/main.rs b/user_input/src/main.rs index 735bc85..6b670ba 100644 --- a/user_input/src/main.rs +++ b/user_input/src/main.rs @@ -11,6 +11,7 @@ extern crate glutin_window; use opengl_graphics::{ GlGraphics, OpenGL }; use graphics::{ Context, Graphics }; use std::rc::Rc; +use std::collections::HashMap; use std::cell::RefCell; use piston::window::{ AdvancedWindow, WindowSettings }; use piston::input::*; @@ -22,6 +23,8 @@ use glfw_window::GlfwWindow as Window; #[cfg(feature = "include_glutin")] use glutin_window::GlutinWindow as Window; +type AxisValues = HashMap<(i32, u8), f64>; + fn main() { let opengl = OpenGL::V3_2; let window: Window = WindowSettings::new("piston-example-user_input", [600, 600]) @@ -33,6 +36,9 @@ fn main() { let window = Rc::new(RefCell::new(window)); let ref mut gl = GlGraphics::new(opengl); let mut cursor = [0.0, 0.0]; + + let mut axis_values: AxisValues = HashMap::new(); + for e in window.clone().events() { if let Some(Button::Mouse(button)) = e.press_args() { println!("Pressed mouse button '{:?}'", button); @@ -53,6 +59,9 @@ fn main() { Button::Joystick(button) => println!("Released joystick button '{:?}'", button), } }; + if let Some(args) = e.joystick_axis_args() { + axis_values.insert((args.id, args.axis), args.position); + } e.mouse_cursor(|x, y| { cursor = [x, y]; println!("Mouse moved '{} {}'", x, y); @@ -73,6 +82,7 @@ fn main() { gl.draw(args.viewport(), |c, g| { graphics::clear([1.0; 4], g); draw_rectangles(cursor, &window.borrow(), &c, g); + draw_axis_values(&mut axis_values, &window.borrow(), &c, g); } ); } @@ -123,3 +133,29 @@ fn draw_rectangles( ], &c.draw_state, c.transform, g); } + +fn draw_axis_values( + axis_values: &mut AxisValues, + window: &Window, + c: &Context, + g: &mut G +) { + use piston::window::Window; + + let window_height = window.size().height as f64; + let max_axis_height = 200.0; + let offset = 10.0; + let top = window_height - (max_axis_height + offset); + let color = [1.0, 0.0, 0.0, 1.0]; + let width = 10.0; + let mut draw = |i, v: f64| { + let i = i as f64; + let height = (v + 1.0) / 2.0 * max_axis_height; + let rect = [offset + i * (width + offset), + top + max_axis_height - height, width, height]; + graphics::rectangle(color, rect, c.transform, g); + }; + for (i, &v) in axis_values.values().enumerate() { + draw(i, v); + } +}