From e57c8d44b78f797261353bad1710e98db97107e2 Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Tue, 11 Jul 2023 15:29:05 -0400 Subject: [PATCH] feat: add example of interaction changing color of joysticks --- examples/multiple_joysticks_mobile/Cargo.lock | 2 +- examples/multiple_joysticks_mobile/src/lib.rs | 22 +++++++++++++++++-- examples/multiple_joysticks_pc/Cargo.lock | 3 ++- examples/multiple_joysticks_pc/src/main.rs | 22 +++++++++++++++++-- examples/simple_mobile/Cargo.lock | 2 +- examples/simple_mobile/src/lib.rs | 18 ++++++++++++++- examples/simple_pc/Cargo.lock | 2 +- examples/simple_pc/src/main.rs | 18 ++++++++++++++- 8 files changed, 79 insertions(+), 10 deletions(-) diff --git a/examples/multiple_joysticks_mobile/Cargo.lock b/examples/multiple_joysticks_mobile/Cargo.lock index 394e2c4..79195a2 100644 --- a/examples/multiple_joysticks_mobile/Cargo.lock +++ b/examples/multiple_joysticks_mobile/Cargo.lock @@ -2960,7 +2960,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "virtual_joystick" -version = "1.1.1" +version = "1.1.2" dependencies = [ "bevy", "serde", diff --git a/examples/multiple_joysticks_mobile/src/lib.rs b/examples/multiple_joysticks_mobile/src/lib.rs index 484a15a..c04c308 100644 --- a/examples/multiple_joysticks_mobile/src/lib.rs +++ b/examples/multiple_joysticks_mobile/src/lib.rs @@ -62,7 +62,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { axis: VirtualJoystickAxis::Horizontal, behaviour: VirtualJoystickType::Fixed, }) - .set_color(TintColor(Color::WHITE)) + .set_color(TintColor(Color::WHITE.with_a(0.2))) .set_style(Style { size: Size::all(Val::Px(150.)), position_type: PositionType::Absolute, @@ -88,7 +88,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { axis: VirtualJoystickAxis::Vertical, behaviour: VirtualJoystickType::Fixed, }) - .set_color(TintColor(Color::WHITE)) + .set_color(TintColor(Color::WHITE.with_a(0.2))) .set_style(Style { size: Size::all(Val::Px(150.)), position_type: PositionType::Absolute, @@ -107,6 +107,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { fn update_joystick( mut joystick: EventReader>, mut player: Query<(&mut Transform, &Player)>, + mut joystick_color: Query<(&mut TintColor, &VirtualJoystickNode)>, time_step: Res, ) { let (mut player, player_data) = player.single_mut(); @@ -114,6 +115,23 @@ fn update_joystick( for j in joystick.iter() { let Vec2 { x, y } = j.axis(); + match j.get_type() { + VirtualJoystickEventType::Press | VirtualJoystickEventType::Drag => { + for (mut color, node) in joystick_color.iter_mut() { + if node.id == j.id() { + *color = TintColor(Color::WHITE); + } + } + } + VirtualJoystickEventType::Up => { + for (mut color, node) in joystick_color.iter_mut() { + if node.id == j.id() { + *color = TintColor(Color::WHITE.with_a(0.2)); + } + } + } + } + match j.id() { JoystickController::MovementX => { player.translation.x += x * player_data.0 * time_step.period.as_secs_f32(); diff --git a/examples/multiple_joysticks_pc/Cargo.lock b/examples/multiple_joysticks_pc/Cargo.lock index 3405558..4dee92d 100644 --- a/examples/multiple_joysticks_pc/Cargo.lock +++ b/examples/multiple_joysticks_pc/Cargo.lock @@ -3263,10 +3263,11 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "virtual_joystick" -version = "1.0.3" +version = "1.1.2" dependencies = [ "bevy", "bevy-inspector-egui", + "serde", ] [[package]] diff --git a/examples/multiple_joysticks_pc/src/main.rs b/examples/multiple_joysticks_pc/src/main.rs index 6acf11e..887f53d 100644 --- a/examples/multiple_joysticks_pc/src/main.rs +++ b/examples/multiple_joysticks_pc/src/main.rs @@ -55,7 +55,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { axis: VirtualJoystickAxis::Horizontal, behaviour: VirtualJoystickType::Fixed, }) - .set_color(TintColor(Color::WHITE)) + .set_color(TintColor(Color::WHITE.with_a(0.2))) .set_style(Style { size: Size::all(Val::Px(150.)), position_type: PositionType::Absolute, @@ -81,7 +81,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { axis: VirtualJoystickAxis::Vertical, behaviour: VirtualJoystickType::Fixed, }) - .set_color(TintColor(Color::WHITE)) + .set_color(TintColor(Color::WHITE.with_a(0.2))) .set_style(Style { size: Size::all(Val::Px(150.)), position_type: PositionType::Absolute, @@ -99,6 +99,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { fn update_joystick( mut joystick: EventReader>, + mut joystick_color: Query<(&mut TintColor, &VirtualJoystickNode)>, mut player: Query<(&mut Transform, &Player)>, time_step: Res, ) { @@ -107,6 +108,23 @@ fn update_joystick( for j in joystick.iter() { let Vec2 { x, y } = j.snap_axis(None); + match j.get_type() { + VirtualJoystickEventType::Press | VirtualJoystickEventType::Drag => { + for (mut color, node) in joystick_color.iter_mut() { + if node.id == j.id() { + *color = TintColor(Color::WHITE); + } + } + } + VirtualJoystickEventType::Up => { + for (mut color, node) in joystick_color.iter_mut() { + if node.id == j.id() { + *color = TintColor(Color::WHITE.with_a(0.2)); + } + } + } + } + match j.id() { JoystickController::MovementX => { player.translation.x += x * player_data.0 * time_step.period.as_secs_f32(); diff --git a/examples/simple_mobile/Cargo.lock b/examples/simple_mobile/Cargo.lock index 25e7095..c46987e 100644 --- a/examples/simple_mobile/Cargo.lock +++ b/examples/simple_mobile/Cargo.lock @@ -2960,7 +2960,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "virtual_joystick" -version = "1.1.1" +version = "1.1.2" dependencies = [ "bevy", "serde", diff --git a/examples/simple_mobile/src/lib.rs b/examples/simple_mobile/src/lib.rs index cde0adf..6304491 100644 --- a/examples/simple_mobile/src/lib.rs +++ b/examples/simple_mobile/src/lib.rs @@ -74,7 +74,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { axis: VirtualJoystickAxis::Both, behaviour: VirtualJoystickType::Fixed, }) - .set_color(TintColor(Color::WHITE)) + .set_color(TintColor(Color::WHITE.with_a(0.2))) .set_style(Style { size: Size::all(Val::Px(150.)), position_type: PositionType::Absolute, @@ -91,6 +91,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { fn update_joystick( mut joystick: EventReader>, + mut joystick_color: Query<(&mut TintColor, &VirtualJoystickNode)>, mut player: Query<(&mut Transform, &Player)>, time_step: Res, ) { @@ -98,6 +99,21 @@ fn update_joystick( for j in joystick.iter() { let Vec2 { x, y } = j.axis(); + match j.get_type() { + VirtualJoystickEventType::Press | VirtualJoystickEventType::Drag => { + let (mut color, node) = joystick_color.single_mut(); + if node.id == j.id() { + *color = TintColor(Color::WHITE); + } + } + VirtualJoystickEventType::Up => { + let (mut color, node) = joystick_color.single_mut(); + if node.id == j.id() { + *color = TintColor(Color::WHITE.with_a(0.2)); + } + } + } + player.translation.x += x * player_data.0 * time_step.period.as_secs_f32(); player.translation.y += y * player_data.0 * time_step.period.as_secs_f32(); } diff --git a/examples/simple_pc/Cargo.lock b/examples/simple_pc/Cargo.lock index 088306b..4dee92d 100644 --- a/examples/simple_pc/Cargo.lock +++ b/examples/simple_pc/Cargo.lock @@ -3263,7 +3263,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "virtual_joystick" -version = "1.1.0" +version = "1.1.2" dependencies = [ "bevy", "bevy-inspector-egui", diff --git a/examples/simple_pc/src/main.rs b/examples/simple_pc/src/main.rs index 88003b1..26d14bb 100644 --- a/examples/simple_pc/src/main.rs +++ b/examples/simple_pc/src/main.rs @@ -47,7 +47,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { axis: VirtualJoystickAxis::Both, behaviour: VirtualJoystickType::Floating, }) - .set_color(TintColor(Color::WHITE)) + .set_color(TintColor(Color::WHITE.with_a(0.2))) .set_style(Style { size: Size::all(Val::Px(150.)), position_type: PositionType::Absolute, @@ -65,6 +65,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res) { fn update_joystick( mut joystick: EventReader>, + mut joystick_color: Query<(&mut TintColor, &VirtualJoystickNode)>, mut player: Query<(&mut Transform, &Player)>, time_step: Res, ) { @@ -72,6 +73,21 @@ fn update_joystick( for j in joystick.iter() { let Vec2 { x, y } = j.axis(); + match j.get_type() { + VirtualJoystickEventType::Press | VirtualJoystickEventType::Drag => { + let (mut color, node) = joystick_color.single_mut(); + if node.id == j.id() { + *color = TintColor(Color::WHITE); + } + } + VirtualJoystickEventType::Up => { + let (mut color, node) = joystick_color.single_mut(); + if node.id == j.id() { + *color = TintColor(Color::WHITE.with_a(0.2)); + } + } + } + player.translation.x += x * player_data.0 * time_step.period.as_secs_f32(); player.translation.y += y * player_data.0 * time_step.period.as_secs_f32(); }