camera ui

This commit is contained in:
2025-03-21 13:25:28 +01:00
parent bf96078168
commit 255c264702
2 changed files with 62 additions and 1 deletions

BIN
assets/ui/camera.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

View File

@@ -17,8 +17,12 @@ pub struct CameraRotation(pub f32);
#[reflect(Resource)] #[reflect(Resource)]
pub struct CameraState { pub struct CameraState {
pub cutscene: bool, pub cutscene: bool,
pub look_around: bool,
} }
#[derive(Component, Reflect, Debug, Default)]
struct CameraUi;
#[derive(Component, Reflect, Debug)] #[derive(Component, Reflect, Debug)]
#[reflect(Component)] #[reflect(Component)]
pub struct MainCamera { pub struct MainCamera {
@@ -40,7 +44,16 @@ pub fn plugin(app: &mut App) {
app.init_resource::<CameraState>(); app.init_resource::<CameraState>();
app.add_systems(Startup, startup); app.add_systems(Startup, startup);
app.add_systems(Update, (update, rotate_view_keyboard, rotate_view_gamepad)); app.add_systems(
Update,
(
update,
update_ui,
update_look_around,
rotate_view_keyboard,
rotate_view_gamepad,
),
);
} }
fn startup(mut commands: Commands) { fn startup(mut commands: Commands) {
@@ -51,6 +64,54 @@ fn startup(mut commands: Commands) {
)); ));
} }
fn update_look_around(controls: Res<Controls>, mut cam_state: ResMut<CameraState>) {
let look_around =
controls.keyboard_state.view_mode || controls.gamepad_state.map_or(false, |g| g.view_mode);
if look_around != cam_state.look_around {
cam_state.look_around = look_around;
}
}
fn update_ui(
mut commands: Commands,
cam_state: Res<CameraState>,
asset_server: Res<AssetServer>,
query: Query<Entity, With<CameraUi>>,
) {
if cam_state.is_changed() {
let show_ui = cam_state.look_around || cam_state.cutscene;
if show_ui {
let image = asset_server.load("ui/camera.png");
commands
.spawn((
CameraUi,
Node {
margin: UiRect::top(Val::Px(20.))
.with_left(Val::Auto)
.with_right(Val::Auto),
justify_content: JustifyContent::Center,
..default()
},
))
.with_child((
Node {
display: Display::Block,
position_type: PositionType::Absolute,
..default()
},
ImageNode::new(image),
));
} else {
for entity in query.iter() {
commands.entity(entity).despawn_recursive();
}
}
}
}
fn update( fn update(
mut cam: Query< mut cam: Query<
(&MainCamera, &mut Transform, &CameraRotation), (&MainCamera, &mut Transform, &CameraRotation),