Skip to content

Commit

Permalink
Added visual axis values to "user_input" example
Browse files Browse the repository at this point in the history
  • Loading branch information
bvssvni committed Oct 3, 2015
1 parent bbc9aa0 commit bececa4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions user_input/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"]
36 changes: 36 additions & 0 deletions user_input/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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])
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
);
}
Expand Down Expand Up @@ -123,3 +133,29 @@ fn draw_rectangles<G: Graphics>(
],
&c.draw_state, c.transform, g);
}

fn draw_axis_values<G: Graphics>(
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);
}
}

0 comments on commit bececa4

Please sign in to comment.