rust_minifb/examples/title_cursor.rs

136 lines
3.2 KiB
Rust
Raw Normal View History

2016-06-24 21:08:01 +10:00
extern crate minifb;
2019-11-27 18:03:33 +11:00
use minifb::{CursorStyle, Key, MouseMode, Scale, Window, WindowOptions};
2016-06-24 21:08:01 +10:00
const WIDTH: usize = 640;
const HEIGHT: usize = 360;
struct Rect {
x: usize,
y: usize,
width: usize,
height: usize,
color: u32,
cursor_style: CursorStyle,
}
impl Rect {
pub fn is_inside(&self, xf: f32, yf: f32) -> bool {
let x = xf as usize;
let y = yf as usize;
let xe = self.x + self.width;
let ye = self.y + self.height;
2019-11-27 18:03:33 +11:00
if (y >= self.y) && (y <= ye) && (x >= self.x) && (x <= xe) {
2016-06-24 21:08:01 +10:00
true
} else {
false
}
}
}
fn fill_rect(dest: &mut [u32], rect: &Rect) {
for y in 0..rect.height {
for x in 0..rect.width {
dest[((rect.y + y) * WIDTH) + rect.x + x] = rect.color;
}
}
}
fn main() {
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
2019-11-27 18:03:33 +11:00
let mut window = Window::new(
"I haz no title :(",
WIDTH,
HEIGHT,
WindowOptions {
scale: Scale::X2,
..WindowOptions::default()
},
)
.expect("Unable to Open Window");
2016-06-24 21:08:01 +10:00
let rects = [
2019-11-27 18:03:33 +11:00
Rect {
x: 0,
y: 0,
width: 160,
height: 180,
color: 0x00b27474,
cursor_style: CursorStyle::Arrow,
},
Rect {
x: 160,
y: 0,
width: 160,
height: 180,
color: 0x00b28050,
cursor_style: CursorStyle::Ibeam,
},
Rect {
x: 320,
y: 0,
width: 160,
height: 180,
color: 0x00a9b250,
cursor_style: CursorStyle::Crosshair,
},
Rect {
x: 480,
y: 0,
width: 160,
height: 180,
color: 0x0060b250,
cursor_style: CursorStyle::ClosedHand,
},
Rect {
x: 0,
y: 180,
width: 160,
height: 180,
color: 0x004fb292,
cursor_style: CursorStyle::OpenHand,
},
Rect {
x: 160,
y: 180,
width: 160,
height: 180,
color: 0x004f71b2,
cursor_style: CursorStyle::ResizeLeftRight,
},
Rect {
x: 320,
y: 180,
width: 160,
height: 180,
color: 0x008850b2,
cursor_style: CursorStyle::ResizeUpDown,
},
Rect {
x: 480,
y: 180,
width: 160,
height: 180,
color: 0x00b25091,
cursor_style: CursorStyle::ResizeAll,
},
2016-06-24 21:08:01 +10:00
];
2016-07-26 21:43:45 +10:00
window.set_title("Different cursor on each color region");
2016-06-24 21:08:01 +10:00
while window.is_open() && !window.is_key_down(Key::Escape) {
let (mx, my) = window.get_mouse_pos(MouseMode::Clamp).unwrap();
for rect in &rects {
fill_rect(&mut buffer, rect);
if rect.is_inside(mx, my) {
window.set_cursor_style(rect.cursor_style);
}
}
// We unwrap here as we want this code to exit if it fails
window.update_with_buffer(&buffer).unwrap();
2016-06-24 21:08:01 +10:00
}
}