Skip to content

Commit

Permalink
Add conversion from tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 19, 2024
1 parent 02f70d0 commit 2d7ef61
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
12 changes: 12 additions & 0 deletions src/action_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,15 @@ impl From<Vec3> for ActionValue {
ActionValue::Axis3D(value)
}
}

impl From<(f32, f32)> for ActionValue {
fn from(value: (f32, f32)) -> Self {
ActionValue::Axis2D(value.into())
}
}

impl From<(f32, f32, f32)> for ActionValue {
fn from(value: (f32, f32, f32)) -> Self {
ActionValue::Axis3D(value.into())
}
}
6 changes: 3 additions & 3 deletions src/input_context/input_modifier/negate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ mod tests {
);
assert_eq!(
Negate::x(true).apply(&world, 0.0, Vec3::ONE.into()),
Vec3::new(-1.0, 1.0, 1.0).into(),
(-1.0, 1.0, 1.0).into(),
);
assert_eq!(
Negate::y(true).apply(&world, 0.0, Vec3::ONE.into()),
Vec3::new(1.0, -1.0, 1.0).into(),
(1.0, -1.0, 1.0).into(),
);
assert_eq!(
Negate::z(true).apply(&world, 0.0, Vec3::ONE.into()),
Vec3::new(1.0, 1.0, -1.0).into(),
(1.0, 1.0, -1.0).into(),
);
}
}
4 changes: 2 additions & 2 deletions src/input_context/input_modifier/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ mod tests {
assert_eq!(modifier.apply(&world, 0.0, 1.0.into()), 2.0.into());
assert_eq!(
modifier.apply(&world, 0.0, Vec2::ONE.into()),
Vec2::new(2.0, 2.0).into()
(2.0, 2.0).into()
);
assert_eq!(
modifier.apply(&world, 0.0, Vec3::ONE.into()),
Vec3::new(2.0, 2.0, 2.0).into()
(2.0, 2.0, 2.0).into()
);
}
}
4 changes: 2 additions & 2 deletions src/input_context/input_modifier/scale_by_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ mod tests {
assert_eq!(ScaleByDelta.apply(&world, delta, 0.5.into()), 0.25.into());
assert_eq!(
ScaleByDelta.apply(&world, delta, Vec2::ONE.into()),
Vec2::new(0.5, 0.5).into()
(0.5, 0.5).into()
);
assert_eq!(
ScaleByDelta.apply(&world, delta, Vec3::ONE.into()),
Vec3::new(0.5, 0.5, 0.5).into()
(0.5, 0.5, 0.5).into()
);
}
}
48 changes: 24 additions & 24 deletions src/input_context/input_modifier/swizzle_axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ impl InputModifier for SwizzleAxis {
}
ActionValue::Axis2D(value) => match self {
SwizzleAxis::YXZ => value.yx().into(),
SwizzleAxis::ZYX => Vec2::new(0.0, value.y).into(),
SwizzleAxis::XZY => Vec2::new(value.x, 0.0).into(),
SwizzleAxis::YZX => Vec2::new(value.y, 0.0).into(),
SwizzleAxis::ZXY => Vec2::new(0.0, value.x).into(),
SwizzleAxis::ZYX => (0.0, value.y).into(),
SwizzleAxis::XZY => (value.x, 0.0).into(),
SwizzleAxis::YZX => (value.y, 0.0).into(),
SwizzleAxis::ZXY => (0.0, value.x).into(),
},
ActionValue::Axis3D(value) => match self {
SwizzleAxis::YXZ => value.yxz().into(),
Expand All @@ -58,12 +58,12 @@ mod tests {
assert_eq!(swizzle.apply(&world, 0.0, true.into()), true.into());
assert_eq!(swizzle.apply(&world, 0.0, 1.0.into()), 1.0.into());
assert_eq!(
swizzle.apply(&world, 0.0, Vec2::new(0.0, 1.0).into()),
Vec2::new(1.0, 0.0).into(),
swizzle.apply(&world, 0.0, (0.0, 1.0).into()),
(1.0, 0.0).into(),
);
assert_eq!(
swizzle.apply(&world, 0.0, Vec3::new(0.0, 1.0, 2.0).into()),
Vec3::new(1.0, 0.0, 2.0).into(),
swizzle.apply(&world, 0.0, (0.0, 1.0, 2.0).into()),
(1.0, 0.0, 2.0).into(),
);
}

Expand All @@ -75,12 +75,12 @@ mod tests {
assert_eq!(swizzle.apply(&world, 0.0, true.into()), true.into());
assert_eq!(swizzle.apply(&world, 0.0, 1.0.into()), 1.0.into());
assert_eq!(
swizzle.apply(&world, 0.0, Vec2::new(0.0, 1.0).into()),
Vec2::new(0.0, 1.0).into(),
swizzle.apply(&world, 0.0, (0.0, 1.0).into()),
(0.0, 1.0).into(),
);
assert_eq!(
swizzle.apply(&world, 0.0, Vec3::new(0.0, 1.0, 2.0).into()),
Vec3::new(2.0, 1.0, 0.0).into(),
swizzle.apply(&world, 0.0, (0.0, 1.0, 2.0).into()),
(2.0, 1.0, 0.0).into(),
);
}

Expand All @@ -92,12 +92,12 @@ mod tests {
assert_eq!(modifier.apply(&world, 0.0, true.into()), true.into());
assert_eq!(modifier.apply(&world, 0.0, 1.0.into()), 1.0.into());
assert_eq!(
modifier.apply(&world, 0.0, Vec2::new(0.0, 1.0).into()),
Vec2::new(0.0, 0.0).into(),
modifier.apply(&world, 0.0, (0.0, 1.0).into()),
(0.0, 0.0).into(),
);
assert_eq!(
modifier.apply(&world, 0.0, Vec3::new(0.0, 1.0, 2.0).into()),
Vec3::new(0.0, 2.0, 1.0).into(),
modifier.apply(&world, 0.0, (0.0, 1.0, 2.0).into()),
(0.0, 2.0, 1.0).into(),
);
}

Expand All @@ -109,12 +109,12 @@ mod tests {
assert_eq!(modifier.apply(&world, 0.0, true.into()), true.into());
assert_eq!(modifier.apply(&world, 0.0, 1.0.into()), 1.0.into());
assert_eq!(
modifier.apply(&world, 0.0, Vec2::new(0.0, 1.0).into()),
Vec2::new(1.0, 0.0).into(),
modifier.apply(&world, 0.0, (0.0, 1.0).into()),
(1.0, 0.0).into(),
);
assert_eq!(
modifier.apply(&world, 0.0, Vec3::new(0.0, 1.0, 2.0).into()),
Vec3::new(1.0, 2.0, 0.0).into(),
modifier.apply(&world, 0.0, (0.0, 1.0, 2.0).into()),
(1.0, 2.0, 0.0).into(),
);
}

Expand All @@ -126,12 +126,12 @@ mod tests {
assert_eq!(modifier.apply(&world, 0.0, true.into()), true.into());
assert_eq!(modifier.apply(&world, 0.0, 1.0.into()), 1.0.into());
assert_eq!(
modifier.apply(&world, 0.0, Vec2::new(0.0, 1.0).into()),
Vec2::new(0.0, 0.0).into(),
modifier.apply(&world, 0.0, (0.0, 1.0).into()),
(0.0, 0.0).into(),
);
assert_eq!(
modifier.apply(&world, 0.0, Vec3::new(0.0, 1.0, 2.0).into()),
Vec3::new(2.0, 0.0, 1.0).into(),
modifier.apply(&world, 0.0, (0.0, 1.0, 2.0).into()),
(2.0, 0.0, 1.0).into(),
);
}
}
4 changes: 2 additions & 2 deletions tests/dim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn axis2d() {
let recorded = app.world().resource::<RecordedActions>();
let events = recorded.get::<Axis2D>(entity).unwrap();
let event = events.last().unwrap();
assert_eq!(event.value, Vec2::new(1.0, 0.0).into());
assert_eq!(event.value, (1.0, 0.0).into());

app.world_mut()
.resource_mut::<ButtonInput<KeyCode>>()
Expand Down Expand Up @@ -147,7 +147,7 @@ fn axis3d() {
let recorded = app.world().resource::<RecordedActions>();
let events = recorded.get::<Axis3D>(entity).unwrap();
let event = events.last().unwrap();
assert_eq!(event.value, Vec3::new(1.0, 0.0, 0.0).into());
assert_eq!(event.value, (1.0, 0.0, 0.0).into());

app.world_mut()
.resource_mut::<ButtonInput<KeyCode>>()
Expand Down

0 comments on commit 2d7ef61

Please sign in to comment.