-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathinput_mouse_local_position.rs
57 lines (45 loc) · 1.41 KB
/
input_mouse_local_position.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
use notan::draw::*;
use notan::math::Vec2;
use notan::prelude::*;
#[derive(Default, AppState)]
struct State {
rot: f32,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(State::default)
.add_config(DrawConfig)
.draw(draw)
.build()
}
fn draw(app: &mut App, gfx: &mut Graphics, state: &mut State) {
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
{
// rectangle's size
let size = (400.0, 300.0);
// get the draw builder
let mut rect = draw.rect((0.0, 0.0), size);
// set the rectangle's transformation
rect.rotate_from((size.0 * 0.5, size.1 * 0.5), state.rot)
.translate(200.0, 150.0);
// get the local position based on the current projection + matrix
let local = rect.screen_to_local_position(app.mouse.x, app.mouse.y);
// if it's in bound set color red otherwise white
let color = rect_color(local, size);
rect.color(color);
}
// rotate the rectangle
state.rot += app.timer.delta_f32();
gfx.render(&draw);
}
// Set the color to red if the mouse is inside the bounds of the matrix
fn rect_color(local: Vec2, size: (f32, f32)) -> Color {
let (width, height) = size;
let in_bounds = local.x >= 0.0 && local.x <= width && local.y >= 0.0 && local.y <= height;
if in_bounds {
Color::RED
} else {
Color::WHITE
}
}