Skip to content
Snippets Groups Projects
text.rs 3.53 KiB
Newer Older
StarToaster's avatar
StarToaster committed
use bevy::prelude::*;
use kayak_font::Alignment;
use kayak_ui_macros::rsx;
StarToaster's avatar
StarToaster committed

use crate::{
    context::WidgetName,
    prelude::KayakWidgetContext,
    styles::{KCursorIcon, KStyle, RenderCommand, StyleProp},
    widget::Widget,
StarArawn's avatar
StarArawn committed
};

use super::ElementBundle;

#[derive(Component, Debug, PartialEq, Clone)]
pub struct TextProps {
MrGVSV's avatar
MrGVSV committed
    /// The string to display
    pub content: String,
MrGVSV's avatar
MrGVSV committed
    /// The name of the font to use
    ///
    /// The given font must already be loaded into the [`KayakContext`](kayak_core::KayakContext)
    pub font: Option<String>,
MrGVSV's avatar
MrGVSV committed
    /// The height of a line of text (currently in pixels)
    pub line_height: Option<f32>,
    /// If true, displays the default text cursor when hovered.
    ///
Gino Valente's avatar
Gino Valente committed
    /// This _will_ override the `cursor` style.
    pub show_cursor: bool,
MrGVSV's avatar
MrGVSV committed
    /// The font size (in pixels)
    ///
    /// Negative values have no effect
    pub size: f32,
StarToaster's avatar
StarToaster committed
    /// Text alignment.
    pub alignment: Alignment,
    /// Custom styles to pass in.
    pub user_styles: KStyle,
StarToaster's avatar
StarToaster committed
    /// Basic word wrapping.
    /// Defautls to true
    pub word_wrap: bool,
impl Default for TextProps {
    fn default() -> Self {
            content: String::new(),
            font: None,
            line_height: None,
            show_cursor: false,
            size: -1.0,
StarToaster's avatar
StarToaster committed
            alignment: Alignment::Start,
StarToaster's avatar
StarToaster committed
            word_wrap: true,
            user_styles: Default::default(),
StarToaster's avatar
StarToaster committed
impl Widget for TextProps {}
/// A widget that renders text
///
StarToaster's avatar
StarToaster committed
#[derive(Bundle)]
pub struct TextWidgetBundle {
    pub text: TextProps,
    pub styles: KStyle,
    pub widget_name: WidgetName,
}

impl Default for TextWidgetBundle {
    fn default() -> Self {
        Self {
            text: Default::default(),
            styles: KStyle {
                ..Default::default()
            },
StarToaster's avatar
StarToaster committed
            widget_name: TextProps::default().get_name(),
        }
StarArawn's avatar
StarArawn committed
    }
pub fn text_render(
    In((widget_context, entity)): In<(KayakWidgetContext, Entity)>,
    mut commands: Commands,
    mut query: Query<&TextProps>,
StarToaster's avatar
StarToaster committed
) -> bool {
    if let Ok(text) = query.get_mut(entity) {
        let mut style = KStyle::default();
        style = style.with_style(&text.user_styles).with_style(KStyle {
            render_command: StyleProp::Value(RenderCommand::Text {
                content: text.content.clone(),
                alignment: text.alignment,
StarToaster's avatar
StarToaster committed
                word_wrap: text.word_wrap,
            }),
            font: if let Some(ref font) = text.font {
                StyleProp::Value(font.clone())
            } else {
                StyleProp::default()
            },
            cursor: if text.show_cursor {
                StyleProp::Value(KCursorIcon(CursorIcon::Text))
            } else {
                StyleProp::default()
            },
            font_size: if text.size >= 0.0 {
                StyleProp::Value(text.size)
            } else {
                StyleProp::default()
            },
            line_height: if let Some(line_height) = text.line_height {
                StyleProp::Value(line_height)
            } else {
                StyleProp::default()
            },
            // bottom: Units::Stretch(1.0).into(),
            // top: Units::Stretch(1.0).into(),
            // left: Units::Stretch(0.0).into(),
            // right: Units::Stretch(0.0).into(),
            ..Default::default()
        });
        // style.cursor = StyleProp::Value(KCursorIcon(CursorIcon::Hand));

        let parent_id = Some(entity);
        rsx! {
            <ElementBundle styles={style} />
        }
StarArawn's avatar
StarArawn committed
}