use input;
use position::{Dimensions, Point};
use utils::vec2_sub;
use widget;
#[derive(Clone, Debug, PartialEq)]
pub enum Input {
Press(input::Button),
Release(input::Button),
Resize(f64, f64),
Motion(input::Motion),
Touch(input::Touch),
Text(String),
Focus(bool),
Redraw,
}
#[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),
Motion(Option<widget::Id>, Motion),
Touch(Option<widget::Id>, input::Touch),
WindowResized(Dimensions),
Click(Option<widget::Id>, Click),
DoubleClick(Option<widget::Id>, DoubleClick),
Tap(Option<widget::Id>, Tap),
Drag(Option<widget::Id>, Drag),
Scroll(Option<widget::Id>, Scroll),
WidgetCapturesInputSource(widget::Id, input::Source),
WidgetUncapturesInputSource(widget::Id, input::Source),
}
#[derive(Clone, PartialEq, Debug)]
pub enum Widget {
Text(Text),
Motion(Motion),
Touch(input::Touch),
Press(Press),
Release(Release),
Click(Click),
DoubleClick(DoubleClick),
Tap(Tap),
Drag(Drag),
Scroll(Scroll),
WindowResized(Dimensions),
CapturesInputSource(input::Source),
UncapturesInputSource(input::Source),
}
#[derive(Clone, PartialEq, Debug)]
pub struct Text {
pub string: String,
pub modifiers: input::keyboard::ModifierKey,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Motion {
pub motion: input::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 Tap {
pub id: input::touch::Id,
pub xy: Point,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Scroll {
pub x: f64,
pub y: f64,
pub modifiers: input::keyboard::ModifierKey,
}
impl Motion {
pub fn relative_to(&self, xy: Point) -> Motion {
let motion = match self.motion {
input::Motion::MouseCursor { x, y } => input::Motion::MouseCursor {
x: x - xy[0],
y: y - xy[1],
},
motion => motion,
};
Motion {
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 Tap {
pub fn relative_to(&self, xy: Point) -> Self {
Tap {
xy: vec2_sub(self.xy, xy),
..*self
}
}
}
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<input::Motion> for Input {
fn from(motion: input::Motion) -> Self {
Input::Motion(motion)
}
}
impl From<input::Touch> for Input {
fn from(touch: input::Touch) -> Self {
Input::Touch(touch)
}
}
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<Motion> for Widget {
fn from(motion: Motion) -> Self {
Widget::Motion(motion)
}
}
impl From<input::Touch> for Widget {
fn from(touch: input::Touch) -> Self {
Widget::Touch(touch)
}
}
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<Tap> for Widget {
fn from(tap: Tap) -> Self {
Widget::Tap(tap)
}
}
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)
}
}