~starkingdoms/starkingdoms

ref: 94336e646a19dd15e9b88289757a2057676450a5 starkingdoms/crates/unified/src/ship_editor/input.rs -rw-r--r-- 14.6 KiB
94336e64ghostly_zsh ship editor feat: attachment 4 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::f64::consts::PI;
use avian2d::physics_transform::{Position, Rotation};
use avian2d::prelude::{AngularVelocity, LinearVelocity};
use bevy::camera::visibility::RenderLayers;
use bevy::input::mouse::{MouseScrollUnit, MouseWheel};
use bevy::input_focus::InputFocus;
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use crate::client::components::Me;
use crate::shared::attachment::{Joint, JointOf, Joints, PartInShip, Peer, SnapOf, SnapOfJoint};
use crate::shared::ecs::MAIN_LAYER;
use crate::ship_editor::components::{GhostCamera, GhostModule, MainCamera, Part, PartConfigHolder, SpawnPartRequest, GHOST_RENDER_LAYER};
use crate::ship_editor::spawn_joints;
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,
    target: Option<Entity>,
    peer: Option<Entity>,
}

fn on_scroll(
    mut scroll_events: MessageReader<MouseWheel>,
    mut main_camera: Single<(&mut Transform, &mut Projection), (With<Camera2d>, With<MainCamera>)>,
    mut ghost_camera: Single<(&mut Transform, &mut Projection), (With<Camera2d>, With<GhostCamera>, Without<MainCamera>)>,
    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 main_transform = main_camera.0.clone();
    let mut ghost_transform = ghost_camera.0.clone();
    let Projection::Orthographic(ref mut main_projection) = *main_camera.1 else { return };
    let Projection::Orthographic(ref mut ghost_projection) = *ghost_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 *= main_projection.scale;
                    main_projection.scale *= 0.95;
                    ghost_projection.scale = main_projection.scale;
                    let scaled_rel_pos = rel_pos * 0.95;
                    main_transform.translation += scaled_rel_pos.extend(0.0) - rel_pos.extend(0.0);
                    ghost_transform.translation = main_transform.translation;
                } else {
                    let mut rel_pos = vec2(window.width() / 2.0, -window.height() / 2.0) - cursor_pos;
                    rel_pos *= main_projection.scale;
                    main_projection.scale *= 1.05;
                    ghost_projection.scale = main_projection.scale;
                    let scaled_rel_pos = rel_pos * 1.05;
                    main_transform.translation += scaled_rel_pos.extend(0.0) - rel_pos.extend(0.0);
                    ghost_transform.translation = main_transform.translation;
                }
            }
        }
    }
    *main_camera.0 = main_transform;
    *ghost_camera.0 = ghost_transform;
}

fn on_click(
    ev: Res<ButtonInput<MouseButton>>,
    mut drag: ResMut<ShipEditorDrag>,
    camera: Single<&Transform, (With<Camera2d>, With<MainCamera>)>,
    window: Single<&Window, With<PrimaryWindow>>,
    mut ghost_module: Query<(Entity, &mut Transform, &mut Sprite, &Joints), (With<GhostModule>, Without<MainCamera>)>,
    snaps: Query<(&SnapOf, &SnapOfJoint)>,
    joints: Query<(&Joint, &JointOf, &Transform, Option<&Peer>, Entity), (Without<GhostModule>, Without<MainCamera>)>,
    mut parts: Query<
        (
            &mut Transform,
            Option<&PartInShip>,
            Entity,
            &Joints,
            &Part,
        ),
        (Without<Joint>, Without<GhostModule>, Without<MainCamera>),
    >,
    asset_server: Res<AssetServer>,
    mut commands: Commands,
) {
    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;

        if let Some(snap_target) = drag.target && let Some(peer_snap) = drag.peer {
            let Ok((ghost_entity, mut ghost_transform, mut ghost_sprite, ghost_joints)) = ghost_module.single_mut() else { return };
            let Ok((target_snap_part, target_snap_joint)) = snaps.get(snap_target) else {
                return;
            };
            let Ok((source_snap_part, source_snap_joint)) = snaps.get(peer_snap) else {
                return;
            };

            let Ok((target_jt, target_jt_of, target_jt_xform, target_jt_peer, target_jt_id)) = joints.get(target_snap_joint.0) else {
                return;
            };
            let Ok((source_jt, source_jt_of, _source_jt_xform, source_jt_peer, source_jt_id)) = joints.get(source_snap_joint.0) else {
                return;
            };

            if target_jt_peer.is_some() {
                warn!("drag request: cannot attach to a joint that already has a peer, ignoring request");
                return;
            }
            if source_jt_peer.is_some() {
                warn!("drag request: dragging from a part that is already attached is currently not supported, ignoring request");
                return;
            }

            let Ok((target_xform, target_in_ship, target_entity, _, _)) = parts.get(target_jt_of.0) else {
                return;
            };

            // attached (housing)
            /*let Ok((_, _, source_entity, source_joints, _)) = parts.get(source_jt_of.0) else {
                return;
            };*/

            commands.entity(source_jt_id).insert(Peer {
                peer_joint_entity_id: target_jt_id,
                physics_joint: Entity::PLACEHOLDER, // reusing components and physics joint is not used in the ship editor
            });
            commands.entity(target_jt_id).insert(Peer {
                peer_joint_entity_id: source_jt_id,
                physics_joint: Entity::PLACEHOLDER,
            });
            let target_position = target_xform.mul_transform(*target_jt_xform);
            ghost_transform.translation = target_position.translation;
            ghost_transform.rotation = target_position.rotation
                * source_jt.transform.rotation.inverse()
                * Quat::from_rotation_z(PI as f32);

            debug!("spawning part");
            ghost_sprite.color = Color::srgb(1.0, 1.0, 1.0);
            commands.entity(ghost_entity)
                .insert(Part)
                .insert(MAIN_LAYER)
                .remove::<RenderLayers>()
                .remove::<GhostModule>();
        }
    }
}

fn camera_drag(
    drag: ResMut<ShipEditorDrag>,
    mut main_camera: Single<(&mut Transform, &Projection), (With<Camera2d>, With<MainCamera>)>,
    mut ghost_camera: Single<(&mut Transform, &Projection), (With<Camera2d>, With<GhostCamera>, Without<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 = main_camera.1.clone();
    let Projection::Orthographic(projection) = projection else { return };
    let main_transform = &mut main_camera.0;
    let ghost_transform = &mut ghost_camera.0;

    main_transform.translation = drag.init_camera_pos.extend(0.0) - (cursor.with_y(-cursor.y) - drag.init_cursor_pos).extend(0.0)*projection.scale;
    ghost_transform.translation = main_transform.translation;
}
fn ghost_drag(
    mut drag: ResMut<ShipEditorDrag>,
    window: Single<&Window, With<PrimaryWindow>>,
    mut ghost_module: Single<(Entity, &mut Transform), With<GhostModule>>,
    mut main_camera: Single<(&Camera, &GlobalTransform), (With<Camera2d>, With<MainCamera>)>,
    mut ghost_camera: Single<(&Camera, &GlobalTransform), (With<Camera2d>, With<GhostCamera>)>,
    snaps: Query<(&Transform, &SnapOfJoint, &SnapOf, Entity), Without<GhostModule>>,
    joints: Query<(&Transform, &JointOf, Entity), (Without<Peer>, Without<GhostModule>)>,
    parts: Query<&GlobalTransform, (Or<(With<Part>, With<GhostModule>)>)>,
) {
    const SNAP_CUTOFF: f32 = 25.0;
    if !drag.is_dragging || !drag.is_holding_module { return }
    let (ghost_entity, mut ghost_transform) = ghost_module.into_inner();
    let Some(cursor) = window.cursor_position() else { return };
    let (ghost_camera, ghost_camera_transform) = *ghost_camera;
    let (main_camera, main_camera_transform) = *main_camera;
    let Ok(ghost_position) = ghost_camera.viewport_to_world_2d(ghost_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
    };
    let Ok(global_cursor_position) = ghost_camera.viewport_to_world_2d(main_camera_transform, cursor) else {
        error!("Unable to convert cursor position to world space");
        return
    };

    let mut closest_distance = f32::INFINITY;
    let mut closest_snap_transform = None;
    let mut closest_snap = None;
    let mut closest_ghost_snap = None;
    for (snap_local_transform, snap_joint, snap_part, snap_id) in snaps {
        if snap_part.0 == ghost_entity { continue }

        let Ok(part_transform) = parts.get(snap_part.0) else { continue };

        let snap_global_translation = part_transform.mul_transform(*snap_local_transform);

        let distance_to_cursor = global_cursor_position.distance(snap_global_translation.translation().xy());

        if distance_to_cursor > closest_distance {
            continue;
        }
        if distance_to_cursor > SNAP_CUTOFF {
            continue;
        }


        let Ok((offset, _, _)) = joints.get(snap_joint.0) else {
            continue;
        };
        let global_joint_offset = part_transform.transform_point(offset.translation);

        let mut target_transform = Transform {
            translation: global_joint_offset,
            rotation: ghost_transform.rotation,
            scale: Vec3::splat(1.0),
        };

        // figure out the ghost's best snap to use
        let mut closest_peer_snap_distance = f32::INFINITY;
        let mut closest_peer_snap = None;
        let mut closest_joint_transform = None;
        for (our_snap_local_transform, our_snap_joint, our_snap_part, our_snap_id) in &snaps {
            if ghost_entity != our_snap_part.0 {
                continue;
            }

            let our_snap_global_translation =
                target_transform.mul_transform(*our_snap_local_transform);

            let distance = our_snap_global_translation
                .translation
                .distance(snap_global_translation.translation());
            if distance > closest_peer_snap_distance {
                continue;
            }

            let Ok((our_joint, _, _)) = joints.get(our_snap_joint.0) else {
                continue;
            };

            closest_peer_snap_distance = distance;
            closest_peer_snap = Some(our_snap_id);
            closest_joint_transform = Some(our_joint);
        }

        target_transform.rotation = part_transform.rotation()
            * (offset.rotation * closest_joint_transform.unwrap().rotation.inverse())
            * Quat::from_rotation_z(180.0f32.to_radians());
        closest_distance = distance_to_cursor;
        closest_snap_transform = Some(target_transform);
        closest_snap = Some(snap_id);
        closest_ghost_snap = closest_peer_snap;
    }
    if let Some(transform) = closest_snap_transform {
        *ghost_transform = transform;
    } else {
        ghost_transform.translation = ghost_position.extend(0.0);
    }
    drag.target = closest_snap;
    drag.peer = closest_ghost_snap;
}

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;
                    if drag.target.is_none() {
                        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;
                    if drag.target.is_none() {
                        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
                };
                let entity = commands.spawn((
                    sprite,
                    Transform::from_translation(ghost_position.extend(0.0)),
                    GhostModule,
                    PartConfigHolder(part_entry.0.clone()),
                    GHOST_RENDER_LAYER,
                ));
                spawn_joints(&part_entry.0, entity.id(), commands.reborrow());
            }
        }
    }
}