pub struct SkClosures<'a> { /* private fields */ }
Expand description
Since winit v0.30 we have to implement winit::application::ApplicationHandler and run the app with SkClosures::run or SkClosures::run_app
§Example
stereokit_rust::test_init_sk!(); // !!!! Get a proper way to initialize sk !!!!
use stereokit_rust::{maths::{Matrix, Pose}, model::Model, system::Renderer, system::Log,
material::Material, tools::title::Title, util::named_colors,
framework::{SkClosures, StepperAction}, sk::QuitReason};
let model = Model::from_file("cuve.glb", None).expect("Missing cube.glb").copy();
let material = Material::from_file("shaders/brick_pbr.hlsl.sks", None)
.expect("Missing shader");
let transform = Matrix::t_r([0.0, 0.0, -6.5], [90.0, 0.0, 0.0]);
let mut title = Title::new("SkClosures", None, None, None);
sk.send_event(StepperAction::add("Title_ID", title));
let mut iter = 0;
let mut hidden_time = std::time::SystemTime::now();
filename_scr = "screenshots/sk_closures.jpeg";
SkClosures::new(sk, |sk, token| {
// Main loop where we draw stuff and do things!!
if iter > number_of_steps {sk.quit(None)}
model.draw_with_material(token, &material, transform , None, None);
iter+=1;
if iter == number_of_steps {
// render screenshot
system::Renderer::screenshot(token, filename_scr, 90,
maths::Pose::look_at(from_scr, at_scr),
width_scr, height_scr, Some(fov_scr) );
}
})
.on_sleeping_step(|_sk, _token| {
// This is called every 200ms when the app is sleeping
// when the android headset is off
let now = std::time::SystemTime::now();
if let Ok(duration) = now.duration_since(hidden_time) {
if duration.as_secs() > 15 {
Log::info("HIDDEN STEP -------> Dreaming ");
hidden_time = now;hidden_time = now;
}
}
})
.shutdown(|sk| {
// This is called when the app is shutting down
assert_eq!(sk.get_quit_reason(), QuitReason::User);
Log::info(format!("QuitReason is {:?}", sk.get_quit_reason()));
})
.run(event_loop);

Implementations§
Source§impl<'a> SkClosures<'a>
impl<'a> SkClosures<'a>
Sourcepub fn run_app<U: FnMut(&mut Sk, &MainThreadToken) + 'a, S: FnMut(&mut Sk) + 'a>(
sk: Sk,
event_loop: EventLoop<StepperAction>,
on_step: U,
on_shutdown: S,
)
pub fn run_app<U: FnMut(&mut Sk, &MainThreadToken) + 'a, S: FnMut(&mut Sk) + 'a>( sk: Sk, event_loop: EventLoop<StepperAction>, on_step: U, on_shutdown: S, )
Common way to run the main loop with only step and shutdown. This will block the thread until the application is
shuting down.
If you need a process when the headset is going to sleep or track window events: use SkClosures::new instead.
- sk: the stereokit context.
- event_loop: the winit event loop.
- on_step: a callback that will be called every frame.
- on_shutdown: a callback that will be called when the application is shutting down. After on_step and after the steppers have been shutdown.
see also SkClosures::new
§Example
stereokit_rust::test_init_sk!(); // !!!! Get a proper way to initialize sk !!!!
use stereokit_rust::{maths::{Matrix, Pose}, model::Model, system::Renderer, system::Log,
framework::SkClosures , sk::QuitReason};
let model = Model::from_file("cuve.glb", None).expect("Missing cube.glb").copy();
let transform = Matrix::IDENTITY;
let mut iter = 0;
SkClosures::run_app(sk, event_loop, |sk: &mut Sk, token: &MainThreadToken| {
// Main loop where we draw stuff and do things!!
if iter > number_of_steps {sk.quit(None)}
model.draw(token, transform , None, None);
iter += 1;
},|sk: &mut Sk| {
// This is called when the app is shutting down
assert_eq!(sk.get_quit_reason(), QuitReason::User);
Log::info(format!("QuitReason is {:?}", sk.get_quit_reason()));
});
Sourcepub fn new<U: FnMut(&mut Sk, &MainThreadToken) + 'a>(sk: Sk, on_step: U) -> Self
pub fn new<U: FnMut(&mut Sk, &MainThreadToken) + 'a>(sk: Sk, on_step: U) -> Self
Create a new SkClosures with a step function.
Add some callbacks with SkClosures::on_sleeping_step, SkClosures::on_window_event and SkClosures::shutdown
- sk : the Sk context.
- on_step : the function to call on each step.
see also SkClosures::run_app
§Example
stereokit_rust::test_init_sk!(); // !!!! Get a proper way to initialize sk !!!!
use stereokit_rust::{maths::{Matrix, Pose}, model::Model, system::Renderer, system::Log,
framework::SkClosures , sk::QuitReason};
let model = Model::from_file("cuve.glb", None).expect("Missing cube.glb").copy();
let transform = Matrix::IDENTITY;
let mut iter = 0;
let mut hidden_time = std::time::SystemTime::now();
SkClosures::new(sk, |sk, token| {
// Main loop where we draw stuff and do things!!
if iter > number_of_steps {sk.quit(None)}
model.draw(token, transform , None, None);
iter+=1;
})
.on_sleeping_step(|_sk, _token| {
// This is called every 200ms when the app is sleeping
// when the android headset is off
let now = std::time::SystemTime::now();
if let Ok(duration) = now.duration_since(hidden_time) {
if duration.as_secs() > 15 {
Log::info("HIDDEN STEP -------> Dreaming ");
hidden_time = now;hidden_time = now;
}
}
})
.shutdown(|sk| {
// This is called when the app is shutting down
assert_eq!(sk.get_quit_reason(), QuitReason::User);
Log::info(format!("QuitReason is {:?}", sk.get_quit_reason()));
})
.run(event_loop);
Sourcepub fn on_sleeping_step<U: FnMut(&mut Sk, &MainThreadToken) + 'a>(
&mut self,
on_sleeping_step: U,
) -> &mut Self
pub fn on_sleeping_step<U: FnMut(&mut Sk, &MainThreadToken) + 'a>( &mut self, on_sleeping_step: U, ) -> &mut Self
Add a sleeping step function to this SkClosures. This will be called every 200ms when the headset is down. Only for Android apps and architectures where the headset can be turned off.
on_sleeping_step
- The function to call when the headset is down.
May be set after SkClosures::new see examples SkClosures
Sourcepub fn on_window_event<U: FnMut(&mut Sk, WindowEvent) + 'a>(
&mut self,
on_window_event: U,
) -> &mut Self
pub fn on_window_event<U: FnMut(&mut Sk, WindowEvent) + 'a>( &mut self, on_window_event: U, ) -> &mut Self
Not usefull right now, but will be used to handle external controller events in the future.
on_window_event
- The function to call when a window event is received that as not been handled by the Steppers controller.
May be set after SkClosures::new
Sourcepub fn shutdown<S: FnMut(&mut Sk) + 'a>(&mut self, on_shutdown: S) -> &mut Self
pub fn shutdown<S: FnMut(&mut Sk) + 'a>(&mut self, on_shutdown: S) -> &mut Self
Add a shutdown function to this SkClosures. This will be called when the app is shutting down. After all steppers have been shutdown, then the app will exit.
on_shutdown
- The function to call when the app is shutting down.
May be set after SkClosures::new see examples SkClosures
Sourcepub fn run(&mut self, event_loop: EventLoop<StepperAction>)
pub fn run(&mut self, event_loop: EventLoop<StepperAction>)
Run the main loop. This will block until the app is shutting down.
Have to be launched after SkClosures::new and eventually SkClosures::on_sleeping_step and SkClosures::shutdown see examples SkClosures
event_loop
- The event loop to run the app on. Created by Sk::init_with_event_loop.
Trait Implementations§
Source§impl ApplicationHandler<StepperAction> for SkClosures<'_>
impl ApplicationHandler<StepperAction> for SkClosures<'_>
Source§fn user_event(
&mut self,
_event_loop: &ActiveEventLoop,
user_event: StepperAction,
)
fn user_event( &mut self, _event_loop: &ActiveEventLoop, user_event: StepperAction, )
EventLoopProxy::send_event
.Source§fn resumed(&mut self, _event_loop: &ActiveEventLoop)
fn resumed(&mut self, _event_loop: &ActiveEventLoop)
Source§fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
window_id: WindowId,
event: WindowEvent,
)
fn window_event( &mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent, )
Source§fn about_to_wait(&mut self, event_loop: &ActiveEventLoop)
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop)
Source§fn suspended(&mut self, _event_loop: &ActiveEventLoop)
fn suspended(&mut self, _event_loop: &ActiveEventLoop)
Source§fn exiting(&mut self, _event_loop: &ActiveEventLoop)
fn exiting(&mut self, _event_loop: &ActiveEventLoop)
Source§fn memory_warning(&mut self, _event_loop: &ActiveEventLoop)
fn memory_warning(&mut self, _event_loop: &ActiveEventLoop)
Source§fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: StartCause)
fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: StartCause)
Source§fn device_event(
&mut self,
event_loop: &ActiveEventLoop,
device_id: DeviceId,
event: DeviceEvent,
)
fn device_event( &mut self, event_loop: &ActiveEventLoop, device_id: DeviceId, event: DeviceEvent, )
Auto Trait Implementations§
impl<'a> Freeze for SkClosures<'a>
impl<'a> !RefUnwindSafe for SkClosures<'a>
impl<'a> !Send for SkClosures<'a>
impl<'a> !Sync for SkClosures<'a>
impl<'a> Unpin for SkClosures<'a>
impl<'a> !UnwindSafe for SkClosures<'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
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>
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>
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)
&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)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.