Skip to content
Snippets Groups Projects
  • sam's avatar
    Simplify main render system (#106) · 0bb15dea
    sam authored
    * add TODOs
    
    * separate CosmicEdit from display
    
    breaks loads of stuff: sizing, click functions, cursor hovering just a few
    
    Only tested on login example
    
    not optimized; spams handle weak clones to ensure got correct handle (will be fixed by new handle swapping routine)
    
    not sure about the API changes but might just need to get used to it, is much more extensible
    
    * break render function apart
    
    centered padding now broken, not tested other text positions
    
    * reshape earlier in render to fix padding issue
    
    * move image creation to own system
    
    * resize internal sprite to computed UI size
    
    * prevent panic on zero size UI elements
    
    * clicks + editor-display one-to-many relationships
    
    * add render systemset
    
    * fix hoverable invisible sprites
    
    * update examples
    
    * fix auto-height bugs
    
    * clippy
    Unverified
    0bb15dea
render.rs 20.74 KiB
use std::time::Duration;

use bevy::{
    asset::HandleId,
    prelude::*,
    render::{render_resource::Extent3d, texture::DEFAULT_IMAGE_HANDLE},
    utils::HashMap,
    window::{PrimaryWindow, WindowScaleFactorChanged},
};
use cosmic_text::{Affinity, Edit, Metrics, SwashCache};
use image::{imageops::FilterType, GenericImageView};

use crate::{
    get_text_size, get_x_offset_center, get_y_offset_center, CosmicAttrs, CosmicBackground,
    CosmicEditor, CosmicFontSystem, CosmicMetrics, CosmicMode, CosmicSource, CosmicText,
    CosmicTextPosition, FillColor, Focus, PasswordInput, PlaceholderAttrs, PlaceholderText,
    ReadOnly, XOffset, DEFAULT_SCALE_PLACEHOLDER,
};

#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
pub enum CosmicRenderSet {
    Setup,
    Shaping,
    Sizing,
    Cursor,
    Padding,
    Draw,
}

#[derive(Resource)]
pub(crate) struct SwashCacheState {
    pub swash_cache: SwashCache,
}

#[derive(Resource)]
pub(crate) struct CursorBlinkTimer(pub Timer);

#[derive(Resource)]
pub(crate) struct CursorVisibility(pub bool);

#[derive(Resource, Default)]
pub(crate) struct PasswordValues(pub HashMap<Entity, (String, usize)>);

#[derive(Component)]
pub(crate) struct Placeholder;

#[derive(Component, Default)]
pub struct CosmicPadding(pub Vec2);

#[derive(Component, Default)]
pub struct CosmicWidgetSize(pub Vec2);

pub(crate) fn cosmic_padding(
    mut query: Query<(
        &mut CosmicPadding,
        &CosmicTextPosition,
        &CosmicEditor,
        &CosmicWidgetSize,
    )>,
) {
    for (mut padding, position, editor, size) in query.iter_mut() {
        padding.0 = match position {
            CosmicTextPosition::Center => Vec2::new(
                get_x_offset_center(size.0.x, editor.0.buffer()) as f32,
                get_y_offset_center(size.0.y, editor.0.buffer()) as f32,
            ),
            CosmicTextPosition::TopLeft { padding } => Vec2::new(*padding as f32, *padding as f32),
            CosmicTextPosition::Left { padding } => Vec2::new(
                *padding as f32,
                get_y_offset_center(size.0.y, editor.0.buffer()) as f32,