dear_imgui_rs/widget/input/numeric/
single.rs1use super::super::validation::validate_input_scalar_flags;
2use crate::ui::Ui;
3use crate::{InputScalarFlags, sys};
4use std::borrow::Cow;
5
6#[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 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 pub fn step(mut self, step: i32) -> Self {
31 self.step = step;
32 self
33 }
34
35 pub fn step_fast(mut self, step_fast: i32) -> Self {
37 self.step_fast = step_fast;
38 self
39 }
40
41 pub fn flags(mut self, flags: InputScalarFlags) -> Self {
43 self.flags = flags;
44 self
45 }
46
47 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#[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 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 pub fn step(mut self, step: f32) -> Self {
90 self.step = step;
91 self
92 }
93
94 pub fn step_fast(mut self, step_fast: f32) -> Self {
96 self.step_fast = step_fast;
97 self
98 }
99
100 pub fn format(mut self, format: impl Into<Cow<'ui, str>>) -> Self {
102 self.format = Some(format.into());
103 self
104 }
105
106 pub fn flags(mut self, flags: InputScalarFlags) -> Self {
108 self.flags = flags;
109 self
110 }
111
112 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#[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 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 pub fn step(mut self, step: f64) -> Self {
158 self.step = step;
159 self
160 }
161
162 pub fn step_fast(mut self, step_fast: f64) -> Self {
164 self.step_fast = step_fast;
165 self
166 }
167
168 pub fn format(mut self, format: impl Into<Cow<'ui, str>>) -> Self {
170 self.format = Some(format.into());
171 self
172 }
173
174 pub fn flags(mut self, flags: InputScalarFlags) -> Self {
176 self.flags = flags;
177 self
178 }
179
180 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}