lambda/component.rs
1use std::{
2 fmt::Debug,
3 time::Duration,
4};
5
6use crate::{
7 events::Events,
8 render::{
9 command::RenderCommand,
10 RenderContext,
11 },
12};
13
14/// The Component Interface for allowing Component based data structures
15/// like the ComponentStack to store components with various purposes
16/// and implementations to work together.
17pub trait Component<R, E>
18where
19 R: Sized + Debug,
20 E: Sized + Debug,
21{
22 /// The attach function is called when the component is added to the
23 /// component data storage a runtime is using.
24 fn on_attach(&mut self, render_context: &mut RenderContext) -> Result<R, E>;
25
26 /// The detach function is called when the component is removed from the
27 /// component data storage a runtime is using.
28 fn on_detach(&mut self, render_context: &mut RenderContext) -> Result<R, E>;
29
30 /// The event function is called every time an event is received from
31 /// the windowing system/event loop.
32 fn on_event(&mut self, event: Events) -> Result<R, E>;
33
34 /// The update function is called every frame and is used to update
35 /// the state of the component.
36 fn on_update(&mut self, last_frame: &Duration) -> Result<R, E>;
37
38 /// Render commands returned from this function will be executed
39 /// by the renderer immediately.
40 fn on_render(
41 &mut self,
42 render_context: &mut RenderContext,
43 ) -> Vec<RenderCommand>;
44}