InputHandler

Struct InputHandler 

Source
pub struct InputHandler<PageId, ButtonId> {
    pub button_selected: Option<ButtonId>,
    pub mouse_position: (f32, f32),
    pub cursor_position: usize,
    pub text_selection_range: Option<(usize, usize)>,
    /* private fields */
}

Fields§

§button_selected: Option<ButtonId>§mouse_position: (f32, f32)§cursor_position: usize§text_selection_range: Option<(usize, usize)>

Implementations§

Source§

impl<PageId: Copy + Eq + Debug, ButtonId: Copy + Eq + Debug> InputHandler<PageId, ButtonId>

Source

pub fn new(enable_rollback_pages: bool) -> Self

Examples found in repository?
examples/SimpleExample/demo.rs (line 42)
22fn main()
23{
24    // To Be Ignored, Just An Setup To Configure The Build
25    setup_build();
26    let window_config = WindowConfig 
27    {
28        window_title: "SimpleExample".to_string(),
29        icon: (None, None),
30        // Recommended to start with 16:9 aspect ratio
31        start_window_size: (800, 450),
32        // Recommended to have minimum size with 16:9 aspect ratio
33        window_minimum_size: (800, 450),
34        resizable: true,
35        centered: true,
36        // By Default SDL_LOGICAL_PRESENTATION_STRETCH Is Set, Only Setting It Here For Demonstration Purpose
37        different_sdl_presentation_mode: Some(SDL_LOGICAL_PRESENTATION_STRETCH),
38        font: ("JetBrainsMono".to_string(), Some("Bold".to_string()))
39    };
40
41    let mut window_modules = create_window(window_config);
42    let mut input_handler = InputHandler::new(true);
43    let mut app_state = AppState::new(PageId::Page1, window_modules.canvas.window().size(), window_modules.stretch_mode_status);
44    let mut page_data = PageData::new(&app_state);
45    let renderer_config = RendererConfig { canvas: window_modules.canvas, texture_creator: &window_modules.texture_creator, ttf_context: &window_modules.ttf_context, font_path: &window_modules.font_path, decrease_color_when_selected: Some((25, 25, 25)), selection_color: Some((0, 0, 200, 125)), assets_dir: None };
46    let mut renderer = Renderer::new(renderer_config);
47
48    populate_page_data(&mut page_data);
49
50    // Wrap the button_action function in a mutable closure so it can capture
51    // additional context if needed. Passing a closure here allows the
52    // button handler API to accept additional arguments beyond the default.
53    let mut button_action_closure = |app_state: &mut AppState<PageId, ButtonId>, button_id: &ButtonId, page_data: &mut PageData<PageId, ButtonId>| button_action(app_state, button_id, page_data);
54
55    loop
56    {
57        println!("{:?}", page_data.persistent_elements_to_render.is_some());
58        //using 900 / your_refresh_rate to a very crispy experience
59        std::thread::sleep(Duration::from_millis(900 / get_monitor_refresh_rate()));
60        app_state.update_window_size(renderer.canvas.window().size().0, renderer.canvas.window().size().1);
61        input_handler.handle_input(&mut window_modules.event_pump, &mut window_modules.clipboard_system, &mut page_data, &mut app_state, &mut button_action_closure);
62        page_data.create_current_page(&mut app_state);
63        renderer.render(&page_data, &mut app_state, &input_handler);
64    }
65}
More examples
Hide additional examples
examples/AdvancedExample/demo.rs (line 50)
29fn main()
30{
31    // To Be Ignored, Just An Setup To Configure Some Assets
32    setup_build();
33    list_embedded(&ASSETS);
34
35    let window_config = WindowConfig {
36        window_title: "AdvancedExample".to_string(),
37        icon: (Some("image_example/example_2.bmp".to_string()), Some(&ASSETS)),
38        // Recommended to start with 16:9 aspect ratio
39        start_window_size: (800, 450),
40        // Recommended to have minimum size with 16:9 aspect ratio
41        window_minimum_size: (800, 450),
42        resizable: true,
43        centered: true,
44        // By Default SDL_LOGICAL_PRESENTATION_STRETCH Is Set, Only Setting It Here For Demonstration Purpose
45        different_sdl_presentation_mode: Some(SDL_LOGICAL_PRESENTATION_STRETCH),
46        font: ("JetBrainsMono".to_string(), Some("Bold".to_string()))
47    };
48    let mut window_modules = create_window(window_config);
49    //bool is reffered to the rollback pages system, with "Mouse side buttons" or ("Alt" + "Arrows Keys") | (true = Page Rollback On), (false = Page Rollback Off)
50    let mut input_handler = InputHandler::new(true);
51    let mut app_state = AppState::new(PageId::Page1, window_modules.canvas.window().size(), window_modules.stretch_mode_status);
52    let mut page_data = PageData::new(&app_state);
53
54    let renderer_config = RendererConfig { canvas: window_modules.canvas, texture_creator: &window_modules.texture_creator, ttf_context: &window_modules.ttf_context, font_path: &window_modules.font_path, decrease_color_when_selected: Some((25, 25, 25)), selection_color: Some((0, 0, 200, 125)), assets_dir: Some(&ASSETS) };
55    let mut renderer = Renderer::new(renderer_config);
56
57
58    populate_page_data(&mut page_data);
59
60    // Wrap the button_action function in a mutable closure so it can capture
61    // additional context if needed. Passing a closure here allows the
62    // button handler API to accept additional arguments beyond the default.
63    let mut button_action_closure = |app_state: &mut AppState<PageId, ButtonId>, button_id: &ButtonId, page_data: &mut PageData<PageId, ButtonId>| button_action(app_state, button_id, page_data);
64
65    loop
66    {
67        //using (900 / your_refresh_rate) to a very crispy experience
68        std::thread::sleep(Duration::from_millis(900 / get_monitor_refresh_rate()));
69        input_handler.handle_input(&mut window_modules.event_pump, &mut window_modules.clipboard_system, &mut page_data, &mut app_state, &mut button_action_closure);
70        app_state.update_window_size(renderer.canvas.window().size().0, renderer.canvas.window().size().1);
71        page_data.create_current_page(&mut app_state);
72        renderer.render(&page_data, &mut app_state, &input_handler);
73    }
74}
Source

pub fn poll(&self, event_pump: &mut EventPump) -> InputEvent

Source

pub fn handle_input( &mut self, event_pump: &mut EventPump, clipboard_util: &mut ClipboardUtil, page_data: &mut PageData<PageId, ButtonId>, app_state: &mut AppState<PageId, ButtonId>, button_action: &mut dyn Fn(&mut AppState<PageId, ButtonId>, &ButtonId, &mut PageData<PageId, ButtonId>), )

Examples found in repository?
examples/SimpleExample/demo.rs (line 61)
22fn main()
23{
24    // To Be Ignored, Just An Setup To Configure The Build
25    setup_build();
26    let window_config = WindowConfig 
27    {
28        window_title: "SimpleExample".to_string(),
29        icon: (None, None),
30        // Recommended to start with 16:9 aspect ratio
31        start_window_size: (800, 450),
32        // Recommended to have minimum size with 16:9 aspect ratio
33        window_minimum_size: (800, 450),
34        resizable: true,
35        centered: true,
36        // By Default SDL_LOGICAL_PRESENTATION_STRETCH Is Set, Only Setting It Here For Demonstration Purpose
37        different_sdl_presentation_mode: Some(SDL_LOGICAL_PRESENTATION_STRETCH),
38        font: ("JetBrainsMono".to_string(), Some("Bold".to_string()))
39    };
40
41    let mut window_modules = create_window(window_config);
42    let mut input_handler = InputHandler::new(true);
43    let mut app_state = AppState::new(PageId::Page1, window_modules.canvas.window().size(), window_modules.stretch_mode_status);
44    let mut page_data = PageData::new(&app_state);
45    let renderer_config = RendererConfig { canvas: window_modules.canvas, texture_creator: &window_modules.texture_creator, ttf_context: &window_modules.ttf_context, font_path: &window_modules.font_path, decrease_color_when_selected: Some((25, 25, 25)), selection_color: Some((0, 0, 200, 125)), assets_dir: None };
46    let mut renderer = Renderer::new(renderer_config);
47
48    populate_page_data(&mut page_data);
49
50    // Wrap the button_action function in a mutable closure so it can capture
51    // additional context if needed. Passing a closure here allows the
52    // button handler API to accept additional arguments beyond the default.
53    let mut button_action_closure = |app_state: &mut AppState<PageId, ButtonId>, button_id: &ButtonId, page_data: &mut PageData<PageId, ButtonId>| button_action(app_state, button_id, page_data);
54
55    loop
56    {
57        println!("{:?}", page_data.persistent_elements_to_render.is_some());
58        //using 900 / your_refresh_rate to a very crispy experience
59        std::thread::sleep(Duration::from_millis(900 / get_monitor_refresh_rate()));
60        app_state.update_window_size(renderer.canvas.window().size().0, renderer.canvas.window().size().1);
61        input_handler.handle_input(&mut window_modules.event_pump, &mut window_modules.clipboard_system, &mut page_data, &mut app_state, &mut button_action_closure);
62        page_data.create_current_page(&mut app_state);
63        renderer.render(&page_data, &mut app_state, &input_handler);
64    }
65}
More examples
Hide additional examples
examples/AdvancedExample/demo.rs (line 69)
29fn main()
30{
31    // To Be Ignored, Just An Setup To Configure Some Assets
32    setup_build();
33    list_embedded(&ASSETS);
34
35    let window_config = WindowConfig {
36        window_title: "AdvancedExample".to_string(),
37        icon: (Some("image_example/example_2.bmp".to_string()), Some(&ASSETS)),
38        // Recommended to start with 16:9 aspect ratio
39        start_window_size: (800, 450),
40        // Recommended to have minimum size with 16:9 aspect ratio
41        window_minimum_size: (800, 450),
42        resizable: true,
43        centered: true,
44        // By Default SDL_LOGICAL_PRESENTATION_STRETCH Is Set, Only Setting It Here For Demonstration Purpose
45        different_sdl_presentation_mode: Some(SDL_LOGICAL_PRESENTATION_STRETCH),
46        font: ("JetBrainsMono".to_string(), Some("Bold".to_string()))
47    };
48    let mut window_modules = create_window(window_config);
49    //bool is reffered to the rollback pages system, with "Mouse side buttons" or ("Alt" + "Arrows Keys") | (true = Page Rollback On), (false = Page Rollback Off)
50    let mut input_handler = InputHandler::new(true);
51    let mut app_state = AppState::new(PageId::Page1, window_modules.canvas.window().size(), window_modules.stretch_mode_status);
52    let mut page_data = PageData::new(&app_state);
53
54    let renderer_config = RendererConfig { canvas: window_modules.canvas, texture_creator: &window_modules.texture_creator, ttf_context: &window_modules.ttf_context, font_path: &window_modules.font_path, decrease_color_when_selected: Some((25, 25, 25)), selection_color: Some((0, 0, 200, 125)), assets_dir: Some(&ASSETS) };
55    let mut renderer = Renderer::new(renderer_config);
56
57
58    populate_page_data(&mut page_data);
59
60    // Wrap the button_action function in a mutable closure so it can capture
61    // additional context if needed. Passing a closure here allows the
62    // button handler API to accept additional arguments beyond the default.
63    let mut button_action_closure = |app_state: &mut AppState<PageId, ButtonId>, button_id: &ButtonId, page_data: &mut PageData<PageId, ButtonId>| button_action(app_state, button_id, page_data);
64
65    loop
66    {
67        //using (900 / your_refresh_rate) to a very crispy experience
68        std::thread::sleep(Duration::from_millis(900 / get_monitor_refresh_rate()));
69        input_handler.handle_input(&mut window_modules.event_pump, &mut window_modules.clipboard_system, &mut page_data, &mut app_state, &mut button_action_closure);
70        app_state.update_window_size(renderer.canvas.window().size().0, renderer.canvas.window().size().1);
71        page_data.create_current_page(&mut app_state);
72        renderer.render(&page_data, &mut app_state, &input_handler);
73    }
74}
Source

pub fn insert_text( &mut self, text_to_insert: &str, app_state: &AppState<PageId, ButtonId>, page_data: &mut PageData<PageId, ButtonId>, _is_paste: bool, )

Source

pub fn backspace( &mut self, app_state: &mut AppState<PageId, ButtonId>, page_data: &mut PageData<PageId, ButtonId>, )

Source

pub fn copy( &mut self, clipboard_util_option: Option<&mut ClipboardUtil>, app_state: &AppState<PageId, ButtonId>, page_data: &mut PageData<PageId, ButtonId>, is_cut_operation: bool, )

Source

pub fn select_all( &mut self, app_state: &AppState<PageId, ButtonId>, page_data: &mut PageData<PageId, ButtonId>, )

Source

pub fn delete_all( &mut self, app_state: &AppState<PageId, ButtonId>, page_data: &mut PageData<PageId, ButtonId>, )

Source

pub fn move_cursor( &mut self, move_right: bool, shift_held: bool, app_state: &AppState<PageId, ButtonId>, page_data: &mut PageData<PageId, ButtonId>, )

Source

pub fn navigate_history( &self, move_forward: bool, app_state: &mut AppState<PageId, ButtonId>, page_data: &mut PageData<PageId, ButtonId>, )

Source

pub fn paste( &mut self, clipboard_util_option: Option<&mut ClipboardUtil>, app_state: &AppState<PageId, ButtonId>, page_data: &mut PageData<PageId, ButtonId>, )

Source

pub fn push_state(&mut self, page_data: &PageData<PageId, ButtonId>)

Source

pub fn undo(&mut self, page_data: &mut PageData<PageId, ButtonId>)

Source

pub fn get_current_input_length( &self, app_state: &AppState<PageId, ButtonId>, page_data: &PageData<PageId, ButtonId>, button_id: ButtonId, ) -> usize

Auto Trait Implementations§

§

impl<PageId, ButtonId> Freeze for InputHandler<PageId, ButtonId>
where ButtonId: Freeze,

§

impl<PageId, ButtonId> RefUnwindSafe for InputHandler<PageId, ButtonId>
where ButtonId: RefUnwindSafe, PageId: RefUnwindSafe,

§

impl<PageId, ButtonId> Send for InputHandler<PageId, ButtonId>
where ButtonId: Send, PageId: Send,

§

impl<PageId, ButtonId> Sync for InputHandler<PageId, ButtonId>
where ButtonId: Sync, PageId: Sync,

§

impl<PageId, ButtonId> Unpin for InputHandler<PageId, ButtonId>
where ButtonId: Unpin, PageId: Unpin,

§

impl<PageId, ButtonId> UnwindSafe for InputHandler<PageId, ButtonId>
where ButtonId: UnwindSafe, PageId: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.