use bevy::{ecs::component::Component, reflect::Reflect};
#[derive(Debug, Component, Copy, Clone, Reflect)]
pub struct RotationConstraints {
pub allow_x: bool,
pub allow_y: bool,
pub allow_z: bool,
}
impl Default for RotationConstraints {
fn default() -> Self {
Self::allow()
}
}
impl RotationConstraints {
#[must_use]
pub fn lock() -> Self {
Self {
allow_x: false,
allow_y: false,
allow_z: false,
}
}
#[must_use]
pub fn allow() -> Self {
Self {
allow_x: true,
allow_y: true,
allow_z: true,
}
}
#[must_use]
pub fn is_lock(&self) -> bool {
!self.allow_x && !self.allow_y && !self.allow_z
}
#[must_use]
pub fn is_allow(&self) -> bool {
self.allow_x && self.allow_y && self.allow_z
}
#[must_use]
pub fn restrict_to_x_only() -> Self {
Self {
allow_x: true,
allow_y: false,
allow_z: false,
}
}
#[must_use]
pub fn restrict_to_y_only() -> Self {
Self {
allow_x: false,
allow_y: true,
allow_z: false,
}
}
#[must_use]
pub fn restrict_to_z_only() -> Self {
Self {
allow_x: false,
allow_y: false,
allow_z: true,
}
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[test]
fn is_lock() {
assert!(RotationConstraints::lock().is_lock());
}
#[rstest]
fn is_not_lock(
#[values(
RotationConstraints::allow(),
RotationConstraints { allow_x: false, ..RotationConstraints::allow() },
RotationConstraints { allow_y: false, ..RotationConstraints::allow() },
RotationConstraints { allow_z: false, ..RotationConstraints::allow() },
)]
constraints: RotationConstraints,
) {
assert!(!constraints.is_lock());
}
#[test]
fn is_allow() {
assert!(RotationConstraints::allow().is_allow());
}
#[rstest]
fn is_not_allow(
#[values(
RotationConstraints::lock(),
RotationConstraints { allow_x: true, ..RotationConstraints::lock() },
RotationConstraints { allow_y: true, ..RotationConstraints::lock() },
RotationConstraints { allow_z: true, ..RotationConstraints::lock() },
)]
constraints: RotationConstraints,
) {
assert!(!constraints.is_allow());
}
}