Skip to content

Commit

Permalink
Merge pull request PistonDevelopers#307 from bvssvni/master
Browse files Browse the repository at this point in the history
Added `_args` methods to use with if let
  • Loading branch information
bvssvni committed Jan 3, 2015
2 parents a30078a + 54d9fb4 commit a2b520c
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub trait FocusEvent {
fn from_focused(focused: bool) -> Option<Self>;
/// Calls closure if this is a focus event.
fn focus<U>(&self, f: |bool| -> U) -> Option<U>;
/// Returns focus arguments.
fn focus_args(&self) -> Option<bool> {
self.focus(|val| val)
}
}

impl<T: GenericEvent> FocusEvent for T {
Expand Down
12 changes: 12 additions & 0 deletions src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub trait MouseCursorEvent {
fn from_xy(x: f64, y: f64) -> Option<Self>;
/// Calls closure if this is a mouse cursor event.
fn mouse_cursor<U>(&self, f: |f64, f64| -> U) -> Option<U>;
/// Returns mouse cursor arguments.
fn mouse_cursor_args(&self) -> Option<[f64; 2]> {
self.mouse_cursor(|x, y| [x, y])
}
}

impl<T: GenericEvent> MouseCursorEvent for T {
Expand Down Expand Up @@ -36,6 +40,10 @@ pub trait MouseRelativeEvent {
fn from_xy(x: f64, y: f64) -> Option<Self>;
/// Calls closure if this is a mouse relative event.
fn mouse_relative<U>(&self, f: |f64, f64| -> U) -> Option<U>;
/// Returns mouse relative arguments.
fn mouse_relative_args(&self) -> Option<[f64; 2]> {
self.mouse_relative(|x, y| [x, y])
}
}

impl<T: GenericEvent> MouseRelativeEvent for T {
Expand Down Expand Up @@ -63,6 +71,10 @@ pub trait MouseScrollEvent {
fn from_xy(x: f64, y: f64) -> Option<Self>;
/// Calls a closure if this is a mouse scroll event.
fn mouse_scroll<U>(&self, f: |f64, f64| -> U) -> Option<U>;
/// Returns mouse scroll arguments.
fn mouse_scroll_args(&self) -> Option<[f64; 2]> {
self.mouse_scroll(|x, y| [x, y])
}
}

impl<T: GenericEvent> MouseScrollEvent for T {
Expand Down
4 changes: 4 additions & 0 deletions src/press.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub trait PressEvent {
fn from_button(button: Button) -> Option<Self>;
/// Calls closure if this is a press event.
fn press<U>(&self, f: |Button| -> U) -> Option<U>;
/// Returns press arguments.
fn press_args(&self) -> Option<Button> {
self.press(|button| button)
}
}

impl<T: GenericEvent> PressEvent for T {
Expand Down
4 changes: 4 additions & 0 deletions src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub trait ReleaseEvent {
fn from_button(button: Button) -> Option<Self>;
/// Calls closure if this is a release event.
fn release<U>(&self, f: |Button| -> U) -> Option<U>;
/// Returns release arguments.
fn release_args(&self) -> Option<Button> {
self.release(|button| button)
}
}

impl<T: GenericEvent> ReleaseEvent for T {
Expand Down
4 changes: 4 additions & 0 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub trait RenderEvent {
fn from_render_args(args: &RenderArgs) -> Option<Self>;
/// Calls closure if this is a render event.
fn render<U>(&self, f: |&RenderArgs| -> U) -> Option<U>;
/// Returns render arguments.
fn render_args(&self) -> Option<RenderArgs> {
self.render(|args| args.clone())
}
}

impl<T: GenericEvent> RenderEvent for T {
Expand Down
4 changes: 4 additions & 0 deletions src/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub trait ResizeEvent {
fn from_width_height(w: u32, h: u32) -> Option<Self>;
/// Calls closure if this is a resize event.
fn resize<U>(&self, f: |u32, u32| -> U) -> Option<U>;
/// Returns resize arguments.
fn resize_args(&self) -> Option<[u32; 2]> {
self.resize(|x, y| [x, y])
}
}

impl<T: GenericEvent> ResizeEvent for T {
Expand Down
5 changes: 5 additions & 0 deletions src/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::intrinsics::TypeId;
use std::borrow::ToOwned;

use GenericEvent;
use ptr::Ptr;
Expand All @@ -9,6 +10,10 @@ pub trait TextEvent {
fn from_text(text: &str) -> Option<Self>;
/// Calls closure if this is a text event.
fn text<U>(&self, f: |&str| -> U) -> Option<U>;
/// Returns text arguments.
fn text_args(&self) -> Option<String> {
self.text(|text| text.to_owned())
}
}

impl<T: GenericEvent> TextEvent for T {
Expand Down
4 changes: 4 additions & 0 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub trait UpdateEvent {
fn from_dt(dt: f64) -> Option<Self>;
/// Calls closure if this is an update event.
fn update<U>(&self, f: |&UpdateArgs| -> U) -> Option<U>;
/// Returns update arguments.
fn update_args(&self) -> Option<UpdateArgs> {
self.update(|args| args.clone())
}
}

impl<T: GenericEvent> UpdateEvent for T {
Expand Down

0 comments on commit a2b520c

Please sign in to comment.