Newer
Older
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::fmt::{Display, Formatter};
use bevy::math::{Vec2, Vec3};
use bevy::prelude::Component;
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Horizontal {
Left,
#[default]
Right,
}
impl From<f32> for Horizontal {
fn from(other: f32) -> Self {
if other < 0.0 {
Self::Left
} else {
Self::Right
}
}
}
impl Display for Horizontal {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Horizontal::Left => f.write_str("left"),
Horizontal::Right => f.write_str("right"),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Vertical {
Up,
#[default]
Down,
}
impl From<f32> for Vertical {
fn from(other: f32) -> Self {
if other < 0.0 {
Self::Up
} else {
Self::Down
}
}
}
impl Display for Vertical {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Vertical::Up => f.write_str("up"),
Vertical::Down => f.write_str("down"),
}
}
}
#[derive(Clone, Debug, Component, PartialEq, Eq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Directionality {
pub vertical: Vertical,
pub horizontal: Horizontal,
}
impl From<Vec2> for Directionality {
fn from(other: Vec2) -> Self {
Self {
horizontal: other.x.into(),
vertical: other.y.into(),
}
}
}
impl From<Vec3> for Directionality {
fn from(other: Vec3) -> Self {
Self {
horizontal: other.x.into(),
vertical: other.y.into(),
}
}
}
impl Display for Directionality {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}_{}", self.horizontal, self.vertical)
}
}