Skip to content

Commit bf5d2b2

Browse files
committed
better selection
1 parent 362d1f9 commit bf5d2b2

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/selection.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use bevy::prelude::*;
2+
use crate::chart::event::LineEvent;
3+
use crate::chart::note::Note;
24

35
use crate::project::project_loaded;
46

@@ -10,7 +12,7 @@ pub struct Selected;
1012

1113
/// Select a [Entity] in the world
1214
#[derive(Event)]
13-
pub struct SelectEvent(pub Entity);
15+
pub struct SelectEvent(pub Entity, pub bool);
1416

1517
pub struct SelectionPlugin;
1618

@@ -21,8 +23,39 @@ impl Plugin for SelectionPlugin {
2123
}
2224
}
2325

24-
pub fn handle_select_event(mut commands: Commands, mut select_events: EventReader<SelectEvent>) {
26+
pub fn handle_select_event(
27+
mut commands: Commands,
28+
mut select_events: EventReader<SelectEvent>,
29+
note_query: Query<&Note>,
30+
event_query: Query<&LineEvent>,
31+
32+
selected_notes_query: Query<Entity, (With<Selected>, With<Note>, Without<LineEvent>)>,
33+
selected_events_query: Query<Entity, (With<Selected>, With<LineEvent>, Without<Note>)>,
34+
35+
selected_notes_and_events_query: Query<Entity, (With<Selected>, Or<(With<Note>, With<LineEvent>)>)>,
36+
) {
2537
for event in select_events.read() {
38+
if event.1 {
39+
// unselect all notes and events
40+
for entity in &selected_notes_and_events_query {
41+
commands.entity(entity).remove::<Selected>();
42+
}
43+
} else {
44+
// selecting both notes and events is not allowed
45+
if note_query.get(event.0).is_ok() {
46+
// target is note, unselect all events
47+
for entity in &selected_events_query {
48+
commands.entity(entity).remove::<Selected>();
49+
}
50+
}
51+
if event_query.get(event.0).is_ok() {
52+
// target is event, unselect all notes
53+
for entity in &selected_notes_query {
54+
commands.entity(entity).remove::<Selected>();
55+
}
56+
}
57+
}
58+
2659
commands.entity(event.0).insert(Selected);
2760
}
2861
}

src/tab/timeline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub fn timeline_ui_system(
9999
)
100100
.clicked()
101101
{
102-
select_events.send(SelectEvent(entity));
102+
select_events.send(SelectEvent(entity, true));
103103
}
104104
}
105105

@@ -159,7 +159,7 @@ pub fn timeline_ui_system(
159159
);
160160

161161
if response.clicked() {
162-
select_events.send(SelectEvent(entity));
162+
select_events.send(SelectEvent(entity, true));
163163
}
164164
}
165165

0 commit comments

Comments
 (0)