From 64310920013b7c7e1b93cb60bce8a77e7b70735f Mon Sep 17 00:00:00 2001 From: sam edelsten <samedelsten1@gmail.com> Date: Wed, 10 Apr 2024 09:20:26 +0100 Subject: [PATCH] working infiniteline (when mode != center) --- examples/sprite_and_ui_clickable.rs | 3 +++ src/input.rs | 32 ++++++++++++++++++------- src/layout.rs | 37 +++++++++++++++++------------ src/lib.rs | 4 ++-- src/render.rs | 15 ++++++++---- 5 files changed, 62 insertions(+), 29 deletions(-) diff --git a/examples/sprite_and_ui_clickable.rs b/examples/sprite_and_ui_clickable.rs index 375c1eb..0afb584 100644 --- a/examples/sprite_and_ui_clickable.rs +++ b/examples/sprite_and_ui_clickable.rs @@ -16,6 +16,7 @@ fn setup(mut commands: Commands) { )), max_lines: CosmicMaxLines(1), mode: CosmicMode::InfiniteLine, + text_position: CosmicTextPosition::Left { padding: 5 }, ..default() }) .id(); @@ -36,6 +37,8 @@ fn setup(mut commands: Commands) { // Sprite editor commands.spawn((CosmicEditBundle { + max_lines: CosmicMaxLines(1), + mode: CosmicMode::InfiniteLine, sprite_bundle: SpriteBundle { // Sets size of text box sprite: Sprite { diff --git a/src/input.rs b/src/input.rs index ab4af4e..135703e 100644 --- a/src/input.rs +++ b/src/input.rs @@ -153,7 +153,7 @@ pub(crate) fn input_mouse( camera_transform, ) { let (mut x, y) = point(node_cursor_pos); - x += x_offset.min as i32; + x += x_offset.left as i32; if shift { editor.action(&mut font_system.0, Action::Drag { x, y }); } else { @@ -193,7 +193,7 @@ pub(crate) fn input_mouse( camera_transform, ) { let (mut x, y) = point(node_cursor_pos); - x += x_offset.min as i32; + x += x_offset.left as i32; if active_editor.is_changed() && !shift { editor.action(&mut font_system.0, Action::Click { x, y }); } else { @@ -355,13 +355,28 @@ pub(crate) fn input_kb( if keys.just_pressed(KeyCode::Backspace) & !readonly { // fix for issue #8 let select = editor.selection(); - if select != Selection::None { - // TODO: fix zero-width selections if needed - // - // if editor.cursor().line == select.line && editor.cursor().index == select.index { - // editor.set_selection(Selection::None); - // } + match select { + Selection::Line(cursor) => { + if editor.cursor().line == cursor.line && editor.cursor().index == cursor.index + { + editor.set_selection(Selection::None); + } + } + Selection::Normal(cursor) => { + if editor.cursor().line == cursor.line && editor.cursor().index == cursor.index + { + editor.set_selection(Selection::None); + } + } + Selection::Word(cursor) => { + if editor.cursor().line == cursor.line && editor.cursor().index == cursor.index + { + editor.set_selection(Selection::None); + } + } + Selection::None => {} } + *is_deleting = true; #[cfg(target_arch = "wasm32")] editor.action(&mut font_system.0, Action::Backspace); @@ -372,6 +387,7 @@ pub(crate) fn input_kb( } if keys.just_pressed(KeyCode::Delete) && !readonly { editor.action(&mut font_system.0, Action::Delete); + editor.with_buffer_mut(|b| b.set_redraw(true)); } if keys.just_pressed(KeyCode::Escape) { editor.action(&mut font_system.0, Action::Escape); diff --git a/src/layout.rs b/src/layout.rs index b0cd386..498dd76 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -120,25 +120,26 @@ pub fn set_x_offset( &CosmicMode, &CosmicEditor, &CosmicWidgetSize, - &CosmicPadding, + &CosmicTextPosition, )>, ) { - for (mut x_offset, mode, editor, size, padding) in query.iter_mut() { + for (mut x_offset, mode, editor, size, position) in query.iter_mut() { if mode != &CosmicMode::InfiniteLine { return; } let mut cursor_x = 0.; + let cursor = editor.cursor(); if let Some(line) = editor.with_buffer(|b| b.clone()).layout_runs().next() { for (idx, glyph) in line.glyphs.iter().enumerate() { - if editor.cursor().affinity == Affinity::Before { - if idx <= editor.cursor().index { + if cursor.affinity == Affinity::Before { + if idx <= cursor.index { cursor_x += glyph.w; } else { break; } - } else if idx < editor.cursor().index { + } else if idx < cursor.index { cursor_x += glyph.w; } else { break; @@ -146,19 +147,25 @@ pub fn set_x_offset( } } - if x_offset.min == 0. && x_offset.max == 0. { - x_offset.max = size.x; + let padding_x = match position { + CosmicTextPosition::Center => 5., + CosmicTextPosition::TopLeft { padding } => *padding as f32, + CosmicTextPosition::Left { padding } => *padding as f32, + }; + + if x_offset.width == 0. { + x_offset.width = size.x - padding_x * 2.; } - if cursor_x > x_offset.max { - let diff = cursor_x - x_offset.max; - x_offset.min += diff; - x_offset.max = cursor_x; + let right = x_offset.width + x_offset.left; + + if cursor_x > right { + let diff = cursor_x - right; + x_offset.left += diff; } - if cursor_x < x_offset.min { - let diff = x_offset.min - cursor_x; - x_offset.min = cursor_x; - x_offset.max -= diff; + if cursor_x < x_offset.left { + let diff = x_offset.left - cursor_x; + x_offset.left -= diff; } } } diff --git a/src/lib.rs b/src/lib.rs index 96b04a1..b14e0c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,8 +78,8 @@ pub struct ReadOnly; // tag component #[derive(Component, Debug, Default)] pub struct XOffset { - pub min: f32, - pub max: f32, + pub left: f32, + pub width: f32, } #[derive(Component, Deref, DerefMut)] diff --git a/src/render.rs b/src/render.rs index e66e95d..7d7ed6b 100644 --- a/src/render.rs +++ b/src/render.rs @@ -4,8 +4,8 @@ use image::{imageops::FilterType, GenericImageView}; use crate::{ layout::{CosmicPadding, CosmicWidgetSize}, - CosmicBackground, CosmicBuffer, CosmicEditor, CosmicFontSystem, CursorColor, DefaultAttrs, - FillColor, ReadOnly, SelectionColor, XOffset, + CosmicBackground, CosmicBuffer, CosmicEditor, CosmicFontSystem, CosmicTextPosition, + CursorColor, DefaultAttrs, FillColor, ReadOnly, SelectionColor, XOffset, }; #[derive(Resource)] @@ -77,6 +77,7 @@ pub(crate) fn render_texture( &CosmicPadding, &XOffset, Option<&ReadOnly>, + &CosmicTextPosition, )>, mut font_system: ResMut<CosmicFontSystem>, mut images: ResMut<Assets<Image>>, @@ -95,6 +96,7 @@ pub(crate) fn render_texture( padding, x_offset, readonly_opt, + position, ) in query.iter_mut() { // Draw background @@ -133,6 +135,11 @@ pub(crate) fn render_texture( .color_opt .unwrap_or(cosmic_text::Color::rgb(0, 0, 0)); + let x_offset_divisor = match position { + CosmicTextPosition::Center => 2., + _ => 1., + }; + let draw_closure = |x, y, w, h, color| { for row in 0..h as i32 { for col in 0..w as i32 { @@ -140,8 +147,8 @@ pub(crate) fn render_texture( &mut pixels, size.0.x as i32, size.0.y as i32, - x + col + padding.0.x as i32 - x_offset.min as i32, - y + row + padding.0.y as i32, + x + col + padding.x as i32 - (x_offset.left / x_offset_divisor) as i32, + y + row + padding.y as i32, color, ); } -- GitLab