Skip to content

Commit

Permalink
feat: add example of interaction changing color of joysticks
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Jul 11, 2023
1 parent 7e97149 commit e57c8d4
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/multiple_joysticks_mobile/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions examples/multiple_joysticks_mobile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
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,
Expand All @@ -88,7 +88,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
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,
Expand All @@ -107,13 +107,31 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
fn update_joystick(
mut joystick: EventReader<VirtualJoystickEvent<JoystickController>>,
mut player: Query<(&mut Transform, &Player)>,
mut joystick_color: Query<(&mut TintColor, &VirtualJoystickNode<JoystickController>)>,
time_step: Res<FixedTime>,
) {
let (mut player, player_data) = player.single_mut();

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();
Expand Down
3 changes: 2 additions & 1 deletion examples/multiple_joysticks_pc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions examples/multiple_joysticks_pc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
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,
Expand All @@ -81,7 +81,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
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,
Expand All @@ -99,6 +99,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {

fn update_joystick(
mut joystick: EventReader<VirtualJoystickEvent<JoystickController>>,
mut joystick_color: Query<(&mut TintColor, &VirtualJoystickNode<JoystickController>)>,
mut player: Query<(&mut Transform, &Player)>,
time_step: Res<FixedTime>,
) {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_mobile/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion examples/simple_mobile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
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,
Expand All @@ -91,13 +91,29 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {

fn update_joystick(
mut joystick: EventReader<VirtualJoystickEvent<String>>,
mut joystick_color: Query<(&mut TintColor, &VirtualJoystickNode<JoystickController>)>,
mut player: Query<(&mut Transform, &Player)>,
time_step: Res<FixedTime>,
) {
let (mut player, player_data) = player.single_mut();

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();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_pc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion examples/simple_pc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
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,
Expand All @@ -65,13 +65,29 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {

fn update_joystick(
mut joystick: EventReader<VirtualJoystickEvent<String>>,
mut joystick_color: Query<(&mut TintColor, &VirtualJoystickNode<JoystickController>)>,
mut player: Query<(&mut Transform, &Player)>,
time_step: Res<FixedTime>,
) {
let (mut player, player_data) = player.single_mut();

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();
}
Expand Down

0 comments on commit e57c8d4

Please sign in to comment.