lightyear_inputs_leafwing 0.27.0

IO primitives for the lightyear networking library
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use bevy_platform::time::Instant;

use crate::action_diff::ActionDiff;
use crate::action_state::{ActionStateWrapper, ActionStateWrapperReadOnlyItem, LeafwingUserAction};
use alloc::vec::Vec;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::entity::{EntityMapper, MapEntities};
use core::time::Duration;
use leafwing_input_manager::Actionlike;
use leafwing_input_manager::InputControlKind;
use leafwing_input_manager::action_state::{ActionKindData, ActionState};
use leafwing_input_manager::input_map::InputMap;
use lightyear_core::prelude::Tick;
use lightyear_inputs::input_buffer::{Compressed, InputBuffer};
use lightyear_inputs::input_message::{ActionStateSequence, InputSnapshot};
use serde::{Deserialize, Serialize};

pub type LeafwingBuffer<A> = InputBuffer<LeafwingSnapshot<A>, A>;
impl<A: LeafwingUserAction> InputSnapshot for LeafwingSnapshot<A> {
    fn decay_tick(&mut self, tick_duration: Duration) {
        self.tick(Instant::now(), Instant::now() + tick_duration);
    }
}

#[derive(Debug, Clone, PartialEq, Deref, DerefMut)]
pub struct LeafwingSnapshot<A: LeafwingUserAction>(pub ActionState<A>);

impl<A: LeafwingUserAction> From<ActionState<A>> for LeafwingSnapshot<A> {
    fn from(state: ActionState<A>) -> Self {
        Self(state)
    }
}

impl<A: LeafwingUserAction> Default for LeafwingSnapshot<A> {
    fn default() -> Self {
        Self(ActionState::default())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LeafwingSequence<A: Actionlike> {
    pub(crate) start_state: ActionState<A>,
    pub(crate) diffs: Vec<Vec<ActionDiff<A>>>,
}

impl<A: LeafwingUserAction> MapEntities for LeafwingSequence<A> {
    fn map_entities<M: EntityMapper>(&mut self, _: &mut M) {}
}

impl<A: LeafwingUserAction> ActionStateSequence for LeafwingSequence<A> {
    type Action = A;
    type Snapshot = LeafwingSnapshot<A>;
    type State = ActionStateWrapper<A>;
    type Marker = InputMap<A>;

    fn len(&self) -> usize {
        self.diffs.len() + 1
    }

    fn get_snapshots_from_message(
        self,
        tick_duration: Duration,
    ) -> impl Iterator<Item = Compressed<Self::Snapshot>> {
        let start_iter = core::iter::once(Compressed::Input(LeafwingSnapshot(
            self.start_state.clone(),
        )));
        let diffs_iter = self.diffs.into_iter().scan(
            self.start_state,
            move |state: &mut ActionState<A>, diffs_for_tick: Vec<ActionDiff<A>>| {
                state.tick(Instant::now() + tick_duration, Instant::now());
                for diff in diffs_for_tick {
                    diff.apply(state);
                }
                state.set_update_state_from_state();
                state.set_fixed_update_state_from_state();

                // TODO: how can we check if the state is the same as before, to return InputData::SameAsPrecedent instead?
                Some(Compressed::Input(LeafwingSnapshot(state.clone())))
            },
        );
        start_iter.chain(diffs_iter)
    }

    /// Add the inputs for the `num_ticks` ticks starting from `self.end_tick - num_ticks + 1` up to `self.end_tick`
    ///
    /// If we don't have a starting `ActionState` from the `input_buffer`, we start from the first tick for which
    /// we have an `ActionState`.
    fn build_from_input_buffer<'w, 's>(
        input_buffer: &InputBuffer<Self::Snapshot, Self::Action>,
        num_ticks: u32,
        end_tick: Tick,
    ) -> Option<Self> {
        let mut diffs = Vec::new();
        // find the first tick for which we have an `ActionState` buffered
        let mut start_tick = end_tick - num_ticks + 1;
        while start_tick <= end_tick {
            if input_buffer.get(start_tick).is_some() {
                break;
            }
            start_tick += 1;
        }

        // there are no ticks for which we have an `ActionState` buffered, so we send nothing
        if start_tick > end_tick {
            return None;
        }
        let start_state = input_buffer.get(start_tick).unwrap().clone();
        let mut tick = start_tick + 1;
        while tick <= end_tick {
            let diffs_for_tick = ActionDiff::<A>::create(
                // TODO: if the input_delay changes, this could leave gaps in the InputBuffer, which we will fill with Default
                input_buffer
                    .get(tick - 1)
                    .unwrap_or(&LeafwingSnapshot::<A>::default()),
                input_buffer
                    .get(tick)
                    .unwrap_or(&LeafwingSnapshot::<A>::default()),
            );
            diffs.push(diffs_for_tick);
            tick += 1;
        }
        Some(Self {
            start_state: start_state.0,
            diffs,
        })
    }

    fn to_snapshot<'w, 's>(state: ActionStateWrapperReadOnlyItem<A>) -> Self::Snapshot {
        LeafwingSnapshot(state.inner.clone())
    }

    fn from_snapshot<'w, 's>(state: &mut ActionState<A>, snapshot: &Self::Snapshot) {
        *state = snapshot.0.clone();
    }

    /// Apply snapshot with transition-aware button state handling.
    ///
    /// The wire format (`ActionDiff`) collapses `JustPressed` into `Pressed`, so
    /// raw-cloning a snapshot on the server loses `JustPressed`/`JustReleased`.
    ///
    /// This method instead:
    /// 1. Ticks the state (`JustPressed→Pressed`, `JustReleased→Released`)
    /// 2. For each action, compares current vs snapshot and calls `press()`/`release()`
    ///    to produce correct `JustPressed`/`JustReleased` transitions
    /// 3. Applies axis values directly
    fn from_snapshot_transitions<'w>(state: &mut ActionState<A>, snapshot: &Self::Snapshot) {
        // Advance button state machine so JustPressed→Pressed between consecutive
        // fixed ticks within a single frame (tick_action_state only runs in PreUpdate).
        state.tick(Instant::now(), Instant::now());

        let new = &snapshot.0;
        for (action, new_data) in new.all_action_data() {
            match &new_data.kind_data {
                ActionKindData::Button(new_button) => {
                    let is_pressed = new_button.pressed();
                    if is_pressed {
                        // press() sets JustPressed unless already Pressed
                        state.press(action);
                    } else {
                        // release() sets JustReleased unless already Released
                        state.release(action);
                    }
                }
                ActionKindData::Axis(axis_data) => {
                    state.set_value(action, axis_data.value);
                }
                ActionKindData::DualAxis(dual_data) => {
                    state.set_axis_pair(action, dual_data.pair);
                }
                ActionKindData::TripleAxis(triple_data) => {
                    state.set_axis_triple(action, triple_data.triple);
                }
            }
        }

        // Release any buttons present in current state but absent from snapshot
        let snapshot_keys: Vec<A> = new.keys();
        for action in state.keys() {
            if action.input_control_kind() != InputControlKind::Button {
                continue;
            }
            if !snapshot_keys.contains(&action) && state.pressed(&action) {
                state.release(&action);
            }
        }
    }
}

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

    use alloc::vec;
    use bevy_reflect::Reflect;
    use leafwing_input_manager::Actionlike;
    use serde::{Deserialize, Serialize};
    use std::time::Duration;
    use test_log::test;

    #[derive(
        Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Debug, Hash, Reflect, Actionlike,
    )]
    enum Action {
        Jump,
        Run,
    }

    #[test]
    fn test_create_message() {
        let mut input_buffer = InputBuffer::default();
        let mut action_state = ActionState::<Action>::default();
        input_buffer.set(Tick(2), ActionState::default().into());
        action_state.press(&Action::Jump);
        input_buffer.set(Tick(3), action_state.clone().into());
        action_state.release(&Action::Jump);
        input_buffer.set(Tick(7), action_state.clone().into());

        let sequence =
            LeafwingSequence::<Action>::build_from_input_buffer(&input_buffer, 9, Tick(10))
                .unwrap();
        assert_eq!(
            sequence,
            LeafwingSequence::<Action> {
                // tick 2
                start_state: ActionState::default(),
                diffs: vec![
                    // tick 3
                    vec![ActionDiff::Pressed {
                        action: Action::Jump,
                    }],
                    vec![],
                    vec![],
                    vec![],
                    // tick 7
                    vec![ActionDiff::Released {
                        action: Action::Jump,
                    }],
                    vec![],
                    vec![],
                    vec![],
                ]
            }
        );
    }

    #[test]
    fn test_build_from_input_buffer_empty() {
        let input_buffer: InputBuffer<_, _> = InputBuffer::default();
        let sequence =
            LeafwingSequence::<Action>::build_from_input_buffer(&input_buffer, 5, Tick(10));
        assert!(sequence.is_none());
    }

    #[test]
    fn test_build_from_input_buffer_partial_overlap() {
        let mut input_buffer = InputBuffer::default();
        let mut action_state = ActionState::<Action>::default();
        action_state.press(&Action::Jump);
        input_buffer.set(Tick(8), action_state.clone().into());
        action_state.release(&Action::Jump);
        action_state.press(&Action::Run);
        input_buffer.set(Tick(10), action_state.clone().into());

        // Only ticks 8 and 10 are set, so sequence should start at 8
        let sequence =
            LeafwingSequence::<Action>::build_from_input_buffer(&input_buffer, 5, Tick(12))
                .unwrap();
        assert_eq!(
            sequence.start_state,
            input_buffer.get(Tick(8)).unwrap().clone().0
        );
        assert_eq!(sequence.diffs.len(), 4);
    }

    #[test]
    fn test_update_buffer_extends_left_and_right() {
        let mut input_buffer = InputBuffer::default();
        let mut action_state = ActionState::<Action>::default();
        action_state.press(&Action::Jump);
        input_buffer.set(Tick(6), action_state.clone().into());
        input_buffer.last_remote_tick = Some(Tick(6));
        let sequence = LeafwingSequence::<Action> {
            // Tick 5
            start_state: action_state.clone(),
            diffs: vec![
                // Tick 6
                vec![],
                // Tick 7
                vec![ActionDiff::Pressed {
                    action: Action::Run,
                }],
                // Tick 8
                vec![ActionDiff::Released {
                    action: Action::Jump,
                }],
            ],
        };
        let mismatch = sequence.update_buffer(&mut input_buffer, Tick(8), Duration::default());
        assert_eq!(mismatch, Some(Tick(7)));

        // NOTE: The action_state from the sequence are ticked to avoid having JustPressed on each tick!
        let mut expected = action_state.clone();
        assert_eq!(input_buffer.get(Tick(6)).unwrap().0, expected);

        expected.tick(Instant::now(), Instant::now());
        expected.press(&Action::Run);
        expected.set_update_state_from_state();
        expected.set_fixed_update_state_from_state();
        assert_eq!(input_buffer.get(Tick(7)).unwrap().0, expected);

        expected.tick(Instant::now(), Instant::now());
        expected.release(&Action::Jump);
        expected.set_update_state_from_state();
        expected.set_fixed_update_state_from_state();
        assert_eq!(input_buffer.get(Tick(8)).unwrap().0, expected);
    }

    #[test]
    fn test_update_buffer_overwrites_existing() {
        let mut input_buffer = InputBuffer::default();
        let mut action_state = ActionState::<Action>::default();
        action_state.press(&Action::Jump);
        input_buffer.set(Tick(2), action_state.clone().into());
        let sequence = LeafwingSequence::<Action> {
            start_state: action_state.clone(),
            diffs: vec![vec![ActionDiff::Released {
                action: Action::Jump,
            }]],
        };
        // Should overwrite tick 3
        sequence.update_buffer(&mut input_buffer, Tick(3), Duration::default());
        assert_eq!(input_buffer.get(Tick(2)).unwrap().0, action_state);

        let mut expected = action_state.clone();
        expected.release(&Action::Jump);
        expected.set_update_state_from_state();
        expected.set_fixed_update_state_from_state();
        assert_eq!(input_buffer.get(Tick(3)).unwrap().0, expected);
    }

    /// Simulates the late-join rebroadcast scenario: client 1 holds [Jump]
    /// for several ticks, sends an InputMessage. A new client (empty buffer)
    /// receives the rebroadcast. The new client's buffer should contain
    /// the pressed action for all ticks in the message.
    #[test]
    fn test_rebroadcast_to_empty_buffer() {
        // Client 1 has been pressing Jump for ticks 5..10
        let mut sender_buffer = InputBuffer::default();
        let mut action_state = ActionState::<Action>::default();
        action_state.press(&Action::Jump);
        for tick in 5..=10 {
            sender_buffer.set(Tick(tick), action_state.clone().into());
        }

        // Build the message that client 1 sends (history_depth=6, end_tick=10)
        let sequence =
            LeafwingSequence::<Action>::build_from_input_buffer(&sender_buffer, 6, Tick(10))
                .unwrap();

        // New client receives it into an empty buffer
        let mut receiver_buffer: InputBuffer<LeafwingSnapshot<Action>, Action> =
            InputBuffer::default();
        let mismatch = sequence.update_buffer(&mut receiver_buffer, Tick(10), Duration::default());

        // Every tick should have Jump pressed
        for tick in 5..=10 {
            let snapshot = receiver_buffer
                .get(Tick(tick))
                .unwrap_or_else(|| panic!("missing input at tick {tick}"));
            assert!(
                snapshot.pressed(&Action::Jump),
                "Jump should be pressed at tick {tick}, got: {:?}",
                snapshot.get_pressed()
            );
        }

        // get_last should also return Jump pressed
        let last = receiver_buffer
            .get_last()
            .expect("buffer should not be empty");
        assert!(
            last.pressed(&Action::Jump),
            "get_last should return Jump pressed, got: {:?}",
            last.get_pressed()
        );
    }

    /// Like test_rebroadcast_to_empty_buffer, but the sender's buffer uses
    /// SameAsPrecedent compression (which happens when set() is called with
    /// the same value on consecutive ticks — the real-world case).
    #[test]
    fn test_rebroadcast_to_empty_buffer_with_compression() {
        let mut sender_buffer = InputBuffer::default();
        let mut action_state = ActionState::<Action>::default();
        action_state.press(&Action::Jump);
        // set() compresses consecutive identical values to SameAsPrecedent
        for tick in 5..=10 {
            sender_buffer.set(Tick(tick), LeafwingSnapshot(action_state.clone()));
        }
        // Verify compression is in effect
        assert!(
            sender_buffer.get(Tick(5)).is_some(),
            "tick 5 should be present"
        );
        assert!(
            sender_buffer.get(Tick(10)).is_some(),
            "tick 10 should be present (via SameAsPrecedent)"
        );

        let sequence =
            LeafwingSequence::<Action>::build_from_input_buffer(&sender_buffer, 6, Tick(10))
                .unwrap();

        let mut receiver_buffer: InputBuffer<LeafwingSnapshot<Action>, Action> =
            InputBuffer::default();
        sequence.update_buffer(&mut receiver_buffer, Tick(10), Duration::default());

        for tick in 5..=10 {
            let snapshot = receiver_buffer
                .get(Tick(tick))
                .unwrap_or_else(|| panic!("missing input at tick {tick}"));
            assert!(
                snapshot.pressed(&Action::Jump),
                "Jump should be pressed at tick {tick}, got: {:?}",
                snapshot.get_pressed()
            );
        }
    }

    /// Test that get_action_state (which uses get(), not get_predict()) returns
    /// the correct input for ticks within the buffer range after a rebroadcast.
    /// This simulates the late-join scenario where the client's current tick is
    /// within the buffer range.
    #[test]
    fn test_rebroadcast_get_within_range() {
        let mut sender_buffer = InputBuffer::default();
        let mut action_state = ActionState::<Action>::default();
        action_state.press(&Action::Jump);
        for tick in 80..=100 {
            sender_buffer.set(Tick(tick), LeafwingSnapshot(action_state.clone()));
        }

        let sequence =
            LeafwingSequence::<Action>::build_from_input_buffer(&sender_buffer, 20, Tick(100))
                .unwrap();

        let mut receiver_buffer: InputBuffer<LeafwingSnapshot<Action>, Action> =
            InputBuffer::default();
        sequence.update_buffer(&mut receiver_buffer, Tick(100), Duration::default());

        // Message covers ticks 81..=100 (20 ticks), not 80
        // Simulate what the client does: get(tick) for current simulation tick
        for tick in 81..=100 {
            let snapshot = receiver_buffer.get(Tick(tick));
            assert!(
                snapshot.is_some(),
                "get({tick}) should return Some for ticks in the buffer"
            );
            assert!(
                snapshot.unwrap().pressed(&Action::Jump),
                "Jump should be pressed at tick {tick}"
            );
        }

        // get() for ticks BEYOND the buffer should return None (this is the
        // late-join gap: the client's current tick is often beyond end_tick)
        assert!(
            receiver_buffer.get(Tick(101)).is_none(),
            "get(101) should return None — beyond buffer"
        );

        // get_predict() should return last value for ticks beyond buffer
        let predicted = receiver_buffer.get_predict(Tick(105));
        assert!(predicted.is_some(), "get_predict(105) should return Some");
        assert!(
            predicted.unwrap().pressed(&Action::Jump),
            "predicted should have Jump pressed"
        );
    }

    /// Simulates multiple rebroadcast messages arriving in the same frame
    /// (as happens in practice with redundant input sending).
    /// After processing all messages, get_last() should return the pressed state.
    #[test]
    fn test_multiple_rebroadcast_messages() {
        let mut sender_buffer = InputBuffer::default();
        let mut action_state = ActionState::<Action>::default();
        action_state.press(&Action::Jump);
        for tick in 470..=490 {
            sender_buffer.set(Tick(tick), LeafwingSnapshot(action_state.clone()));
        }

        let mut receiver_buffer: InputBuffer<LeafwingSnapshot<Action>, Action> =
            InputBuffer::default();

        // Simulate 3 messages arriving with different end_ticks (as the server
        // forwards each client packet)
        for end_tick in [485u32, 487, 490] {
            let sequence = LeafwingSequence::<Action>::build_from_input_buffer(
                &sender_buffer,
                20,
                Tick(end_tick),
            )
            .unwrap();
            sequence.update_buffer(&mut receiver_buffer, Tick(end_tick), Duration::default());
        }

        // Buffer should cover through tick 490
        assert_eq!(receiver_buffer.end_tick(), Some(Tick(490)));

        // get_last should have Jump pressed
        let last = receiver_buffer
            .get_last()
            .expect("buffer should not be empty");
        assert!(
            last.pressed(&Action::Jump),
            "get_last should return Jump pressed after multiple messages, got: {:?}",
            last.get_pressed()
        );

        // Check specific ticks
        for tick in 471..=490 {
            let snapshot = receiver_buffer.get(Tick(tick));
            assert!(
                snapshot.is_some_and(|s| s.pressed(&Action::Jump)),
                "Jump should be pressed at tick {tick}"
            );
        }

        // Extending the buffer (as extend_input_buffers_for_late_join does)
        // should preserve the pressed state
        if let Some(last_val) = receiver_buffer.get_last().cloned() {
            receiver_buffer.set(Tick(496), last_val);
        }
        let extended = receiver_buffer
            .get(Tick(496))
            .expect("tick 496 should exist");
        assert!(
            extended.pressed(&Action::Jump),
            "After extension, tick 496 should have Jump pressed, got: {:?}",
            extended.get_pressed()
        );
    }

    /// Check that if the input buffer has ticks beyond the previous mismatch, we still find the mismatch correctly
    #[test]
    fn test_update_buffer_mismatch() {
        let mut input_buffer = InputBuffer::default();
        let mut action_state = ActionState::<Action>::default();
        action_state.press(&Action::Jump);
        input_buffer.set(Tick(2), action_state.clone().into());
        input_buffer.last_remote_tick = Some(Tick(2));
        input_buffer.set(Tick(3), action_state.clone().into());
        input_buffer.set(Tick(4), action_state.clone().into());

        let sequence = LeafwingSequence::<Action> {
            start_state: action_state.clone(),
            diffs: vec![vec![ActionDiff::Released {
                action: Action::Jump,
            }]],
        };
        // Should overwrite tick 3
        sequence.update_buffer(&mut input_buffer, Tick(3), Duration::default());
        assert_eq!(input_buffer.get(Tick(2)).unwrap().0, action_state);

        let mut expected = action_state.clone();
        expected.release(&Action::Jump);
        expected.set_update_state_from_state();
        expected.set_fixed_update_state_from_state();
        assert_eq!(input_buffer.get(Tick(3)).unwrap().0, expected);
    }
}