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
use super::*; #[derive(Default)] pub struct Selector { button: Button, line_edit: LineEdit, choices: Vec<Button>, open: bool, editing: bool, pub choice: usize, } impl Selector { pub fn draw<'s>(&'s mut self, r: &mut RenderLock<'s>, t: &'s Theme, pos: Vec2, size: Vec2, options: &'s mut [String]) -> usize { let Self { button, line_edit, choices, open, editing, choice, } = self; let text = &options[*choice]; if !*open { choices.clear(); let pressed = &mut button.pressed as *mut bool; if button.draw(r, t, pos, size, text) { unsafe { *pressed = false; } *open = true; line_edit.text = text.into(); } } else { choices.resize_def(options.len()); for (n, c) in choices.iter_mut().enumerate() { if c.draw(r, t, pos.sum(size.mul((0, n + 1))), size, &options[n]) { *open = false; *editing = false; *choice = n; return *choice; } } let text = &mut options[*choice as usize]; if *editing { if !r.focused(LUID(line_edit)) { *text = line_edit.text.to_string(); *open = false; *editing = false; } } else { *open &= r.hovers_in((pos, pos.sum(size.mul((1, options.len() + 1))))) } *editing |= r.focused(LUID(line_edit)); line_edit.draw(r, t, pos, size); } *choice } }