From: Ian Jackson Date: Fri, 26 Jun 2020 16:30:28 +0000 (+0100) Subject: new display for ids etc. X-Git-Tag: otter-0.2.0~1556 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=3bd94f40ba3aaeb6454cb3cc755d429cb720d6d3;p=otter.git new display for ids etc. --- diff --git a/src/bin/server.rs b/src/bin/server.rs index 634f936e..5a3c4819 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -74,7 +74,7 @@ fn session(form : Json) -> Result { for (gpid, pr) in &g.gs.pieces { let id : slotmap::KeyData = gpid.into(); let pri = PieceRenderInstructions { - id : id.as_ffi(), + id : VisiblePieceId(id.as_ffi()), face : pr.face, }; defs.push(pr.p.svg_defs(&pri)); diff --git a/src/gamestate.rs b/src/gamestate.rs index ceb2d67b..6d2c3280 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -5,14 +5,35 @@ slotmap::new_key_type!{ pub struct PieceId; } -type VisiblePieceId = u64; +pub struct VisiblePieceId (pub u64); +#[derive(Debug)] pub struct PieceRenderInstructions { pub id : VisiblePieceId, pub face : FaceId, } +pub type VisiblePieceIdSvgIds = &'static [&'static str]; + +impl Display for VisiblePieceId { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(f, "{}-{}", self.0 >> 32, self.0 & 0xffffffff) + } +} +impl Debug for VisiblePieceId { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + ::fmt(self, f) + } +} + +impl PieceRenderInstructions { + pub fn id_piece(&self) -> String { format!("piece{:}", self.id) } + pub fn id_select(&self) -> String { format!("select{:}", self.id) } + pub fn id_other(&self, w : &str) -> String { format!("d{}-{}", self.id, w) } +} + pub trait Piece : Send + Debug { + fn svg_other_ids(&self) -> VisiblePieceIdSvgIds; fn svg_defs(&self, pri : &PieceRenderInstructions) -> String; } diff --git a/src/imports.rs b/src/imports.rs index 478a6663..36f54cda 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -1,7 +1,7 @@ pub use std::io; pub use std::io::{BufReader,Read}; -pub use std::fmt::Debug; +pub use std::fmt::{self,Display,Debug}; pub use std::thread; pub use std::time::Duration; pub use std::sync::{Arc,Mutex,RwLock}; diff --git a/src/pieces.rs b/src/pieces.rs index 3c3d1c99..5ba90784 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -12,8 +12,9 @@ struct SimpleShape { } impl Piece for SimpleShape { + fn svg_other_ids(&self) -> VisiblePieceIdSvgIds { &["base"] } fn svg_defs(&self, pri : &PieceRenderInstructions) -> String { - format!(r#"{}"#, pri.id, self.shape) + format!(r#"{}"#, pri.id_other("base"), self.shape) } }