veloren_common/comp/
gizmos.rs

1//! Gizmos are used to visually debug server-side values.
2
3use enum_map::EnumMap;
4use serde::{Deserialize, Serialize};
5use specs::{Component, DenseVecStorage};
6use vek::{Rgba, Sphere, Vec3};
7
8use crate::{resources::Time, uid::Uid};
9
10#[derive(Serialize, Deserialize, Debug, Clone)]
11pub enum Shape {
12    Sphere(Sphere<f32, f32>),
13    LineStrip(Vec<Vec3<f32>>),
14}
15
16#[derive(Serialize, Deserialize, Debug, Clone)]
17pub struct Gizmos {
18    pub shape: Shape,
19    pub color: Rgba<u8>,
20    /// If `None`, this ends when the next batch of gizmos are recieved.
21    pub end_time: Option<Time>,
22}
23
24impl Gizmos {
25    pub fn sphere(pos: Vec3<f32>, radius: f32, color: impl Into<Rgba<u8>>) -> Self {
26        Self {
27            shape: Shape::Sphere(Sphere {
28                center: pos,
29                radius,
30            }),
31            color: color.into(),
32            end_time: None,
33        }
34    }
35
36    pub fn line(a: Vec3<f32>, b: Vec3<f32>, color: impl Into<Rgba<u8>>) -> Self {
37        Self {
38            shape: Shape::LineStrip(vec![a, b]),
39            color: color.into(),
40            end_time: None,
41        }
42    }
43
44    pub fn line_strip(points: Vec<Vec3<f32>>, color: impl Into<Rgba<u8>>) -> Self {
45        Self {
46            shape: Shape::LineStrip(points),
47            color: color.into(),
48            end_time: None,
49        }
50    }
51
52    /// If specified, this shape will be displayed up to `time`.
53    ///
54    /// If not specified this will end the next time a batch of gizmos are sent.
55    pub fn with_end_time(mut self, time: Time) -> Self {
56        self.end_time = Some(time);
57        self
58    }
59}
60
61#[derive(
62    Serialize,
63    Deserialize,
64    strum::EnumString,
65    strum::Display,
66    strum::EnumIter,
67    PartialEq,
68    enum_map::Enum,
69    Clone,
70    Copy,
71)]
72pub enum GizmoSubscription {
73    PathFinding,
74}
75
76#[derive(Default, Clone)]
77pub enum GizmoContext {
78    #[default]
79    Disabled,
80    Enabled,
81    /// If applicable, only draw debug information with this specific target.
82    EnabledWithTarget(Uid),
83}
84
85/// A component to subscribe to certain visual debug information.
86pub struct GizmoSubscriber {
87    pub gizmos: EnumMap<GizmoSubscription, GizmoContext>,
88    /// The range of the debug drawing, in world space.
89    pub range: f32,
90}
91
92impl Default for GizmoSubscriber {
93    fn default() -> Self {
94        GizmoSubscriber {
95            gizmos: EnumMap::default(),
96            range: 32.0,
97        }
98    }
99}
100
101impl Component for GizmoSubscriber {
102    type Storage = DenseVecStorage<GizmoSubscriber>;
103}