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
use crate::{
    draw_list::Aligment,
    hash,
    types::{Color, Rect, Vector2},
    ui::InputCharacter,
    Id, Layout, Ui,
};

pub struct Editbox {
    id: Id,
    position: Vector2,
    size: Vector2,
    line_height: f32,
}

fn current_cursor_x_postion(text: &str, mut cursor: u32) -> u32 {
    let mut line_position = 0;
    while cursor > 0 && text.chars().nth(cursor as usize - 1).unwrap_or('x') != '\n' {
        cursor -= 1;
        line_position += 1;
    }
    line_position
}

impl Editbox {
    pub fn new(id: Id, position: Vector2, size: Vector2) -> Editbox {
        Editbox {
            id,
            position,
            size,
            line_height: 14.0,
        }
    }

    pub fn line_height(self, line_height: f32) -> Self {
        Self {
            line_height,
            ..self
        }
    }

    pub fn ui(self, ui: &mut Ui, text: &mut String) {
        let context = ui.get_active_window_context();

        let cursor = context.storage.entry(hash!(self.id, "cursor")).or_insert(0);

        if context.focused {
            for character in context.input.input_buffer.drain(0..) {
                use crate::input_handler::KeyCode;

                match character {
                    InputCharacter::Char(character) => {
                        if character != 13 as char && character != 10 as char {
                            text.insert(*cursor as usize, character);
                            *cursor += 1;
                        }
                    }
                    InputCharacter::ControlCode(code) => match code {
                        KeyCode::Enter => {
                            text.insert(*cursor as usize, '\n');
                            *cursor += 1;
                        }
                        KeyCode::Backspace => {
                            if *cursor > 0 {
                                text.remove(*cursor as usize - 1);
                                *cursor -= 1;
                            }
                        }
                        KeyCode::Delete => {
                            if *cursor < text.len() as u32 && text.len() != 0 {
                                text.remove(*cursor as usize);
                            }
                        }
                        KeyCode::Right => {
                            if *cursor < text.len() as u32 {
                                *cursor += 1;
                            }
                        }
                        KeyCode::Left => {
                            if *cursor > 0 {
                                *cursor -= 1;
                            }
                        }
                        KeyCode::Home => {
                            let line_position = current_cursor_x_postion(&text, *cursor);
                            *cursor -= line_position;
                        }
                        KeyCode::End => {
                            while *cursor < text.len() as u32
                                && text.chars().nth(*cursor as usize).unwrap_or('x') != '\n'
                            {
                                *cursor += 1;
                            }
                        }
                        KeyCode::Up => {
                            let line_position = current_cursor_x_postion(&text, *cursor);
                            *cursor -= line_position;
                            if *cursor != 0 {
                                *cursor -= 1;
                                let new_line_position = current_cursor_x_postion(&text, *cursor);
                                *cursor -= new_line_position;
                                *cursor += line_position.min(new_line_position);
                            }
                        }
                        KeyCode::Down => {
                            let line_position = current_cursor_x_postion(&text, *cursor);
                            while *cursor < text.len() as u32
                                && text.chars().nth(*cursor as usize).unwrap_or('x') != '\n'
                            {
                                *cursor += 1;
                            }
                            if text.len() != 0 && *cursor < text.len() as u32 - 1 {
                                *cursor += 1;
                                let new_line_position = current_cursor_x_postion(&text, *cursor);
                                for _ in 0..line_position {
                                    if text.chars().nth(*cursor as usize).unwrap_or('x') == '\n'
                                        || *cursor == text.len() as u32 - 1
                                    {
                                        break;
                                    }
                                    *cursor += 1;
                                }
                            }
                        }
                        _ => {}
                    },
                }
            }
        }

        let color = context.global_style.text(context.focused);
        let pos = context
            .window
            .cursor
            .fit(self.size, Layout::Free(self.position));

        context.window.draw_list.draw_rect(
            Rect::new(
                self.position.x + pos.x,
                self.position.y + pos.y,
                self.size.x,
                self.size.y,
            ),
            context.global_style.editbox_background(context.focused),
            None,
        );

        let parent = ui.get_active_window_context();
        parent.window.childs.push(self.id);
        let parent_id = Some(parent.window.id);

        let mut context = ui.begin_window(self.id, parent_id, pos, self.size);

        let size = Vector2::new(150., self.line_height * text.split('\n').count() as f32);

        let pos = context.window.cursor.fit(size, Layout::Free(self.position));

        context.window.draw_list.clip(context.window.content_rect());

        context.scroll_area();

        let cursor = *context.storage.entry(hash!(self.id, "cursor")).or_insert(0);

        let mut x = 10.;
        let mut y = 0.;

        for n in 0..text.len() + 1 {
            let character = text.chars().nth(n).unwrap_or(' ');
            if n == cursor as usize {
                context.window.draw_list.draw_rect(
                    Rect::new(pos.x + x, pos.y + y, 2., 8.),
                    Color::new(0., 0., 0., 1.),
                    None,
                );
            }
            if character != '\n' {
                context.window.draw_list.draw_label(
                    &character.to_string(),
                    pos + Vector2::new(x, y),
                    color,
                );
            }
            x += 10.;
            if character == '\n' {
                y += self.line_height;
                x = 10.;
            }
        }

        let context = ui.get_active_window_context();

        context.window.draw_list.clip(None);

        ui.end_window();
    }
}

impl Ui {
    pub fn editbox(&mut self, id: Id, position: Vector2, size: Vector2, text: &mut String) {
        Editbox::new(id, position, size).ui(self, text)
    }
}