chiark / gitweb /
currency: Move qty into xdata
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 18 Apr 2022 19:03:38 +0000 (20:03 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 18 Apr 2022 19:07:15 +0000 (20:07 +0100)
When we multigrab, we must split, which involves mutating the value of
the grabbed piece.  So the value must be in xdata, not ipc.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/currency.rs

index 9ad96dfda5bbe9e87bed6c162fa6686ff8e181b4..1a69fc22844a6210ec6a2a0b16fa05a0bdda426b 100644 (file)
@@ -30,10 +30,17 @@ pub struct Spec {
 pub struct Banknote {
   itemname: String,
   image: Arc<dyn InertPieceTrait>,
-  qty: Qty,
   currency: String,
 }
 
+#[derive(Debug,Serialize,Deserialize)]
+pub struct Value {
+  qty: Qty,
+}
+
+#[typetag::serde(name="Currency")]
+impl PieceXData for Value { fn dummy() -> Self { Value { qty: 0 } } }
+
 #[typetag::serde(name="Currency")]
 impl PieceSpec for Spec {
   #[throws(SpecError)]
@@ -54,11 +61,13 @@ impl PieceSpec for Spec {
         exp: 1,              exp_why: "needed".into(),
       });
     }
+
+    let _value: &mut Value = gpc.xdata_mut(|| Value { qty })?;
         
     let bnote = Banknote {
       image: image.into(),
       currency: currency.clone(),
-      itemname, qty,
+      itemname,
     };
 
     let special = PieceSpecialProperties {
@@ -90,9 +99,10 @@ impl PieceBaseTrait for Banknote {
 impl PieceTrait for Banknote {
   #[throws(IE)]
   fn describe_html(&self, gpc: &GPiece, _: &GameOccults) -> Html {
+    let value: &Value = gpc.xdata.get_exp()?;
     hformat!("{}, {}{}",
              self.image.describe_html(gpc.face)?,
-             self.qty, &self.currency)
+             value.qty, &self.currency)
   }
 
   #[throws(IE)]
@@ -100,6 +110,7 @@ impl PieceTrait for Banknote {
                vpid: VisiblePieceId) {
     self.image.svg(f, vpid, gpc.face, &gpc.xdata)?;
     
+    let value: &Value = gpc.xdata.get_exp()?;
     let label_font_size = QTY_FONT_SIZE;
     let label_y_adj = label_font_size * SVG_FONT_Y_ADJUST_OF_FONT_SIZE;
 
@@ -107,6 +118,6 @@ impl PieceTrait for Banknote {
             r##"<{} text-align="center" text-anchor="middle" x="0" y="{}" font-size="{}">{}{}</text>"##,
             HTML_TEXT_LABEL_ELEM_START,
             label_y_adj, label_font_size,
-            self.qty, &self.currency)?;
+            value.qty, &self.currency)?;
   }
 }