From: Ian Jackson Date: Tue, 26 Jan 2021 23:02:39 +0000 (+0000) Subject: wdt: Break out posg2posw, nfc X-Git-Tag: otter-0.4.0~674 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8ef19275c5e4550688a0d630b996ece0e0f227ad;p=otter.git wdt: Break out posg2posw, nfc Signed-off-by: Ian Jackson --- diff --git a/wdriver.rs b/wdriver.rs index a0858d23..35dbae41 100644 --- a/wdriver.rs +++ b/wdriver.rs @@ -774,43 +774,11 @@ impl<'g> WindowGuard<'g> { w: self, } } -} - -pub type WebCoord = i32; -pub type WebPos = (WebCoord, WebCoord); - -pub struct PieceElement<'g> { - pieceid: &'g str, - w: &'g WindowGuard<'g>, - elem: t4::WebElement<'g>, -} - -impl<'g> Deref for PieceElement<'g> { - type Target = t4::WebElement<'g>; - fn deref<'i>(&'i self) -> &'i t4::WebElement<'g> { &self.elem } -} - -impl<'g> PieceElement<'g> { - #[throws(AE)] - pub fn posg(&self) -> Pos { - (||{ - let a = |a| Ok::<_,AE>( - self.get_attribute(a)?.ok_or(anyhow!("{}", a))?.parse()? - ); - let x = a("x")?; - let y = a("y")?; - Ok::<_,AE>(PosC([x,y])) - })() - .with_context(|| self.pieceid.to_owned()) - .context("read position of piece out of x,y attributes")? - } #[throws(AE)] - pub fn posw(&self) -> WebPos { - let posg = self.posg()?; - - let mat = self.w.matrix.get_or_try_init(||{ - let ary = self.w.su.driver.execute_script(r#" + pub fn posg2posw(&'g self, posg: Pos) -> WebPos { + let mat = self.matrix.get_or_try_init(||{ + let ary = self.su.driver.execute_script(r#" let m = space.getScreenCTM(); return [m.a, m.b, m.c, m.d, m.e, m.f]; "#)?; @@ -857,9 +825,46 @@ impl<'g> PieceElement<'g> { coord()?, coord()?, )) + })() + .context("convert game position to web page coordinates")? + } +} + +pub type WebCoord = i32; +pub type WebPos = (WebCoord, WebCoord); + +pub struct PieceElement<'g> { + pieceid: &'g str, + w: &'g WindowGuard<'g>, + elem: t4::WebElement<'g>, +} + +impl<'g> Deref for PieceElement<'g> { + type Target = t4::WebElement<'g>; + fn deref<'i>(&'i self) -> &'i t4::WebElement<'g> { &self.elem } +} + +impl<'g> PieceElement<'g> { + #[throws(AE)] + pub fn posg(&self) -> Pos { + (||{ + let a = |a| Ok::<_,AE>( + self.get_attribute(a)?.ok_or(anyhow!("{}", a))?.parse()? + ); + let x = a("x")?; + let y = a("y")?; + Ok::<_,AE>(PosC([x,y])) })() .with_context(|| self.pieceid.to_owned()) - .context("find piece position")? + .context("read position of piece out of x,y attributes")? + } + + #[throws(AE)] + pub fn posw(&self) -> WebPos { + let posg = self.posg()?; + + self.w.posg2posw(posg) + .with_context(|| self.pieceid.to_owned())? } }