~starkingdoms/starkingdoms

ref: 5591c7a32888c9950b5d21d1d18a6150907d6ea7 starkingdoms/crates/unified/src/ship_editor/input.rs -rw-r--r-- 6.5 KiB
5591c7a3ghostly_zsh feat: part list & dragging ghosts 9 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
156
157
158
159
use bevy::input::mouse::{MouseScrollUnit, MouseWheel};
use bevy::input_focus::InputFocus;
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use crate::ship_editor::components::{GhostCamera, GhostModule, MainCamera, GHOST_RENDER_LAYER};
use crate::ship_editor::ui::PartEntry;

pub fn input_plugin(app: &mut App) {
    app
        .insert_resource(ShipEditorDrag::default())
        .add_systems(Update, on_scroll)
        .add_systems(Update, on_click)
        .add_systems(Update, camera_drag)
        .add_systems(Update, ghost_drag)
        .add_systems(Update, part_button_interaction);
}

#[derive(Resource, Default)]
struct ShipEditorDrag {
    is_dragging: bool,
    is_holding_module: bool,
    init_cursor_pos: Vec2,
    init_camera_pos: Vec2,
}

fn on_scroll(
    mut scroll_events: MessageReader<MouseWheel>,
    mut camera: Single<(&mut Transform, &mut Projection), With<Camera2d>>,
    window: Single<&Window, With<PrimaryWindow>>,
) {
    let Some(cursor_pos) = window.cursor_position() else { return };
    let cursor_pos = cursor_pos.with_y(-cursor_pos.y);

    let mut transform = camera.0.clone();
    let Projection::Orthographic(ref mut projection) = *camera.1 else { return };
    for ev in scroll_events.read() {
        match ev.unit {
            MouseScrollUnit::Line | MouseScrollUnit::Pixel => {
                if ev.y > 0.0 {
                    let mut rel_pos = vec2(window.width() / 2.0, -window.height() / 2.0) - cursor_pos;
                    rel_pos *= projection.scale;
                    projection.scale *= 0.95;
                    let scaled_rel_pos = rel_pos * 0.95;
                    transform.translation += scaled_rel_pos.extend(0.0) - rel_pos.extend(0.0);
                } else {
                    let mut rel_pos = vec2(window.width() / 2.0, -window.height() / 2.0) - cursor_pos;
                    rel_pos *= projection.scale;
                    projection.scale *= 1.05;
                    let scaled_rel_pos = rel_pos * 1.05;
                    transform.translation += scaled_rel_pos.extend(0.0) - rel_pos.extend(0.0);
                }
            }
        }
    }
    *camera.0 = transform;
}

fn on_click(
    ev: Res<ButtonInput<MouseButton>>,
    mut drag: ResMut<ShipEditorDrag>,
    camera: Single<&Transform, (With<Camera2d>, With<MainCamera>)>,
    window: Single<&Window, With<PrimaryWindow>>,
) {
    let Some(cursor_pos) = window.cursor_position() else { return };
    if ev.just_pressed(MouseButton::Left) {
        drag.is_dragging = true;
        drag.init_cursor_pos = cursor_pos.with_y(-cursor_pos.y);
        drag.init_camera_pos = camera.translation.truncate();
    }
    if ev.just_released(MouseButton::Left) {
        drag.is_dragging = false;
    }
}

fn camera_drag(
    drag: ResMut<ShipEditorDrag>,
    mut camera: Single<(&mut Transform, &Projection), (With<Camera2d>, With<MainCamera>)>,
    window: Single<&Window, With<PrimaryWindow>>,
) {
    if !drag.is_dragging || drag.is_holding_module { return }
    let Some(cursor) = window.cursor_position() else { return };
    let projection = camera.1.clone();
    let Projection::Orthographic(projection) = projection else { return };
    let transform = &mut camera.0;

    transform.translation = drag.init_camera_pos.extend(0.0) - (cursor.with_y(-cursor.y) - drag.init_cursor_pos).extend(0.0)*projection.scale;
}
fn ghost_drag(
    drag: Res<ShipEditorDrag>,
    window: Single<&Window, With<PrimaryWindow>>,
    mut ghost_module: Query<&mut Transform, With<GhostModule>>,
    mut ghost_camera: Single<(&Camera, &GlobalTransform), (With<Camera2d>, With<GhostCamera>)>,
) {
    if !drag.is_dragging || !drag.is_holding_module { return }
    let Some(cursor) = window.cursor_position() else { return };
    let (ghost_camera, camera_transform) = *ghost_camera;
    let Ok(ghost_position) = ghost_camera.viewport_to_world_2d(camera_transform, cursor) else {
        error!("Unable to convert cursor position to world space");
        return // return because this error happens when something is invalid with the camera
    };

    for mut ghost_transform in ghost_module.iter_mut() {
        ghost_transform.translation = ghost_position.extend(0.0);
    }
}

fn part_button_interaction(
    mouse_ev: Res<ButtonInput<MouseButton>>,
    part_button_query: Query<(Entity, &PartEntry, &Interaction), (With<Button>, Changed<Interaction>)>,
    ghost_part_query: Query<Entity, With<GhostModule>>,
    mut drag: ResMut<ShipEditorDrag>,
    mut input_focus: ResMut<InputFocus>,
    mut commands: Commands,
    mut ghost_camera: Single<(&Camera, &GlobalTransform), (With<Camera2d>, With<GhostCamera>)>,
    asset_server: Res<AssetServer>,
    window: Single<&Window, With<PrimaryWindow>>,
) {
    let Some(cursor) = window.cursor_position() else { return };
    let (ghost_camera, camera_transform) = *ghost_camera;
    for (entity, part_entry, interaction) in part_button_query.iter() {
        match interaction {
            Interaction::None => {
                if !mouse_ev.pressed(MouseButton::Left) {
                    drag.is_holding_module = false;
                    for ghost_entity in ghost_part_query.iter() {
                        commands.entity(ghost_entity).despawn();
                    }
                }
            }
            Interaction::Hovered => {
                if !mouse_ev.pressed(MouseButton::Left) {
                    drag.is_holding_module = false;
                    for ghost_entity in ghost_part_query.iter() {
                        commands.entity(ghost_entity).despawn();
                    }
                }
            }
            Interaction::Pressed => {
                input_focus.set(entity);
                drag.is_holding_module = true;
                debug!("pressed {:?}", part_entry.0.part.name);

                let mut sprite = Sprite::from_image(asset_server.load(part_entry.0.part.sprite_disconnected.clone()));
                sprite.custom_size = Some(vec2(part_entry.0.physics.width as f32, part_entry.0.physics.height as f32));
                sprite.color = Color::srgb(0.7, 0.7, 0.7);
                let Ok(ghost_position) = ghost_camera.viewport_to_world_2d(camera_transform, cursor) else {
                    error!("Unable to convert cursor position to world space");
                    return // return because this error happens when something is invalid with the camera
                };
                commands.spawn((
                    sprite,
                    Transform::from_translation(ghost_position.extend(0.0)),
                    GhostModule,
                    GHOST_RENDER_LAYER,
                ));
            }
        }
    }
}