Skip to main content

dear_imgui_rs/widget/input/numeric/
single.rs

1use super::super::validation::validate_input_scalar_flags;
2use crate::ui::Ui;
3use crate::{InputScalarFlags, sys};
4use std::borrow::Cow;
5
6/// Builder for integer input widget
7#[derive(Debug)]
8#[must_use]
9pub struct InputInt<'ui> {
10    ui: &'ui Ui,
11    label: Cow<'ui, str>,
12    step: i32,
13    step_fast: i32,
14    flags: InputScalarFlags,
15}
16
17impl<'ui> InputInt<'ui> {
18    /// Creates a new integer input builder
19    pub fn new(ui: &'ui Ui, label: impl Into<Cow<'ui, str>>) -> Self {
20        Self {
21            ui,
22            label: label.into(),
23            step: 1,
24            step_fast: 100,
25            flags: InputScalarFlags::NONE,
26        }
27    }
28
29    /// Sets the step value
30    pub fn step(mut self, step: i32) -> Self {
31        self.step = step;
32        self
33    }
34
35    /// Sets the fast step value
36    pub fn step_fast(mut self, step_fast: i32) -> Self {
37        self.step_fast = step_fast;
38        self
39    }
40
41    /// Sets the flags for the input
42    pub fn flags(mut self, flags: InputScalarFlags) -> Self {
43        self.flags = flags;
44        self
45    }
46
47    /// Builds the integer input widget
48    pub fn build(self, value: &mut i32) -> bool {
49        validate_input_scalar_flags("InputInt::build()", self.flags);
50        let label_ptr = self.ui.scratch_txt(self.label.as_ref());
51        self.ui.run_with_bound_context(|| unsafe {
52            sys::igInputInt(
53                label_ptr,
54                value as *mut i32,
55                self.step,
56                self.step_fast,
57                self.flags.raw(),
58            )
59        })
60    }
61}
62
63/// Builder for float input widget
64#[derive(Debug)]
65#[must_use]
66pub struct InputFloat<'ui> {
67    ui: &'ui Ui,
68    label: Cow<'ui, str>,
69    step: f32,
70    step_fast: f32,
71    format: Option<Cow<'ui, str>>,
72    flags: InputScalarFlags,
73}
74
75impl<'ui> InputFloat<'ui> {
76    /// Creates a new float input builder
77    pub fn new(ui: &'ui Ui, label: impl Into<Cow<'ui, str>>) -> Self {
78        Self {
79            ui,
80            label: label.into(),
81            step: 0.0,
82            step_fast: 0.0,
83            format: None,
84            flags: InputScalarFlags::NONE,
85        }
86    }
87
88    /// Sets the step value
89    pub fn step(mut self, step: f32) -> Self {
90        self.step = step;
91        self
92    }
93
94    /// Sets the fast step value
95    pub fn step_fast(mut self, step_fast: f32) -> Self {
96        self.step_fast = step_fast;
97        self
98    }
99
100    /// Sets the display format
101    pub fn format(mut self, format: impl Into<Cow<'ui, str>>) -> Self {
102        self.format = Some(format.into());
103        self
104    }
105
106    /// Sets the flags for the input
107    pub fn flags(mut self, flags: InputScalarFlags) -> Self {
108        self.flags = flags;
109        self
110    }
111
112    /// Builds the float input widget
113    pub fn build(self, value: &mut f32) -> bool {
114        validate_input_scalar_flags("InputFloat::build()", self.flags);
115        let format = self.format.as_deref().unwrap_or("%.3f");
116        let (label_ptr, format_ptr) = self.ui.scratch_txt_two(self.label.as_ref(), format);
117
118        self.ui.run_with_bound_context(|| unsafe {
119            sys::igInputFloat(
120                label_ptr,
121                value as *mut f32,
122                self.step,
123                self.step_fast,
124                format_ptr,
125                self.flags.raw(),
126            )
127        })
128    }
129}
130
131/// Builder for double input widget
132#[derive(Debug)]
133#[must_use]
134pub struct InputDouble<'ui> {
135    ui: &'ui Ui,
136    label: Cow<'ui, str>,
137    step: f64,
138    step_fast: f64,
139    format: Option<Cow<'ui, str>>,
140    flags: InputScalarFlags,
141}
142
143impl<'ui> InputDouble<'ui> {
144    /// Creates a new double input builder
145    pub fn new(ui: &'ui Ui, label: impl Into<Cow<'ui, str>>) -> Self {
146        Self {
147            ui,
148            label: label.into(),
149            step: 0.0,
150            step_fast: 0.0,
151            format: None,
152            flags: InputScalarFlags::NONE,
153        }
154    }
155
156    /// Sets the step value
157    pub fn step(mut self, step: f64) -> Self {
158        self.step = step;
159        self
160    }
161
162    /// Sets the fast step value
163    pub fn step_fast(mut self, step_fast: f64) -> Self {
164        self.step_fast = step_fast;
165        self
166    }
167
168    /// Sets the display format
169    pub fn format(mut self, format: impl Into<Cow<'ui, str>>) -> Self {
170        self.format = Some(format.into());
171        self
172    }
173
174    /// Sets the flags for the input
175    pub fn flags(mut self, flags: InputScalarFlags) -> Self {
176        self.flags = flags;
177        self
178    }
179
180    /// Builds the double input widget
181    pub fn build(self, value: &mut f64) -> bool {
182        validate_input_scalar_flags("InputDouble::build()", self.flags);
183        let format = self.format.as_deref().unwrap_or("%.6f");
184        let (label_ptr, format_ptr) = self.ui.scratch_txt_two(self.label.as_ref(), format);
185
186        self.ui.run_with_bound_context(|| unsafe {
187            sys::igInputDouble(
188                label_ptr,
189                value as *mut f64,
190                self.step,
191                self.step_fast,
192                format_ptr,
193                self.flags.raw(),
194            )
195        })
196    }
197}