AdvancedExample/
demo.rs

1use crate::ui::pages::ButtonId;
2use crate::{actions::buttons_actions::button_action, system::setup_page_data::populate_page_data, ui::pages::PageId};
3use rust_page_system::include_project_assets;
4use rust_page_system::{
5    Renderer, list_embedded,
6    system::{
7        input_handler::InputHandler,
8        page_system::PageData,
9        renderer::RendererConfig,
10        state::AppState,
11        window::{WindowConfig, create_window, get_monitor_refresh_rate}
12    }
13};
14
15use include_dir::Dir;
16pub static ASSETS: Dir = include_project_assets!();
17
18use sdl3::sys::render::SDL_LOGICAL_PRESENTATION_STRETCH;
19use std::time::Duration;
20
21// To Be Ignored, Just An Setup To Configure Some Assets
22use crate::build::setup_build;
23mod build;
24
25pub mod actions;
26pub mod system;
27pub mod ui;
28
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}