use input;
use position::{Dimensions, Point};
use utils::vec2_sub;
use widget;
pub use piston_input::Input;
#[doc(inline)]
pub use piston_input::Motion;
#[derive(Clone, PartialEq, Debug)]
pub enum Event {
Raw(Input),
Ui(Ui)
}
#[derive(Clone, PartialEq, Debug)]
pub enum Ui {
Text(Option<widget::Id>, Text),
Press(Option<widget::Id>, Press),
Release(Option<widget::Id>, Release),
Move(Option<widget::Id>, Move),
WindowResized(Dimensions),
Click(Option<widget::Id>, Click),
DoubleClick(Option<widget::Id>, DoubleClick),
Drag(Option<widget::Id>, Drag),
Scroll(Option<widget::Id>, Scroll),
WidgetCapturesMouse(widget::Id),
WidgetUncapturesMouse(widget::Id),
WidgetCapturesKeyboard(widget::Id),
WidgetUncapturesKeyboard(widget::Id),
}
#[derive(Clone, PartialEq, Debug)]
pub enum Widget {
Text(Text),
Move(Move),
Press(Press),
Release(Release),
Click(Click),
DoubleClick(DoubleClick),
Drag(Drag),
Scroll(Scroll),
WindowResized(Dimensions),
CapturesMouse,
UncapturesMouse,
CapturesKeyboard,
UncapturesKeyboard,
}
#[derive(Clone, PartialEq, Debug)]
pub struct Text {
pub string: String,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Move {
pub motion: Motion,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Button {
Keyboard(input::Key),
Mouse(input::MouseButton, Point),
Controller(input::ControllerButton),
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Press {
pub button: Button,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct MousePress {
pub button: input::MouseButton,
pub xy: Point,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct KeyPress {
pub key: input::Key,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Release {
pub button: Button,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct MouseRelease {
pub button: input::MouseButton,
pub xy: Point,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct KeyRelease {
pub key: input::Key,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Drag {
pub button: input::MouseButton,
pub origin: Point,
pub from: Point,
pub to: Point,
pub delta_xy: Point,
pub total_delta_xy: Point,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Click {
pub button: input::MouseButton,
pub xy: Point,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct DoubleClick {
pub button: input::MouseButton,
pub xy: Point,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Scroll {
pub x: f64,
pub y: f64,
pub modifiers: input::keyboard::ModifierKey,
}
impl Move {
pub fn relative_to(&self, xy: Point) -> Move {
let motion = match self.motion {
Motion::MouseCursor(x, y) => Motion::MouseCursor(x - xy[0], y - xy[1]),
motion => motion,
};
Move {
motion: motion,
..*self
}
}
}
impl Button {
pub fn relative_to(&self, xy: Point) -> Button {
match *self {
Button::Mouse(m_button, self_xy) => Button::Mouse(m_button, vec2_sub(self_xy, xy)),
button => button,
}
}
}
impl Press {
pub fn relative_to(&self, xy: Point) -> Press {
Press {
button: self.button.relative_to(xy),
..*self
}
}
pub fn mouse(self) -> Option<MousePress> {
match self.button {
Button::Mouse(button, xy) =>
Some(MousePress {
button: button,
xy: xy,
modifiers: self.modifiers,
}),
_ => None,
}
}
pub fn key(self) -> Option<KeyPress> {
match self.button {
Button::Keyboard(key) =>
Some(KeyPress {
key: key,
modifiers: self.modifiers,
}),
_ => None,
}
}
}
impl Release {
pub fn relative_to(&self, xy: Point) -> Release {
Release {
button: self.button.relative_to(xy),
..*self
}
}
pub fn mouse(self) -> Option<MouseRelease> {
match self.button {
Button::Mouse(button, xy) =>
Some(MouseRelease {
button: button,
xy: xy,
modifiers: self.modifiers,
}),
_ => None,
}
}
pub fn key(self) -> Option<KeyRelease> {
match self.button {
Button::Keyboard(key) =>
Some(KeyRelease {
key: key,
modifiers: self.modifiers,
}),
_ => None,
}
}
}
impl Click {
pub fn relative_to(&self, xy: Point) -> Click {
Click {
xy: vec2_sub(self.xy, xy),
..*self
}
}
}
impl DoubleClick {
pub fn relative_to(&self, xy: Point) -> DoubleClick {
DoubleClick {
xy: vec2_sub(self.xy, xy),
..*self
}
}
}
impl Drag {
pub fn relative_to(&self, xy: Point) -> Drag {
Drag{
origin: vec2_sub(self.origin, xy),
from: vec2_sub(self.from, xy),
to: vec2_sub(self.to, xy),
..*self
}
}
}
impl From<Ui> for Event {
fn from(ui: Ui) -> Self {
Event::Ui(ui)
}
}
impl From<Input> for Event {
fn from(input: Input) -> Self {
Event::Raw(input)
}
}
impl From<Text> for Widget {
fn from(text: Text) -> Self {
Widget::Text(text)
}
}
impl From<Move> for Widget {
fn from(move_: Move) -> Self {
Widget::Move(move_)
}
}
impl From<Press> for Widget {
fn from(press: Press) -> Self {
Widget::Press(press)
}
}
impl From<Release> for Widget {
fn from(release: Release) -> Self {
Widget::Release(release)
}
}
impl From<Click> for Widget {
fn from(click: Click) -> Self {
Widget::Click(click)
}
}
impl From<DoubleClick> for Widget {
fn from(double_click: DoubleClick) -> Self {
Widget::DoubleClick(double_click)
}
}
impl From<Scroll> for Widget {
fn from(scroll: Scroll) -> Self {
Widget::Scroll(scroll)
}
}
impl From<Drag> for Widget {
fn from(drag: Drag) -> Self {
Widget::Drag(drag)
}
}