#need to move Text2DBundle sections separately, or separate them entirely, but unsure how

2 messages · Page 1 of 1 (latest)

small shadow
#

hey guys, i have two text sections within a Text2DBundle and ** i'd like to move them individually.** ideally i'd have them as separate bundles but i'm unsure of how to go about that. in my program, the player inputs text which is displayed, and then an output based on this input is displayed which is apart of a different section. here is my relevant code:

// startup function which initalises bevy elements
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    // initialise bevy 2D camera
    commands.spawn(Camera2dBundle::default());

    commands.spawn(
        TextBundle::from_sections([
            TextSection {
                value: "[ALPHA]".to_string(),
                style: TextStyle {
                    font: asset_server.load("fonts/modern_dos_8x8.ttf"),
                    font_size: 9.0,
                    color: Color::PINK,
                },
            },
        ])
        .with_style(Style {
            position_type: PositionType::Absolute,
            ..default()
        }),
    );
    // ! these are the sections where the input and output are displayed
    commands.spawn(Text2dBundle {
        text: Text {
            sections: vec![
                // input
                TextSection {
                    value: "".to_string(),
                    style: TextStyle {
                        font: asset_server.load("fonts/win_95.otf"),
                        font_size: 20.0,
                        color: Color::ANTIQUE_WHITE,
                    },
                },
                // output
                TextSection {
                    value: "".to_string(),
                    style: TextStyle {
                        font: asset_server.load("fonts/win_95.otf"),
                        font_size: 20.0,
                        color: Color::YELLOW,
                    },
                },
            ],
            ..Default::default()
        },
        ..Default::default()
    });
}

// input handling function for core gameplay loop
fn text_input(
    kbd: Res<Input<KeyCode>>,
    mut evr_char: EventReader<ReceivedCharacter>,
    mut input: Local<String>,
    mut display_text: Query<&mut Text, Without<Node>>,
    mut player_data: ResMut<PlayerData>,
    mut json_data: ResMut<JsonData>) {  
        // send input data to Text2DBundle to be displayed
        display_text.single_mut().sections[0].value = input.clone();

        // submit input to be interpreted
        if kbd.just_pressed(KeyCode::Return) {
            let output = interpret_input(&mut input, &mut player_data, &mut json_data.json_value);

            println!("[DEBUG] {}", &*input);
            println!("[DEBUG] {}", output);
            
            input.clear();
            
            // send output data to Text2DBundle to be displayed
            display_text.single_mut().sections[1].value = output.clone();

        }

        // backspace handling
        if kbd.just_pressed(KeyCode::Back) {
            input.pop();
        }

        // handle keyboard input
        for ev in evr_char.iter() {
            // ignore control (special) characters
            if !ev.char.is_control() {
                input.push(ev.char);
            }
        }
}

if anyone could lend a hand i'd appreciate huge, i'm assuming there is some sort of way to separate them into different bundles so i can move them but from searching i haven't been able to find anything. thanks!

#

input is white and output is yellow for that image