From 1963ca64d9d253510f256d3e9d098c080c292cd5 Mon Sep 17 00:00:00 2001 From: cohaereo Date: Sun, 14 Jan 2024 00:42:33 +0100 Subject: [PATCH] Global entity tag --- CHANGELOG.md | 2 ++ src/ecs/component_panels.rs | 12 +++++++++++- src/ecs/tags.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd4c473a..16045bab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) - Add Delete Button on Inspector Panel by @Froggy618157725 in [#11](https://github.com/cohaereo/alkahest/pull/11) - Show Havok shapes for 80809121 (Push Surfaces) by @DeltaDesigns in [#9](https://github.com/cohaereo/alkahest/pull/9) - Add Global Utility Objects by @Froggy618167725 in [#12](https://github.com/cohaereo/alkahest/pull/12) +- Lazy entity updating by @cohaereo +- Global entity tag by @cohaereo ### Changed diff --git a/src/ecs/component_panels.rs b/src/ecs/component_panels.rs index 32e39592..96a19911 100644 --- a/src/ecs/component_panels.rs +++ b/src/ecs/component_panels.rs @@ -25,7 +25,7 @@ use super::{ StaticInstances, Visible, }, resolve_entity_icon, resolve_entity_name, - tags::Tags, + tags::{insert_tag, remove_tag, EntityTag, Tags}, transform::{OriginalTransform, Transform}, Scene, }; @@ -109,8 +109,10 @@ pub fn show_inspector_panel( } let mut global = e.get::<&Global>().map_or(false, |g| g.0); + let mut global_changed = false; if e.has::() { if ui.checkbox(&mut global, "Show in all Maps").clicked() { + global_changed = true; if let Some(mut g) = e.get::<&mut Global>() { g.0 = global; } else { @@ -120,6 +122,14 @@ pub fn show_inspector_panel( ui.separator(); } show_inspector_components(ui, e, resources); + + if global_changed { + if global { + insert_tag(scene, ent, EntityTag::Global); + } else { + remove_tag(scene, ent, EntityTag::Global); + } + } } fn show_inspector_components(ui: &mut egui::Ui, e: EntityRef<'_>, resources: &Resources) { diff --git a/src/ecs/tags.rs b/src/ecs/tags.rs index ace909a1..3c333c46 100644 --- a/src/ecs/tags.rs +++ b/src/ecs/tags.rs @@ -1,15 +1,18 @@ +use std::fmt::Display; + use egui::Color32; use hecs::Entity; use nohash_hasher::IntSet; -use crate::{overlays::UiExt, util::text::name_to_color}; +use crate::{icons::ICON_WEB, overlays::UiExt, util::text::name_to_color}; use super::Scene; -#[derive(strum::Display, strum::EnumIter, Hash, PartialEq, Eq)] +#[derive(strum::EnumIter, Hash, PartialEq, Eq)] pub enum EntityTag { Activity, Ambient, + Global, Havok, Utility, User, @@ -26,6 +29,19 @@ impl EntityTag { } } +impl Display for EntityTag { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + EntityTag::Activity => write!(f, "Activity"), + EntityTag::Ambient => write!(f, "Ambient"), + EntityTag::Global => write!(f, "{} Global", ICON_WEB), + EntityTag::Havok => write!(f, "Havok"), + EntityTag::Utility => write!(f, "Utility"), + EntityTag::User => write!(f, "User"), + } + } +} + #[derive(Default)] pub struct Tags(pub IntSet); @@ -57,3 +73,9 @@ pub fn insert_tag(scene: &mut Scene, ent: Entity, tag: EntityTag) { scene.insert_one(ent, Tags::from_iter([tag])).ok(); } + +pub fn remove_tag(scene: &mut Scene, ent: Entity, tag: EntityTag) { + if let Ok(Some(mut e)) = scene.entity(ent).map(|e| e.get::<&mut Tags>()) { + e.0.remove(&tag); + } +}