Skip to main content

pretty_text_effects/
lib.rs

1//! Built-in text effects for [`bevy_pretty_text`].
2//!
3//! `bevy_pretty_effects` is an optional dependency, enabled by default in [`bevy_pretty_text`]
4//! with the `default_effects` feature.
5//!
6//! For creating custom effects, see [`pretty_text::dynamic_effects`].
7//!
8//! [`PrettyTextEffectAppExt`]: pretty_text::dynamic_effects::PrettyTextEffectAppExt
9//! [`DynamicEffect`]: pretty_text::dynamic_effects::DynamicEffect
10//! [`bevy_pretty_text`]: https://docs.rs/bevy_pretty_text
11
12#![allow(clippy::type_complexity)]
13#![warn(missing_debug_implementations, missing_docs, clippy::doc_markdown)]
14
15use bevy::prelude::*;
16use bevy_pretty_text::glyph::{Glyph, GlyphSpanEntity};
17
18mod glitch;
19mod scramble;
20mod shake;
21mod wave;
22mod wobble;
23
24pub use glitch::Glitch;
25pub use scramble::{Scramble, ScrambleLifetime, ScrambleSpeed};
26pub use shake::Shake;
27pub use wave::Wave;
28pub use wobble::Wobble;
29
30extern crate pretty_text as bevy_pretty_text;
31
32/// `pretty_text_effects`'s top-level plugin.
33///
34/// This initializes the built-in text effects for [`bevy_pretty_text`].
35#[derive(Debug)]
36pub struct EffectsPlugin;
37
38impl Plugin for EffectsPlugin {
39    fn build(&self, app: &mut App) {
40        glitch::plugin(app);
41        scramble::plugin(app);
42        shake::plugin(app);
43        wave::plugin(app);
44        wobble::plugin(app);
45    }
46}
47
48/// This observer triggers whenever a [`Glyph`] is spawned and checks if the
49/// glyph's [`GlyphSpanEntity`] has the target `Effect`. If it does, then `Marker`
50/// is inserted into the glyph.
51pub fn apply_effect_on_glyphs<Effect: Component, Marker: Default + Component>(
52    trigger: Trigger<OnAdd, Glyph>,
53    mut commands: Commands,
54    spans: Query<&GlyphSpanEntity>,
55    effects: Query<&Effect>,
56) -> Result {
57    let span_entity = spans.get(trigger.target())?;
58    if effects.get(span_entity.0).is_ok() {
59        commands.entity(trigger.target()).insert(Marker::default());
60    }
61    Ok(())
62}