chiark / gitweb /
UI operations, before redo
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Sep 2020 21:57:05 +0000 (22:57 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Sep 2020 21:57:05 +0000 (22:57 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/gamestate.rs
src/pieces.rs
src/shapelib.rs

index db2f00e250bc924e226a0e078cddfb832fdd6f43..2caf94ad1c84ed80f4a4a289ecd654d7875e527f 100644 (file)
@@ -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::<RawFaceId>() % 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<FaceId>)
                        -> Result<FaceId,SpecError>;
+  fn ui_operations<'p>(&'p self) -> Result<Option<
+      Box<dyn ExactSizeIterator<Item=&UoDescription> +'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;
index f30e536101c56ac0c43231226bcc7748870c65b6..77528f6f93f2df7837e96a650509dc6665fdd188 100644 (file)
@@ -143,6 +143,25 @@ impl Piece for SimpleShape {
     face
   }
 
+  #[throws(IE)]
+  fn ui_operations(&self) -> Box<dyn ExactSizeIterator<Item=&UoDescription>> {
+    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 }
 }
 
index bd46f0147a5ed54ff7d9e8eda493b15a3e2259d6..d80b06df629ca535b2f6ed007b1f007bfdc54a19 100644 (file)
@@ -184,6 +184,8 @@ impl Piece for Item {
   }
 
   fn itemname(&self) -> &str { &self.itemname }
+
+  
 }
 
 #[throws(SpecError)]