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: boolImplementations§
Source§impl<PageId: Copy + Eq + Debug, ButtonId: Copy + Eq + Debug> AppState<PageId, ButtonId>
impl<PageId: Copy + Eq + Debug, ButtonId: Copy + Eq + Debug> AppState<PageId, ButtonId>
Sourcepub fn new(
start_page: PageId,
window_size: (u32, u32),
stretch_mode_is_on: bool,
) -> Self
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
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}Sourcepub fn change_current_page(
&mut self,
page_data: &mut PageData<PageId, ButtonId>,
next_page: PageId,
button_id: &ButtonId,
)
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
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}Sourcepub fn is_transition_active(&self) -> bool
pub fn is_transition_active(&self) -> bool
Returns whether a scene transition is currently active.
Sourcepub fn update_window_size(&mut self, width: u32, height: u32)
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
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}Sourcepub fn begin_capturing_input(&mut self, button: ButtonId)
pub fn begin_capturing_input(&mut self, button: ButtonId)
Begin capturing user input for a specific button ID
Sourcepub fn stop_capturing_input(&mut self)
pub fn stop_capturing_input(&mut self)
Stop capturing user input
Sourcepub fn is_capturing(&self) -> bool
pub fn is_capturing(&self) -> bool
Check if currently capturing input
Trait Implementations§
Auto Trait Implementations§
impl<PageId, ButtonId> Freeze for AppState<PageId, ButtonId>
impl<PageId, ButtonId> RefUnwindSafe for AppState<PageId, ButtonId>where
PageId: RefUnwindSafe,
ButtonId: RefUnwindSafe,
impl<PageId, ButtonId> Send for AppState<PageId, ButtonId>
impl<PageId, ButtonId> Sync for AppState<PageId, ButtonId>
impl<PageId, ButtonId> Unpin for AppState<PageId, ButtonId>
impl<PageId, ButtonId> UnwindSafe for AppState<PageId, ButtonId>where
PageId: UnwindSafe,
ButtonId: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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.