e2rcore/implement/ui/
input_default_glutin.rs

1extern crate glutin;
2
3use interface::i_ui::{ IUi, InputFiltered, KeyCode, Coord, State };
4
5pub struct XformInput {
6    _moving_avg: u32,
7}
8
9impl Default for XformInput {
10
11    fn default() -> Self {
12        XformInput {
13            _moving_avg: 1, //todo
14        }
15    }
16
17}
18
19impl IUi for XformInput {
20    
21    type EventInput = glutin::Event;
22    type EventInputFiltered = InputFiltered;
23
24    fn new() -> Self {
25        Default::default()
26    }
27    
28    fn process_input_events( & mut self, e: & [ Self::EventInput ], win_offset: (i32,i32), win_size: (u32,u32) ) -> Vec< InputFiltered > {
29        let filtered = e.iter()
30            .map( |x| InputFiltered::from( ( x, win_offset, win_size ) ) )
31            .filter( |x| if let &InputFiltered::Ignored = x { false } else { true } )
32            .inspect( |x| { trace!("{:?}", x); () } )
33            .collect();
34        filtered
35    }
36}
37
38
39///convert glutin::Event into i_ui::InputFiltered
40impl< 'a > From< (& 'a glutin::Event, (i32,i32), (u32,u32) )> for InputFiltered {
41    fn from( input: (& 'a glutin::Event, (i32,i32), (u32,u32) ) ) -> Self {
42        let e = input.0;
43        let win_offset = input.1;
44        let win_size = input.2;
45        match e {
46            &glutin::Event::WindowEvent{ ref event, .. } => match event {
47                &glutin::WindowEvent::CloseRequested => {
48                    InputFiltered::Button { key: KeyCode::Close, state: State::Press }
49                },
50                &glutin::WindowEvent::Resized( logical_size ) => { //todo
51                    InputFiltered::Ignored
52                },
53                &glutin::WindowEvent::ReceivedCharacter(x) => {
54                    match x {
55                        'a' | 'A' => InputFiltered::Button { key: KeyCode::A, state: State::Press },
56                        'b' | 'B' => InputFiltered::Button { key: KeyCode::B, state: State::Press },
57                        'c' | 'C' => InputFiltered::Button { key: KeyCode::C, state: State::Press },
58                        'd' | 'D' => InputFiltered::Button { key: KeyCode::D, state: State::Press },
59                        'e' | 'E' => InputFiltered::Button { key: KeyCode::E, state: State::Press },
60                        'f' | 'F' => InputFiltered::Button { key: KeyCode::F, state: State::Press },
61                        'g' | 'G' => InputFiltered::Button { key: KeyCode::G, state: State::Press },
62                        'h' | 'H' => InputFiltered::Button { key: KeyCode::H, state: State::Press },
63                        'i' | 'I' => InputFiltered::Button { key: KeyCode::I, state: State::Press },
64                        'j' | 'J' => InputFiltered::Button { key: KeyCode::J, state: State::Press },
65                        'k' | 'K' => InputFiltered::Button { key: KeyCode::K, state: State::Press },
66                        'l' | 'L' => InputFiltered::Button { key: KeyCode::L, state: State::Press },
67                        'm' | 'M' => InputFiltered::Button { key: KeyCode::M, state: State::Press },
68                        'n' | 'N' => InputFiltered::Button { key: KeyCode::N, state: State::Press },
69                        'o' | 'O' => InputFiltered::Button { key: KeyCode::O, state: State::Press },
70                        'p' | 'P' => InputFiltered::Button { key: KeyCode::P, state: State::Press },
71                        'q' | 'Q' => InputFiltered::Button { key: KeyCode::Q, state: State::Press },
72                        'r' | 'R' => InputFiltered::Button { key: KeyCode::R, state: State::Press },
73                        's' | 'S' => InputFiltered::Button { key: KeyCode::S, state: State::Press },
74                        't' | 'T' => InputFiltered::Button { key: KeyCode::T, state: State::Press },
75                        'u' | 'U' => InputFiltered::Button { key: KeyCode::U, state: State::Press },
76                        'v' | 'V' => InputFiltered::Button { key: KeyCode::V, state: State::Press },
77                        'w' | 'W' => InputFiltered::Button { key: KeyCode::W, state: State::Press },
78                        'x' | 'X' => InputFiltered::Button { key: KeyCode::X, state: State::Press },
79                        'y' | 'Y' => InputFiltered::Button { key: KeyCode::Y, state: State::Press },
80                        'z' | 'Z' => InputFiltered::Button { key: KeyCode::Z, state: State::Press },
81                        '0' => InputFiltered::Button { key: KeyCode::Num0, state: State::Press },
82                        '1' => InputFiltered::Button { key: KeyCode::Num1, state: State::Press },
83                        '2' => InputFiltered::Button { key: KeyCode::Num2, state: State::Press },
84                        '3' => InputFiltered::Button { key: KeyCode::Num3, state: State::Press },
85                        '4' => InputFiltered::Button { key: KeyCode::Num4, state: State::Press },
86                        '5' => InputFiltered::Button { key: KeyCode::Num5, state: State::Press },
87                        '6' => InputFiltered::Button { key: KeyCode::Num6, state: State::Press },
88                        '7' => InputFiltered::Button { key: KeyCode::Num7, state: State::Press },
89                        '8' => InputFiltered::Button { key: KeyCode::Num8, state: State::Press },
90                        '9' => InputFiltered::Button { key: KeyCode::Num9, state: State::Press },
91                        ' ' => InputFiltered::Button { key: KeyCode::Space, state: State::Press },
92                        _ => {
93                            trace!("key events ignored: {:?}", e );
94                            InputFiltered::Ignored
95                        },
96                    }
97                },
98                // issues: conversion from measured analog reading to physical or logical screen unit
99                &glutin::WindowEvent::AxisMotion { ref axis, ref value, .. } => {
100                    // info!("mouse input: {:?}, {:?}", axis, value );
101                    let ( coord, val ) = match axis {
102                        &0 => {
103                            // println!("window mouse event x: {}, win offset: {:?}", *value as f32, win_offset );
104                            ( Coord::X, *value as f32 - win_offset.0 as f32 )
105                        },
106                        &1 => {
107                            ( Coord::Y, *value as f32 - win_offset.1 as f32 )
108                        },
109                        _ => {
110                            ( Coord::Z, *value as f32 )
111                        },
112                    };
113                    InputFiltered::MouseCoord( coord, val )
114                },
115                // &glutin::WindowEvent::CursorMoved { ref device_id, ref position/*logical pos*/, .. } => {
116                //     info!("mouse input: {:?}", position );
117                //     InputFiltered::MouseCoord2( position.x as f32 - win_offset.0 as f32,
118                //                                 position.y as f32 - win_offset.1 as f32 )
119                // },
120                &glutin::WindowEvent::KeyboardInput { ref input, .. } => {
121                    match input {
122                        &glutin::KeyboardInput {
123                            ref state,
124                            ref virtual_keycode,
125                            ..
126                        } => {
127                            // trace!("probing: {:?}", input );
128                            let k = match virtual_keycode {
129                                &Some(glutin::VirtualKeyCode::Left) => Some( KeyCode::Left ),
130                                &Some(glutin::VirtualKeyCode::Right) => Some( KeyCode::Right ),
131                                &Some(glutin::VirtualKeyCode::Up) => Some( KeyCode::Up ),
132                                &Some(glutin::VirtualKeyCode::Down) => Some( KeyCode::Down ),
133                                _ => None,
134                            };
135                            let s = match state {
136                                &glutin::ElementState::Pressed => State::Press,
137                                _ => State::Release,
138                            };
139                            if let Some( x ) = k {
140                                InputFiltered::Button { key: x, state: s }
141                            } else {
142                                InputFiltered::Ignored
143                            }
144                        },
145                    }
146                },
147                &glutin::WindowEvent::MouseInput { ref state, ref button, .. } => {
148                    let k = match button {
149                        &glutin::MouseButton::Left  => {
150                            Some( KeyCode::MouseL )
151                        },
152                        &glutin::MouseButton::Right  => {
153                            Some( KeyCode::MouseR )
154                        },
155                        _ => None
156                    };
157                    let s = match state {
158                        &glutin::ElementState::Pressed => State::Press,
159                        _ => State::Release,
160                    };
161                    if let Some( x ) = k {
162                        InputFiltered::Button { key: x, state: s }
163                    } else {
164                        InputFiltered::Ignored
165                    }
166                },
167                _ => {
168                    trace!("events input ignored: {:?}", e );
169                    InputFiltered::Ignored
170                },
171            },
172            _ => InputFiltered::Ignored
173        }
174    }
175}