Skip to main content

dear_implot/
axis_types.rs

1use crate::sys;
2
3pub(crate) const IMPLOT_AUTO: i32 = -1;
4
5/// Choice of Y axis for multi-axis plots
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7#[repr(u32)]
8pub enum YAxisChoice {
9    First = 0,
10    Second = 1,
11    Third = 2,
12}
13
14/// Convert an Option<YAxisChoice> into an i32. Picks IMPLOT_AUTO for None.
15pub(crate) fn y_axis_choice_option_to_i32(y_axis_choice: Option<YAxisChoice>) -> i32 {
16    match y_axis_choice {
17        Some(choice) => choice as i32,
18        None => IMPLOT_AUTO,
19    }
20}
21
22/// X axis selector matching ImPlot's ImAxis values
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24#[repr(i32)]
25pub enum XAxis {
26    X1 = 0,
27    X2 = 1,
28    X3 = 2,
29}
30
31/// Y axis selector matching ImPlot's ImAxis values
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33#[repr(i32)]
34pub enum YAxis {
35    Y1 = 3,
36    Y2 = 4,
37    Y3 = 5,
38}
39
40impl YAxis {
41    /// Convert a Y axis (Y1..Y3) to the 0-based index used by ImPlotPlot_YAxis_Nil
42    pub(crate) fn to_index(self) -> i32 {
43        (self as i32) - 3
44    }
45}
46
47/// Any ImPlot axis selector matching ImPlot's ImAxis values.
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
49#[repr(i32)]
50pub enum Axis {
51    X1 = 0,
52    X2 = 1,
53    X3 = 2,
54    Y1 = 3,
55    Y2 = 4,
56    Y3 = 5,
57}
58
59impl Axis {
60    pub(crate) fn to_sys(self) -> sys::ImAxis {
61        self as sys::ImAxis
62    }
63}
64
65impl From<XAxis> for Axis {
66    fn from(axis: XAxis) -> Self {
67        match axis {
68            XAxis::X1 => Self::X1,
69            XAxis::X2 => Self::X2,
70            XAxis::X3 => Self::X3,
71        }
72    }
73}
74
75impl From<YAxis> for Axis {
76    fn from(axis: YAxis) -> Self {
77        match axis {
78            YAxis::Y1 => Self::Y1,
79            YAxis::Y2 => Self::Y2,
80            YAxis::Y3 => Self::Y3,
81        }
82    }
83}