Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hotbar slot switching + fix closing keybind #329

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/render/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,21 +458,19 @@ impl Screen for Hud {
.as_ref()
.unwrap()
.clone()
.write_packet(HeldItemChange {
slot: new_slot as i16,
});
.inventory_context
.write()
.hotbar_index = new_slot;
self.hud_context.write().slot_index = new_slot;
self.hud_context.write().dirty_slot_index = true;
self.hud_context
.read()
.server
.as_ref()
.unwrap()
.clone()
.inventory_context
.clone()
.write()
.hotbar_index = new_slot;
self.hud_context.clone().write().slot_index = new_slot;
self.hud_context.clone().write().dirty_slot_index = true;
.write_packet(HeldItemChange {
slot: new_slot as i16,
});
}

fn on_resize(
Expand Down
12 changes: 8 additions & 4 deletions src/render/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::render::hud::Hud;
use crate::render::Renderer;
use crate::screen::{Screen, ScreenSystem};
use crate::ui::{Container, ImageRef, TextBoxRef, TextRef, VAttach};
use crate::{ui, Game, KeyCmp};
use crate::{ui, Actionkey, Game};
use parking_lot::RwLock;
use std::sync::Arc;
use winit::keyboard::{Key, NamedKey, PhysicalKey};
Expand Down Expand Up @@ -99,12 +99,16 @@ impl Screen for InventoryWindow {
}

fn on_key_press(&mut self, key: (Key, PhysicalKey), down: bool, game: &mut Game) {
let is_e = key.0.eq_ignore_case('e');
if is_e && !down && !self.initial_release {
let is_inv = if let PhysicalKey::Code(code) = key.1 {
game.keybinds.get(code, &key.0).map(|kb| kb.action) == Some(Actionkey::OpenInv)
} else {
false
};
if is_inv && !down && !self.initial_release {
self.initial_release = true;
return;
}
if (key.0 == Key::Named(NamedKey::Escape) || is_e) && !down {
if (key.0 == Key::Named(NamedKey::Escape) || is_inv) && !down {
self.inventory_context
.write()
.try_close_inventory(&game.screen_sys);
Expand Down
64 changes: 64 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use leafish_protocol::item::Stack;
use leafish_protocol::protocol::login::Account;
use leafish_protocol::protocol::mapped_packet::MappablePacket;
use leafish_protocol::protocol::mapped_packet::MappedPacket;
use leafish_protocol::protocol::packet::play::serverbound::HeldItemChange;
use leafish_protocol::protocol::packet::Hand;
use leafish_protocol::protocol::Conn;
use log::{debug, error, info, warn};
Expand Down Expand Up @@ -1287,6 +1288,69 @@ impl Server {
return true;
}
}
Actionkey::Hotbar1 => {
if down {
self.inventory_context.write().hotbar_index = 0;
self.hud_context.write().update_slot_index(0);
self.write_packet(HeldItemChange { slot: 0 });
}
}
Actionkey::Hotbar2 => {
if down {
self.inventory_context.write().hotbar_index = 1;
self.hud_context.write().update_slot_index(1);
self.write_packet(HeldItemChange { slot: 1 });
}
}
Actionkey::Hotbar3 => {
if down {
self.inventory_context.write().hotbar_index = 2;
self.hud_context.write().update_slot_index(2);
self.write_packet(HeldItemChange { slot: 2 });
}
}
Actionkey::Hotbar4 => {
if down {
self.inventory_context.write().hotbar_index = 3;
self.hud_context.write().update_slot_index(3);
self.write_packet(HeldItemChange { slot: 3 });
}
}
Actionkey::Hotbar5 => {
if down {
self.inventory_context.write().hotbar_index = 4;
self.hud_context.write().update_slot_index(4);
self.write_packet(HeldItemChange { slot: 4 });
}
}
Actionkey::Hotbar6 => {
if down {
self.inventory_context.write().hotbar_index = 5;
self.hud_context.write().update_slot_index(5);
self.write_packet(HeldItemChange { slot: 5 })
}
}
Actionkey::Hotbar7 => {
if down {
self.inventory_context.write().hotbar_index = 6;
self.hud_context.write().update_slot_index(6);
self.write_packet(HeldItemChange { slot: 6 });
}
}
Actionkey::Hotbar8 => {
if down {
self.inventory_context.write().hotbar_index = 7;
self.hud_context.write().update_slot_index(7);
self.write_packet(HeldItemChange { slot: 7 });
}
}
Actionkey::Hotbar9 => {
if down {
self.inventory_context.write().hotbar_index = 8;
self.hud_context.write().update_slot_index(8);
self.write_packet(HeldItemChange { slot: 8 });
}
}
_ => {}
};
}
Expand Down
72 changes: 72 additions & 0 deletions src/settings/default_keybinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,77 @@ pub fn create_keybinds() -> Vec<(Key, Keybind)> {
action: Actionkey::ToggleChat,
},
),
(
Key::Character(SmolStr::new_inline("1")),
Keybind {
name: "keybind_hotbar_1",
description: "Keybind for selecting slot 1 of the hotbar",
action: Actionkey::Hotbar1,
},
),
(
Key::Character(SmolStr::new_inline("2")),
Keybind {
name: "keybind_hotbar_2",
description: "Keybind for selecting slot 2 of the hotbar",
action: Actionkey::Hotbar2,
},
),
(
Key::Character(SmolStr::new_inline("3")),
Keybind {
name: "keybind_hotbar_3",
description: "Keybind for selecting slot 3 of the hotbar",
action: Actionkey::Hotbar3,
},
),
(
Key::Character(SmolStr::new_inline("4")),
Keybind {
name: "keybind_hotbar_4",
description: "Keybind for selecting slot 4 of the hotbar",
action: Actionkey::Hotbar4,
},
),
(
Key::Character(SmolStr::new_inline("5")),
Keybind {
name: "keybind_hotbar_5",
description: "Keybind for selecting slot 5 of the hotbar",
action: Actionkey::Hotbar5,
},
),
(
Key::Character(SmolStr::new_inline("6")),
Keybind {
name: "keybind_hotbar_6",
description: "Keybind for selecting slot 6 of the hotbar",
action: Actionkey::Hotbar6,
},
),
(
Key::Character(SmolStr::new_inline("7")),
Keybind {
name: "keybind_hotbar_7",
description: "Keybind for selecting slot 7 of the hotbar",
action: Actionkey::Hotbar7,
},
),
(
Key::Character(SmolStr::new_inline("8")),
Keybind {
name: "keybind_hotbar_8",
description: "Keybind for selecting slot 8 of the hotbar",
action: Actionkey::Hotbar8,
},
),
(
Key::Character(SmolStr::new_inline("9")),
Keybind {
name: "keybind_hotbar_9",
description: "Keybind for selecting slot 9 of the hotbar",
action: Actionkey::Hotbar9,
},
),
]
}
29 changes: 28 additions & 1 deletion src/settings/keybinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ pub enum Actionkey {
ToggleHud,
ToggleDebug,
ToggleChat,
Hotbar1,
Hotbar2,
Hotbar3,
Hotbar4,
Hotbar5,
Hotbar6,
Hotbar7,
Hotbar8,
Hotbar9,
}

impl FromStr for Actionkey {
Expand All @@ -198,13 +207,22 @@ impl FromStr for Actionkey {
"keybind_toggle_hud" => Ok(Actionkey::ToggleHud),
"keybind_toggle_debug_info" => Ok(Actionkey::ToggleDebug),
"keybind_toggle_chat" => Ok(Actionkey::ToggleChat),
"keybind_hotbar_1" => Ok(Actionkey::Hotbar1),
"keybind_hotbar_2" => Ok(Actionkey::Hotbar2),
"keybind_hotbar_3" => Ok(Actionkey::Hotbar3),
"keybind_hotbar_4" => Ok(Actionkey::Hotbar4),
"keybind_hotbar_5" => Ok(Actionkey::Hotbar5),
"keybind_hotbar_6" => Ok(Actionkey::Hotbar6),
"keybind_hotbar_7" => Ok(Actionkey::Hotbar7),
"keybind_hotbar_8" => Ok(Actionkey::Hotbar8),
"keybind_hotbar_9" => Ok(Actionkey::Hotbar9),
_ => Err(()),
}
}
}

impl Actionkey {
const VALUES: [Actionkey; 11] = [
const VALUES: [Actionkey; 20] = [
Actionkey::Forward,
Actionkey::Backward,
Actionkey::Left,
Expand All @@ -216,6 +234,15 @@ impl Actionkey {
Actionkey::ToggleHud,
Actionkey::ToggleDebug,
Actionkey::ToggleChat,
Actionkey::Hotbar1,
Actionkey::Hotbar2,
Actionkey::Hotbar3,
Actionkey::Hotbar4,
Actionkey::Hotbar5,
Actionkey::Hotbar6,
Actionkey::Hotbar7,
Actionkey::Hotbar8,
Actionkey::Hotbar9,
];

pub fn values() -> &'static [Actionkey] {
Expand Down
Loading