nightshade 0.14.0

A cross-platform data-oriented game engine.
Documentation
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::ecs::world::World;

pub type SystemFn = fn(&mut World);

#[derive(Clone)]
pub struct FrameScheduleEntry {
    pub name: &'static str,
    pub system: SystemFn,
}

#[derive(Clone, Default)]
pub struct FrameSchedule {
    pub entries: Vec<FrameScheduleEntry>,
}

#[derive(Clone, Default)]
pub struct Schedules {
    pub frame: FrameSchedule,
    pub retained_ui: FrameSchedule,
}

pub fn schedule_push(schedule: &mut FrameSchedule, name: &'static str, system: SystemFn) {
    schedule.entries.push(FrameScheduleEntry { name, system });
}

pub fn schedule_insert_before(
    schedule: &mut FrameSchedule,
    target: &str,
    name: &'static str,
    system: SystemFn,
) {
    let position = schedule
        .entries
        .iter()
        .position(|entry| entry.name == target)
        .unwrap_or_else(|| panic!("schedule_insert_before: no system named \"{target}\""));
    schedule
        .entries
        .insert(position, FrameScheduleEntry { name, system });
}

pub fn schedule_insert_after(
    schedule: &mut FrameSchedule,
    target: &str,
    name: &'static str,
    system: SystemFn,
) {
    let position = schedule
        .entries
        .iter()
        .position(|entry| entry.name == target)
        .unwrap_or_else(|| panic!("schedule_insert_after: no system named \"{target}\""))
        + 1;
    schedule
        .entries
        .insert(position, FrameScheduleEntry { name, system });
}

pub fn schedule_remove(schedule: &mut FrameSchedule, name: &str) {
    schedule.entries.retain(|entry| entry.name != name);
}

pub fn schedule_contains(schedule: &FrameSchedule, name: &str) -> bool {
    schedule.entries.iter().any(|entry| entry.name == name)
}

pub fn schedule_run(schedule: &FrameSchedule, world: &mut World) {
    let _span = tracing::info_span!("systems").entered();
    for entry in &schedule.entries {
        (entry.system)(world);
    }
}

pub mod system_names {
    pub const INITIALIZE_AUDIO: &str = "initialize_audio";
    pub const BUILD_AUDIO_BUSES: &str = "build_audio_buses";
    pub const UPDATE_AUDIO: &str = "update_audio";
    #[cfg(target_arch = "wasm32")]
    pub const SYNC_WASM_CANVAS_SIZE: &str = "sync_wasm_canvas_size";
    pub const ENSURE_BOUNDING_VOLUMES: &str = "ensure_bounding_volumes";
    pub const RUN_PHYSICS: &str = "run_physics";
    pub const UPDATE_ANIMATION_PLAYERS: &str = "update_animation_players";
    pub const APPLY_ANIMATIONS: &str = "apply_animations";
    pub const TRANSFORM_SYSTEMS: &str = "transform_systems";
    pub const UPDATE_INSTANCED_MESH_CACHES: &str = "update_instanced_mesh_caches";
    pub const RETAINED_UI: &str = "retained_ui";
    #[cfg(feature = "gizmos")]
    pub const GIZMO_OVERLAY: &str = "gizmo_overlay";
    #[cfg(feature = "gizmos")]
    pub const NAV_GIZMO_OVERLAY: &str = "nav_gizmo_overlay";
    pub const APPLY_IME_ALLOWED: &str = "apply_ime_allowed";
    pub const RESET_MOUSE: &str = "reset_mouse";
    pub const RESET_KEYBOARD: &str = "reset_keyboard";
    pub const RESET_TOUCH: &str = "reset_touch";
    pub const PROCESS_COMMANDS: &str = "process_commands";
    pub const CLEANUP_UNUSED_RESOURCES: &str = "cleanup_unused_resources";
    pub const RUN_NAVMESH: &str = "run_navmesh";
    pub const SYNC_TEXT_MESHES: &str = "sync_text_meshes";
    pub const POLL_FILE_WATCHER: &str = "poll_file_watcher";
    pub const POLL_ASSET_WATCHER: &str = "poll_asset_watcher";
    pub const UPDATE_PARTICLE_EMITTERS: &str = "update_particle_emitters";
}

pub mod retained_ui_system_names {
    pub const INPUT_SYNC: &str = "ui_retained_input_sync";
    pub const GAMEPAD_NAVIGATION: &str = "ui_gamepad_navigation";
    pub const LAYOUT_PICKING: &str = "ui_layout_picking";
    pub const WIDGET_INTERACTION: &str = "ui_widget_interaction";
    pub const EVENT_BUBBLE: &str = "ui_event_bubble";
    pub const EMIT_CLICK_EVENTS: &str = "ui_emit_click_events";
    pub const LAYOUT_STATE_UPDATE: &str = "ui_layout_state_update";
    pub const DOCKED_PANEL_LAYOUT: &str = "ui_docked_panel_layout";
    pub const RESPONSIVE_APPLY: &str = "ui_responsive_apply";
    pub const LAYOUT_COMPUTE: &str = "ui_layout_compute";
    pub const THEME_TRANSITION_TICK: &str = "ui_theme_transition_tick";
    pub const THEME_APPLY: &str = "ui_theme_apply";
    pub const LAYOUT_COLOR_BLEND: &str = "ui_layout_color_blend";
    pub const LAYOUT_TRANSFORM_BLEND: &str = "ui_layout_transform_blend";
    pub const LAYOUT_RENDER_SYNC: &str = "ui_layout_render_sync";
}

pub fn build_default_frame_schedule() -> FrameSchedule {
    let mut schedule = FrameSchedule::default();

    #[cfg(feature = "audio")]
    {
        schedule_push(
            &mut schedule,
            system_names::INITIALIZE_AUDIO,
            crate::ecs::audio::systems::initialize_audio_system,
        );
        schedule_push(
            &mut schedule,
            system_names::BUILD_AUDIO_BUSES,
            crate::ecs::audio::systems::build_audio_buses_system,
        );
        schedule_push(
            &mut schedule,
            system_names::UPDATE_AUDIO,
            crate::ecs::audio::systems::update_audio_system,
        );
    }

    #[cfg(target_arch = "wasm32")]
    schedule_push(
        &mut schedule,
        system_names::SYNC_WASM_CANVAS_SIZE,
        crate::ecs::camera::systems::sync_wasm_canvas_size,
    );
    schedule_push(
        &mut schedule,
        system_names::ENSURE_BOUNDING_VOLUMES,
        crate::ecs::bounding_volume::systems::ensure_bounding_volumes,
    );

    #[cfg(feature = "physics")]
    schedule_push(
        &mut schedule,
        system_names::RUN_PHYSICS,
        crate::ecs::physics::systems::run_physics_systems,
    );

    schedule_push(
        &mut schedule,
        system_names::UPDATE_ANIMATION_PLAYERS,
        crate::ecs::animation::systems::update_animation_players,
    );
    schedule_push(
        &mut schedule,
        system_names::APPLY_ANIMATIONS,
        crate::ecs::animation::systems::apply_animations,
    );
    #[cfg(feature = "navmesh")]
    schedule_push(
        &mut schedule,
        system_names::RUN_NAVMESH,
        crate::ecs::navmesh::systems::run_navmesh_systems,
    );
    schedule_push(
        &mut schedule,
        system_names::SYNC_TEXT_MESHES,
        crate::ecs::text::systems::sync_text_meshes_system,
    );
    schedule_push(
        &mut schedule,
        system_names::UPDATE_PARTICLE_EMITTERS,
        crate::ecs::particles::systems::update_particle_emitters,
    );
    schedule_push(
        &mut schedule,
        system_names::TRANSFORM_SYSTEMS,
        crate::ecs::transform::systems::run_systems,
    );
    schedule_push(
        &mut schedule,
        system_names::UPDATE_INSTANCED_MESH_CACHES,
        crate::ecs::mesh::systems::update_instanced_mesh_caches_system,
    );
    schedule_push(
        &mut schedule,
        system_names::RETAINED_UI,
        run_retained_ui_schedule,
    );
    #[cfg(feature = "gizmos")]
    {
        schedule_push(
            &mut schedule,
            system_names::GIZMO_OVERLAY,
            crate::ecs::gizmos::gizmo_overlay_system,
        );
        schedule_push(
            &mut schedule,
            system_names::NAV_GIZMO_OVERLAY,
            crate::ecs::gizmos::nav_gizmo_overlay_system,
        );
    }
    schedule_push(
        &mut schedule,
        system_names::APPLY_IME_ALLOWED,
        crate::ecs::input::systems::apply_ime_allowed_system,
    );
    schedule_push(
        &mut schedule,
        system_names::RESET_MOUSE,
        crate::ecs::input::systems::reset_mouse_system,
    );
    schedule_push(
        &mut schedule,
        system_names::RESET_KEYBOARD,
        crate::ecs::input::systems::reset_keyboard_system,
    );
    schedule_push(
        &mut schedule,
        system_names::RESET_TOUCH,
        crate::ecs::input::systems::reset_touch_system,
    );
    #[cfg(all(feature = "file_watcher", not(target_arch = "wasm32")))]
    schedule_push(
        &mut schedule,
        system_names::POLL_FILE_WATCHER,
        crate::ecs::file_watcher::poll_file_watcher_system,
    );
    #[cfg(all(
        feature = "assets",
        feature = "file_watcher",
        not(target_arch = "wasm32")
    ))]
    schedule_push(
        &mut schedule,
        system_names::POLL_ASSET_WATCHER,
        crate::ecs::asset_watcher::poll_asset_watcher_system,
    );
    schedule_push(
        &mut schedule,
        system_names::PROCESS_COMMANDS,
        crate::ecs::world::process_commands_system,
    );
    schedule_push(
        &mut schedule,
        system_names::CLEANUP_UNUSED_RESOURCES,
        crate::ecs::world::cleanup_unused_resources_system,
    );

    schedule
}

pub fn build_default_retained_ui_schedule() -> FrameSchedule {
    let mut schedule = FrameSchedule::default();
    schedule_push(
        &mut schedule,
        retained_ui_system_names::INPUT_SYNC,
        crate::ecs::ui::widget_systems::ui_retained_input_sync_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::LAYOUT_PICKING,
        crate::ecs::ui::picking::ui_layout_picking_system,
    );
    // Gamepad navigation has to run after picking. Picking resets every
    // entity's `interaction.clicked` flag at the top of the frame, so if
    // gamepad nav set `clicked = true` for the focused entity before
    // picking, the activation would be wiped before any widget or click
    // emitter could see it. Running it after picking lets the gamepad's
    // South-press flag survive into widget interaction and click event
    // emission.
    #[cfg(feature = "gamepad")]
    schedule_push(
        &mut schedule,
        retained_ui_system_names::GAMEPAD_NAVIGATION,
        crate::ecs::ui::gamepad_navigation::ui_gamepad_navigation_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::WIDGET_INTERACTION,
        crate::ecs::ui::widget_systems::ui_widget_interaction_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::EVENT_BUBBLE,
        crate::ecs::ui::widget_systems::ui_event_bubble_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::EMIT_CLICK_EVENTS,
        crate::ecs::ui::widget_systems::ui_emit_click_events_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::LAYOUT_STATE_UPDATE,
        crate::ecs::ui::systems::ui_layout_state_update_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::DOCKED_PANEL_LAYOUT,
        crate::ecs::ui::systems::ui_docked_panel_layout_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::RESPONSIVE_APPLY,
        crate::ecs::ui::systems::ui_responsive_apply_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::LAYOUT_COMPUTE,
        crate::ecs::ui::systems::ui_layout_compute_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::THEME_TRANSITION_TICK,
        crate::ecs::ui::systems::ui_theme_transition_tick_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::THEME_APPLY,
        crate::ecs::ui::systems::ui_theme_apply_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::LAYOUT_COLOR_BLEND,
        crate::ecs::ui::systems::ui_layout_color_blend_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::LAYOUT_TRANSFORM_BLEND,
        crate::ecs::ui::systems::ui_layout_transform_blend_system,
    );
    schedule_push(
        &mut schedule,
        retained_ui_system_names::LAYOUT_RENDER_SYNC,
        crate::ecs::ui::render_sync::ui_layout_render_sync_system,
    );
    schedule
}

pub fn run_retained_ui_schedule(world: &mut World) {
    let schedule = std::mem::take(&mut world.resources.schedules.retained_ui);
    schedule_run(&schedule, world);
    world.resources.schedules.retained_ui = schedule;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn frame_schedule_contains_one_retained_ui_entry() {
        let schedule = build_default_frame_schedule();
        let count = schedule
            .entries
            .iter()
            .filter(|entry| entry.name == system_names::RETAINED_UI)
            .count();
        assert_eq!(
            count, 1,
            "frame schedule must collapse retained UI into a single entry"
        );
    }

    #[test]
    fn retained_ui_schedule_orders_pipeline_correctly() {
        let schedule = build_default_retained_ui_schedule();
        let names: Vec<&str> = schedule.entries.iter().map(|entry| entry.name).collect();

        let expected_prefix = [
            retained_ui_system_names::INPUT_SYNC,
            retained_ui_system_names::LAYOUT_PICKING,
            #[cfg(feature = "gamepad")]
            retained_ui_system_names::GAMEPAD_NAVIGATION,
            retained_ui_system_names::WIDGET_INTERACTION,
            retained_ui_system_names::EVENT_BUBBLE,
            retained_ui_system_names::EMIT_CLICK_EVENTS,
            retained_ui_system_names::LAYOUT_STATE_UPDATE,
            retained_ui_system_names::DOCKED_PANEL_LAYOUT,
            retained_ui_system_names::RESPONSIVE_APPLY,
            retained_ui_system_names::LAYOUT_COMPUTE,
            retained_ui_system_names::THEME_TRANSITION_TICK,
            retained_ui_system_names::THEME_APPLY,
            retained_ui_system_names::LAYOUT_COLOR_BLEND,
            retained_ui_system_names::LAYOUT_TRANSFORM_BLEND,
            retained_ui_system_names::LAYOUT_RENDER_SYNC,
        ];
        assert_eq!(names, expected_prefix);
    }
}