~starkingdoms/starkingdoms

ref: 7498934a5a0ba4a15b9cd5781824e95aacb48af2 starkingdoms/crates/unified/src/client/crafting/ui.rs -rw-r--r-- 5.0 KiB
7498934a — core feat: bevy 0.18 22 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use bevy::{input_focus::{AutoFocus, InputFocus}, ui::RelativeCursorPosition};

use crate::{attachment::PartInShip, client::colors, ecs::{CanCraft, CraftingUi, MainCamera, Me, ToggleDrillEvent}, prelude::*};

pub fn crafting_ui_plugin(app: &mut App) {
    app.add_systems(Update, (close_button, drill_button));
}

#[derive(Component)]
struct CloseButton(Entity); // stores corresponding menu entity
#[derive(Component)]
struct PreviousInteraction(Interaction);
#[derive(Component)]
struct DrillButton(Entity); // stores corresponding part

pub fn open_crafting_ui(
    ev: On<Pointer<Press>>,
    crafting_parts: Query<(Entity, &Transform), (With<PartInShip>, With<CanCraft>)>,
    hearty: Query<(Entity, &Transform), (With<Me>, With<CanCraft>)>,
    camera: Single<(Entity, &Camera, &GlobalTransform), (With<MainCamera>, Without<PartInShip>)>,
    commands: Commands,
) {
    if matches!(ev.button, PointerButton::Secondary) {
        let (entity, transform) = if let Ok(part) = crafting_parts.get(ev.entity) {
            part
        } else if let Ok(part) = hearty.get(ev.entity) {
            part
        } else {
            return
        };
        // we have our crafting entity!
        // now make the ui
        setup_ui(entity, transform, commands, camera);
    }
}

fn setup_ui(
    parent_part: Entity,
    parent_transform: &Transform,
    mut commands: Commands,
    camera: Single<(Entity, &Camera, &GlobalTransform), (With<MainCamera>, Without<PartInShip>)>,
) {
    let parent_pos = camera.1.world_to_viewport(camera.2, parent_transform.translation).unwrap();
    let entity = commands.spawn((
        UiTargetCamera(camera.0),
        Node {
            position_type: PositionType::Absolute,
            left: Val::Px(parent_pos.x),
            top: Val::Px(parent_pos.y),
            width: Val::Px(100.0),
            height: Val::Px(100.0),
            ..default()
        },
        AutoFocus,
        CraftingUi,
        BackgroundColor(colors::MANTLE),
        RelativeCursorPosition::default(),
    ))
    .with_children(|parent| {
        parent.spawn((
            Node {
                width: Val::Px(25.0),
                height: Val::Px(25.0),
                justify_content: JustifyContent::Center,
                align_content: AlignContent::Center,
                ..Default::default()
            },
            Button,
            BackgroundColor(colors::RED),
            CloseButton(parent.target_entity()),
            PreviousInteraction(Interaction::None),
        ))
        .with_children(|parent| {
            parent.spawn((
                Node {
                    ..Default::default()
                },
                Text::new("x"),
            ));
        });
        parent.spawn((
            Node {
                ..Default::default()
            },
            Button,
            DrillButton(parent_part),
            BackgroundColor(colors::CRUST),
            PreviousInteraction(Interaction::None),
        ))
        .with_children(|parent| {
            parent.spawn((
                Node {
                    ..Default::default()
                },
                Text::new("Start Drill"),
            ));
        });
    });
}

fn drill_button(
    mut interaction_query: Query<
        (&Interaction, &mut PreviousInteraction, &mut BackgroundColor, &DrillButton, &mut Button),
        Changed<Interaction>,
    >,
    mut toggle_drill_writer: MessageWriter<ToggleDrillEvent>,
) {
    for (interaction, mut previous_interaction, mut color, drill_button, mut button) in &mut interaction_query {
        match *interaction {
            Interaction::Pressed => {
                *color = colors::SURFACE_1.into();
            }
            Interaction::Hovered => {
                *color = colors::SURFACE_0.into();
                if previous_interaction.0 == Interaction::Pressed {
                    // released
                    toggle_drill_writer.write(ToggleDrillEvent { drill_entity: drill_button.0 });
                }
            }
            Interaction::None => {
                *color = colors::CRUST.into();
            }
        }
        previous_interaction.0 = *interaction;
    }
}

fn close_button(
    mut commands: Commands,
    mut interaction_query: Query<
        (&Interaction, &mut PreviousInteraction, &mut BackgroundColor, &CloseButton, &mut Button),
        Changed<Interaction>,
    >,
    mouse: Res<ButtonInput<MouseButton>>,
) {
    for (interaction, mut previous_interaction, mut color,
        close_button, mut button) in &mut interaction_query
    {
        match *interaction {
            Interaction::Pressed => {
                *color = colors::MAROON.into();
            }
            Interaction::Hovered => {
                *color = colors::PINK.into();
                if previous_interaction.0 == Interaction::Pressed {
                    commands.entity(close_button.0).despawn();
                }
            }
            Interaction::None => {
                *color = colors::RED.into();
            }
        }
        previous_interaction.0 = *interaction;
    }
}