Trait IStepper

Source
pub trait IStepper {
    // Required methods
    fn initialize(&mut self, id: StepperId, sk: Rc<RefCell<SkInfo>>) -> bool;
    fn step(&mut self, token: &MainThreadToken);

    // Provided methods
    fn initialize_done(&mut self) -> bool { ... }
    fn enabled(&self) -> bool { ... }
    fn shutdown(&mut self) { ... }
    fn shutdown_done(&mut self) -> bool { ... }
}
Expand description

See derive macro crate::IStepper which is usefull to implement this trait. This is a lightweight standard interface for fire-and-forget systems that can be attached to StereoKit! This is particularly handy for extensions/plugins that need to run in the background of your application, or even for managing some of your own simpler systems.

ISteppers can be added before or after the call to Sk.initialize, and this does affect when the IStepper.initialize call will be made! IStepper.initialize is always called after Sk.initialize. This can be important to note when writing code that uses SK functions that are dependant on initialization, you’ll want to avoid putting this code in the constructor, and add them to Initialize instead.

ISteppers also pay attention to threading! Initialize and Step always happen on the main thread, even if the constructor is called on a different one. https://stereokit.net/Pages/StereoKit.Framework/IStepper.html

see also crate::IStepper

§Examples

use stereokit_rust::{ font::Font, material::Material, maths::{Matrix, Quat, Vec3},
                      mesh::Mesh, system::{Text, TextStyle}, util::named_colors};

/// The basic Stepper.
pub struct AStepper {
    id: StepperId,
    sk_info: Option<Rc<RefCell<SkInfo>>>,
    pub transform: Matrix,
    round_cube: Option<Mesh>,
    pub text: String,
    text_style: Option<TextStyle>,
}

unsafe impl Send for AStepper {}

/// This code may be called in some threads or before sk_init()
/// so no StereoKit assets here.
impl Default for AStepper {
    fn default() -> Self {
        Self {
            id: "AStepper".to_string(),
            sk_info: None,
            transform: Matrix::r([0.0, 180.0, 0.0]),
            round_cube: None,
            text: "IStepper\ntrait".to_owned(),
            text_style: None,
        }
    }
}

/// All the code here run in the main thread
impl IStepper for AStepper {
    fn initialize(&mut self, id: StepperId, sk_info: Rc<RefCell<SkInfo>>) -> bool {
        self.id = id;
        self.sk_info = Some(sk_info);
        self.round_cube = Some(Mesh::generate_rounded_cube(Vec3::ONE / 5.0, 0.02, None));
        self.text_style = Some(Text::make_style(Font::default(), 0.3, named_colors::BLACK));

        true
    }

    fn step(&mut self, token: &MainThreadToken) {
        if let Some(round_cube) = &self.round_cube {
            round_cube.draw(token, Material::pbr(),
                            self.transform, Some(named_colors::RED.into()), None);
        }
        Text::add_at(token, &self.text, self.transform, self.text_style,
                     None, None, None, None, None, None);
    }
}

sk.send_event(StepperAction::add_default::<AStepper>("My_Basic_Stepper_ID"));

filename_scr = "screenshots/a_stepper.jpeg";
test_screenshot!( // !!!! Get a proper main loop !!!!
    // No code here as we only use AStepper
);
screenshot

Required Methods§

Source

fn initialize(&mut self, id: StepperId, sk: Rc<RefCell<SkInfo>>) -> bool

This is called by StereoKit at the start of the next frame, and on the main thread. This happens before StereoKit’s main Step callback, and always after Sk.initialize. https://stereokit.net/Pages/StereoKit.Framework/IStepper/Initialize.html id : The id of the stepper sk : The SkInfo of the runnin Sk instance.

Return true if the initiaization was succesfull or need to run on many steps and/or in another thread (see IStepper::initialize_done)

Source

fn step(&mut self, token: &MainThreadToken)

This Step method will be called every frame of the application, as long as Enabled is true. This happens immediately before the main application’s Step callback. https://stereokit.net/Pages/StereoKit.Framework/IStepper/Step.html

Provided Methods§

Source

fn initialize_done(&mut self) -> bool

If initialization is to be performed in multiple steps, with or without threads and in order to avoid black or frozen screens, write the on going initialization here

Return false as long as the initialization must keep going then true when the stepper has to be drawn.

Source

fn enabled(&self) -> bool

Is this IStepper enabled? When false, StereoKit will not call Step. This can be a good way to temporarily disable the IStepper without removing or shutting it down. https://stereokit.net/Pages/StereoKit.Framework/IStepper/Enabled.html

Source

fn shutdown(&mut self)

This is called when the IStepper is removed, or the application shuts down. This is always called on the main thread, and happens at the start of the next frame, before the main application’s Step callback. https://stereokit.net/Pages/StereoKit.Framework/IStepper/Shutdown.html

Source

fn shutdown_done(&mut self) -> bool

If shutdown is to be performed in multiple steps, with or without threads and in order to avoid black or frozen screens, write the on going shutdown here

Return false as long as the shutdown must keep going then true when the stepper can be remove.

Implementors§