Skip to content
Snippets Groups Projects
  • sam edelsten's avatar
    Cosmic text 0.11 (#124) · 696fe6f7
    sam edelsten authored
    
    * barely got text entry working again
    
    * remove local optimizations
    
    i should really put them somewhere else on my machine
    
    * delete most stuff to get the basics nearly working
    
    * editable text
    
    lots of cloning but seems to be the only way if buffers are in the ECS
    
    editor widgets don't relayout when edited, but when unfocused the buffer does
    
    * fix backspace on native and make basic_ui wasm editable
    
    * fix mid-edit layout
    
    * reimplement text cursor blinking
    
    uhoh it's looking like im using the not a starting point pr as a starting point lol
    
    * update `basic_sprite` example
    
    adds a util crate for common example systems
    also removes history tracking structs
    
    * `image_background`+`bevy_api_testing` examples
    
    MV `bevy_api_testing.rs` > `sprite_and_ui_clickable.rs`
    
    * remove `CosmicText` in favor of buffer functions
    
    simplifys API and mirrors Bevy's Text functions more closely
    
    * strip autoheight + example, update other examples
    
    * fix wasm compilation
    
    * clippy
    
    * fix text
    
    * fix clippy
    
    * fix wasm
    
    * fix import
    
    * add changelog
    
    * fix mouse cursor hover on sprite widget
    
    * fix editor cursor location when edited
    
    * fix blank widget when focused on startup
    
    * configurable cursor/selection colors
    
    * run layout functions when needed
    
    * fix ui widget mouse cursor
    
    * fix scale change
    
    ---------
    
    Co-authored-by: default avatarDima <Dmytro.Rets@Gamesys.co.uk>
    Co-authored-by: default avatarStaffEngineer <velo.app1@gmail.com>
    Unverified
    696fe6f7
lib.rs 2.86 KiB
// Common functions for examples
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_cosmic_edit::*;

pub fn deselect_editor_on_esc(i: Res<ButtonInput<KeyCode>>, mut focus: ResMut<FocusedWidget>) {
    if i.just_pressed(KeyCode::Escape) {
        focus.0 = None;
    }
}

pub fn change_active_editor_sprite(
    mut commands: Commands,
    windows: Query<&Window, With<PrimaryWindow>>,
    buttons: Res<ButtonInput<MouseButton>>,
    mut cosmic_edit_query: Query<
        (&mut Sprite, &GlobalTransform, &Visibility, Entity),
        (With<CosmicBuffer>, Without<ReadOnly>),
    >,
    camera_q: Query<(&Camera, &GlobalTransform)>,
) {
    let window = windows.single();
    let (camera, camera_transform) = camera_q.single();
    if buttons.just_pressed(MouseButton::Left) {
        for (sprite, node_transform, visibility, entity) in &mut cosmic_edit_query.iter_mut() {
            if visibility == Visibility::Hidden {
                continue;
            }
            let size = sprite.custom_size.unwrap_or(Vec2::ONE);
            let x_min = node_transform.affine().translation.x - size.x / 2.;
            let y_min = node_transform.affine().translation.y - size.y / 2.;
            let x_max = node_transform.affine().translation.x + size.x / 2.;
            let y_max = node_transform.affine().translation.y + size.y / 2.;
            if let Some(pos) = window.cursor_position() {
                if let Some(pos) = camera.viewport_to_world_2d(camera_transform, pos) {
                    if x_min < pos.x && pos.x < x_max && y_min < pos.y && pos.y < y_max {
                        commands.insert_resource(FocusedWidget(Some(entity)))
                    };
                }
            };
        }
    }
}

pub fn change_active_editor_ui(
    mut commands: Commands,
    mut interaction_query: Query<
        (&Interaction, &CosmicSource),
        (Changed<Interaction>, Without<ReadOnly>),
    >,
) {
    for (interaction, source) in interaction_query.iter_mut() {
        if let Interaction::Pressed = interaction {
            commands.insert_resource(FocusedWidget(Some(source.0)));
        }
    }
}

pub fn print_editor_text(
    text_inputs_q: Query<&CosmicEditor>,
    mut previous_value: Local<Vec<String>>,
) {
    for text_input in text_inputs_q.iter() {
        let current_text: Vec<String> = text_input.with_buffer(|buf| {
            buf.lines
                .iter()
                .map(|bl| bl.text().to_string())
                .collect::<Vec<_>>()
        });
        if current_text == *previous_value {
            return;
        }
        *previous_value = current_text.clone();
        info!("Widget text: {:?}", current_text);
    }
}

pub fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor {
    CosmicColor::rgba(
        (color.r() * 255.) as u8,
        (color.g() * 255.) as u8,
        (color.b() * 255.) as u8,
        (color.a() * 255.) as u8,
    )
}