-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
text_button.rs
42 lines (38 loc) · 1.31 KB
/
text_button.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
use mq::color::WHITE;
use zgui as ui;
mod common;
#[derive(Clone, Copy, Debug)]
enum Message {
Command,
}
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)
}
#[mq::main("ZGui: Text Button 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 = common::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);
gui.draw();
mq::window::next_frame().await;
}
}