Function macroquad::input::mouse_wheel
source · pub fn mouse_wheel() -> (f32, f32)
Examples found in repository?
examples/events.rs (line 13)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
async fn main() {
loop {
clear_background(WHITE);
root_ui().window(hash!(), Vec2::new(20., 20.), Vec2::new(450., 200.), |ui| {
let (mouse_x, mouse_y) = mouse_position();
ui.label(None, &format!("Mouse position: {} {}", mouse_x, mouse_y));
let (mouse_wheel_x, mouse_wheel_y) = mouse_wheel();
ui.label(None, &format!("Mouse wheel x: {}", mouse_wheel_x));
ui.label(None, &format!("Mouse wheel y: {}", mouse_wheel_y));
widgets::Group::new(hash!(), Vec2::new(200., 90.))
.position(Vec2::new(240., 0.))
.ui(ui, |ui| {
ui.label(None, "Pressed kbd keys");
if let Some(key) = get_last_key_pressed() {
ui.label(None, &format!("{:?}", key))
}
});
widgets::Group::new(hash!(), Vec2::new(200., 90.))
.position(Vec2::new(240., 92.))
.ui(ui, |ui| {
ui.label(None, "Pressed mouse keys");
if is_mouse_button_down(MouseButton::Left) {
ui.label(None, "Left");
}
if is_mouse_button_down(MouseButton::Right) {
ui.label(None, "Right");
}
if is_mouse_button_down(MouseButton::Middle) {
ui.label(None, "Middle");
}
});
});
next_frame().await;
}
}
More examples
examples/camera_transformations.rs (line 58)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
async fn main() {
let mut target = (0., 0.);
let mut zoom = 1.0;
let mut rotation = 0.0;
let mut smooth_rotation: f32 = 0.0;
let mut offset = (0., 0.);
loop {
if is_key_down(KeyCode::W) {
target.1 -= 0.1;
}
if is_key_down(KeyCode::S) {
target.1 += 0.1;
}
if is_key_down(KeyCode::A) {
target.0 += 0.1;
}
if is_key_down(KeyCode::D) {
target.0 -= 0.1;
}
if is_key_down(KeyCode::Left) {
offset.0 -= 0.1;
}
if is_key_down(KeyCode::Right) {
offset.0 += 0.1;
}
if is_key_down(KeyCode::Up) {
offset.1 += 0.1;
}
if is_key_down(KeyCode::Down) {
offset.1 -= 0.1;
}
#[cfg(not(target_arch = "wasm32"))]
if is_key_down(KeyCode::Q) | is_key_down(KeyCode::Escape) {
break;
}
match mouse_wheel() {
(_x, y) if y != 0.0 => {
// Normalize mouse wheel values is browser (chromium: 53, firefox: 3)
#[cfg(target_arch = "wasm32")]
let y = if y < 0.0 {
-1.0
} else if y > 0.0 {
1.0
} else {
0.0
};
if is_key_down(KeyCode::LeftControl) {
zoom *= 1.1f32.powf(y);
} else {
rotation += 10.0 * y;
rotation = match rotation {
angle if angle >= 360.0 => angle - 360.0,
angle if angle < 0.0 => angle + 360.0,
angle => angle,
}
}
}
_ => (),
}
smooth_rotation = angle_lerp(smooth_rotation, rotation, 0.1);
clear_background(LIGHTGRAY);
set_camera(&Camera2D {
target: vec2(target.0, target.1),
..Default::default()
});
draw_cross(0., 0., RED);
set_camera(&Camera2D {
target: vec2(target.0, target.1),
rotation: smooth_rotation,
..Default::default()
});
draw_cross(0., 0., GREEN);
set_camera(&Camera2D {
target: vec2(target.0, target.1),
rotation: smooth_rotation,
zoom: vec2(zoom, zoom * screen_width() / screen_height()),
..Default::default()
});
draw_cross(0., 0., BLUE);
set_camera(&Camera2D {
target: vec2(target.0, target.1),
rotation: smooth_rotation,
zoom: vec2(zoom, zoom * screen_width() / screen_height()),
offset: vec2(offset.0, offset.1),
..Default::default()
});
// Render some primitives in camera space
draw_line(-0.4, 0.4, -0.8, 0.9, 0.05, BLUE);
draw_rectangle(-0.3, 0.3, 0.2, 0.2, GREEN);
draw_circle(0., 0., 0.1, YELLOW);
// Back to screen space, render some text
set_default_camera();
draw_text(
format!("target (WASD keys) = ({:+.2}, {:+.2})", target.0, target.1).as_str(),
10.0,
10.0,
15.0,
BLACK,
);
draw_text(
format!("rotation (mouse wheel) = {} degrees", rotation).as_str(),
10.0,
25.0,
15.0,
BLACK,
);
draw_text(
format!("zoom (ctrl + mouse wheel) = {:.2}", zoom).as_str(),
10.0,
40.0,
15.0,
BLACK,
);
draw_text(
format!("offset (arrow keys) = ({:+.2}, {:+.2})", offset.0, offset.1).as_str(),
10.0,
55.0,
15.0,
BLACK,
);
draw_text("HELLO", 30.0, 200.0, 30.0, BLACK);
next_frame().await
}
}