Skip to content

Commit

Permalink
Add shortcuts for undo
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Dec 11, 2024
1 parent 7f6f05d commit db57f1a
Showing 1 changed file with 54 additions and 13 deletions.
67 changes: 54 additions & 13 deletions ui/src/hud/tools_node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::*;
use bevy_enhanced_input::prelude::*;
use strum::{EnumIter, IntoEnumIterator};

use project_harmonia_base::game_world::{
Expand All @@ -10,15 +11,26 @@ pub(super) struct ToolsNodePlugin;

impl Plugin for ToolsNodePlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
Self::apply_history_action
.run_if(in_state(FamilyMode::Building).or_else(in_state(WorldState::City))),
);
app.add_input_context::<ToolsNode>()
.observe(Self::undo)
.observe(Self::redo)
.add_systems(
Update,
Self::apply_history_action
.run_if(in_state(FamilyMode::Building).or_else(in_state(WorldState::City))),
);
}
}

impl ToolsNodePlugin {
fn undo(_trigger: Trigger<Fired<Undo>>, mut history: CommandsHistory) {
history.undo();
}

fn redo(_trigger: Trigger<Fired<Redo>>, mut history: CommandsHistory) {
history.redo();
}

fn apply_history_action(
mut history: CommandsHistory,
mut click_events: EventReader<Click>,
Expand All @@ -35,16 +47,19 @@ impl ToolsNodePlugin {

pub(super) fn setup(parent: &mut ChildBuilder, theme: &Theme) {
parent
.spawn(NodeBundle {
style: Style {
position_type: PositionType::Absolute,
left: Val::Percent(50.0),
padding: theme.padding.normal,
.spawn((
NodeBundle {
style: Style {
position_type: PositionType::Absolute,
left: Val::Percent(50.0),
padding: theme.padding.normal,
..Default::default()
},
background_color: theme.panel_color.into(),
..Default::default()
},
background_color: theme.panel_color.into(),
..Default::default()
})
ToolsNode,
))
.with_children(|parent| {
for button in HistoryButton::iter() {
parent.spawn((button, TextButtonBundle::symbol(theme, button.glyph())));
Expand All @@ -66,3 +81,29 @@ impl HistoryButton {
}
}
}

#[derive(Component)]
struct ToolsNode;

impl InputContext for ToolsNode {
fn context_instance(_world: &World, _entity: Entity) -> ContextInstance {
let mut ctx = ContextInstance::default();

ctx.bind::<Redo>()
.to(KeyCode::KeyZ.with_mod_keys(ModKeys::CONTROL | ModKeys::SHIFT))
.to(GamepadButtonType::RightThumb);
ctx.bind::<Undo>()
.to(KeyCode::KeyZ.with_mod_keys(ModKeys::CONTROL))
.to(GamepadButtonType::LeftThumb);

ctx
}
}

#[derive(Component, InputAction, Debug)]
#[input_action(output = bool)]
struct Undo;

#[derive(Component, InputAction, Debug)]
#[input_action(output = bool)]
struct Redo;

0 comments on commit db57f1a

Please sign in to comment.