|
| 1 | +use bevy::{ |
| 2 | + prelude::*, |
| 3 | + utils::{HashMap, HashSet}, |
| 4 | +}; |
| 5 | + |
| 6 | +pub struct ColorPlugin; |
| 7 | + |
| 8 | +impl Plugin for ColorPlugin { |
| 9 | + fn build(&self, app: &mut App) { |
| 10 | + app.init_resource::<ColorMap>() |
| 11 | + .add_systems(Update, update_colors); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug, Clone)] |
| 16 | +pub enum BbCodeColor { |
| 17 | + Named(String), |
| 18 | + Static(Color), |
| 19 | +} |
| 20 | + |
| 21 | +impl BbCodeColor { |
| 22 | + pub fn to_color(&self, color_map: &ColorMap) -> Option<Color> { |
| 23 | + match self { |
| 24 | + Self::Static(color) => Some(*color), |
| 25 | + Self::Named(name) => color_map.get(name), |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl From<Color> for BbCodeColor { |
| 31 | + fn from(value: Color) -> Self { |
| 32 | + Self::Static(value) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl From<String> for BbCodeColor { |
| 37 | + fn from(value: String) -> Self { |
| 38 | + Self::Named(value) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Debug, Resource, Default)] |
| 43 | +pub struct ColorMap { |
| 44 | + /// The map from name to color. |
| 45 | + map: HashMap<String, Color>, |
| 46 | + |
| 47 | + /// Internal tracker for names where the corresponding color has been updated. |
| 48 | + /// |
| 49 | + /// Used to only update what's needed. |
| 50 | + was_updated: HashSet<String>, |
| 51 | +} |
| 52 | + |
| 53 | +impl ColorMap { |
| 54 | + /// Insert (add or update) a new named color. |
| 55 | + /// |
| 56 | + /// Returns `&mut self` for chaining. |
| 57 | + pub fn insert<N, C>(&mut self, name: N, color: C) -> &mut Self |
| 58 | + where |
| 59 | + N: Into<String>, |
| 60 | + C: Into<Color>, |
| 61 | + { |
| 62 | + let name = name.into(); |
| 63 | + self.map.insert(name.clone(), color.into()); |
| 64 | + self.was_updated.insert(name); |
| 65 | + self |
| 66 | + } |
| 67 | + |
| 68 | + /// Get the color for the given name. |
| 69 | + pub fn get(&self, name: &str) -> Option<Color> { |
| 70 | + self.map.get(name).copied() |
| 71 | + } |
| 72 | + |
| 73 | + /// Determine if any color has been updated. |
| 74 | + pub(crate) fn has_update(&self) -> bool { |
| 75 | + !self.was_updated.is_empty() |
| 76 | + } |
| 77 | + |
| 78 | + /// Determine if the color with the given name has been updated, and if yes to which value. |
| 79 | + /// |
| 80 | + /// You should probably call [`ColorMap::clear_was_updated`] at some point afterwards. |
| 81 | + pub(crate) fn get_update(&self, name: &str) -> Option<Color> { |
| 82 | + if self.was_updated.contains(name) { |
| 83 | + self.map.get(name).copied() |
| 84 | + } else { |
| 85 | + None |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Clear the tracker for the color names which had their values updated. |
| 90 | + pub(crate) fn clear_was_updated(&mut self) { |
| 91 | + self.was_updated.clear(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Tracker for text that's colored via named BBCode components. |
| 96 | +#[derive(Debug, Component)] |
| 97 | +pub struct BbCodeColored { |
| 98 | + pub name: String, |
| 99 | +} |
| 100 | + |
| 101 | +/// Update all colors whose name has changed. |
| 102 | +fn update_colors( |
| 103 | + mut color_map: ResMut<ColorMap>, |
| 104 | + mut colored_text_query: Query<(&BbCodeColored, &mut Text)>, |
| 105 | +) { |
| 106 | + if !color_map.is_changed() || !color_map.has_update() { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + for (colored, mut text) in colored_text_query.iter_mut() { |
| 111 | + if let Some(color) = color_map.get_update(&colored.name) { |
| 112 | + for section in &mut text.sections { |
| 113 | + section.style.color = color; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + color_map.clear_was_updated(); |
| 119 | +} |
0 commit comments