AppState

Struct AppState 

Source
pub struct AppState<PageId, ButtonId> {
    pub current_page: PageId,
    pub scene_transition: Option<SceneTransition<PageId>>,
    pub current_transition_type: Option<TransitionType>,
    pub window_size: (u32, u32),
    pub capturing_input: (bool, Option<ButtonId>),
    pub all_events_disable: bool,
    pub stretch_mode_is_on: bool,
}
Expand description

Global application state that holds page navigation, transitions, and user input tracking.

Fields§

§current_page: PageId§scene_transition: Option<SceneTransition<PageId>>§current_transition_type: Option<TransitionType>§window_size: (u32, u32)§capturing_input: (bool, Option<ButtonId>)§all_events_disable: bool§stretch_mode_is_on: bool

Implementations§

Source§

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

Source

pub fn new( start_page: PageId, window_size: (u32, u32), stretch_mode_is_on: bool, ) -> Self

Create a new app state with a starting page.

Examples found in repository?
examples/SimpleExample/demo.rs (line 43)
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 51)
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 change_current_page( &mut self, page_data: &mut PageData<PageId, ButtonId>, next_page: PageId, button_id: &ButtonId, )

Change to a new page, optionally triggering a transition.

Examples found in repository?
examples/SimpleExample/demo.rs (line 76)
70pub fn button_action(app_state: &mut AppState<PageId, ButtonId>, button_id: &ButtonId, app_data: &mut PageData<PageId, ButtonId>)
71{
72    if !app_state.capturing_input.0
73    {
74        if &ButtonId::ButtonPage1 == button_id
75        {
76            app_state.change_current_page(app_data, PageId::Page1, button_id);
77            return;
78        };
79        if &ButtonId::ButtonSubPage == button_id
80        {
81            app_state.change_current_page(app_data, PageId::Page1SubPage, button_id);
82            return;
83        };
84        if &ButtonId::ButtonBack == button_id
85        {
86            println!("button back clicked");
87            app_state.change_current_page(app_data, PageId::Page1, button_id);
88            return;
89        };
90        // Non Handle Buttons Will Be Considered User Input Buttons
91        app_state.capturing_input = (true, Some(*button_id));
92    }
93}
More examples
Hide additional examples
examples/AdvancedExample/actions/buttons_actions.rs (line 16)
7pub fn button_action(app_state: &mut AppState<PageId, ButtonId>, button_id: &ButtonId, app_data: &mut PageData<PageId, ButtonId>)
8{
9    if !app_state.capturing_input.0
10    {
11        if &ButtonId::ButtonPage1 == button_id
12        {
13            //this disable all eventpump events, it's just here for demonstration porpuse
14            app_state.all_events_disable = true;
15            app_data.forced_persistent_elements = Some(vec![persistent_elements2()]);
16            app_state.change_current_page(app_data, PageId::Page1, button_id);
17            app_state.all_events_disable = false;
18            return;
19        };
20        if &ButtonId::ButtonPage2 == button_id
21        {
22            app_state.change_current_page(app_data, PageId::Page2, button_id);
23            return;
24        };
25        if &ButtonId::ButtonSubPage == button_id
26        {
27            app_state.change_current_page(app_data, PageId::Page2SubPage, button_id);
28            return;
29        };
30        if &ButtonId::ButtonBack == button_id
31        {
32            app_state.change_current_page(app_data, PageId::Page2, button_id);
33            return;
34        };
35        // Non Handle Buttons Will Be Considered User Input Buttons
36        app_state.capturing_input = (true, Some(*button_id));
37    }
38}
Source

pub fn is_transition_active(&self) -> bool

Returns whether a scene transition is currently active.

Source

pub fn update_window_size(&mut self, width: u32, height: u32)

Update window size (e.g., on resize events)

Examples found in repository?
examples/SimpleExample/demo.rs (line 60)
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 70)
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 begin_capturing_input(&mut self, button: ButtonId)

Begin capturing user input for a specific button ID

Source

pub fn stop_capturing_input(&mut self)

Stop capturing user input

Source

pub fn is_capturing(&self) -> bool

Check if currently capturing input

Trait Implementations§

Source§

impl<PageId: Clone, ButtonId: Clone> Clone for AppState<PageId, ButtonId>

Source§

fn clone(&self) -> AppState<PageId, ButtonId>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<PageId: Debug, ButtonId: Debug> Debug for AppState<PageId, ButtonId>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<PageId, ButtonId> UnwindSafe for AppState<PageId, ButtonId>
where PageId: UnwindSafe, ButtonId: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.