SVC16/src/ui.rs
JanNeuendorf 6abfcbc811
Macroquad port (#10)
I ported the gui to the macroquad crate.
This has the following advantages:

    We can easily draw text. This means we can show the number of instructions on screen.
    The filter mode can be set to linear for a different look.
    The dependencies are a lot cleaner now. Just depending on the latest stable version of macroquad makes it a lot more likely that the app builds properly on different platforms (and will continue to do so in the future).

I also use the gilrs crate directly for the gamepad input.
2025-01-05 16:36:51 +01:00

48 lines
1.5 KiB
Rust

use macroquad::prelude::*;
pub struct Layout {
pub x: f32,
pub y: f32,
pub size: f32,
pub font_y: f32,
pub font_x: f32,
pub rect_x: f32,
pub rect_y: f32,
pub font_size: f32,
}
impl Layout {
pub fn generate(linear: bool) -> Self {
let (width, height) = (screen_width(), screen_height());
let minsize = width.min(height);
let image_size = match linear {
false => ((minsize / 256.).floor() * 256.).max(256.),
true => minsize.max(256.),
};
let x = (0. as f32).max((width - image_size) / 2.);
let y = (0. as f32).max((height - image_size) / 2.);
let font_y = y + image_size / 15.;
Self {
x,
y,
size: image_size,
font_y,
font_x: x + 0.01 * image_size,
rect_x: x + 0.005 * image_size,
rect_y: y + 0.01 * image_size,
font_size: image_size / 15.,
}
}
pub fn clamp_mouse(&self) -> (f32, f32) {
let (raw_x, raw_y) = mouse_position();
let clamped_x = (raw_x.clamp(self.x, self.x + self.size) - self.x) / self.size * 255.;
let clamped_y = (raw_y.clamp(self.y, self.y + self.size) - self.y) / self.size * 255.;
(clamped_x, clamped_y)
}
pub fn cursor_in_window(&self) -> bool {
let mp = mouse_position();
mp.0 >= self.x
&& mp.0 < (self.x + self.size)
&& mp.1 >= self.y
&& mp.1 < (self.y + self.size)
}
}