diff --git a/TODO b/TODO index 6472b1a09380c7b98e2666b3c18307c9351d9771..b92a24f9e71e7f7f526216afaf21e6af2e38fcb7 100644 --- a/TODO +++ b/TODO @@ -1,11 +1,12 @@ -[ ] Friendly get/set text fns on CosmicEditor impl +[x] Friendly get/set text fns on CosmicEditor impl BUGS INTRODUCED: [ ] Cursor on readonly [ ] Window resizing broken Used to redraw on click, now redraws on scroll or drag? + Now drag doesn't work either (tested on readonly, might be that) BUGS SQUASHED: diff --git a/examples/readonly.rs b/examples/readonly.rs index 0840ef85bdb8536ff0ce0b28a2053d8ef8f86ae7..049d0431538cbb9aae633559d2838bd04c06b32c 100644 --- a/examples/readonly.rs +++ b/examples/readonly.rs @@ -1,7 +1,7 @@ use bevy::{prelude::*, window::PrimaryWindow}; use bevy_cosmic_edit::{ - cosmic_edit_set_text, ActiveEditor, CosmicAttrs, CosmicEditPlugin, CosmicEditUiBundle, - CosmicFontConfig, CosmicFontSystem, CosmicMetrics, CosmicText, CosmicTextPosition, ReadOnly, + ActiveEditor, CosmicAttrs, CosmicEditPlugin, CosmicEditUiBundle, CosmicFontConfig, + CosmicFontSystem, CosmicMetrics, CosmicText, CosmicTextPosition, ReadOnly, }; use cosmic_text::AttrsOwned; @@ -28,8 +28,7 @@ fn setup( attrs = attrs.family(cosmic_text::Family::Name("Victor Mono")); attrs = attrs.color(cosmic_text::Color::rgb(0x94, 0x00, 0xD3)); - // - let mut cosmic_edit = CosmicEditUiBundle { + let cosmic_edit = CosmicEditUiBundle { style: Style { width: Val::Percent(100.), height: Val::Percent(100.), @@ -44,21 +43,20 @@ fn setup( scale_factor: primary_window.scale_factor() as f32, }, ..default() - }; - - cosmic_edit_set_text( + } + .set_text( CosmicText::OneStyle("😀😀😀 x => y\nRead only widget".to_string()), AttrsOwned::new(attrs), - &mut cosmic_edit.editor.0, &mut font_system.0, ); - // let mut id = None; + // Spawn the CosmicEditUiBundle as a child of root commands.entity(root).with_children(|parent| { id = Some(parent.spawn(cosmic_edit).insert(ReadOnly).id()); }); + // Set active editor commands.insert_resource(ActiveEditor { entity: id }); } diff --git a/src/lib.rs b/src/lib.rs index b8c9257bfbc31062076e0c429349bcb5afe564d9..548c95b4fb50caecec0e968dee830a4e92fa96ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,6 +69,73 @@ impl Default for CosmicEditor { } } +impl CosmicEditor { + pub fn set_text( + &mut self, + text: CosmicText, + attrs: AttrsOwned, + // i'd like to get this automagically but i'm too 3head -bytemunch + font_system: &mut FontSystem, + ) -> &mut Self { + let editor = &mut self.0; + editor.buffer_mut().lines.clear(); + match text { + CosmicText::OneStyle(text) => { + editor.buffer_mut().set_text( + font_system, + text.as_str(), + attrs.as_attrs(), + Shaping::Advanced, + ); + } + // TODO test + CosmicText::MultiStyle(lines) => { + for line in lines { + 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.buffer_mut().lines.push(BufferLine::new( + line_text, + attrs_list, + Shaping::Advanced, + )); + } + } + } + self + } + + /// Retrieves the cosmic text content from an editor. + /// + /// # Arguments + /// + /// * none, takes the rust magic ref to self + /// + /// # Returns + /// + /// A `String` containing the cosmic text content. + pub fn get_text(&self) -> String { + let buffer = self.0.buffer(); + let mut text = String::new(); + let line_count = buffer.lines.len(); + + for (i, line) in buffer.lines.iter().enumerate() { + text.push_str(line.text()); + + if i < line_count - 1 { + text.push('\n'); + } + } + + text + } +} + /// Adds the font system to each editor when added fn cosmic_editor_builder( mut added_editors: Query< @@ -79,7 +146,7 @@ fn cosmic_editor_builder( ) { for (mut editor, attrs, metrics) in added_editors.iter_mut() { // keep old text if set - let mut text = get_cosmic_text(editor.0.buffer()); + let mut text = editor.get_text(); if text.is_empty() { text = "".into(); @@ -167,6 +234,18 @@ pub struct CosmicEditUiBundle { pub background_image: CosmicBackground, } +impl CosmicEditUiBundle { + pub fn set_text( + mut self, + text: CosmicText, + attrs: AttrsOwned, + font_system: &mut FontSystem, + ) -> Self { + self.editor.set_text(text, attrs, font_system); + self + } +} + impl Default for CosmicEditUiBundle { fn default() -> Self { Self { @@ -301,13 +380,13 @@ fn change_active_editor( (Changed<Interaction>, With<CosmicEditor>), >, ) { - for (interaction, cosmic_edit, entity) in interaction_query.iter_mut() { + for (interaction, editor, entity) in interaction_query.iter_mut() { if let Interaction::Pressed = interaction { info!("PRESSED"); commands.insert_resource(ActiveEditor { entity: Some(entity), }); - info!("Widget text: {}", get_cosmic_text(cosmic_edit.0.buffer())); + info!("Widget text: {}", editor.get_text()); } } } @@ -406,73 +485,6 @@ pub fn get_node_cursor_pos( }) } -// Sets text in a CosmicEdit -pub fn cosmic_edit_set_text( - text: CosmicText, - attrs: AttrsOwned, - editor: &mut Editor, - font_system: &mut FontSystem, -) { - println!("SETTING TEXT"); - editor.buffer_mut().lines.clear(); - match text { - CosmicText::OneStyle(text) => { - println!("ONESTYLE {:?}, {:?}", text, attrs); - - editor.buffer_mut().set_text( - font_system, - text.as_str(), - attrs.as_attrs(), - Shaping::Advanced, - ); - } - CosmicText::MultiStyle(lines) => { - println!("M-M-M-M-MULTI-STYLE"); - - for line in lines { - 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.buffer_mut().lines.push(BufferLine::new( - line_text, - attrs_list, - Shaping::Advanced, - )); - } - } - } -} - -/// Retrieves the cosmic text content from an editor. -/// -/// # Arguments -/// -/// * `editor` - A reference to the `Editor` instance containing the text content. -/// -/// # Returns -/// -/// A `String` containing the cosmic text content. -// TODO put this on the CosmicEdit impl -pub fn get_cosmic_text(buffer: &Buffer) -> String { - let mut text = String::new(); - let line_count = buffer.lines.len(); - - for (i, line) in buffer.lines.iter().enumerate() { - text.push_str(line.text()); - - if i < line_count - 1 { - text.push('\n'); - } - } - - text -} - /// Returns texts from a MultiStyle buffer pub fn get_text_spans( buffer: &Buffer,