Struct StepperClosures

Source
pub struct StepperClosures<'a> { /* private fields */ }
Expand description

Helper to create the whole code of a Stepper in method IStepper::initialize() while avoiding multiple fields.

Not really convincing with too many compromises. The privileged solution is the use of the derive macro crate::IStepper

See Demo b_stepper.rs::BStepper Non canonical structure

§Examples:

stereokit_rust::test_init_sk!(); // !!!! Get a proper way to initialize sk !!!!
use stereokit_rust::{font::Font, framework::StepperClosures, material::Material,
                     maths::{Matrix, Quat, Vec3},mesh::Mesh, system::{Renderer, Text},
                     util::{Time, named_colors},};

pub struct BStepper {
    id: StepperId,
    sk_info: Option<Rc<RefCell<SkInfo>>>,
    pub text: String,
    closures: StepperClosures<'static>,
}

unsafe impl Send for BStepper {}

/// This code may be called in some threads, so no StereoKit code
impl Default for BStepper {
    fn default() -> Self {
        Self {
            id: "BStepper".to_string(),
            sk_info: None,
            text: "\nStepperClosures".to_owned(),
            closures: StepperClosures::new(),
        }
    }
}

/// All the code here run in the main thread
impl IStepper for BStepper {
    fn initialize(&mut self, id: StepperId, sk_info: Rc<RefCell<SkInfo>>) -> bool {
        self.id = id;
        self.sk_info = Some(sk_info);

        let mut transform = Matrix::t_r([0.0, 1.0, -0.5], [0.0, 180.0, 0.0]);
        let mut round_cube = Mesh::generate_rounded_cube(Vec3::ONE / 5.0, 0.005, Some(16));
        round_cube.id("round_cube BStepper");
        let text_style = Some(Text::make_style(Font::default(), 0.3, named_colors::GOLD));
        let text = self.text.clone();

        self.closures.set(
            move |token| {
                Renderer::add_mesh(token, &round_cube, Material::pbr(),
                                   transform, Some(named_colors::RED.into()), None);
                Text::add_at(token, &text, transform, text_style,
                             None, None, None, None, None, None);
                // (1) You cannot do that here: self.text = "youpi".into();
            },
            || Log::diag("Closing Stepper B !!!"),
        );
        true
    }

    fn step(&mut self, token: &MainThreadToken) {
        self.closures.step(token);
        // (2) Add here The code about fields that are shared with shutdown >>>
        {}
        //<<<
    }

    fn shutdown(&mut self) {
        self.closures.shutdown();
        // (3) Add here The code about fields that are shared with step >>>
        {}
        //<<<
    }
}

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

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

Implementations§

Source§

impl StepperClosures<'_>

Source

pub fn new() -> Self

Same as default, but with a more explicit name

Source

pub fn set<U: FnMut(&MainThreadToken) + 'static, S: FnMut() + 'static>( &mut self, on_step: U, on_shutdown: S, ) -> &mut Self

Set the stepper closures.

  • on_step - The closure to call on each step.
  • on_shutdown - The closure to call on shutdown.
Source

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

To call on each step.

  • token - The main thread token.
Source

pub fn shutdown(&mut self)

To call on shutdown.

Trait Implementations§

Source§

impl Default for StepperClosures<'_>

Source§

fn default() -> Self

Same as new, but with a more explicit name

Auto Trait Implementations§

§

impl<'a> Freeze for StepperClosures<'a>

§

impl<'a> !RefUnwindSafe for StepperClosures<'a>

§

impl<'a> !Send for StepperClosures<'a>

§

impl<'a> !Sync for StepperClosures<'a>

§

impl<'a> Unpin for StepperClosures<'a>

§

impl<'a> !UnwindSafe for StepperClosures<'a>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more