Skip to content

Commit

Permalink
docs: upgrade to bevy 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Nov 18, 2023
1 parent 4917b14 commit e7a43ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 122 deletions.
150 changes: 29 additions & 121 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Aviable and compatible versions

| bevy | VirtualJoystick |
|--------|-----------------|
| 0.12 | 2.0.1 |
| 0.11 | 2.0.1 |
| 0.10.1 | 1.1.2 |

Expand Down Expand Up @@ -66,7 +67,7 @@ cargo run --example simple -F=inspect
Add to Cargo.toml
```toml
[dependencies]
bevy = "0.11"
bevy = "0.12"
virtual_joystick = "*" # Add your version
```

Expand Down Expand Up @@ -115,150 +116,57 @@ fn create_scene(mut cmd: Commands, asset_server: Res<AssetServer>) {
cmd.spawn_empty().insert(Player(30.));

// Spawn Virtual Joystick at horizontal center
cmd.spawn(
// Define variable for Joystick
VirtualJoystickBundle::new(VirtualJoystickNode {
border_image: asset_server.load("Outline.png"),
knob_image: asset_server.load("Knob.png"),
knob_size: Vec2::new(80., 80.),
create_joystick(
&mut cmd,
asset_server.load("Knob.png"),
asset_server.load("Outline.png"),
None,
None,
Some(Color::ORANGE_RED.with_a(0.3)),
Vec2::new(75., 75.),
Vec2::new(150., 150.),
VirtualJoystickNode {
dead_zone: 0.,
id: JoystickControllerID::Joystick1,
axis: VirtualJoystickAxis::Horizontal,
behaviour: VirtualJoystickType::Fixed,
})
.set_color(TintColor(Color::WHITE))
.set_style(Style {
size: Size::all(Val::Px(150.)),
id: "UniqueJoystick".to_string(),
axis: VirtualJoystickAxis::Both,
behaviour: VirtualJoystickType::Floating,
},
Style {
width: Val::Px(150.),
height: Val::Px(150.),
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Percent(50.),
bottom: Val::Percent(15.),
..default()
},
left: Val::Percent(50.),
bottom: Val::Percent(15.),
..default()
}),
)
// When you add this component you mark this area as interactable for Joystick
.insert(VirtualJoystickInteractionArea);
},
);
}
```

Use variable generated by Joystick
```rust

fn update_joystick(
mut joystick: EventReader<VirtualJoystickEvent<JoystickControllerID>>,
mut joystick: EventReader<VirtualJoystickEvent<String>>,
mut player: Query<(&mut Transform, &Player)>,
time_step: Res<FixedTime>,
time_step: Res<Time>,
) {
// Get player
let (mut player, player_data) = player.single_mut();

// Iter each joystick event
for j in joystick.iter() {
// get axis value 0-1 in x & y
for j in joystick.read() {
let Vec2 { x, y } = j.axis();
// Verify ID of joystick for movement
match j.id() {
JoystickControllerID::Joystick1 => {
// Move player using joystick axis value
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();
player.translation.x += x * player_data.0 * time_step.delta_seconds();
player.translation.y += y * player_data.0 * time_step.delta_seconds();
}
}
}
}
```

# Types

```rust
enum VirtualJoystickAxis {
Both, // Default
Horizontal,
Vertical,
}

enum VirtualJoystickType {
/// Static position
Fixed,
/// Spawn at point click
/// Default
Floating,
/// Follow point on drag
Dynamic,
}

// Component
struct VirtualJoystickNode {
/// Identifier of joystick
/// Note: any type that implements Hash + Clone + Default + Reflect
pub id: S,
/// Image for background or border image on joystick
pub border_image: Handle<Image>,
/// Image for handler knob on joystick
pub knob_image: Handle<Image>,
/// Size for knob on joystick
pub knob_size: Vec2,
/// Zone to ignore movement
pub dead_zone: f32,
/// Define Axis for this joystick
pub axis: VirtualJoystickAxis,
/// Define the behaviour of joystick
pub behaviour: VirtualJoystickType,
}

// Event Type
pub enum VirtualJoystickEventType {
Press,
Drag,
Up
}

// EventReader
struct VirtualJoystickEvent {
/// Get ID of joystick throw event
pub fn id() -> S;

/// Return the Type of Joystick Event
pub fn get_type() -> VirtualJoystickEventType;

/// Raw position of point (Mouse or Touch)
pub fn value() -> Vec2;

/// Axis of Joystick see [crate::VirtualJoystickAxis]
pub fn direction() -> VirtualJoystickAxis;

/// Delta value ranging from 0 to 1 in each vector (x and y)
pub fn axis() -> Vec2;

/// Delta value snaped
pub fn snap_axis(dead_zone: Option<f32>) -> Vec2;
}

// Bundle to spawn
struct VirtualJoystickBundle {
pub fn new(joystick: VirtualJoystickNode) -> Self;

pub fn set_node(mut self, node: Node) -> Self;

pub fn set_style(mut self, style: Style) -> Self;

pub fn set_color(mut self, color: TintColor) -> Self;

pub fn set_focus_policy(mut self, focus_policy: FocusPolicy) -> Self;

pub fn set_transform(mut self, transform: Transform) -> Self;

pub fn set_global_transform(mut self, global_transform: GlobalTransform) -> Self;

pub fn set_visibility(mut self, visibility: Visibility) -> Self;

pub fn set_computed_visibility(mut self, computed_visibility: ComputedVisibility) -> Self;

pub fn set_z_index(mut self, z_index: ZIndex) -> Self;
}
```

# TODOs
- Add more better documentation
- [ ] WIP: Add more better documentation
2 changes: 1 addition & 1 deletion release.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ verify = true
push = true
pre-release-commit-message = "chore: Release {{crate_name}} version {{version}}"
pre-release-replacements = [
{ file = "README.md", search = "\\| 0.11 \\| [0-9\\.-]+ \\|", replace = "| 0.11 | {{version}} |" },
{ file = "README.md", search = "\\| 0.12 \\| [0-9\\.-]+ \\|", replace = "| 0.12 | {{version}} |" },
]
tag-message = "chore: Release {{crate_name}} version {{version}}"
tag-name = "{{version}}"
Expand Down

0 comments on commit e7a43ea

Please sign in to comment.