1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2020-2021 Ian Jackson and contributors to Otter
// SPDX-License-Identifier: AGPL-3.0-or-later
// There is NO WARRANTY.

use crate::prelude::*;

pub type VisiblePieceAngle = PieceAngle;

#[derive(Clone,Debug)]
#[derive(Serialize,Deserialize)]
#[serde(transparent)]
pub struct VisibleAngleTransform(Html);

#[derive(Debug,Clone)]
pub struct PieceRenderInstructions {
  pub vpid: VisiblePieceId,
  pub occulted: PriOcculted,
}
deref_to_field!{PieceRenderInstructions, PriOcculted, occulted}

#[derive(Debug,Clone)]
pub enum PriOccultedGeneral<P,Z> {
  Visible(ShowUnocculted),
  Occulted,
  Displaced(P, Z),
}

pub type PriOcculted = PriOccultedGeneral<Pos, ZLevel>;

impl VisiblePieceAngle {
  pub fn to_transform(self) -> VisibleAngleTransform {
    VisibleAngleTransform(Html::from_html_string(
      base_misc::raw_angle_transform(
        self.to_compass().into()
      )))
  }

  pub fn to_compass(self) -> CompassAngle {
    match self {
      PieceAngle::Compass(compass) => compass,
    }
  }
}

impl<P,Z> PriOccultedGeneral<P,Z> {
  #[throws(IE)]
  pub fn instead<'p>(&self, ioccults: &'p IOccults, p: &'p IPiece)
                 -> Either<ShowUnocculted, &'p dyn OccultedPieceTrait>
  {
    p.show_or_instead(ioccults, self.fully_visible())?
  }

  pub fn fully_visible(&self) -> Option<ShowUnocculted> {
    use PriOG::*;
    match self {
      Visible(v) => Some(*v),
      Occulted | Displaced(..) => None,
    }
  }

  pub fn describe(&self, ioccults: &IOccults, goccults: &GameOccults,
                  gpc: &GPiece, ipc: &IPiece) -> Html
  {
    self.describe_fallible(ioccults, goccults, gpc, ipc)
      .unwrap_or_else(|e| {
        error!("error describing piece: {:?}", e);
        "<internal error describing piece>".to_html()
      })
  }

  #[throws(IE)]
  pub fn describe_fallible(&self, ioccults: &IOccults,
                           goccults: &GameOccults,
                           gpc: &GPiece, ipc: &IPiece) -> Html {
    match self.instead(ioccults, ipc)? {
      Left(y) => ipc.show(y).describe_html(gpc, goccults)?,
      Right(i) => i.describe_html()?,
    }
  }
}

impl PieceRenderInstructions {
  #[throws(IE)]
  pub fn map_piece_update_op(&self, ioccults: &IOccults, gs: &GameState,
                             gpc: &GPiece, ipc: &IPiece,
                             op: PieceUpdateOp<(),()>
  ) -> Option<PieceUpdateOp<PreparedPieceState, ZLevel>>
  {
    use PieceUpdateOp::*;
    use PriOG::*;
    if matches_doesnot!(
      op,
      = Move(_) | MoveQuiet(_) | SetZLevel(_) | SetZLevelQuiet(_),
      ! Delete() | Insert(_) | Modify(_) | ModifyQuiet(_),
    ) {
      match self.occulted {
        Visible(_) | Occulted => (),
        Displaced(..) => return None,
      }
    }

    let op = op.try_map(
      |()|{
        let ns = self.prep_piecestate(ioccults, gs, gpc, ipc)?;
        <Result<_,InternalError>>::Ok(ns)
      },
      |()|{
        Ok(gpc.zlevel.clone())
      }
    )?;
    Some(op)
  }
  
  #[throws(IE)]
  pub fn prep_piecestate(&self, ioccults: &IOccults, gs: &GameState,
                         gpc: &GPiece, ipc: &IPiece)
                         -> PreparedPieceState {
    let pri = self;
    let (pos, zlevel) = pri.pos_zlevel(gpc);
    let occregion = gpc.occult.active_region(&gs.occults)?
      .map(|r| JsonString(r.clone()));
    let (svg, bbox) = pri.make_defs(ioccults, gs, gpc, ipc)?;
    let r = PreparedPieceState {
      pos        : pos,
      held       : gpc.held,
      svg        : svg,
      bbox       : bbox,
      z          : zlevel.z.clone(),
      zg         : zlevel.zg,
      angle      : pri.angle(gpc).to_compass(),
      pinned     : gpc.pinned,
      rotateable : gpc.rotateable(),
      uos        : pri.ui_operations(gs, gpc, ipc)?,
      moveable   : gpc.moveable(),
      facehint   : pri.facehint(gpc),
      occregion,
    };
    dbgc!(pri, ipc, gpc, r);
    r
  }

  #[throws(IE)]
  pub fn prep_pieceimage(&self, ioccults: &IOccults, gs: &GameState,
                         gpc: &GPiece, ipc: &IPiece)
                         -> PreparedPieceImage {
    let pri = self;
    let (svg, bbox) = pri.make_defs(ioccults, gs, gpc, ipc)?;
    let r = PreparedPieceImage {
      svg        : svg,
      bbox       : bbox,
      uos        : pri.ui_operations(gs, gpc, ipc)?,
    };
    dbgc!(pri, ipc, gpc, r);
    r
  }

  pub fn angle(&self, gpc: &GPiece) -> VisiblePieceAngle {
    match self.occulted {
      PriOcculted::Visible(_)                            => gpc.angle,
      PriOcculted::Occulted | PriOcculted::Displaced(..) => default(),
    }
  }

  pub fn facehint(&self, gpc: &GPiece) -> Option<FaceId> {
    match self.occulted {
      PriOcculted::Visible(_)                            => Some(gpc.face),
      PriOcculted::Occulted | PriOcculted::Displaced(..) => None,
    }
  }

  pub fn pos_zlevel<'r>(&'r self, gpc: &'r GPiece) -> (Pos, &'r ZLevel) {
    use PriOcculted as PO;
    match &self.occulted {
      PO::Visible(_) | PO::Occulted => (gpc.pos, &gpc.zlevel),
      PO::Displaced(pos, zlevel) => (*pos, &zlevel),
    }
  }

  #[throws(IE)]
  pub fn make_defs<'p>(&self, ioccults: &IOccults, gs: &GameState,
                         gpc: &GPiece, ipc: &IPiece) -> (Html, Rect)
  {
    let pri = self;
    let instead = pri.instead(ioccults, ipc)?;

    let o: &dyn OutlineTrait = match instead {
      Left(y) => Borrow::<dyn PieceTrait>::borrow(ipc.show(y)).dyn_upcast(),
      Right(i) => i.dyn_upcast(),
    };

    let angle = pri.angle(gpc);
    let bbox = o.bbox_approx()?;

    let dragraise = match o.thresh_dragraise()? {
      Some(n) if n < 0 => throw!(SvgE::NegativeDragraise),
      Some(n) => n,
      None => -1,
    };

    let transform = angle.to_transform();

    let mut defs = Html::new();
    hwrite!(&mut defs,
           r##"<g id="piece{}" transform="{}" data-dragraise="{}">"##,
           pri.vpid, &transform.0, dragraise)?;

    match instead {
      Left(y) => {
        ipc.show(y).svg_piece(&mut defs, gpc, gs, pri.vpid)?;
      },
      Right(i) => {
        i.svg(&mut defs, pri.vpid)?;
      },
    };

    hwrite!(&mut defs, r##"</g>"##)?;
    hwrite!(&mut defs,
           r##"<path id="surround{}" d="{}"/>"##,
           pri.vpid, o.surround_path()?)?;
    (defs, bbox)
  }

  #[throws(InternalError)]
  pub fn ui_operations(&self, gs: &GameState, gpc: &GPiece, ipc: &IPiece)
                       -> Vec<UoDescription>
  {
    let y = match self.occulted {
      PriOcculted::Visible(y)                            => y,
      PriOcculted::Occulted | PriOcculted::Displaced(..) => return vec![],
    };

    type WRC = WhatResponseToClientOp;

    let mut out = vec![];
    if ipc.show(y).nfaces() > 1 {
      out.push(UoDescription {
        wrc: WRC::UpdateSvg,
        kind: UoKind::Global,
        def_key: 'f'.into(),
        opname: "flip".to_string(),
        desc: Html::lit("flip").into(),
      })
    }
    ipc.show(y).add_ui_operations(y, &mut out, gs, gpc)?;
    out
  }
}