Skip to content

Commit

Permalink
Merge pull request #227 from janhohenheim/fix-dialog-choice
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim authored Mar 1, 2023
2 parents b32bd77 + 3fc3aad commit 70318b7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
54 changes: 42 additions & 12 deletions src/player_control/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,45 @@ impl Plugin for ActionsPlugin {
}
}

#[derive(Debug, Clone, Actionlike, Reflect, FromReflect, Default)]
#[derive(Debug, Clone, Copy, Actionlike, Reflect, FromReflect, Default)]
pub enum PlayerAction {
#[default]
Move,
Sprint,
Jump,
Interact,
SpeedUpDialog,
NumberedChoice(u16),
NumberedChoice1,
NumberedChoice2,
NumberedChoice3,
NumberedChoice4,
NumberedChoice5,
NumberedChoice6,
NumberedChoice7,
NumberedChoice8,
NumberedChoice9,
NumberedChoice0,
}

impl PlayerAction {
pub fn numbered_choice(index: u8) -> Self {
match index {
0 => PlayerAction::NumberedChoice0,
1 => PlayerAction::NumberedChoice1,
2 => PlayerAction::NumberedChoice2,
3 => PlayerAction::NumberedChoice3,
4 => PlayerAction::NumberedChoice4,
5 => PlayerAction::NumberedChoice5,
6 => PlayerAction::NumberedChoice6,
7 => PlayerAction::NumberedChoice7,
8 => PlayerAction::NumberedChoice8,
9 => PlayerAction::NumberedChoice9,
_ => panic!(
"Numbered choice index out of range: got {}, expected 0-9",
index
),
}
}
}

#[derive(Debug, Clone, Actionlike, Reflect, FromReflect, Default)]
Expand All @@ -75,16 +105,16 @@ pub fn create_player_action_input_manager_bundle() -> InputManagerBundle<PlayerA
(QwertyScanCode::LShift, PlayerAction::Sprint),
(QwertyScanCode::E, PlayerAction::Interact),
(QwertyScanCode::Space, PlayerAction::SpeedUpDialog),
(QwertyScanCode::Key1, PlayerAction::NumberedChoice(1)),
(QwertyScanCode::Key2, PlayerAction::NumberedChoice(2)),
(QwertyScanCode::Key3, PlayerAction::NumberedChoice(3)),
(QwertyScanCode::Key4, PlayerAction::NumberedChoice(4)),
(QwertyScanCode::Key5, PlayerAction::NumberedChoice(5)),
(QwertyScanCode::Key6, PlayerAction::NumberedChoice(6)),
(QwertyScanCode::Key7, PlayerAction::NumberedChoice(7)),
(QwertyScanCode::Key8, PlayerAction::NumberedChoice(8)),
(QwertyScanCode::Key9, PlayerAction::NumberedChoice(9)),
(QwertyScanCode::Key0, PlayerAction::NumberedChoice(0)),
(QwertyScanCode::Key1, PlayerAction::NumberedChoice1),
(QwertyScanCode::Key2, PlayerAction::NumberedChoice2),
(QwertyScanCode::Key3, PlayerAction::NumberedChoice3),
(QwertyScanCode::Key4, PlayerAction::NumberedChoice4),
(QwertyScanCode::Key5, PlayerAction::NumberedChoice5),
(QwertyScanCode::Key6, PlayerAction::NumberedChoice6),
(QwertyScanCode::Key7, PlayerAction::NumberedChoice7),
(QwertyScanCode::Key8, PlayerAction::NumberedChoice8),
(QwertyScanCode::Key9, PlayerAction::NumberedChoice9),
(QwertyScanCode::Key0, PlayerAction::NumberedChoice0),
])
.insert(VirtualDPad::wasd(), PlayerAction::Move)
.build(),
Expand Down
12 changes: 8 additions & 4 deletions src/world_interaction/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fn present_choices(
match next_page {
NextPage::Continue(next_page_id) => {
let text = create_choice_rich_text(0, "Continue");
if ui.button(text).clicked() || actions.just_pressed(PlayerAction::NumberedChoice(1)) {
if ui.button(text).clicked() || actions.just_pressed(PlayerAction::numbered_choice(1)) {
current_dialog.current_page = next_page_id;
*elapsed_time = 0.0;
}
Expand All @@ -196,9 +196,13 @@ fn present_choices(
.enumerate()
{
let text = create_choice_rich_text(index, &choice.text);
if ui.button(text).clicked()
|| actions.just_pressed(PlayerAction::NumberedChoice(index as u16 + 1))
if ui.button(&text).clicked()
|| actions.just_pressed(PlayerAction::numbered_choice(index as u8 + 1))
{
info!("Picked choice: {:?}", choice_id);
info!("Text: {:?}", text);
info!("Choice: {:?}", choice);
info!("Index: {:?}", index);
picked_choice = Some((choice_id.clone(), choice.clone()));
}
}
Expand All @@ -225,7 +229,7 @@ fn present_choices(
}
NextPage::Exit => {
let text = create_choice_rich_text(0, "Exit");
if ui.button(text).clicked() || actions.just_pressed(PlayerAction::NumberedChoice(1)) {
if ui.button(text).clicked() || actions.just_pressed(PlayerAction::numbered_choice(1)) {
commands.remove_resource::<CurrentDialog>();
actions_frozen.unfreeze();
}
Expand Down

0 comments on commit 70318b7

Please sign in to comment.