re_components/
draw_order.rs

1use arrow2_convert::{ArrowDeserialize, ArrowField, ArrowSerialize};
2
3/// Draw order used for the display order of 2D elements.
4///
5/// Higher values are drawn on top of lower values.
6/// An entity can have only a single draw order component.
7/// Within an entity draw order is governed by the order of the components.
8///
9/// Draw order for entities with the same draw order is generally undefined.
10///
11/// This component is a "mono-component". See [the crate level docs](crate) for details.
12///
13/// ```
14/// use re_components::DrawOrder;
15/// use arrow2_convert::field::ArrowField;
16/// use arrow2::datatypes::{DataType, Field};
17///
18/// assert_eq!(DrawOrder::data_type(), DataType::Float32);
19/// ```
20#[derive(Debug, Clone, Copy, ArrowField, ArrowSerialize, ArrowDeserialize)]
21#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
22#[arrow_field(transparent)]
23pub struct DrawOrder(pub f32);
24
25impl DrawOrder {
26    /// Draw order used for images if no draw order was specified.
27    pub const DEFAULT_IMAGE: DrawOrder = DrawOrder(-10.0);
28
29    /// Draw order used for 2D boxes if no draw order was specified.
30    pub const DEFAULT_BOX2D: DrawOrder = DrawOrder(10.0);
31
32    /// Draw order used for 2D lines if no draw order was specified.
33    pub const DEFAULT_LINES2D: DrawOrder = DrawOrder(20.0);
34
35    /// Draw order used for 2D points if no draw order was specified.
36    pub const DEFAULT_POINTS2D: DrawOrder = DrawOrder(30.0);
37}
38
39impl re_log_types::LegacyComponent for DrawOrder {
40    #[inline]
41    fn legacy_name() -> re_log_types::ComponentName {
42        "rerun.draw_order".into()
43    }
44}
45
46impl std::cmp::PartialEq for DrawOrder {
47    #[inline]
48    fn eq(&self, other: &Self) -> bool {
49        self.0.is_nan() && other.0.is_nan() || self.0 == other.0
50    }
51}
52
53impl std::cmp::Eq for DrawOrder {}
54
55impl std::cmp::PartialOrd for DrawOrder {
56    #[inline]
57    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
58        if other == self {
59            Some(std::cmp::Ordering::Equal)
60        } else if other.0.is_nan() || self.0 < other.0 {
61            Some(std::cmp::Ordering::Less)
62        } else {
63            Some(std::cmp::Ordering::Greater)
64        }
65    }
66}
67
68impl std::cmp::Ord for DrawOrder {
69    #[inline]
70    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
71        self.partial_cmp(other).unwrap()
72    }
73}
74
75impl From<f32> for DrawOrder {
76    #[inline]
77    fn from(value: f32) -> Self {
78        Self(value)
79    }
80}
81
82impl From<DrawOrder> for f32 {
83    #[inline]
84    fn from(value: DrawOrder) -> Self {
85        value.0
86    }
87}
88
89re_log_types::component_legacy_shim!(DrawOrder);