diff options
Diffstat (limited to 'pixel-client/src/ui.rs')
-rw-r--r-- | pixel-client/src/ui.rs | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/pixel-client/src/ui.rs b/pixel-client/src/ui.rs index fa3a9691..045fc46b 100644 --- a/pixel-client/src/ui.rs +++ b/pixel-client/src/ui.rs @@ -36,6 +36,9 @@ pub struct UiState { mouse_position: Vec2, ui_scale: Vec2, + backspace: bool, + text_input: String, + keyboard_focus: FocusDevice, mouse_focus: FocusDevice, } @@ -58,11 +61,15 @@ impl UiState { self.keyboard_focus .update(keyboard.is_scancode_pressed(Scancode::Space)); } + pub fn text_input(&mut self, text: String) { + self.text_input = text; + } pub fn keyboard_event(&mut self, keycode: Keycode, down: bool) { if down { match keycode { Keycode::DOWN => self.keyboard_focus.focus += 1, Keycode::UP if self.keyboard_focus.focus > 0 => self.keyboard_focus.focus -= 1, + Keycode::BACKSPACE => self.backspace = true, _ => (), } } @@ -81,13 +88,17 @@ impl UiState { index: 0, }; ui(&mut u); - + self.end() + } + fn end(&mut self) { if self.mouse_focus.interact_just_released { self.mouse_focus.pressing = None; } if self.keyboard_focus.interact_just_released { self.keyboard_focus.pressing = None; } + self.text_input.clear(); + self.backspace = false; } } @@ -156,6 +167,48 @@ impl<'a, 'b> Ui<'a, 'b> { released } + pub fn textedit(&mut self, w: f32, content: &mut String) { + let c = self.cursor; + let margin = Vec2::splat(4.); + let text_size = self.renderer.draw_text(self.cursor + margin, &content); + let size = margin + Vec2::new(w, text_size.y) + margin; + + self.index += 1; + + let mouse_rel = self.state.mouse_position - c; + if mouse_rel.x >= 0. && mouse_rel.y >= 0. && mouse_rel.x < size.x && mouse_rel.y < size.y { + self.state.mouse_focus.focus = self.index; + } + + if self.state.mouse_focus.interact_just_pressed + && self.state.mouse_focus.focus == self.index + { + self.state.keyboard_focus.focus = self.index; + } + + let keyboard_focus = self.state.keyboard_focus.focus == self.index; + + if keyboard_focus { + *content += &self.state.text_input; + self.state.text_input.clear(); + if self.state.backspace { + content.pop(); + } + } + + let focus = self.state.mouse_focus.focus == self.index || keyboard_focus; + let l = if focus { 50 } else { 30 }; + self.renderer.draw_ui(SpriteDraw::screen( + self.renderer.misc_textures.solid, + i32::MAX - 1, + c, + size, + Some([l, l, l, 200]), + )); + + self.advance(size); + } + pub fn fill(&mut self) { self.renderer.draw_ui(SpriteDraw::screen( self.renderer.misc_textures.solid, |