forked from imgui-rs/imgui-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.rs
222 lines (195 loc) · 5.68 KB
/
input.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use imgui_sys;
use libc::size_t;
use std::marker::PhantomData;
use std::ptr;
use super::{
Ui,
ImGuiInputTextFlags, ImGuiInputTextFlags_CharsDecimal, ImGuiInputTextFlags_CharsHexadecimal,
ImGuiInputTextFlags_CharsUppercase, ImGuiInputTextFlags_CharsNoBlank,
ImGuiInputTextFlags_AutoSelectAll, ImGuiInputTextFlags_EnterReturnsTrue,
ImGuiInputTextFlags_CallbackCompletion, ImGuiInputTextFlags_CallbackHistory,
ImGuiInputTextFlags_CallbackAlways, ImGuiInputTextFlags_CallbackCharFilter,
ImGuiInputTextFlags_AllowTabInput, //ImGuiInputTextFlags_CtrlEnterForNewLine,
ImGuiInputTextFlags_NoHorizontalScroll, ImGuiInputTextFlags_AlwaysInsertMode,
ImStr
};
macro_rules! impl_text_flags {
($T:ident) => {
#[inline]
pub fn flags(self, flags: ImGuiInputTextFlags) -> Self {
$T {
flags: flags,
.. self
}
}
#[inline]
pub fn chars_decimal(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_CharsDecimal, value),
.. self
}
}
#[inline]
pub fn chars_hexadecimal(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_CharsHexadecimal, value),
.. self
}
}
#[inline]
pub fn chars_uppercase(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_CharsUppercase, value),
.. self
}
}
#[inline]
pub fn chars_noblank(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_CharsNoBlank, value),
.. self
}
}
#[inline]
pub fn auto_select_all(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_AutoSelectAll, value),
.. self
}
}
#[inline]
pub fn enter_returns_true(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_EnterReturnsTrue, value),
.. self
}
}
#[inline]
pub fn callback_completion(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_CallbackCompletion, value),
.. self
}
}
#[inline]
pub fn callback_history(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_CallbackHistory, value),
.. self
}
}
#[inline]
pub fn callback_always(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_CallbackAlways, value),
.. self
}
}
#[inline]
pub fn callback_char_filter(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_CallbackCharFilter, value),
.. self
}
}
#[inline]
pub fn allow_tab_input(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_AllowTabInput, value),
.. self
}
}
#[inline]
pub fn no_horizontal_scroll(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_NoHorizontalScroll, value),
.. self
}
}
#[inline]
pub fn always_insert_mode(self, value: bool) -> Self {
$T {
flags: self.flags.with(ImGuiInputTextFlags_AlwaysInsertMode, value),
.. self
}
}
}
}
#[must_use]
pub struct InputText<'ui, 'p> {
label: ImStr<'p>,
buf: &'p mut str,
flags: ImGuiInputTextFlags,
_phantom: PhantomData<&'ui Ui<'ui>>
}
impl<'ui, 'p> InputText<'ui, 'p> {
pub fn new(label: ImStr<'p>, buf: &'p mut str) -> Self {
InputText {
label: label,
buf: buf,
flags: ImGuiInputTextFlags::empty(),
_phantom: PhantomData
}
}
impl_text_flags!(InputText);
// TODO: boxed closure...?
// pub fn callback(self) -> Self { }
pub fn build(self) -> bool {
unsafe {
imgui_sys::igInputText(
self.label.as_ptr(),
// TODO: this is evil. Perhaps something else than &mut str is better
self.buf.as_ptr() as *mut i8,
self.buf.len() as size_t,
self.flags,
None,
ptr::null_mut())
}
}
}
#[must_use]
pub struct InputInt<'ui, 'p> {
label: ImStr<'p>,
value: &'p mut i32,
step: i32,
step_fast: i32,
flags: ImGuiInputTextFlags,
_phantom: PhantomData<&'ui Ui<'ui>>
}
impl<'ui, 'p> InputInt<'ui, 'p> {
pub fn new(label: ImStr<'p>, value: &'p mut i32) -> Self {
InputInt {
label: label,
value: value,
step: 1,
step_fast: 100,
flags: ImGuiInputTextFlags::empty(),
_phantom: PhantomData
}
}
#[inline]
pub fn step(self, value: i32) -> Self {
InputInt {
step: value,
.. self
}
}
#[inline]
pub fn step_fast(self, value: i32) -> Self {
InputInt {
step_fast: value,
.. self
}
}
impl_text_flags!(InputInt);
pub fn build(self) -> bool {
unsafe {
imgui_sys::igInputInt(
self.label.as_ptr(),
self.value as *mut i32,
self.step,
self.step_fast,
self.flags)
}
}
}