-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
pixel_coordinates.rs
67 lines (61 loc) · 1.84 KB
/
pixel_coordinates.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
use mq::{
camera::{set_camera, Camera2D},
color::{RED, WHITE},
math::Rect,
};
use zgui as ui;
mod common;
#[derive(Clone, Copy, Debug)]
enum Message {
Command,
}
pub fn make_and_set_camera(_aspect_ratio: f32) -> Camera2D {
let display_rect = Rect {
x: 0.0,
y: 0.0,
w: mq::window::screen_width(),
h: mq::window::screen_height(),
};
let camera = Camera2D::from_display_rect(display_rect);
set_camera(&camera);
camera
}
fn make_gui(font: mq::text::Font) -> ui::Result<ui::Gui<Message>> {
let mut gui = ui::Gui::new();
let anchor = ui::Anchor(ui::HAnchor::Right, ui::VAnchor::Bottom);
let text = ui::Drawable::text("Button", font);
let button = ui::Button::new(text, 0.2, gui.sender(), Message::Command)?;
gui.add(&ui::pack(button), anchor);
Ok(gui)
}
fn draw_scene() {
let x = 150.0;
let y = 150.0;
let r = 100.0;
let color = RED;
mq::shapes::draw_circle(x, y, r, color);
}
#[mq::main("ZGui: Pixel Coordinates Demo")]
#[macroquad(crate_rename = "mq")]
async fn main() {
let assets = common::Assets::load().await.expect("Can't load assets");
let mut gui = make_gui(assets.font).expect("Can't create the gui");
loop {
// Update the camera and the GUI.
let aspect_ratio = common::aspect_ratio();
let camera = make_and_set_camera(aspect_ratio);
gui.resize_if_needed(aspect_ratio);
// Handle cursor updates.
let pos = common::get_world_mouse_pos(&camera);
gui.move_mouse(pos);
if mq::input::is_mouse_button_pressed(mq::input::MouseButton::Left) {
let message = gui.click(pos);
println!("{:?}", message);
}
// Draw the GUI.
mq::window::clear_background(WHITE);
draw_scene();
gui.draw();
mq::window::next_frame().await;
}
}