nightshade-editor 0.25.0

Interactive map editor for the Nightshade game engine
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use crate::ecs::{EditorMode, EditorWorld};
use crate::systems::input;
use crate::systems::mode::is_descendant_of;
use nightshade::ecs::input::resources::MouseState;
use nightshade::ecs::transform::queries::{chain_from_root_to_leaf, find_group_root};
use nightshade::ecs::ui::types::Rect;
use nightshade::prelude::*;

const CLICK_DRAG_THRESHOLD: f32 = 4.0;

pub fn update(editor_world: &mut EditorWorld, world: &mut World) {
    handle_selection_shortcuts(editor_world, world);

    let mouse = *nightshade::ecs::input::access::mouse_for_active(world);
    let just_pressed = mouse.state.contains(MouseState::LEFT_JUST_PRESSED);
    let just_released = mouse.state.contains(MouseState::LEFT_JUST_RELEASED);
    let held = mouse.state.contains(MouseState::LEFT_CLICKED);
    let in_viewport = input::mouse_in_active_viewport(world);
    let ui_block = input::ui_capturing(world);
    let hud_block = world.resources.user_interface.hud_wants_pointer;

    world.resources.user_interface.gizmos.selection_marquee = None;

    if just_pressed {
        editor_world.resources.picking.press_position = if in_viewport && !ui_block && !hud_block {
            Some(mouse.position)
        } else {
            None
        };
        editor_world.resources.picking.is_dragging = false;
    }

    if held
        && let Some(start) = editor_world.resources.picking.press_position
        && nalgebra_glm::distance(&start, &mouse.position) > CLICK_DRAG_THRESHOLD
    {
        editor_world.resources.picking.is_dragging = true;
    }

    let shift_held = world
        .resources
        .input
        .keyboard
        .is_key_pressed(KeyCode::ShiftLeft)
        || world
            .resources
            .input
            .keyboard
            .is_key_pressed(KeyCode::ShiftRight);
    if held
        && shift_held
        && editor_world.resources.picking.is_dragging
        && !ui_block
        && !gizmo_drag_active(world)
        && let Some(start) = editor_world.resources.picking.press_position
    {
        marquee_select(editor_world, world, start, mouse.position);
    }

    let mut pending_alt = false;
    let mut pending_shift = false;
    let mut requested_pick = false;
    if just_released
        && let Some(start) = editor_world.resources.picking.press_position.take()
        && !editor_world.resources.picking.is_dragging
        && nalgebra_glm::distance(&start, &mouse.position) <= CLICK_DRAG_THRESHOLD
    {
        let render_scale = world
            .resources
            .render_settings
            .render_scale
            .clamp(0.25, 4.0);
        let pick_pos = world
            .resources
            .window
            .active_viewport_rect
            .and_then(|rect| {
                let texture_width = ((rect.width * render_scale).round() as u32).max(1);
                let texture_height = ((rect.height * render_scale).round() as u32).max(1);
                surface_pick_coords(mouse.position, Some(rect), (texture_width, texture_height))
            });
        if let Some((x, y)) = pick_pos {
            world.resources.gpu_picking.request_pick(x, y);
            let keyboard = &world.resources.input.keyboard;
            pending_alt = keyboard.is_key_pressed(KeyCode::AltLeft)
                || keyboard.is_key_pressed(KeyCode::AltRight);
            pending_shift = keyboard.is_key_pressed(KeyCode::ShiftLeft)
                || keyboard.is_key_pressed(KeyCode::ShiftRight);
            requested_pick = true;
        }
    }

    if just_released {
        editor_world.resources.picking.is_dragging = false;
    }

    if requested_pick {
        editor_world.resources.picking.pending_alt = pending_alt;
        editor_world.resources.picking.pending_shift = pending_shift;
    }

    if let Some(result) = world.resources.gpu_picking.take_result() {
        let alt_held = std::mem::take(&mut editor_world.resources.picking.pending_alt);
        let shift_held = std::mem::take(&mut editor_world.resources.picking.pending_shift);
        let picked = result
            .entity_id
            .and_then(|id| {
                world
                    .core
                    .query_entities(nightshade::ecs::world::RENDER_MESH)
                    .find(|entity| entity.id == id)
            })
            .map(|entity| {
                editor_world
                    .resources
                    .camera_gizmos
                    .camera_for_proxy(entity)
                    .unwrap_or(entity)
            })
            .filter(|entity| !editor_world.resources.editor_scene.is_scaffolding(*entity));
        let (new_selection, expand_tree) = match picked {
            None => {
                reset_cycle(editor_world);
                (None, false)
            }
            Some(leaf) => resolve_selection(editor_world, world, leaf, alt_held),
        };
        if shift_held {
            if let Some(entity) = new_selection {
                crate::systems::selection::toggle(editor_world, entity);
            }
        } else {
            crate::systems::selection::set_primary(editor_world, new_selection);
        }
        if expand_tree {
            let focus_target = editor_world.resources.ui.selected_entity;
            focus_tree_on(editor_world, world, focus_target);
        }
    }

    sync_outline(editor_world, world);
}

fn handle_selection_shortcuts(editor_world: &mut EditorWorld, world: &mut World) {
    let keyboard = &world.resources.input.keyboard;
    let ctrl = keyboard.is_key_pressed(KeyCode::ControlLeft)
        || keyboard.is_key_pressed(KeyCode::ControlRight);
    let typing = world
        .resources
        .retained_ui
        .interaction
        .focused_entity
        .is_some_and(|entity| {
            world.ui.get_ui_text_input(entity).is_some()
                || world.ui.get_ui_text_area(entity).is_some()
        });
    let select_all = !typing && !ctrl && keyboard.is_key_pressed(KeyCode::KeyA);
    let deselect = keyboard.is_key_pressed(KeyCode::Escape);

    if select_all && !editor_world.resources.picking.select_all_was_pressed {
        let mut roots: Vec<Entity> = Vec::new();
        for entity in world
            .core
            .query_entities(nightshade::ecs::world::RENDER_MESH)
        {
            if editor_world.resources.editor_scene.is_scaffolding(entity) {
                continue;
            }
            let root = find_group_root(world, entity);
            if !roots.contains(&root) {
                roots.push(root);
            }
        }
        crate::systems::selection::set_many(editor_world, roots);
    }
    editor_world.resources.picking.select_all_was_pressed = select_all;

    if deselect && !editor_world.resources.picking.deselect_was_pressed {
        crate::systems::selection::clear(editor_world);
    }
    editor_world.resources.picking.deselect_was_pressed = deselect;
}

pub fn reset_cycle(editor_world: &mut EditorWorld) {
    editor_world.resources.picking.last_pick_leaf = None;
    editor_world.resources.picking.last_pick_root = None;
    editor_world.resources.picking.cycle_depth = 0;
}

fn resolve_selection(
    editor_world: &mut EditorWorld,
    world: &World,
    leaf: Entity,
    alt_held: bool,
) -> (Option<Entity>, bool) {
    if alt_held {
        editor_world.resources.picking.last_pick_leaf = Some(leaf);
        editor_world.resources.picking.last_pick_root = Some(leaf);
        editor_world.resources.picking.cycle_depth = 0;
        return (Some(leaf), true);
    }
    match editor_world.resources.mode.mode {
        EditorMode::Edit { target } => {
            reset_cycle(editor_world);
            if is_descendant_of(world, leaf, target) {
                (Some(leaf), true)
            } else {
                (None, false)
            }
        }
        EditorMode::Object => {
            let root = find_group_root(world, leaf);
            let chain = chain_from_root_to_leaf(world, root, leaf);
            let last_root = editor_world.resources.picking.last_pick_root;
            let last_leaf = editor_world.resources.picking.last_pick_leaf;
            let current_selection = editor_world.resources.ui.selected_entity;
            let on_same_chain = current_selection.is_some_and(|selected| chain.contains(&selected));
            let cycling = last_leaf == Some(leaf) && last_root == Some(root) && on_same_chain;
            let depth = if cycling {
                let next = editor_world.resources.picking.cycle_depth + 1;
                if next >= chain.len() { 0 } else { next }
            } else {
                0
            };
            editor_world.resources.picking.last_pick_leaf = Some(leaf);
            editor_world.resources.picking.last_pick_root = Some(root);
            editor_world.resources.picking.cycle_depth = depth;
            (Some(chain[depth]), !cycling)
        }
    }
}

fn focus_tree_on(editor_world: &mut EditorWorld, world: &mut World, selection: Option<Entity>) {
    let Some(entity) = selection else {
        return;
    };
    let tree = &mut editor_world.resources.ui_handles.tree;
    let row_index = tree.all_rows.iter().position(|row| row.entity == entity);
    let Some(row_index) = row_index else {
        return;
    };
    let target_depth = tree.all_rows[row_index].depth;
    if target_depth > 0 {
        let mut depth_to_find = target_depth;
        for row in tree.all_rows[..row_index].iter().rev() {
            if row.depth < depth_to_find && row.has_children {
                tree.expanded.insert(row.entity.id);
                if depth_to_find == 1 {
                    break;
                }
                depth_to_find = row.depth;
            }
        }
    }
    tree.last_visible_start = usize::MAX;

    let scroll_entity = world
        .ui
        .get_ui_virtual_list(tree.list)
        .map(|d| d.scroll_entity);
    if let Some(scroll_entity) = scroll_entity {
        let list_height = world
            .ui
            .get_ui_layout_node(tree.list)
            .map(|n| n.computed_rect.height())
            .unwrap_or(0.0);
        let dpi = world.resources.window.cached_scale_factor.max(1.0);
        let logical_height = list_height / dpi;
        let mut visible_position = 0usize;
        let mut skip_until_depth: Option<usize> = None;
        let filter_active = !tree.filter_lower.is_empty();
        for (idx, row) in tree.all_rows.iter().enumerate() {
            if let Some(depth) = skip_until_depth {
                if row.depth > depth {
                    continue;
                } else {
                    skip_until_depth = None;
                }
            }
            let collapsed =
                !tree.cameras_only && row.has_children && !tree.expanded.contains(&row.entity.id);
            let matches_filter =
                !filter_active || row.label.to_lowercase().contains(&tree.filter_lower);
            if matches_filter {
                if idx == row_index {
                    break;
                }
                visible_position += 1;
            }
            if collapsed && !filter_active {
                skip_until_depth = Some(row.depth);
            }
        }
        let row_top = visible_position as f32 * 22.0;
        let row_bottom = row_top + 22.0;
        let current_offset = world
            .ui
            .get_ui_scroll_area(scroll_entity)
            .map(|d| d.scroll_offset)
            .unwrap_or(0.0);
        let mut new_offset = current_offset;
        if row_top < current_offset {
            new_offset = row_top;
        } else if row_bottom > current_offset + logical_height && logical_height > 0.0 {
            new_offset = row_bottom - logical_height;
        }
        if (new_offset - current_offset).abs() > 0.01 {
            ui_scroll_area_set_offset(world, scroll_entity, new_offset);
        }
    }
}

fn sync_outline(editor_world: &EditorWorld, world: &mut World) {
    crate::systems::selection::sync_to_engine(editor_world, world);
}

fn gizmo_drag_active(world: &World) -> bool {
    let gizmos = &world.resources.user_interface.gizmos;
    gizmos.translation_drag.is_some()
        || gizmos.scale_drag.is_some()
        || gizmos.rotation_drag.is_some()
        || gizmos.planar_scale_drag.is_some()
        || gizmos.planar_translation_drag.is_some()
        || gizmos.nav_gizmo_drag.is_some()
}

fn marquee_select(
    editor_world: &mut EditorWorld,
    world: &mut World,
    start: nalgebra_glm::Vec2,
    current: nalgebra_glm::Vec2,
) {
    let Some(camera_entity) = world.resources.active_camera else {
        return;
    };
    let Some(viewport) = world.resources.window.active_viewport_rect else {
        return;
    };
    let min_x = start.x.min(current.x);
    let max_x = start.x.max(current.x);
    let min_y = start.y.min(current.y);
    let max_y = start.y.max(current.y);

    let mut roots: Vec<Entity> = Vec::new();
    let candidates: Vec<Entity> = world
        .core
        .query_entities(
            nightshade::ecs::world::RENDER_MESH | nightshade::ecs::world::GLOBAL_TRANSFORM,
        )
        .collect();
    for entity in candidates {
        if editor_world.resources.editor_scene.is_scaffolding(entity) {
            continue;
        }
        let Some(global) = world.core.get_global_transform(entity) else {
            continue;
        };
        let world_position = global.translation();
        let Some(screen) = project_to_screen(
            world,
            camera_entity,
            world_position,
            viewport.x,
            viewport.y,
            viewport.width,
            viewport.height,
        ) else {
            continue;
        };
        if screen.x >= min_x && screen.x <= max_x && screen.y >= min_y && screen.y <= max_y {
            let root = find_group_root(world, entity);
            if !roots.contains(&root) {
                roots.push(root);
            }
        }
    }

    crate::systems::selection::set_many(editor_world, roots);

    world.resources.user_interface.gizmos.selection_marquee = Some(Rect {
        min: vec2(min_x, min_y),
        max: vec2(max_x, max_y),
    });
}

fn project_to_screen(
    world: &World,
    camera_entity: Entity,
    world_position: Vec3,
    viewport_x: f32,
    viewport_y: f32,
    viewport_width: f32,
    viewport_height: f32,
) -> Option<Vec2> {
    if viewport_height <= 0.0 {
        return None;
    }
    let camera = world.core.get_camera(camera_entity).copied()?;
    let camera_transform = world.core.get_global_transform(camera_entity)?;
    let view = camera_transform
        .0
        .try_inverse()
        .unwrap_or_else(Mat4::identity);
    let aspect = viewport_width / viewport_height;
    let view_projection = camera.projection.matrix_with_aspect(aspect) * view;
    let clip = view_projection
        * nalgebra_glm::Vec4::new(world_position.x, world_position.y, world_position.z, 1.0);
    if clip.w <= 1.0e-6 {
        return None;
    }
    let ndc_x = clip.x / clip.w;
    let ndc_y = clip.y / clip.w;
    let screen_x = viewport_x + (ndc_x * 0.5 + 0.5) * viewport_width;
    let screen_y = viewport_y + (1.0 - (ndc_y * 0.5 + 0.5)) * viewport_height;
    Some(vec2(screen_x, screen_y))
}