From 76408b734e082ee149e558e1e2ee8a1114cef742 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 06:12:31 +0100 Subject: [PATCH] Text displays: an Align enum, and the text shared rather than copied Co-Authored-By: Claude Opus 5.5 --- .../examples/probe/text_display_fixture.rs | 2 +- crates/horizon-client/src/text_display.rs | 29 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/examples/probe/text_display_fixture.rs b/crates/horizon-client/examples/probe/text_display_fixture.rs index 9146c4587..49dc2ada4 100644 --- a/crates/horizon-client/examples/probe/text_display_fixture.rs +++ b/crates/horizon-client/examples/probe/text_display_fixture.rs @@ -21,7 +21,7 @@ pub fn main() -> Result<(), Box> { display.apply(&packet.values); let mut state = serde_json::to_value(&display)?; state["text"] = display.text.plain().into(); - state["alignment"] = display.alignment().into(); + state["alignment"] = format!("{:?}", display.align()).to_uppercase().into(); writeln!(out, "{}", state)?; } Ok(()) diff --git a/crates/horizon-client/src/text_display.rs b/crates/horizon-client/src/text_display.rs index 00bc16e3f..ab0fc4f5f 100644 --- a/crates/horizon-client/src/text_display.rs +++ b/crates/horizon-client/src/text_display.rs @@ -3,7 +3,18 @@ use crate::text::Text; use horizon_protocol::entity_data::Value; use serde::Serialize; +use std::sync::Arc; +/// `TextDisplay.Align`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Align { + Center, + Left, + Right, +} + +/// `Display` and `TextDisplay`'s synced data (ids 8 to 27); the default is +/// what their `defineSynchedData` starts with. #[derive(Clone, Debug, Serialize)] pub struct TextDisplayData { pub interpolation_delay: i32, @@ -21,8 +32,9 @@ pub struct TextDisplayData { pub width: f32, pub height: f32, pub glow_color: i32, + /// Shared: a frame's interpolated copy of the data must not copy it. #[serde(skip)] - pub text: Text, + pub text: Arc, pub line_width: i32, pub background: i32, pub opacity: i8, @@ -47,7 +59,7 @@ impl Default for TextDisplayData { width: 0.0, height: 0.0, glow_color: -1, - text: Text::empty(), + text: Arc::new(Text::empty()), line_width: 200, background: 0x4000_0000, opacity: -1, @@ -76,7 +88,7 @@ impl TextDisplayData { (20, Value::Float(v)) => self.width = *v, (21, Value::Float(v)) => self.height = *v, (22, Value::Int(v)) => self.glow_color = *v, - (23, Value::Text(v)) => self.text = crate::entity_nbt::name_text(v), + (23, Value::Text(v)) => self.text = Arc::new(crate::entity_nbt::name_text(v)), (24, Value::Int(v)) => self.line_width = *v, (25, Value::Int(v)) => self.background = *v, (26, Value::Byte(v)) => self.opacity = *v, @@ -85,13 +97,14 @@ impl TextDisplayData { } } } - pub fn alignment(&self) -> &'static str { + /// `TextDisplay.getAlign`: the left flag wins over the right one. + pub fn align(&self) -> Align { if self.flags & 8 != 0 { - "LEFT" + Align::Left } else if self.flags & 16 != 0 { - "RIGHT" + Align::Right } else { - "CENTER" + Align::Center } } } @@ -105,7 +118,7 @@ mod tests { d.apply(&[(24, 1, Value::Int(80)), (27, 0, Value::Byte(24))]); d.apply(&[(11, 0, Value::Floats(vec![1.0]))]); assert_eq!(d.line_width, 80); - assert_eq!(d.alignment(), "LEFT"); + assert_eq!(d.align(), Align::Left); assert_eq!(d.translation, [0.0; 3]); } }