From: Ian Jackson Date: Sun, 27 Sep 2020 21:57:05 +0000 (+0100) Subject: UI operations, before redo X-Git-Tag: otter-0.2.0~826 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=647bac5934d3345c0e84cf50dfb270507df2b758;p=otter.git UI operations, before redo Signed-off-by: Ian Jackson --- diff --git a/src/gamestate.rs b/src/gamestate.rs index db2f00e2..2caf94ad 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -88,10 +88,45 @@ pub trait Outline : Send + Debug { fn bbox_approx(&self) -> [Pos;2]; } +pub type UoKey = char; + +pub struct UoDescription { + pub def_key: UoKey, + pub opname: String, + pub desc: Html, +} + +impl UoDescription { + pub fn new_flip() -> UoDescription { UoDescription { + def_key: 'f'.into(), + opname: "flip".to_string(), + desc: Html::lit("flip"), + } } +} + +#[throws(ApiPieceOpError)] +pub fn ui_operation_flip(gs: &mut GameState, player: PlayerId, + piece: PieceId, lens: &dyn Lens, + def_key: UoKey, nfaces: RawFaceId) + -> PieceUpdateFromOp { + let pl = gs.players.byid(player)?; + let pc = gs.pieces.byid_mut(piece)?; + pc.face = (pc.face.into::() % nfaces).into(); + (PieceUpdateOp::Modify(()), LogEntry { html: Html(format!( + "{} flipped {}", + &htmlescape::encode_minimal(&pl.nick), + pc.describe_pri(&lens.log_pri(piece, pc)).0))}) +} + #[typetag::serde] pub trait Piece : Outline + Send + Debug { fn resolve_spec_face(&self, face : Option) -> Result; + fn ui_operations<'p>(&'p self) -> Result +'p + >>, IE>; + fn ui_operation(&self, gs: &mut GameState, player: PlayerId, piece: PieceId, + def_key: char, lens: &dyn Lens) -> PieceUpdateResult; // #[throws] doesn't work here for some reason fn svg_piece(&self, f: &mut Html, pri: &PieceRenderInstructions) -> IR; diff --git a/src/pieces.rs b/src/pieces.rs index f30e5361..77528f6f 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -143,6 +143,25 @@ impl Piece for SimpleShape { face } + #[throws(IE)] + fn ui_operations(&self) -> Box> { + if self.colours.len() > 1 { + Box::new(iter::once(UoDescription::new_flip())) + } else { + Box::new(iter::empty()) + } + } + + #[throws(ApiPieceOpError)] + fn ui_operation(&self, gs: &mut GameState, player: PlayerId, piece: PieceId, + def_key: UoKey, lens: &dyn Lens) -> PieceUpdateFromOp { + if let Some(got) = + ui_operation_flip(gs,player,piece,def_key,lens, + self.colours().into())? { return got } + throw!(OE::BadOperation) + } + + fn itemname(&self) -> &str { &self.itemname } } diff --git a/src/shapelib.rs b/src/shapelib.rs index bd46f014..d80b06df 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -184,6 +184,8 @@ impl Piece for Item { } fn itemname(&self) -> &str { &self.itemname } + + } #[throws(SpecError)]