-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathinput.rs
175 lines (152 loc) · 4.55 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
use crate::{
math::{vec2, Vec2},
ui::{
widgets::{Editbox, EditboxState},
ElementState, Id, Layout, Ui, UiContent,
},
};
pub struct InputText<'a> {
id: Id,
label: &'a str,
size: Option<Vec2>,
password: bool,
numbers: bool,
ratio: f32,
pos: Option<Vec2>,
margin: Option<Vec2>,
}
impl<'a> InputText<'a> {
pub fn new(id: Id) -> InputText<'a> {
InputText {
id,
size: None,
label: "",
numbers: false,
password: false,
ratio: 0.5,
pos: None,
margin: None,
}
}
pub fn label<'b>(self, label: &'b str) -> InputText<'b> {
InputText {
id: self.id,
size: self.size,
label,
numbers: self.numbers,
password: self.password,
ratio: self.ratio,
pos: self.pos,
margin: self.margin,
}
}
pub fn size(self, size: Vec2) -> Self {
Self {
size: Some(size),
..self
}
}
pub fn position(self, pos: Vec2) -> Self {
Self {
pos: Some(pos),
..self
}
}
pub fn password(self, password: bool) -> Self {
Self { password, ..self }
}
pub fn ratio(self, ratio: f32) -> Self {
Self { ratio, ..self }
}
pub fn filter_numbers(self) -> Self {
Self {
numbers: true,
..self
}
}
pub fn get_cursor(&self, ui: &mut Ui) -> u32 {
let context = ui.get_active_window_context();
let state = context
.storage_any
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
state.cursor
}
pub fn set_cursor(&self, ui: &mut Ui, cursor: u32) {
let context = ui.get_active_window_context();
let state = context
.storage_any
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
state.cursor = cursor
}
pub fn move_cursor(&self, ui: &mut Ui, amount: i32) {
let context = ui.get_active_window_context();
let state = context
.storage_any
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
if amount > 0 {
state.cursor = state.cursor.saturating_add(amount as u32);
} else if amount < 0 {
state.cursor = state.cursor.saturating_sub(-amount as u32);
}
}
pub fn margin(self, margin: Vec2) -> Self {
Self {
margin: Some(margin),
..self
}
}
pub fn ui(self, ui: &mut Ui, data: &mut String) {
let context = ui.get_active_window_context();
let label_size = context.window.painter.content_with_margins_size(
&context.style.editbox_style,
&UiContent::Label((&*data).into()),
);
let size = self.size.unwrap_or(vec2(
context.window.cursor.area.w - context.style.margin * 2. - context.window.cursor.ident,
label_size.y.max(19.),
));
let pos = self
.pos
.unwrap_or_else(|| context.window.cursor.fit(size, Layout::Vertical));
let editbox_area_w = if self.label.is_empty() {
size.x
} else {
size.x * self.ratio - 15.
};
let mut editbox = Editbox::new(self.id, Vec2::new(editbox_area_w, size.y))
.password(self.password)
.position(pos)
.multiline(false);
if let Some(margin) = self.margin {
editbox = editbox.margin(margin);
}
if self.numbers {
editbox = editbox
.filter(&|character| character.is_digit(10) || character == '.' || character == '-')
}
editbox.ui(ui, data);
let context = ui.get_active_window_context();
if self.label.is_empty() == false {
context.window.painter.draw_element_label(
&context.style.label_style,
Vec2::new(pos.x + size.x * self.ratio, pos.y),
self.label,
ElementState {
focused: context.focused,
..Default::default()
},
);
}
}
}
impl Ui {
pub fn input_text(&mut self, id: Id, label: &str, data: &mut String) {
InputText::new(id).label(label).ui(self, data)
}
pub fn input_password(&mut self, id: Id, label: &str, data: &mut String) {
InputText::new(id)
.label(label)
.password(true)
.ui(self, data)
}
}