From 2d703811c579632ab08cf39737d437c135f90287 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Wed, 11 Oct 2017 11:13:22 -0700 Subject: [PATCH] Add Event::relativize --- src/event.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/event.rs b/src/event.rs index b995748..ce0dda4 100644 --- a/src/event.rs +++ b/src/event.rs @@ -298,6 +298,53 @@ pub enum Event { Exit, } +impl Event { + /// Returns the position of the mouse, if `self` is a mouse event. + pub fn mouse_position(&self) -> Option { + if let Event::Mouse { + offset: _, + position, + event: _, + } = *self + { + Some(position) + } else { + None + } + } + + /// Update `self` with the given offset. + /// + /// If `self` is a mouse event, adds `top_left` to its offset. + /// Otherwise, do nothing. + pub fn relativize(&mut self, top_left: V) + where + V: Into, + { + if let Event::Mouse { + ref mut offset, + position: _, + event: _, + } = *self + { + *offset = *offset + top_left; + } + } + + /// Returns a cloned, relativized event. + /// + /// If `self` is a mouse event, adds `top_left` to its offset. + /// Otherwise, returns a simple clone. + pub fn relativized(&self, top_left: V) -> Self + where + V: Into, + { + let mut result = self.clone(); + result.relativize(top_left); + result + } +} + impl From for Event { fn from(c: char) -> Event { Event::Char(c)