From eb5185b8e4d0e1062a62402dd400608fad9f59e7 Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sat, 28 Nov 2020 15:22:51 -0600 Subject: [PATCH 01/11] Updated changelog --- CHANGELOG.md | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05e7dbaaebd89..abacc1779410f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,12 +10,55 @@ current changes on git with [previous release tags][git_tag_comparison]. ## Unreleased ### Added - +- [`bevy_log`][836] + - Adds logging functionality as a Plugin. + - Changes internal logging to work with the new implementation. +- [Controllable ambient light color][852] + - Added a resource to change the current ambient light color for PBR. +- [Added more basic color constants][859] +- [System Inputs, Outputs, and Chaining][876] +- [Expose an `EventId` for events][894] +- [Added `set_cursor_position` to `Window`][917] +- [Added new Bevy reflection system][926] + - Replaces the properties system ### Changed +- [FileAssetIo includes full path on error][821] +- [Removed ECS query APIs that could easily violate safety from the public interface][829] +- [Changed Query filter API to be easier to understand][834] - [Breaking changes to timer API][914] + - Created getters and setters rather than exposing struct members. - [Removed timer auto-ticking system][931] + - Added an example of how to tick timers manually. +- [Breaking changes to Time API][934] + - Created getters to get `Time` state and made members private. + - Modifying `Time`'s values directly is no longer possible outside of bevy. +- [Use `mailbox` instead of `fifo` for vsync on supported systems][920] ### Fixed - +- [Fixed typos in KeyCode identifiers][857] +- [Don't draw text that isn't visible][893] +- [Use `instant::Instant` for WASM compatibility][895] +- [Fixed duplicated children when spawning a Scene][904] +- [Corrected behaviour of the UI depth system][905] + +[821]: https://github.com/bevyengine/bevy/pull/821 +[829]: https://github.com/bevyengine/bevy/pull/829 +[834]: https://github.com/bevyengine/bevy/pull/834 +[836]: https://github.com/bevyengine/bevy/pull/836 +[852]: https://github.com/bevyengine/bevy/pull/852 +[857]: https://github.com/bevyengine/bevy/pull/857 +[859]: https://github.com/bevyengine/bevy/pull/859 +[876]: https://github.com/bevyengine/bevy/pull/876 +[893]: https://github.com/bevyengine/bevy/pull/893 +[894]: https://github.com/bevyengine/bevy/pull/894 +[895]: https://github.com/bevyengine/bevy/pull/895 +[904]: https://github.com/bevyengine/bevy/pull/904 +[905]: https://github.com/bevyengine/bevy/pull/905 +[914]: https://github.com/bevyengine/bevy/pull/914 +[917]: https://github.com/bevyengine/bevy/pull/917 +[920]: https://github.com/bevyengine/bevy/pull/920 +[926]: https://github.com/bevyengine/bevy/pull/926 +[931]: https://github.com/bevyengine/bevy/pull/931 +[934]: https://github.com/bevyengine/bevy/pull/934 ## Version 0.3.0 (2020-11-03) From 0131a0483d3a572d69884df8aa5d06c60b84d095 Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sun, 29 Nov 2020 02:50:50 -0600 Subject: [PATCH 02/11] Test for bevy_math::FaceToward --- crates/bevy_math/src/face_toward.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/bevy_math/src/face_toward.rs b/crates/bevy_math/src/face_toward.rs index d090989fd4ccd..d594c5a62c652 100644 --- a/crates/bevy_math/src/face_toward.rs +++ b/crates/bevy_math/src/face_toward.rs @@ -19,3 +19,22 @@ impl FaceToward for Mat4 { ) } } + +mod test { + #[test] + fn face_toward_mat4() { + use crate::{FaceToward, Mat4, Vec3, Vec4}; + + // Completely arbitrary arguments + let matrix = Mat4::face_toward( + Vec3::new(50.0, 60.0, 0.0), + Vec3::new(0.0, 0.0, 0.0), + Vec3::new(0.0, 1.0, 0.0), + ); + + assert_eq!(matrix.x_axis, Vec4::new(0.0, 0.0, -1.0, -0.0)); + assert_eq!(matrix.y_axis, Vec4::new(-0.7682213, 0.6401844, 0.0, 0.0)); + assert_eq!(matrix.z_axis, Vec4::new(0.6401844, 0.7682213, 0.0, 0.0)); + assert_eq!(matrix.w_axis, Vec4::new(50.0, 60.0, 0.0, 1.0)); + } +} From 2525dcd5596c03fc104e9a5b141737eadabd92b0 Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sun, 29 Nov 2020 03:07:27 -0600 Subject: [PATCH 03/11] Add test to `bevy_input::Input` --- crates/bevy_input/src/input.rs | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/crates/bevy_input/src/input.rs b/crates/bevy_input/src/input.rs index 72876dcd2da09..d53a6c8b87832 100644 --- a/crates/bevy_input/src/input.rs +++ b/crates/bevy_input/src/input.rs @@ -71,3 +71,79 @@ where self.just_released.iter() } } + +mod test { + + #[test] + fn input_test() { + use crate::Input; + + /// Used for testing `Input` functionality + #[derive(Copy, Clone, Eq, PartialEq, Hash)] + enum DummyInput { + Input1, + Input2, + } + + let mut input = Input::default(); + + // Test pressing + input.press(DummyInput::Input1); + input.press(DummyInput::Input2); + + // Check if they were "just pressed" (pressed on this update) + assert!(input.just_pressed(DummyInput::Input1)); + assert!(input.just_pressed(DummyInput::Input2)); + + // Check if they are also marked as pressed + assert!(input.pressed(DummyInput::Input1)); + assert!(input.pressed(DummyInput::Input2)); + + // Update the `Input` and check press state + input.update(); + + // Check if they're marked "just pressed" + assert!(!input.just_pressed(DummyInput::Input1)); + assert!(!input.just_pressed(DummyInput::Input2)); + + // Check if they're marked as pressed + assert!(input.pressed(DummyInput::Input1)); + assert!(input.pressed(DummyInput::Input2)); + + // Release the inputs and check state + + input.release(DummyInput::Input1); + input.release(DummyInput::Input2); + + // Check if they're marked as "just released" (released on this update) + assert!(input.just_released(DummyInput::Input1)); + assert!(input.just_released(DummyInput::Input2)); + + // Check that they're not incorrectly marked as pressed + assert!(!input.pressed(DummyInput::Input1)); + assert!(!input.pressed(DummyInput::Input2)); + + // Update the `Input` and check for removal from `just_released` + + input.update(); + + // Check that they're not incorrectly marked as just released + assert!(!input.just_released(DummyInput::Input1)); + assert!(!input.just_released(DummyInput::Input2)); + + // Set up an `Input` to test resetting. + let mut input = Input::default(); + + input.press(DummyInput::Input1); + input.release(DummyInput::Input2); + + // Reset the `Input` and test it was reset correctly. + input.reset(DummyInput::Input1); + input.reset(DummyInput::Input2); + + assert!(!input.just_pressed(DummyInput::Input1)); + assert!(!input.pressed(DummyInput::Input1)); + + assert!(!input.just_released(DummyInput::Input2)); + } +} From 769196d5ea6d2f15790a61beece74f621a6a18db Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sun, 29 Nov 2020 11:47:36 -0600 Subject: [PATCH 04/11] Add test for bevy_input::Touches --- crates/bevy_input/src/touch.rs | 169 +++++++++++++++++++++++++++++---- 1 file changed, 149 insertions(+), 20 deletions(-) diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 9e78f7d05bedf..2a1f2de6af4fb 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -2,7 +2,6 @@ use bevy_app::{EventReader, Events}; use bevy_ecs::{Local, Res, ResMut}; use bevy_math::Vec2; use bevy_utils::HashMap; -use core::ops::DerefMut; /// Represents a touch event /// @@ -168,39 +167,169 @@ impl Touches { .iter() .map(move |(id, _)| self.pressed.get(id).unwrap()) } -} -/// Updates the Touches resource with the latest TouchInput events -pub fn touch_screen_input_system( - mut state: Local, - mut touch_state: ResMut, - touch_input_events: Res>, -) { - let touch_state = touch_state.deref_mut(); - touch_state.just_pressed.clear(); - touch_state.just_released.clear(); - for event in state.touch_event_reader.iter(&touch_input_events) { + fn process_touch_event(&mut self, event: &TouchInput) { match event.phase { TouchPhase::Started => { - touch_state.pressed.insert(event.id, event.into()); - touch_state.just_pressed.insert(event.id, event.into()); + self.pressed.insert(event.id, event.into()); + self.just_pressed.insert(event.id, event.into()); } TouchPhase::Moved => { - let mut new_touch = touch_state.pressed.get(&event.id).cloned().unwrap(); + let mut new_touch = self.pressed.get(&event.id).cloned().unwrap(); new_touch.previous_position = new_touch.position; new_touch.previous_force = new_touch.force; new_touch.position = event.position; new_touch.force = event.force; - touch_state.pressed.insert(event.id, new_touch); + self.pressed.insert(event.id, new_touch); } TouchPhase::Ended => { - touch_state.just_released.insert(event.id, event.into()); - touch_state.pressed.remove_entry(&event.id); + self.just_released.insert(event.id, event.into()); + self.pressed.remove_entry(&event.id); } TouchPhase::Cancelled => { - touch_state.just_cancelled.insert(event.id, event.into()); - touch_state.pressed.remove_entry(&event.id); + self.just_cancelled.insert(event.id, event.into()); + self.pressed.remove_entry(&event.id); } }; } + + fn update(&mut self) { + self.just_pressed.clear(); + self.just_released.clear(); + } +} + +/// Updates the Touches resource with the latest TouchInput events +pub fn touch_screen_input_system( + mut state: Local, + mut touch_state: ResMut, + touch_input_events: Res>, +) { + touch_state.update(); + + for event in state.touch_event_reader.iter(&touch_input_events) { + touch_state.process_touch_event(event); + } +} + +mod test { + #[test] + fn touch() { + use crate::{touch::TouchPhase, TouchInput, Touches}; + use bevy_math::Vec2; + + let mut touches = Touches::default(); + + let touch_event_1 = TouchInput { + phase: TouchPhase::Started, + position: Vec2::new(4.0, 4.0), + force: None, + id: 4, + }; + + let touch_event_2 = TouchInput { + phase: TouchPhase::Started, + position: Vec2::new(4.0, 4.0), + force: None, + id: 5, + }; + + // Process the two touch events + touches.process_touch_event(&touch_event_1); + touches.process_touch_event(&touch_event_2); + + // Check that the touches are pressed and just pressed + + assert!(touches.just_pressed(4)); + assert!(touches.just_pressed(5)); + + assert!(touches.get_pressed(4).is_some()); + assert!(touches.get_pressed(5).is_some()); + + // Check that the touches are not released or just released + + assert!(!touches.just_released(4)); + assert!(!touches.just_released(5)); + + assert!(touches.get_released(4).is_none()); + assert!(touches.get_released(4).is_none()); + + // Test iteration + + // Check that the touch events are in the pressed iterator + assert_eq!(touches.iter().count(), 2); + + // Check that the touch events are in the just pressed iterator + assert_eq!(touches.iter_just_pressed().count(), 2); + + // Check that the just released iterator returns nothing + assert_eq!(touches.iter_just_released().count(), 0); + + // Update the `Touches` and check that touches have been removed from just touched + touches.update(); + + assert!(!touches.just_pressed(4)); + assert!(!touches.just_pressed(5)); + + assert!(touches.get_pressed(4).is_some()); + assert!(touches.get_pressed(5).is_some()); + + // Test moving + let new_touch_event = TouchInput { + phase: TouchPhase::Moved, + position: Vec2::new(5.0, 4.0), + force: None, + id: 4, + }; + + touches.process_touch_event(&new_touch_event); + + let touch = touches.get_pressed(4).unwrap(); + + assert_eq!(touch.start_position, touch_event_1.position); + assert_eq!(touch.start_force, touch_event_1.force); + assert_eq!(touch.previous_position, touch_event_1.position); + assert_eq!(touch.previous_force, touch_event_1.force); + assert_eq!(touch.position, new_touch_event.position); + assert_eq!(touch.force, new_touch_event.force); + + // Test removal + let touch_event_1 = TouchInput { + phase: TouchPhase::Cancelled, + position: Vec2::new(4.0, 4.0), + force: None, + id: 4, + }; + + let touch_event_2 = TouchInput { + phase: TouchPhase::Cancelled, + position: Vec2::new(4.0, 4.0), + force: None, + id: 5, + }; + + touches.process_touch_event(&touch_event_1); + touches.process_touch_event(&touch_event_2); + + assert!(touches.just_cancelled(4)); + assert!(touches.just_cancelled(5)); + + assert_eq!(touches.iter_just_cancelled().count(), 2); + + touches.update(); + + assert_eq!(touches.iter_just_cancelled().count(), 0); + + // Test ending + let touch_event = TouchInput { + phase: TouchPhase::Ended, + position: Vec2::new(0.0, 0.0), + force: None, + id: 4, + }; + + touches.process_touch_event(&touch_event); + + assert!(touches.get_pressed(4).is_none()); + } } From 5fe340af9fd90b33e818236c0d18ee4c8c886e1a Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sun, 29 Nov 2020 14:56:08 -0600 Subject: [PATCH 05/11] Start splitting tests up --- crates/bevy_input/src/touch.rs | 71 +++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 2a1f2de6af4fb..3c6539e5d9eff 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -213,8 +213,71 @@ pub fn touch_screen_input_system( } mod test { + + #[test] + fn touch_update() {} + + #[test] + fn touch_process() { + use crate::{touch::TouchPhase, TouchInput, Touches}; + use bevy_math::Vec2; + + let mut touches = Touches::default(); + + // Test adding a `TouchPhase::Started` + + let touch_event = TouchInput { + phase: TouchPhase::Started, + position: Vec2::new(4.0, 4.0), + force: None, + id: 4, + }; + + touches.update(); + touches.process_touch_event(&touch_event); + + assert!(touches.pressed.get(&touch_event.id).is_some()); + assert!(touches.just_pressed.get(&touch_event.id).is_some()); + + // Test adding a `TouchPhase::Moved` + + let moved_touch_event = TouchInput { + phase: TouchPhase::Started, + position: Vec2::new(5.0, 5.0), + force: None, + id: touch_event.id, + }; + + touches.update(); + touches.process_touch_event(&moved_touch_event); + + assert_eq!( + touches + .pressed + .get(&moved_touch_event.id) + .expect("Missing from pressed after move.") + .previous_position, + touch_event.position + ); + + // Test cancelling an event + + let cancel_touch_event = TouchInput { + phase: TouchPhase::Cancelled, + position: Vec2::new(1.0, 1.0), + force: None, + id: touch_event.id, + }; + + touches.update(); + touches.process_touch_event(&cancel_touch_event); + + assert!(touches.just_cancelled.get(&cancel_touch_event.id).is_some()); + assert!(touches.pressed.get(&cancel_touch_event.id).is_none()); + } + #[test] - fn touch() { + fn touch_pressed() { use crate::{touch::TouchPhase, TouchInput, Touches}; use bevy_math::Vec2; @@ -332,4 +395,10 @@ mod test { assert!(touches.get_pressed(4).is_none()); } + + #[test] + fn touch_released() {} + + #[test] + fn touch_cancelled() {} } From 011da949853296abb7598acd2711bbc8bb39eb6b Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sun, 29 Nov 2020 23:40:30 -0600 Subject: [PATCH 06/11] Fix bug with bevy_input::Touches --- crates/bevy_input/src/touch.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 3c6539e5d9eff..a52c089ac5b85 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -155,7 +155,7 @@ impl Touches { pub fn iter_just_released(&self) -> impl Iterator + '_ { self.just_released .iter() - .map(move |(id, _)| self.pressed.get(id).unwrap()) + .map(move |(id, _)| self.just_released.get(id).unwrap()) } pub fn just_cancelled(&self, id: u64) -> bool { @@ -165,7 +165,7 @@ impl Touches { pub fn iter_just_cancelled(&self) -> impl Iterator + '_ { self.just_cancelled .iter() - .map(move |(id, _)| self.pressed.get(id).unwrap()) + .map(move |(id, _)| self.just_cancelled.get(id).unwrap()) } fn process_touch_event(&mut self, event: &TouchInput) { @@ -251,14 +251,12 @@ mod test { touches.update(); touches.process_touch_event(&moved_touch_event); - assert_eq!( - touches - .pressed - .get(&moved_touch_event.id) - .expect("Missing from pressed after move.") - .previous_position, - touch_event.position - ); + assert_eq!(touches + .pressed + .get(&moved_touch_event.id) + .expect("Missing from pressed after move.") + .previous_position + , touch_event.position); // Test cancelling an event From cad5a830a775aa6785233d1aa63ca3416cc1cdd8 Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sun, 29 Nov 2020 23:52:26 -0600 Subject: [PATCH 07/11] Add touch_update test and fix bug with bevy_input::Touches --- crates/bevy_input/src/touch.rs | 63 +++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index a52c089ac5b85..334796698613f 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -196,6 +196,7 @@ impl Touches { fn update(&mut self) { self.just_pressed.clear(); self.just_released.clear(); + self.just_cancelled.clear(); } } @@ -215,7 +216,35 @@ pub fn touch_screen_input_system( mod test { #[test] - fn touch_update() {} + fn touch_update() { + use crate::{touch::Touch, Touches}; + use bevy_math::Vec2; + + let mut touches = Touches::default(); + + let touch_event = Touch { + id: 4, + start_position: Vec2::new(0.0, 0.0), + start_force: None, + previous_position: Vec2::new(0.0, 0.0), + previous_force: None, + position: Vec2::new(0.0, 0.0), + force: None, + }; + + // Add a touch to `just_pressed`, 'just_released', and 'just cancelled' + + touches.just_pressed.insert(4, touch_event); + touches.just_released.insert(4, touch_event); + touches.just_cancelled.insert(4, touch_event); + + touches.update(); + + // Verify that all the `just_x` maps are cleared + assert!(touches.just_pressed.is_empty()); + assert!(touches.just_released.is_empty()); + assert!(touches.just_cancelled.is_empty()); + } #[test] fn touch_process() { @@ -242,7 +271,7 @@ mod test { // Test adding a `TouchPhase::Moved` let moved_touch_event = TouchInput { - phase: TouchPhase::Started, + phase: TouchPhase::Moved, position: Vec2::new(5.0, 5.0), force: None, id: touch_event.id, @@ -251,12 +280,14 @@ mod test { touches.update(); touches.process_touch_event(&moved_touch_event); - assert_eq!(touches - .pressed - .get(&moved_touch_event.id) - .expect("Missing from pressed after move.") - .previous_position - , touch_event.position); + assert_eq!( + touches + .pressed + .get(&moved_touch_event.id) + .expect("Missing from pressed after move.") + .previous_position, + touch_event.position + ); // Test cancelling an event @@ -272,6 +303,22 @@ mod test { assert!(touches.just_cancelled.get(&cancel_touch_event.id).is_some()); assert!(touches.pressed.get(&cancel_touch_event.id).is_none()); + + // Test ending an event + + let end_touch_event = TouchInput { + phase: TouchPhase::Ended, + position: Vec2::new(4.0, 4.0), + force: None, + id: 4, + }; + + touches.update(); + touches.process_touch_event(&touch_event); + touches.process_touch_event(&end_touch_event); + + assert!(touches.just_released.get(&touch_event.id).is_some()); + assert!(touches.pressed.get(&touch_event.id).is_none()); } #[test] From f0284a212c8ffccd47f4faab04c3503c8b0f74dc Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Mon, 30 Nov 2020 00:04:30 -0600 Subject: [PATCH 08/11] Add final bevy_input::Touches tests. --- crates/bevy_input/src/touch.rs | 121 ++++++++------------------------- 1 file changed, 30 insertions(+), 91 deletions(-) diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 334796698613f..29fd0765390c5 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -328,122 +328,61 @@ mod test { let mut touches = Touches::default(); - let touch_event_1 = TouchInput { + let touch_event = TouchInput { phase: TouchPhase::Started, position: Vec2::new(4.0, 4.0), force: None, id: 4, }; - let touch_event_2 = TouchInput { - phase: TouchPhase::Started, - position: Vec2::new(4.0, 4.0), - force: None, - id: 5, - }; - - // Process the two touch events - touches.process_touch_event(&touch_event_1); - touches.process_touch_event(&touch_event_2); - - // Check that the touches are pressed and just pressed - - assert!(touches.just_pressed(4)); - assert!(touches.just_pressed(5)); - - assert!(touches.get_pressed(4).is_some()); - assert!(touches.get_pressed(5).is_some()); - - // Check that the touches are not released or just released - - assert!(!touches.just_released(4)); - assert!(!touches.just_released(5)); - - assert!(touches.get_released(4).is_none()); - assert!(touches.get_released(4).is_none()); - - // Test iteration - - // Check that the touch events are in the pressed iterator - assert_eq!(touches.iter().count(), 2); - - // Check that the touch events are in the just pressed iterator - assert_eq!(touches.iter_just_pressed().count(), 2); - - // Check that the just released iterator returns nothing - assert_eq!(touches.iter_just_released().count(), 0); - - // Update the `Touches` and check that touches have been removed from just touched - touches.update(); - - assert!(!touches.just_pressed(4)); - assert!(!touches.just_pressed(5)); - - assert!(touches.get_pressed(4).is_some()); - assert!(touches.get_pressed(5).is_some()); - - // Test moving - let new_touch_event = TouchInput { - phase: TouchPhase::Moved, - position: Vec2::new(5.0, 4.0), - force: None, - id: 4, - }; + // Register the touch and test that it was registered correctly + touches.process_touch_event(&touch_event); - touches.process_touch_event(&new_touch_event); + assert!(touches.get_pressed(touch_event.id).is_some()); + assert!(touches.just_pressed(touch_event.id)); + assert_eq!(touches.iter().count(), 1); + } - let touch = touches.get_pressed(4).unwrap(); + #[test] + fn touch_released() { + use crate::{touch::TouchPhase, TouchInput, Touches}; + use bevy_math::Vec2; - assert_eq!(touch.start_position, touch_event_1.position); - assert_eq!(touch.start_force, touch_event_1.force); - assert_eq!(touch.previous_position, touch_event_1.position); - assert_eq!(touch.previous_force, touch_event_1.force); - assert_eq!(touch.position, new_touch_event.position); - assert_eq!(touch.force, new_touch_event.force); + let mut touches = Touches::default(); - // Test removal - let touch_event_1 = TouchInput { - phase: TouchPhase::Cancelled, + let touch_event = TouchInput { + phase: TouchPhase::Ended, position: Vec2::new(4.0, 4.0), force: None, id: 4, }; - let touch_event_2 = TouchInput { - phase: TouchPhase::Cancelled, - position: Vec2::new(4.0, 4.0), - force: None, - id: 5, - }; - - touches.process_touch_event(&touch_event_1); - touches.process_touch_event(&touch_event_2); - - assert!(touches.just_cancelled(4)); - assert!(touches.just_cancelled(5)); + // Register the touch and test that it was registered correctly + touches.process_touch_event(&touch_event); - assert_eq!(touches.iter_just_cancelled().count(), 2); + assert!(touches.get_released(touch_event.id).is_some()); + assert!(touches.just_released(touch_event.id)); + assert_eq!(touches.iter_just_released().count(), 1); + } - touches.update(); + #[test] + fn touch_cancelled() { + use crate::{touch::TouchPhase, TouchInput, Touches}; + use bevy_math::Vec2; - assert_eq!(touches.iter_just_cancelled().count(), 0); + let mut touches = Touches::default(); - // Test ending let touch_event = TouchInput { - phase: TouchPhase::Ended, - position: Vec2::new(0.0, 0.0), + phase: TouchPhase::Cancelled, + position: Vec2::new(4.0, 4.0), force: None, id: 4, }; + // Register the touch and test that it was registered correctly touches.process_touch_event(&touch_event); - assert!(touches.get_pressed(4).is_none()); + assert!(touches.just_cancelled(touch_event.id)); + assert_eq!(touches.iter_just_cancelled().count(), 1); } - - #[test] - fn touch_released() {} - - #[test] - fn touch_cancelled() {} } From 8128b05612866aa813ff7304e34927f2b2fba5a3 Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Mon, 30 Nov 2020 22:38:49 -0600 Subject: [PATCH 09/11] Add #[cfg(test)] where needed --- crates/bevy_input/src/input.rs | 1 + crates/bevy_input/src/touch.rs | 1 + crates/bevy_math/src/face_toward.rs | 1 + 3 files changed, 3 insertions(+) diff --git a/crates/bevy_input/src/input.rs b/crates/bevy_input/src/input.rs index d53a6c8b87832..31e0b9980c4b0 100644 --- a/crates/bevy_input/src/input.rs +++ b/crates/bevy_input/src/input.rs @@ -72,6 +72,7 @@ where } } +#[cfg(test)] mod test { #[test] diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 29fd0765390c5..560588bb295df 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -213,6 +213,7 @@ pub fn touch_screen_input_system( } } +#[cfg(test)] mod test { #[test] diff --git a/crates/bevy_math/src/face_toward.rs b/crates/bevy_math/src/face_toward.rs index d594c5a62c652..f873964b18afc 100644 --- a/crates/bevy_math/src/face_toward.rs +++ b/crates/bevy_math/src/face_toward.rs @@ -20,6 +20,7 @@ impl FaceToward for Mat4 { } } +#[cfg(test)] mod test { #[test] fn face_toward_mat4() { From fdda5a34ddf9aff2f89e616289eb79536b760811 Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Mon, 30 Nov 2020 22:51:23 -0600 Subject: [PATCH 10/11] Switch method of iteration --- crates/bevy_input/src/touch.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 560588bb295df..bea7f516d5a63 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -138,10 +138,8 @@ impl Touches { self.just_pressed.contains_key(&id) } - pub fn iter_just_pressed(&self) -> impl Iterator + '_ { - self.just_pressed - .iter() - .map(move |(id, _)| self.pressed.get(id).unwrap()) + pub fn iter_just_pressed(&self) -> impl Iterator { + self.just_pressed.values() } pub fn get_released(&self, id: u64) -> Option<&Touch> { @@ -152,10 +150,8 @@ impl Touches { self.just_released.contains_key(&id) } - pub fn iter_just_released(&self) -> impl Iterator + '_ { - self.just_released - .iter() - .map(move |(id, _)| self.just_released.get(id).unwrap()) + pub fn iter_just_released(&self) -> impl Iterator { + self.just_released.values() } pub fn just_cancelled(&self, id: u64) -> bool { @@ -163,9 +159,7 @@ impl Touches { } pub fn iter_just_cancelled(&self) -> impl Iterator + '_ { - self.just_cancelled - .iter() - .map(move |(id, _)| self.just_cancelled.get(id).unwrap()) + self.just_cancelled.values() } fn process_touch_event(&mut self, event: &TouchInput) { From 9d1512891a1658d28d6e6f8f16a1b389102fc21e Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Mon, 30 Nov 2020 22:38:43 -0800 Subject: [PATCH 11/11] Update touch.rs remove unnecessary `+ '_` lifetime bound --- crates/bevy_input/src/touch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index bea7f516d5a63..1a23e240796fc 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -158,7 +158,7 @@ impl Touches { self.just_cancelled.contains_key(&id) } - pub fn iter_just_cancelled(&self) -> impl Iterator + '_ { + pub fn iter_just_cancelled(&self) -> impl Iterator { self.just_cancelled.values() }