-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_test.rs
135 lines (116 loc) · 3.93 KB
/
input_test.rs
1
2
3
4
5
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
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
//! Example that just prints out all the input events.
use ggez;
use ggez::event::{self, Axis, Button, GamepadId, KeyCode, KeyMods, MouseButton};
use ggez::graphics::{self, DrawMode};
use ggez::input;
use ggez::{Context, GameResult};
struct MainState {
pos_x: f32,
pos_y: f32,
mouse_down: bool,
}
impl MainState {
fn new() -> MainState {
MainState {
pos_x: 100.0,
pos_y: 100.0,
mouse_down: false,
}
}
}
impl event::EventHandler for MainState {
fn update(&mut self, ctx: &mut Context) -> GameResult {
if input::keyboard::is_key_pressed(ctx, KeyCode::A) {
println!("The A key is pressed");
if input::keyboard::is_mod_active(ctx, input::keyboard::KeyMods::SHIFT) {
println!("The shift key is held too.");
}
println!(
"Full list of pressed keys: {:?}",
input::keyboard::pressed_keys(ctx)
);
}
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult {
graphics::clear(ctx, [0.1, 0.2, 0.3, 1.0].into());
let rectangle = graphics::Mesh::new_rectangle(
ctx,
DrawMode::fill(),
graphics::Rect {
x: self.pos_x,
y: self.pos_y,
w: 400.0,
h: 300.0,
},
graphics::WHITE,
)?;
graphics::draw(ctx, &rectangle, (ggez::nalgebra::Point2::new(0.0, 0.0),))?;
graphics::present(ctx)?;
Ok(())
}
fn mouse_button_down_event(&mut self, _ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
self.mouse_down = true;
println!("Mouse button pressed: {:?}, x: {}, y: {}", button, x, y);
}
fn mouse_button_up_event(&mut self, _ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
self.mouse_down = false;
println!("Mouse button released: {:?}, x: {}, y: {}", button, x, y);
}
fn mouse_motion_event(&mut self, _ctx: &mut Context, x: f32, y: f32, xrel: f32, yrel: f32) {
if self.mouse_down {
self.pos_x = x;
self.pos_y = y;
}
println!(
"Mouse motion, x: {}, y: {}, relative x: {}, relative y: {}",
x, y, xrel, yrel
);
}
fn mouse_wheel_event(&mut self, _ctx: &mut Context, x: f32, y: f32) {
println!("Mousewheel event, x: {}, y: {}", x, y);
}
fn key_down_event(
&mut self,
_ctx: &mut Context,
keycode: KeyCode,
keymod: KeyMods,
repeat: bool,
) {
println!(
"Key pressed: {:?}, modifier {:?}, repeat: {}",
keycode, keymod, repeat
);
}
fn key_up_event(&mut self, _ctx: &mut Context, keycode: KeyCode, keymod: KeyMods) {
println!("Key released: {:?}, modifier {:?}", keycode, keymod);
}
fn text_input_event(&mut self, _ctx: &mut Context, ch: char) {
println!("Text input: {}", ch);
}
fn gamepad_button_down_event(&mut self, _ctx: &mut Context, btn: Button, id: GamepadId) {
println!("Gamepad button pressed: {:?} Gamepad_Id: {:?}", btn, id);
}
fn gamepad_button_up_event(&mut self, _ctx: &mut Context, btn: Button, id: GamepadId) {
println!("Gamepad button released: {:?} Gamepad_Id: {:?}", btn, id);
}
fn gamepad_axis_event(&mut self, _ctx: &mut Context, axis: Axis, value: f32, id: GamepadId) {
println!(
"Axis Event: {:?} Value: {} Gamepad_Id: {:?}",
axis, value, id
);
}
fn focus_event(&mut self, _ctx: &mut Context, gained: bool) {
if gained {
println!("Focus gained");
} else {
println!("Focus lost");
}
}
}
pub fn main() -> GameResult {
let cb = ggez::ContextBuilder::new("input_test", "ggez");
let (ctx, event_loop) = &mut cb.build()?;
let state = &mut MainState::new();
event::run(ctx, event_loop, state)
}