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
);

Implementations§
Source§impl StepperClosures<'_>
impl StepperClosures<'_>
Sourcepub fn set<U: FnMut(&MainThreadToken) + 'static, S: FnMut() + 'static>(
&mut self,
on_step: U,
on_shutdown: S,
) -> &mut Self
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.
Sourcepub fn step(&mut self, token: &MainThreadToken)
pub fn step(&mut self, token: &MainThreadToken)
To call on each step.
token
- The main thread token.
Trait Implementations§
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> 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> 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.