pub struct MlxWindow { /* private fields */ }
Expand description
Hook api holder. Needed in most Mlx methods.
With hooks, you can provide closures that will run when an event occurs.
The mlx.event_loop method should run for these hooks to be executed.
Implementations§
Source§impl MlxWindow
impl MlxWindow
Sourcepub fn mouse_hook<F, Args>(&self, cb: F, args: &'static Args)
pub fn mouse_hook<F, Args>(&self, cb: F, args: &'static Args)
Hook running whenever a mouse event is received.
F should be a closure taking 4 arguments: the buttons, x, y and the data you provide as last argument of the mouse_hook call.
Usage:
let arg = (2, 3);
window.mouse_hook(|buttons, x, y, args| {
println!("{} {}, {}, ({}, {})", buttons, x, y, args.0, args.1);
}, &arg);
Sourcepub fn key_hook<F, Args>(&self, cb: F, args: &'static Args)
pub fn key_hook<F, Args>(&self, cb: F, args: &'static Args)
Hook running whenever a key event is received.
F should be a closure taking 2 arguments: the keycode and the data you provide as last argument of the mouse_hook call.
Usage:
let arg = (2, 3);
window.key_hook(|keycode, args| {
println!("{}, ({}, {})", keycode, args.0, args.1);
}, &arg);
Examples found in repository?
6fn main() {
7 let mlx = Mlx::new().unwrap();
8
9 let width = 1080;
10 let height = 720;
11 let window = mlx.new_window(width, height, "Mlx example").unwrap();
12
13 let image = match mlx.new_image(width, height) {
14 Ok(img) => img,
15 Err(e) => match e {
16 MlxError::Any(s) => return println!("{}", s),
17 _ => return,
18 },
19 };
20
21 let _bytes_per_pixel = image.bits_per_pixel / 8;
22 let offset = image.size_line / 2 + image.size_line * height / 2;
23 for i in 1..100 {
24 // we assume there are 32 bits per pixel here, because its just an example.
25 // color encoding can go from 8 bits to 48 bits.
26 let offset = offset + i * 4;
27 match image.endian {
28 Endian::Little => {
29 image.write_to(offset + 2, 0xff); // R
30 image.write_to(offset + 1, 0x0); // G
31 image.write_to(offset, 0x0); // B
32 }
33 Endian::Big => {
34 image.write_to(offset, 0xff); // R
35 image.write_to(offset + 1, 0x0); // G
36 image.write_to(offset + 2, 0x0); // B
37 }
38 }
39 }
40
41 window.key_hook(
42 move |keycode, _| {
43 // you can also check keycodes using the `xev` command
44 println!("{}", keycode);
45
46 // `q`
47 if keycode == 113 {
48 process::exit(0);
49 // Enter
50 } else if keycode == 97 {
51 let x = width / 2;
52 let y = height / 2;
53 let color = 0xffffff;
54 for i in 0..50 {
55 mlx.pixel_put(&window, x + i, y + i, color);
56 }
57 } else if keycode == 98 {
58 mlx.put_image_to_window(&window, &image, 0, 0);
59 }
60 },
61 &(),
62 );
63
64 // this will loop forever
65 mlx.event_loop();
66}
Sourcepub fn expose_hook<F, Args>(&self, cb: F, args: &'static Args)where
F: FnMut(&'static Args) + 'static,
pub fn expose_hook<F, Args>(&self, cb: F, args: &'static Args)where
F: FnMut(&'static Args) + 'static,
Hook running whenever an ‘expose’ event is received.
F should be a closure taking the data you pass as an argument.
Usage:
let arg = (2, 3);
window.expose_hook(|args| {
println!("({}, {})", args.0, args.1);
}, &arg);
The expose hook is running whenever the window or part of it should be redrawn.
Sourcepub fn loop_hook<F, Args>(&self, cb: F, args: &'static Args)where
F: FnMut(&'static Args) + 'static,
pub fn loop_hook<F, Args>(&self, cb: F, args: &'static Args)where
F: FnMut(&'static Args) + 'static,
Hook running when no event occurs.
F should be a closure taking the data you pass as an argument.
Usage:
let arg = (2, 3);
window.loop_hook(|args| {
println!("({}, {})", args.0, args.1);
}, &arg);
Sourcepub fn hook<F, Args>(
&self,
x_event: i32,
x_mask: i32,
cb: F,
args: &'static Args,
)where
F: FnMut(&'static Args) + 'static,
pub fn hook<F, Args>(
&self,
x_event: i32,
x_mask: i32,
cb: F,
args: &'static Args,
)where
F: FnMut(&'static Args) + 'static,
Hook running whenever the event you specify occurs.
F should be a closure taking the data you pass as an argument.
Usage:
let arg = (2, 3);
let x_event = 2; // keypress
let x_mask = 0; // no mask
window.hook(x_event, x_mask, |args| {
println!("({}, {})", args.0, args.1);
}, &arg);
You can find informations on x events in /usr/include/X11/X.h
around line 180 and x event masks around line 150.