Skip to content

Commit 6299e3d

Browse files
bushrat011899mgi388Carter0
authored
Add examples/helpers/* as library examples (#18288)
# Objective Some of Bevy's examples contain boilerplate which is split out into the `helpers` folder. This allows examples to have access to common functionality without building into Bevy directly. However, these helpers are themselves quite high-quality code, and we do intend for users to read them and even use them. But, we don't list them in the examples document, and they aren't explicitly checked in CI, only transitively through examples which import them. ## Solution - Added `camera_controller` and `widgets` as library examples. ## Testing - CI --- ## Notes - Library examples are identical to any other example, just with `crate-type = ["lib"]` in the `Cargo.toml`. Since they are marked as libraries, they don't require a `main` function but do require public items to be documented. - Library examples opens the possibility of creating examples which don't need to be actual runnable applications. This may be more appropriate for certain ECS examples, and allows for adding helpers which (currently) don't have an example that needs them without them going stale. - I learned about this as a concept during research for `no_std` examples, but believe it has value for Bevy outside that specific niche. --------- Co-authored-by: mgi388 <135186256+mgi388@users.noreply.github.com> Co-authored-by: Carter Weinberg <weinbergcarter@gmail.com>
1 parent 4d47de8 commit 6299e3d

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4263,3 +4263,27 @@ name = "Occlusion Culling"
42634263
description = "Demonstration of Occlusion Culling"
42644264
category = "3D Rendering"
42654265
wasm = false
4266+
4267+
[[example]]
4268+
name = "camera_controller"
4269+
path = "examples/helpers/camera_controller.rs"
4270+
doc-scrape-examples = true
4271+
crate-type = ["lib"]
4272+
4273+
[package.metadata.example.camera_controller]
4274+
name = "Camera Controller"
4275+
description = "Example Free-Cam Styled Camera Controller"
4276+
category = "Helpers"
4277+
wasm = true
4278+
4279+
[[example]]
4280+
name = "widgets"
4281+
path = "examples/helpers/widgets.rs"
4282+
doc-scrape-examples = true
4283+
crate-type = ["lib"]
4284+
4285+
[package.metadata.example.widgets]
4286+
name = "Widgets"
4287+
description = "Example UI Widgets"
4288+
category = "Helpers"
4289+
wasm = true

examples/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ git checkout v0.4.0
5151
- [ECS (Entity Component System)](#ecs-entity-component-system)
5252
- [Games](#games)
5353
- [Gizmos](#gizmos)
54+
- [Helpers](#helpers)
5455
- [Input](#input)
5556
- [Math](#math)
5657
- [Movement](#movement)
@@ -352,6 +353,13 @@ Example | Description
352353
[Axes](../examples/gizmos/axes.rs) | Demonstrates the function of axes gizmos
353354
[Light Gizmos](../examples/gizmos/light_gizmos.rs) | A scene showcasing light gizmos
354355

356+
## Helpers
357+
358+
Example | Description
359+
--- | ---
360+
[Camera Controller](../examples/helpers/camera_controller.rs) | Example Free-Cam Styled Camera Controller
361+
[Widgets](../examples/helpers/widgets.rs) | Example UI Widgets
362+
355363
## Input
356364

357365
Example | Description

examples/helpers/camera_controller.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//! To use in your own application:
33
//! - Copy the code for the [`CameraControllerPlugin`] and add the plugin to your App.
44
//! - Attach the [`CameraController`] component to an entity with a [`Camera3d`].
5+
//!
6+
//! Unlike other examples, which demonstrate an application, this demonstrates a plugin library.
57
68
use bevy::{
79
input::mouse::{AccumulatedMouseMotion, AccumulatedMouseScroll, MouseScrollUnit},
@@ -10,6 +12,7 @@ use bevy::{
1012
};
1113
use std::{f32::consts::*, fmt};
1214

15+
/// A freecam-style camera controller plugin.
1316
pub struct CameraControllerPlugin;
1417

1518
impl Plugin for CameraControllerPlugin {
@@ -23,26 +26,48 @@ impl Plugin for CameraControllerPlugin {
2326
/// it because it felt nice.
2427
pub const RADIANS_PER_DOT: f32 = 1.0 / 180.0;
2528

29+
/// Camera controller [`Component`].
2630
#[derive(Component)]
2731
pub struct CameraController {
32+
/// Enables this [`CameraController`] when `true`.
2833
pub enabled: bool,
34+
/// Indicates if this controller has been initialized by the [`CameraControllerPlugin`].
2935
pub initialized: bool,
36+
/// Multiplier for pitch and yaw rotation speed.
3037
pub sensitivity: f32,
38+
/// [`KeyCode`] for forward translation.
3139
pub key_forward: KeyCode,
40+
/// [`KeyCode`] for backward translation.
3241
pub key_back: KeyCode,
42+
/// [`KeyCode`] for left translation.
3343
pub key_left: KeyCode,
44+
/// [`KeyCode`] for right translation.
3445
pub key_right: KeyCode,
46+
/// [`KeyCode`] for up translation.
3547
pub key_up: KeyCode,
48+
/// [`KeyCode`] for down translation.
3649
pub key_down: KeyCode,
50+
/// [`KeyCode`] to use [`run_speed`](CameraController::run_speed) instead of
51+
/// [`walk_speed`](CameraController::walk_speed) for translation.
3752
pub key_run: KeyCode,
53+
/// [`MouseButton`] for grabbing the mouse focus.
3854
pub mouse_key_cursor_grab: MouseButton,
55+
/// [`KeyCode`] for grabbing the keyboard focus.
3956
pub keyboard_key_toggle_cursor_grab: KeyCode,
57+
/// Multiplier for unmodified translation speed.
4058
pub walk_speed: f32,
59+
/// Multiplier for running translation speed.
4160
pub run_speed: f32,
61+
/// Multiplier for how the mouse scroll wheel modifies [`walk_speed`](CameraController::walk_speed)
62+
/// and [`run_speed`](CameraController::run_speed).
4263
pub scroll_factor: f32,
64+
/// Friction factor used to exponentially decay [`velocity`](CameraController::velocity) over time.
4365
pub friction: f32,
66+
/// This [`CameraController`]'s pitch rotation.
4467
pub pitch: f32,
68+
/// This [`CameraController`]'s yaw rotation.
4569
pub yaw: f32,
70+
/// This [`CameraController`]'s translation velocity.
4671
pub velocity: Vec3,
4772
}
4873

examples/helpers/widgets.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Simple widgets for example UI.
2+
//!
3+
//! Unlike other examples, which demonstrate an application, this demonstrates a plugin library.
24
35
use bevy::{ecs::system::EntityCommands, prelude::*};
46

0 commit comments

Comments
 (0)