~starkingdoms/starkingdoms

fb51c894e9f4da525b26e922d494159d420d61c7 — ghostly_zsh a day ago 5ed17b2
feat: drill button text changes
2 files changed, 55 insertions(+), 19 deletions(-)

M crates/unified/src/client/crafting/ui.rs
M crates/unified/src/server/part.rs
M crates/unified/src/client/crafting/ui.rs => crates/unified/src/client/crafting/ui.rs +44 -17
@@ 1,6 1,6 @@
use bevy::{input_focus::{AutoFocus, InputFocus}, ui::RelativeCursorPosition};

use crate::{attachment::PartInShip, client::colors, ecs::{CanCraft, CraftingUi, MainCamera, Me, ToggleDrillEvent}, prelude::*};
use crate::{attachment::PartInShip, client::colors, ecs::{CanCraft, CraftingUi, Drill, MainCamera, Me, ToggleDrillEvent}, prelude::*};

pub fn crafting_ui_plugin(app: &mut App) {
    app.add_systems(Update, (close_button, drill_button));


@@ 19,6 19,7 @@ pub fn open_crafting_ui(
    hearty: Query<(Entity, &Transform), (With<Me>, With<CanCraft>)>,
    camera: Single<(Entity, &Camera, &GlobalTransform), (With<MainCamera>, Without<PartInShip>)>,
    commands: Commands,
    drills: Query<&Drill>,
) {
    if matches!(ev.button, PointerButton::Secondary) {
        let (entity, transform) = if let Ok(part) = crafting_parts.get(ev.entity) {


@@ 30,7 31,7 @@ pub fn open_crafting_ui(
        };
        // we have our crafting entity!
        // now make the ui
        setup_ui(entity, transform, commands, camera);
        setup_ui(entity, transform, commands, camera, drills);
    }
}



@@ 39,6 40,7 @@ fn setup_ui(
    parent_transform: &Transform,
    mut commands: Commands,
    camera: Single<(Entity, &Camera, &GlobalTransform), (With<MainCamera>, Without<PartInShip>)>,
    drills: Query<&Drill>,
) {
    let parent_pos = camera.1.world_to_viewport(camera.2, parent_transform.translation).unwrap();
    let entity = commands.spawn((


@@ 78,34 80,46 @@ fn setup_ui(
                Text::new("x"),
            ));
        });
        parent.spawn((
            Node {
                ..Default::default()
            },
            Button,
            DrillButton(parent_part),
            BackgroundColor(colors::CRUST),
            PreviousInteraction(Interaction::None),
        ))
        .with_children(|parent| {
        // only add the drill button if the part is a drill
        if drills.get(parent_part).is_ok() {
            parent.spawn((
                Node {
                    ..Default::default()
                },
                Text::new("Start Drill"),
            ));
        });
                Button,
                DrillButton(parent_part),
                BackgroundColor(colors::CRUST),
                PreviousInteraction(Interaction::None),
            ))
            .with_children(|parent| {
                parent.spawn((
                    Node {
                        ..Default::default()
                    },
                    Text::new("Start Drill"),
                ));
            });
        }
    });
}

fn drill_button(
    mut interaction_query: Query<
        (&Interaction, &mut PreviousInteraction, &mut BackgroundColor, &DrillButton, &mut Button),
        (
            &Interaction,
            &mut PreviousInteraction,
            &mut BackgroundColor,
            &DrillButton,
            &mut Button,
            &Children,
        ),
        Changed<Interaction>,
    >,
    mut toggle_drill_writer: MessageWriter<ToggleDrillEvent>,
    mut text_query: Query<&mut Text>,
    drills: Query<&Drill>,
) {
    for (interaction, mut previous_interaction, mut color, drill_button, mut button) in &mut interaction_query {
    for (interaction, mut previous_interaction, mut color, drill_button, mut button, children) in &mut interaction_query {
        match *interaction {
            Interaction::Pressed => {
                *color = colors::SURFACE_1.into();


@@ 114,6 128,19 @@ fn drill_button(
                *color = colors::SURFACE_0.into();
                if previous_interaction.0 == Interaction::Pressed {
                    // released
                    let mut text = text_query.get_mut(children[0]).unwrap();
                    let Ok(drill) = drills.get(drill_button.0) else {
                        error!("A former drill is now not a drill, causing a problem in the drill button");
                        previous_interaction.0 = *interaction;
                        return
                    };
                    // the text is flipped because drill.drilling is an old value,
                    // which was now toggled
                    if drill.drilling {
                        **text = "Start Drill".to_string();
                    } else {
                        **text = "Stop Drill".to_string();
                    }
                    toggle_drill_writer.write(ToggleDrillEvent { drill_entity: drill_button.0 });
                }
            }

M crates/unified/src/server/part.rs => crates/unified/src/server/part.rs +11 -2
@@ 1,6 1,6 @@
use crate::attachment::{Joint, JointId, JointOf, Joints, Peer, SnapOf, SnapOfJoint};
use crate::config::part::{CoolingConfig, CraftingConfig, JointConfig, PartConfig};
use crate::ecs::{CanCraft, Cooler, Part, PartHandle, Radiator, Temperature};
use crate::config::part::{CoolingConfig, CraftingConfig, DrillConfig, JointConfig, PartConfig};
use crate::ecs::{CanCraft, Cooler, Drill, Part, PartHandle, Radiator, Temperature};
use crate::prelude::*;
use bevy_replicon::prelude::Replicated;
use crate::ecs::thruster::{PartThrusters, Thruster, ThrusterBundle, ThrusterId, ThrusterOfPart};


@@ 38,6 38,9 @@ fn handle_ready_parts(
            if let Some(ref cooling_config) = strong_config.cooling {
                cooling_bundle(&mut commands.entity(entity), &cooling_config);
            }
            if let Some(ref drill_config) = strong_config.drill {
                drill_bundle(&mut commands.entity(entity), &drill_config);
            }
            spawn_joints(strong_config, entity, commands.reborrow());
            spawn_thrusters(strong_config, entity, commands.reborrow());
        }


@@ 153,6 156,12 @@ fn cooling_bundle(entity: &mut EntityCommands, config: &CoolingConfig) {
        heat_cooling_constant: config.heat_cooling_constant,
    });
}
fn drill_bundle(entity: &mut EntityCommands, config: &DrillConfig) {
    entity.insert(Drill {
        drilling: false,
        resource_multiplier: config.resource_multiplier,
    });
}
fn spawn_joint_bundle(joint: &JointConfig, part: &PartConfig, parent: &Entity) -> impl Bundle {
    let j_comp = Joint {
        id: JointId::from_part_and_joint_id(part.part.name.clone(), joint.id.clone()),