1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use Context;
use crateVertex2;
use Transform;
use CanvasRenderingContext2d;
/// Defines the priority of the [Renderable](Renderable) - lower layers have higher priority (e.g.
/// a `Layer::One` [Renderable](Renderable) will always appear on top of a `Layer::Two`
/// [Renderable](Renderable)
/// The `Renderable` is a description of something that can be rendered.
/// Multiple renderables can be added to a single [Component](Component)
///
/// Make sure to use the `shape_factory` where possible to avoid specifying lists of vertices
/// manually.
///
/// ## Example
///
/// ```rust
/// use twors::{prelude::*, shape_factory};
///
/// pub fn new() -> Renderable {
/// Renderable {
/// transform: Transform::default(),
/// vertices: shape_factory::square(40.0),
/// style: |ctx: &CanvasRenderingContext2d| {
/// ctx.set_fill_style_str("orange");
/// ctx.set_line_width(1.0);
/// ctx.set_stroke_style_str("black");
/// ctx.stroke();
/// ctx.fill();
/// },
/// layer: twors::Layer::One,
/// }
/// }
/// ```
/// The `Component` is the bread and butter of our application - see the methods' documentation
/// and the example below for details.
///
/// ## Example
///
/// ```rust
/// use twors::prelude::*;
///
/// #[derive(Component)]
/// pub struct MyComponent {
/// component_state: i32,
///
/// // Those fields are mandatory for every component
/// transform: Transform,
/// renderables: Vec<Renderable>,
/// }
///
/// impl MyComponent {
/// pub fn new() -> Self {
/// Self {
/// component_state: 0,
///
/// transform: Transform::default(),
/// renderables: Vec::default(), // No renderables - the component will not be visible
/// }
/// }
/// }
///
/// impl ComponentLifecycle for MyComponent {
/// fn update(&mut self, ctx: &mut Context) {
/// // Read mouse & keyboard input
/// if ctx.input.mouse.is_pressed(Mouse::LMB) || ctx.input.keyboard.is_down(Key::A) {}
///
/// // Modify component state
/// self.component_state += 1;
///
/// // Use delta_time for movement
/// const SPEED: f32 = 20.0;
/// self.transform.position.x += SPEED * ctx.delta_time();
/// }
/// }
/// ```