Skip to content
Snippets Groups Projects
input.rs 18.2 KiB
Newer Older
sam's avatar
sam committed
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
#![allow(clippy::too_many_arguments, clippy::type_complexity)]

use std::time::Duration;

use bevy::{
    input::mouse::{MouseScrollUnit, MouseWheel},
    prelude::*,
    window::PrimaryWindow,
};
use cosmic_text::{Action, AttrsList, BufferLine, Cursor, Edit, Shaping};

use crate::{
    get_node_cursor_pos, get_timestamp, get_x_offset_center, get_y_offset_center,
    save_edit_history, CosmicAttrs, CosmicEditHistory, CosmicEditor, CosmicFontSystem,
    CosmicMaxChars, CosmicMaxLines, CosmicTextChanged, CosmicTextPosition, Focus, ReadOnly,
    XOffset,
};

pub(crate) fn input_mouse(
    windows: Query<&Window, With<PrimaryWindow>>, // Mouse
    active_editor: Res<Focus>,                    // Both
    keys: Res<Input<KeyCode>>,                    // Both
    buttons: Res<Input<MouseButton>>,             // Mouse
    mut cosmic_edit_query: Query<(
        &mut CosmicEditor,   // Both
        &GlobalTransform,    // Mouse
        &CosmicTextPosition, // Mouse, to determine point
        Entity,              // Both
        &XOffset,            // Mouse
        Option<&mut Node>,
        Option<&mut Sprite>,
    )>,
    mut font_system: ResMut<CosmicFontSystem>,    // Both
    mut scroll_evr: EventReader<MouseWheel>,      // Mouse
    camera_q: Query<(&Camera, &GlobalTransform)>, // Mouse
) {
    if active_editor.0.is_none() {
        return;
    }

    let primary_window = windows.single();
    let scale_factor = primary_window.scale_factor() as f32;
    let (camera, camera_transform) = camera_q.iter().find(|(c, _)| c.is_active).unwrap();
    for (mut editor, node_transform, text_position, entity, x_offset, node_opt, sprite_opt) in
        &mut cosmic_edit_query.iter_mut()
    {
        if active_editor.0 != Some(entity) {
            continue;
        }

        let (width, height, is_ui_node) = match node_opt {
            Some(node) => (node.size().x, node.size().y, true),
            None => {
                let sprite = sprite_opt.unwrap();
                let size = sprite.custom_size.unwrap();
                (size.x, size.y, false)
            }
        };

        let shift = keys.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]);

        // if shift key is pressed
        let already_has_selection = editor.0.select_opt().is_some();
        if shift && !already_has_selection {
            let cursor = editor.0.cursor();
            editor.0.set_select_opt(Some(cursor));
        }

        let (padding_x, padding_y) = match text_position {
            CosmicTextPosition::Center => (
                get_x_offset_center(width * scale_factor, editor.0.buffer()),
                get_y_offset_center(height * scale_factor, editor.0.buffer()),
            ),
            CosmicTextPosition::TopLeft { padding } => (*padding, *padding),
            CosmicTextPosition::Left { padding } => (
                *padding,
                get_y_offset_center(height * scale_factor, editor.0.buffer()),
            ),
        };
        let point = |node_cursor_pos: (f32, f32)| {
            (
                (node_cursor_pos.0 * scale_factor) as i32 - padding_x,
                (node_cursor_pos.1 * scale_factor) as i32 - padding_y,
            )
        };

        if buttons.just_pressed(MouseButton::Left) {
            if let Some(node_cursor_pos) = get_node_cursor_pos(
                primary_window,
                node_transform,
                (width, height),
                is_ui_node,
                camera,
                camera_transform,
            ) {
                let (mut x, y) = point(node_cursor_pos);
                x += x_offset.0.unwrap_or((0., 0.)).0 as i32;
                if shift {
                    editor.0.action(&mut font_system.0, Action::Drag { x, y });
                } else {
                    editor.0.action(&mut font_system.0, Action::Click { x, y });
                }
            }
            return;
        }
        if buttons.pressed(MouseButton::Left) {
            if let Some(node_cursor_pos) = get_node_cursor_pos(
                primary_window,
                node_transform,
                (width, height),
                is_ui_node,
                camera,
                camera_transform,
            ) {
                let (mut x, y) = point(node_cursor_pos);
                x += x_offset.0.unwrap_or((0., 0.)).0 as i32;
                if active_editor.is_changed() && !shift {
                    editor.0.action(&mut font_system.0, Action::Click { x, y });
                } else {
                    editor.0.action(&mut font_system.0, Action::Drag { x, y });
                }
            }
            return;
        }
        for ev in scroll_evr.iter() {
            match ev.unit {
                MouseScrollUnit::Line => {
                    editor.0.action(
                        &mut font_system.0,
                        Action::Scroll {
                            lines: -ev.y as i32,
                        },
                    );
                }
                MouseScrollUnit::Pixel => {
                    let line_height = editor.0.buffer().metrics().line_height;
                    editor.0.action(
                        &mut font_system.0,
                        Action::Scroll {
                            lines: -(ev.y / line_height) as i32,
                        },
                    );
                }
            }
        }
    }
}

/// Handles undo/redo, copy/paste and char input
pub(crate) fn input_kb(
    active_editor: Res<Focus>,                    // Both
    keys: Res<Input<KeyCode>>,                    // Both
    mut char_evr: EventReader<ReceivedCharacter>, // Kb
    mut cosmic_edit_query: Query<(
        &mut CosmicEditor,      // Both
        &mut CosmicEditHistory, // Kb - Undo
        &CosmicAttrs,           // Kb - Undo
        &CosmicMaxLines,        // Kb
        &CosmicMaxChars,        // Kb
        Entity,                 // Both
        Option<&ReadOnly>,
    )>,
    mut evw_changed: EventWriter<CosmicTextChanged>, // Kb
    mut font_system: ResMut<CosmicFontSystem>,       // Both
    mut is_deleting: Local<bool>,                    // Kb
    mut edits_duration: Local<Option<Duration>>,     // Kb - Undo
    mut undoredo_duration: Local<Option<Duration>>,  // Kb - Undo
) {
    for (mut editor, mut edit_history, attrs, max_lines, max_chars, entity, readonly_opt) in
        &mut cosmic_edit_query.iter_mut()
    {
        if active_editor.0 != Some(entity) {
            continue;
        }

        let readonly = readonly_opt.is_some();

        let attrs = &attrs.0;

        let now_ms = get_timestamp();

        #[cfg(target_os = "macos")]
        let command = keys.any_pressed([KeyCode::SuperLeft, KeyCode::SuperRight]);

        #[cfg(not(target_os = "macos"))]
        let command = keys.any_pressed([KeyCode::ControlLeft, KeyCode::ControlRight]);

        let shift = keys.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]);

        #[cfg(target_os = "macos")]
        let option = keys.any_pressed([KeyCode::AltLeft, KeyCode::AltRight]);

        // if shift key is pressed
        let already_has_selection = editor.0.select_opt().is_some();
        if shift && !already_has_selection {
            let cursor = editor.0.cursor();
            editor.0.set_select_opt(Some(cursor));
        }

        #[cfg(target_os = "macos")]
        let should_jump = command && option;
        #[cfg(not(target_os = "macos"))]
        let should_jump = command;

        if should_jump && keys.just_pressed(KeyCode::Left) {
            editor.0.action(&mut font_system.0, Action::PreviousWord);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }
        if should_jump && keys.just_pressed(KeyCode::Right) {
            editor.0.action(&mut font_system.0, Action::NextWord);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }
        if should_jump && keys.just_pressed(KeyCode::Home) {
            editor.0.action(&mut font_system.0, Action::BufferStart);
            // there's a bug with cosmic text where it doesn't update the visual cursor for this action
            // TODO: fix upstream
            editor.0.buffer_mut().set_redraw(true);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }
        if should_jump && keys.just_pressed(KeyCode::End) {
            editor.0.action(&mut font_system.0, Action::BufferEnd);
            // there's a bug with cosmic text where it doesn't update the visual cursor for this action
            // TODO: fix upstream
            editor.0.buffer_mut().set_redraw(true);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }

        if keys.just_pressed(KeyCode::Left) {
            editor.0.action(&mut font_system.0, Action::Left);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }
        if keys.just_pressed(KeyCode::Right) {
            editor.0.action(&mut font_system.0, Action::Right);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }
        if keys.just_pressed(KeyCode::Up) {
            editor.0.action(&mut font_system.0, Action::Up);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }
        if keys.just_pressed(KeyCode::Down) {
            editor.0.action(&mut font_system.0, Action::Down);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }

        if keys.just_pressed(KeyCode::Back) {
            #[cfg(target_arch = "wasm32")]
            editor.0.action(&mut font_system.0, Action::Backspace);
            *is_deleting = true;
        }
        if keys.just_released(KeyCode::Back) {
            *is_deleting = false;
        }
        if keys.just_pressed(KeyCode::Delete) {
            editor.0.action(&mut font_system.0, Action::Delete);
        }
        if keys.just_pressed(KeyCode::Escape) {
            editor.0.action(&mut font_system.0, Action::Escape);
        }
        if command && keys.just_pressed(KeyCode::A) {
            editor.0.action(&mut font_system.0, Action::BufferEnd);
            let current_cursor = editor.0.cursor();
            editor.0.set_select_opt(Some(Cursor {
                line: 0,
                index: 0,
                affinity: current_cursor.affinity,
                color: current_cursor.color,
            }));
            return;
        }
        if keys.just_pressed(KeyCode::Home) {
            editor.0.action(&mut font_system.0, Action::Home);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }
        if keys.just_pressed(KeyCode::End) {
            editor.0.action(&mut font_system.0, Action::End);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }
        if keys.just_pressed(KeyCode::PageUp) {
            editor.0.action(&mut font_system.0, Action::PageUp);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }
        if keys.just_pressed(KeyCode::PageDown) {
            editor.0.action(&mut font_system.0, Action::PageDown);
            if !shift {
                editor.0.set_select_opt(None);
            }
            return;
        }

        // redo
        #[cfg(not(target_os = "windows"))]
        let requested_redo = command && shift && keys.just_pressed(KeyCode::Z) && !readonly;
        #[cfg(target_os = "windows")]
        let requested_redo = command && keys.just_pressed(KeyCode::Y);

        if requested_redo {
            let edits = &edit_history.edits;
            if edits.is_empty() {
                return;
            }
            if edit_history.current_edit == edits.len() - 1 {
                return;
            }
            let idx = edit_history.current_edit + 1;
            if let Some(current_edit) = edits.get(idx) {
                editor.0.buffer_mut().lines.clear();
                for line in current_edit.lines.iter() {
                    let mut line_text = String::new();
                    let mut attrs_list = AttrsList::new(attrs.as_attrs());
                    for (text, attrs) in line.iter() {
                        let start = line_text.len();
                        line_text.push_str(text);
                        let end = line_text.len();
                        attrs_list.add_span(start..end, attrs.as_attrs());
                    }
                    editor.0.buffer_mut().lines.push(BufferLine::new(
                        line_text,
                        attrs_list,
                        Shaping::Advanced,
                    ));
                }
                editor.0.set_cursor(current_edit.cursor);
                editor.0.buffer_mut().set_redraw(true);
                edit_history.current_edit += 1;
            }
            *undoredo_duration = Some(Duration::from_millis(now_ms as u64));
            evw_changed.send(CosmicTextChanged((entity, editor.get_text())));
            return;
        }
        // undo
        let requested_undo = command && keys.just_pressed(KeyCode::Z) && !readonly;

        if requested_undo {
            let edits = &edit_history.edits;
            if edits.is_empty() {
                return;
            }
            if edit_history.current_edit <= 1 {
                return;
            }
            let idx = edit_history.current_edit - 1;
            if let Some(current_edit) = edits.get(idx) {
                editor.0.buffer_mut().lines.clear();
                for line in current_edit.lines.iter() {
                    let mut line_text = String::new();
                    let mut attrs_list = AttrsList::new(attrs.as_attrs());
                    for (text, attrs) in line.iter() {
                        let start = line_text.len();
                        line_text.push_str(text);
                        let end = line_text.len();
                        attrs_list.add_span(start..end, attrs.as_attrs());
                    }
                    editor.0.buffer_mut().lines.push(BufferLine::new(
                        line_text,
                        attrs_list,
                        Shaping::Advanced,
                    ));
                }
                editor.0.set_cursor(current_edit.cursor);
                editor.0.buffer_mut().set_redraw(true);
                edit_history.current_edit -= 1;
            }
            *undoredo_duration = Some(Duration::from_millis(now_ms as u64));
            evw_changed.send(CosmicTextChanged((entity, editor.get_text())));
            return;
        }

        let mut is_clipboard = false;
        #[cfg(not(target_arch = "wasm32"))]
        {
            if let Ok(mut clipboard) = arboard::Clipboard::new() {
                if command && keys.just_pressed(KeyCode::C) {
                    if let Some(text) = editor.0.copy_selection() {
                        clipboard.set_text(text).unwrap();
                        return;
                    }
                }
                if command && keys.just_pressed(KeyCode::X) && !readonly {
                    if let Some(text) = editor.0.copy_selection() {
                        clipboard.set_text(text).unwrap();
                        editor.0.delete_selection();
                    }
                    is_clipboard = true;
                }
                if command && keys.just_pressed(KeyCode::V) && !readonly {
                    if let Ok(text) = clipboard.get_text() {
                        for c in text.chars() {
                            if max_chars.0 == 0 || editor.get_text().len() < max_chars.0 {
                                if c == 0xA as char {
                                    if max_lines.0 == 0
                                        || editor.0.buffer().lines.len() < max_lines.0
                                    {
                                        editor.0.action(&mut font_system.0, Action::Insert(c));
                                    }
                                } else {
                                    editor.0.action(&mut font_system.0, Action::Insert(c));
                                }
                            }
                        }
                    }
                    is_clipboard = true;
                }
            }
        }

        // fix for issue #8
        if let Some(select) = editor.0.select_opt() {
            if editor.0.cursor().line == select.line && editor.0.cursor().index == select.index {
                editor.0.set_select_opt(None);
            }
        }

        let mut is_edit = is_clipboard;
        let mut is_return = false;
        if keys.just_pressed(KeyCode::Return) && !readonly {
            is_return = true;
            if (max_lines.0 == 0 || editor.0.buffer().lines.len() < max_lines.0)
                && (max_chars.0 == 0 || editor.get_text().len() < max_chars.0)
            {
                // to have new line on wasm rather than E
                is_edit = true;
                editor.0.action(&mut font_system.0, Action::Insert('\n'));
            }
        }

        if !(is_clipboard || is_return || readonly) {
            for char_ev in char_evr.iter() {
                is_edit = true;
                if *is_deleting {
                    editor.0.action(&mut font_system.0, Action::Backspace);
                } else if max_chars.0 == 0 || editor.get_text().len() < max_chars.0 {
                    editor
                        .0
                        .action(&mut font_system.0, Action::Insert(char_ev.char));
                }
            }
        }

        if !is_edit || readonly {
            return;
        }

        evw_changed.send(CosmicTextChanged((entity, editor.get_text())));

        if let Some(last_edit_duration) = *edits_duration {
            if Duration::from_millis(now_ms as u64) - last_edit_duration
                > Duration::from_millis(150)
            {
                save_edit_history(&mut editor.0, attrs, &mut edit_history);
                *edits_duration = Some(Duration::from_millis(now_ms as u64));
            }
        } else {
            save_edit_history(&mut editor.0, attrs, &mut edit_history);
            *edits_duration = Some(Duration::from_millis(now_ms as u64));
        }
    }
}