~starkingdoms/starkingdoms

ref: 785a5a087f78903b09c8e0cbcf1454bbe44c114f starkingdoms/crates/unified/src/ship_editor/select.rs -rw-r--r-- 2.2 KiB
785a5a08ghostly_zsh ship editor feat: shift clicking and regular clicking work how they probably should 3 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
use crate::prelude::*;
use crate::ship_editor::components::{Part, PartConfigHolder, Selectable, SelectedCount, OUTLINE_RENDER_LAYER};
use crate::ship_editor::input::ShipEditorDrag;

pub fn click_select(
    ev: On<Pointer<Click>>,
    keys: Res<ButtonInput<KeyCode>>,
    mut parts: Query<(Entity, &Transform, &PartConfigHolder, &mut Selectable), With<Part>>,
    drag: Res<ShipEditorDrag>,
    mut selected_count: ResMut<SelectedCount>,
    mut commands: Commands,
    asset_server: Res<AssetServer>,
) {
    if !drag.can_select { return }

    let Ok((part_entity, part_transform, part, mut part_selectable)) = parts.get_mut(ev.entity) else {
        error!("No Part found upon part selection. The observer probably wasn't removed.");
        return;
    };
    if !part_selectable.is_selected {
        let mut sprite = Sprite::from_image(asset_server.load("textures/outline.png"));
        sprite.custom_size = Some(vec2(part.0.physics.width as f32, part.0.physics.height as f32) * 1.0625);
        let outline_entity = commands.spawn((
            part_transform.clone(),
            sprite,
        ));
        part_selectable.outline_entity = Some(outline_entity.id());
        part_selectable.is_selected = true;
        selected_count.0 += 1;
    } else { 'select: {
        if !(keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight)) && selected_count.0 > 1 { break 'select; }
        part_selectable.is_selected = false;
        if let Some(outline_entity) = part_selectable.outline_entity {
            commands.entity(outline_entity).despawn();
        }
        part_selectable.outline_entity = None;
        selected_count.0 -= 1;
    } }
    if !(keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight)) {
        for (part_entity, part_transform, part, mut part_selectable) in parts.iter_mut() {
            if part_entity == ev.entity { continue }

            if let Some(outline_entity) = part_selectable.outline_entity {
                part_selectable.is_selected = false;
                commands.entity(outline_entity).despawn();
                part_selectable.outline_entity = None;
                selected_count.0 -= 1;
            }
        }
    }
}