Struct SkClosures

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

Implementations§

Source§

impl<'a> SkClosures<'a>

Source

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()));
});
Source

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

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

Source

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

Source

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

Source

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

Trait Implementations§

Source§

impl ApplicationHandler<StepperAction> for SkClosures<'_>

Source§

fn user_event( &mut self, _event_loop: &ActiveEventLoop, user_event: StepperAction, )

Emitted when an event is sent from EventLoopProxy::send_event.
Source§

fn resumed(&mut self, _event_loop: &ActiveEventLoop)

Emitted when the application has been resumed. Read more
Source§

fn window_event( &mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent, )

Emitted when the OS sends an event to a winit window.
Source§

fn about_to_wait(&mut self, event_loop: &ActiveEventLoop)

Emitted when the event loop is about to block and wait for new events. Read more
Source§

fn suspended(&mut self, _event_loop: &ActiveEventLoop)

Emitted when the application has been suspended. Read more
Source§

fn exiting(&mut self, _event_loop: &ActiveEventLoop)

Emitted when the event loop is being shut down. Read more
Source§

fn memory_warning(&mut self, _event_loop: &ActiveEventLoop)

Emitted when the application has received a memory warning. Read more
Source§

fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: StartCause)

Emitted when new events arrive from the OS to be processed. Read more
Source§

fn device_event( &mut self, event_loop: &ActiveEventLoop, device_id: DeviceId, event: DeviceEvent, )

Emitted when the OS sends an event to a device.

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