mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-27 11:16:03 +00:00
Add Event::relativize
This commit is contained in:
parent
e07e156ad0
commit
2d703811c5
47
src/event.rs
47
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<Vec2> {
|
||||
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<V>(&mut self, top_left: V)
|
||||
where
|
||||
V: Into<Vec2>,
|
||||
{
|
||||
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<V>(&self, top_left: V) -> Self
|
||||
where
|
||||
V: Into<Vec2>,
|
||||
{
|
||||
let mut result = self.clone();
|
||||
result.relativize(top_left);
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl From<char> for Event {
|
||||
fn from(c: char) -> Event {
|
||||
Event::Char(c)
|
||||
|
Loading…
Reference in New Issue
Block a user