bevy_codex/
systems.rs

1use bevy::{color::palettes::css::YELLOW, prelude::*, sprite::Anchor};
2use bevy_lunex::prelude::*;
3
4use crate::{components::Quit, events::SelectEvent, splash::components::SplashScreen};
5
6pub fn init_ui_cam(mut commands: Commands, assets: Res<AssetServer>) {
7    commands.spawn(camera()).with_children(|cam| {
8        let mut cursor = Cursor2d::new();
9        cursor.request_cursor(CursorIcon::Default, 0.0);
10        cursor.request_cursor(CursorIcon::Pointer, 1.0);
11        cursor.request_cursor(CursorIcon::Grab, 2.0);
12
13        cam.spawn(StyledCursorBundle {
14            cursor,
15            sprite: SpriteBundle {
16                texture: assets.load("images/ui/cursor_pointer3D_shadow.png"),
17                transform: Transform {
18                    scale: Vec3::new(0.45, 0.45, 1.0),
19                    ..default()
20                },
21                sprite: Sprite {
22                    color: YELLOW.into(),
23                    anchor: Anchor::TopLeft,
24                    ..default()
25                },
26                ..default()
27            },
28            ..default()
29        });
30    });
31
32    commands.spawn(SplashScreen);
33}
34pub fn exit(mut app_exit_event_writer: EventWriter<AppExit>, quit: Query<Entity, Added<Quit>>) {
35    for _ in &quit {
36        app_exit_event_writer.send(AppExit::Success);
37    }
38}
39
40/// Function to return camera will all appropriate settings
41pub fn camera() -> impl Bundle {
42    (
43        MainUi,
44        Camera2dBundle {
45            transform: Transform::from_xyz(0.0, 0.0, 1000.0),
46            camera: Camera {
47                hdr: true,
48                ..default()
49            },
50            ..default()
51        },
52        InheritedVisibility::default(),
53    )
54}
55
56pub fn create_root(commands: &mut Commands, r_size: (f32, f32)) {
57    commands
58        .spawn(SpatialBundle::default())
59        .with_children(|route| {
60            route
61                .spawn((
62                    UiTreeBundle::<MainUi>::from(UiTree::new2d("Main")),
63                    MovableByCamera,
64                ))
65                .with_children(|ui| {
66                    let root = UiLink::<MainUi>::path("Root");
67                    ui.spawn((root.clone(), UiLayout::window().size(r_size).pack::<Base>()));
68                });
69        });
70}
71pub fn button_click(mut events: EventReader<UiClickEvent>, mut ev_w: EventWriter<SelectEvent>) {
72    for event in events.read() {
73        ev_w.send(SelectEvent(event.clone()));
74    }
75}