Skip to main content

dear_imgui_rs/widget/input/numeric/
int_vectors.rs

1use super::super::validation::validate_input_scalar_flags;
2use crate::ui::Ui;
3use crate::{InputScalarFlags, sys};
4
5/// Builder for a 2-component int input widget.
6#[must_use]
7pub struct InputInt2<'ui, 'p, L> {
8    label: L,
9    value: &'p mut [i32; 2],
10    flags: InputScalarFlags,
11    ui: &'ui Ui,
12}
13
14impl<'ui, 'p, L: AsRef<str>> InputInt2<'ui, 'p, L> {
15    /// Constructs a new input int2 builder.
16    #[doc(alias = "InputInt2")]
17    pub fn new(ui: &'ui Ui, label: L, value: &'p mut [i32; 2]) -> Self {
18        InputInt2 {
19            label,
20            value,
21            flags: InputScalarFlags::empty(),
22            ui,
23        }
24    }
25
26    /// Sets the input text flags
27    #[inline]
28    pub fn flags(mut self, flags: InputScalarFlags) -> Self {
29        self.flags = flags;
30        self
31    }
32
33    /// Builds the input int2 widget.
34    ///
35    /// Returns true if any value was changed.
36    pub fn build(self) -> bool {
37        validate_input_scalar_flags("InputInt2::build()", self.flags);
38        let label_cstr = self.ui.scratch_txt(self.label);
39
40        self.ui.run_with_bound_context(|| unsafe {
41            sys::igInputInt2(label_cstr, self.value.as_mut_ptr(), self.flags.raw())
42        })
43    }
44}
45
46/// Builder for a 3-component int input widget.
47#[must_use]
48pub struct InputInt3<'ui, 'p, L> {
49    label: L,
50    value: &'p mut [i32; 3],
51    flags: InputScalarFlags,
52    ui: &'ui Ui,
53}
54
55impl<'ui, 'p, L: AsRef<str>> InputInt3<'ui, 'p, L> {
56    /// Constructs a new input int3 builder.
57    #[doc(alias = "InputInt3")]
58    pub fn new(ui: &'ui Ui, label: L, value: &'p mut [i32; 3]) -> Self {
59        InputInt3 {
60            label,
61            value,
62            flags: InputScalarFlags::empty(),
63            ui,
64        }
65    }
66
67    /// Sets the input text flags
68    #[inline]
69    pub fn flags(mut self, flags: InputScalarFlags) -> Self {
70        self.flags = flags;
71        self
72    }
73
74    /// Builds the input int3 widget.
75    ///
76    /// Returns true if any value was changed.
77    pub fn build(self) -> bool {
78        validate_input_scalar_flags("InputInt3::build()", self.flags);
79        let label_cstr = self.ui.scratch_txt(self.label);
80
81        self.ui.run_with_bound_context(|| unsafe {
82            sys::igInputInt3(label_cstr, self.value.as_mut_ptr(), self.flags.raw())
83        })
84    }
85}
86
87/// Builder for a 4-component int input widget.
88#[must_use]
89pub struct InputInt4<'ui, 'p, L> {
90    label: L,
91    value: &'p mut [i32; 4],
92    flags: InputScalarFlags,
93    ui: &'ui Ui,
94}
95
96impl<'ui, 'p, L: AsRef<str>> InputInt4<'ui, 'p, L> {
97    /// Constructs a new input int4 builder.
98    #[doc(alias = "InputInt4")]
99    pub fn new(ui: &'ui Ui, label: L, value: &'p mut [i32; 4]) -> Self {
100        InputInt4 {
101            label,
102            value,
103            flags: InputScalarFlags::empty(),
104            ui,
105        }
106    }
107
108    /// Sets the input text flags
109    #[inline]
110    pub fn flags(mut self, flags: InputScalarFlags) -> Self {
111        self.flags = flags;
112        self
113    }
114
115    /// Builds the input int4 widget.
116    ///
117    /// Returns true if any value was changed.
118    pub fn build(self) -> bool {
119        validate_input_scalar_flags("InputInt4::build()", self.flags);
120        let label_cstr = self.ui.scratch_txt(self.label);
121
122        self.ui.run_with_bound_context(|| unsafe {
123            sys::igInputInt4(label_cstr, self.value.as_mut_ptr(), self.flags.raw())
124        })
125    }
126}