bevy-yoleck icon indicating copy to clipboard operation
bevy-yoleck copied to clipboard

Exclusive input mode for edit systems

Open idanarye opened this issue 2 years ago • 2 comments

Example usecase - #21. When an entity refers to another entity, we want the first entity to click the second entity, we want to be able to do this by editing the target entity, doing something to indicate that we want to pick an entity to refer to, and then click on the other entity.

For this, we need to be able to send an edit system to an "exclusive input mode". In that mode:

  • Other edit systems don't run.
  • Yoleck ignores directives.
    • Actually, there is one directive it won't ignore - a directive for cancelling the exclusive mode.
    • Vpeol should send that directive when Escape is pressed.
  • The exclusive edit system handles the clicks (how? should it get the directives themselves?)

idanarye avatar Apr 13 '23 14:04 idanarye

I imagine the syntax as something like this:

fn edit_system_with_exclusive_mode(
    mut ui: ResMut<YoleckUi>,
    mut edit: YoleckEdit<&mut OtherEntityUuid>,
    mut exclusive: YoleckExclusive, // this captures the system (like `YoleckMarking` does)
    uuid_query: Query<&VpeolUuid>,
) {
    let Ok(mut other_entity_uuid) = edit.get_single_mut() else { return };

    if let Some(exclusive) = exclusive.exclusive() {
        // Or maybe this should use passed-data?
        if let Some(clicked_entity) = exclusive.get_clicked() {
            if let Ok(uuid) = uuid_query.get(clicked_entity) {
                other_entity_uuid.0 = uuid.0;
                exclusive.finish();
            }
        }
    } else {
        if ui.button("Pick Other Entity").clicked() {
            exclusive.start();
        }
    }
}

idanarye avatar Apr 13 '23 14:04 idanarye

Alternatively, maybe this can be combined with #20's mechanism?

fn edit_system_that_initiates_the_exclusive_mode(
    mut ui: ResMut<YoleckUi>,
    mut edit: YoleckEdit<With<&OtherEntityUuid>>,
    mut exclusive: YoleckExclusive,
    uuid_query: Query<&VpeolUuid>,
) {
    let Ok(_) = edit.get_single_mut() else { return };

    if ui.button("Pick Other Entity").clicked() {
        exclusive.enqueue(edit_system_for_the_exclusive_mode);
    }
}

fn edit_system_for_the_exclusive_mode(
    mut exclusive_input: Res<YoleckExclusiveInput>,
    mut edit: YoleckEdit<&mut OtherEntityUuid>,
) -> Option<YoleckExclusiveVeredict> {
    let mut other_entity_uuid = edit.get_single_mut()?;
    if let Some(clicked_entity) = exclusive_input.get_clicked() {
        if let Ok(uuid) = uuid_query.get(clicked_entity) {
            other_entity_uuid.0 = uuid.0;
            return Some(YoleckExclusiveVeredict::Done);
        }
    }
    None
}

idanarye avatar Jun 06 '23 13:06 idanarye