2022-03-19 10:38:26 +11:00
|
|
|
//! Utilities for writing VIZIA widgets.
|
|
|
|
|
2022-06-18 09:59:53 +10:00
|
|
|
use vizia::prelude::*;
|
2022-03-19 10:38:26 +11:00
|
|
|
|
|
|
|
/// An extension trait for [`Modifiers`] that adds platform-independent getters.
|
|
|
|
pub trait ModifiersExt {
|
|
|
|
/// Returns true if the Command (on macOS) or Ctrl (on any other platform) key is pressed.
|
|
|
|
fn command(&self) -> bool;
|
|
|
|
|
|
|
|
/// Returns true if the Alt (or Option on macOS) key is pressed.
|
|
|
|
fn alt(&self) -> bool;
|
|
|
|
|
|
|
|
/// Returns true if the Shift key is pressed.
|
|
|
|
fn shift(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModifiersExt for Modifiers {
|
|
|
|
fn command(&self) -> bool {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
let result = self.contains(Modifiers::LOGO);
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
let result = self.contains(Modifiers::CTRL);
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn alt(&self) -> bool {
|
|
|
|
self.contains(Modifiers::ALT)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shift(&self) -> bool {
|
|
|
|
self.contains(Modifiers::SHIFT)
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 11:17:13 +11:00
|
|
|
|
2022-03-19 12:08:00 +11:00
|
|
|
/// Remap a `[0, 1]` value to an x-coordinate within the current entity's bounding box. The value
|
|
|
|
/// will be clamped to `[0, 1]` if it isn't already in that range. This ignores the border width.
|
|
|
|
pub fn remap_current_entity_x_t(cx: &Context, t: f32) -> f32 {
|
2022-06-18 09:59:53 +10:00
|
|
|
let border_width = match cx.style_ref().border_width.get(cx.current()) {
|
|
|
|
Some(Units::Pixels(x)) => *x,
|
2022-03-19 12:08:00 +11:00
|
|
|
_ => 0.0,
|
|
|
|
};
|
2022-06-18 09:59:53 +10:00
|
|
|
let x_pos = cx.cache_ref().get_posx(cx.current()) + border_width;
|
|
|
|
let width = cx.cache_ref().get_width(cx.current()) - (border_width * 2.0);
|
2022-03-19 12:08:00 +11:00
|
|
|
x_pos + (width * t.clamp(0.0, 1.0))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remap a `[0, 1]` value to a y-coordinate within the current entity's bounding box. The value
|
|
|
|
/// will be clamped to `[0, 1]` if it isn't already in that range. This ignores the border width.
|
|
|
|
pub fn remap_current_entity_y_t(cx: &Context, t: f32) -> f32 {
|
2022-06-18 09:59:53 +10:00
|
|
|
let border_width = match cx.style_ref().border_width.get(cx.current()) {
|
|
|
|
Some(Units::Pixels(x)) => *x,
|
2022-03-19 12:08:00 +11:00
|
|
|
_ => 0.0,
|
|
|
|
};
|
2022-06-18 09:59:53 +10:00
|
|
|
let y_pos = cx.cache_ref().get_posy(cx.current()) + border_width;
|
|
|
|
let height = cx.cache_ref().get_height(cx.current()) - (border_width * 2.0);
|
2022-03-19 12:08:00 +11:00
|
|
|
y_pos + (height * t.clamp(0.0, 1.0))
|
|
|
|
}
|
|
|
|
|
2022-03-19 11:17:13 +11:00
|
|
|
/// Remap an x-coordinate to a `[0, 1]` value within the current entity's bounding box. The value
|
2022-03-19 11:40:17 +11:00
|
|
|
/// will be clamped to `[0, 1]` if it isn't already in that range. This ignores the border width.
|
2022-03-19 11:17:13 +11:00
|
|
|
pub fn remap_current_entity_x_coordinate(cx: &Context, x_coord: f32) -> f32 {
|
2022-06-18 09:59:53 +10:00
|
|
|
let border_width = match cx.style_ref().border_width.get(cx.current()) {
|
|
|
|
Some(Units::Pixels(x)) => *x,
|
2022-03-19 11:40:17 +11:00
|
|
|
_ => 0.0,
|
|
|
|
};
|
2022-06-18 09:59:53 +10:00
|
|
|
let x_pos = cx.cache_ref().get_posx(cx.current()) + border_width;
|
|
|
|
let width = cx.cache_ref().get_width(cx.current()) - (border_width * 2.0);
|
2022-03-19 11:17:13 +11:00
|
|
|
((x_coord - x_pos) / width).clamp(0.0, 1.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remap an y-coordinate to a `[0, 1]` value within the current entity's bounding box. The value
|
2022-03-19 11:40:17 +11:00
|
|
|
/// will be clamped to `[0, 1]` if it isn't already in that range. This ignores the border width.
|
2022-03-19 11:17:13 +11:00
|
|
|
pub fn remap_current_entity_y_coordinate(cx: &Context, y_coord: f32) -> f32 {
|
2022-06-18 09:59:53 +10:00
|
|
|
let border_width = match cx.style_ref().border_width.get(cx.current()) {
|
|
|
|
Some(Units::Pixels(x)) => *x,
|
2022-03-19 11:40:17 +11:00
|
|
|
_ => 0.0,
|
|
|
|
};
|
2022-06-18 09:59:53 +10:00
|
|
|
let y_pos = cx.cache_ref().get_posy(cx.current()) + border_width;
|
|
|
|
let height = cx.cache_ref().get_height(cx.current()) - (border_width * 2.0);
|
2022-03-19 11:17:13 +11:00
|
|
|
((y_coord - y_pos) / height).clamp(0.0, 1.0)
|
|
|
|
}
|