Compare commits

..

No commits in common. "9504eb7a88c07ca0705a476b2ed97e9a2c6faa82" and "58a3b74d2b4c0b18e726515404888f194b5c1f0e" have entirely different histories.

3 changed files with 9 additions and 237 deletions

View File

@ -2,10 +2,9 @@
[ext_resource type="PackedScene" uid="uid://cqiwdajl71ujv" path="res://scenes/ui/slider.tscn" id="1_8wfqe"] [ext_resource type="PackedScene" uid="uid://cqiwdajl71ujv" path="res://scenes/ui/slider.tscn" id="1_8wfqe"]
[node name="settings" type="SettingsPanel" unique_id=999912383 node_paths=PackedStringArray("general_grid", "volume_grid", "keybinds_grid")] [node name="settings" type="SettingsPanel" unique_id=999912383 node_paths=PackedStringArray("general_grid", "volume_grid")]
general_grid = NodePath("v_box_container/tab_container/general") general_grid = NodePath("v_box_container/tab_container/general")
volume_grid = NodePath("v_box_container/tab_container/volume") volume_grid = NodePath("v_box_container/tab_container/volume")
keybinds_grid = NodePath("v_box_container/tab_container/keybinds")
slider_scene = ExtResource("1_8wfqe") slider_scene = ExtResource("1_8wfqe")
anchors_preset = 8 anchors_preset = 8
anchor_left = 0.5 anchor_left = 0.5
@ -20,7 +19,6 @@ grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
[node name="v_box_container" type="VBoxContainer" parent="." unique_id=1384128810] [node name="v_box_container" type="VBoxContainer" parent="." unique_id=1384128810]
custom_minimum_size = Vector2(500, 500)
layout_mode = 2 layout_mode = 2
[node name="label" type="Label" parent="v_box_container" unique_id=2036746921] [node name="label" type="Label" parent="v_box_container" unique_id=2036746921]
@ -34,22 +32,20 @@ size_flags_vertical = 3
current_tab = 0 current_tab = 0
[node name="general" type="GridContainer" parent="v_box_container/tab_container" unique_id=833231169] [node name="general" type="GridContainer" parent="v_box_container/tab_container" unique_id=833231169]
custom_minimum_size = Vector2(400, 0)
custom_maximum_size = Vector2(400, -1)
layout_mode = 2 layout_mode = 2
columns = 2 columns = 2
metadata/_tab_index = 0 metadata/_tab_index = 0
[node name="volume" type="GridContainer" parent="v_box_container/tab_container" unique_id=1992916365] [node name="volume" type="GridContainer" parent="v_box_container/tab_container" unique_id=1992916365]
visible = false visible = false
custom_minimum_size = Vector2(400, 0)
custom_maximum_size = Vector2(400, -1)
layout_mode = 2 layout_mode = 2
columns = 2 columns = 2
metadata/_tab_index = 1 metadata/_tab_index = 1
[node name="keybinds" type="GridContainer" parent="v_box_container/tab_container" unique_id=852698711]
visible = false
layout_mode = 2
columns = 2
metadata/_tab_index = 2
[node name="button" type="Button" parent="v_box_container" unique_id=1301469030] [node name="button" type="Button" parent="v_box_container" unique_id=1301469030]
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 4 size_flags_horizontal = 4

View File

@ -4,11 +4,7 @@ use std::{
}; };
use godot::{ use godot::{
classes::{ classes::{AudioServer, FileAccess, file_access::ModeFlags},
AudioServer, FileAccess, InputEvent, InputEventKey, InputEventMouse, InputEventMouseButton,
InputMap, file_access::ModeFlags,
},
global::{Key, MouseButton},
prelude::*, prelude::*,
tools::get_autoload_by_name, tools::get_autoload_by_name,
}; };
@ -27,7 +23,6 @@ pub struct GameSettings {
pub default_name: String, pub default_name: String,
pub recent_servers: Vec<RecentServer>, pub recent_servers: Vec<RecentServer>,
pub volume: VolumeSettings, pub volume: VolumeSettings,
pub keybinds: KeybindSettings,
} }
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
@ -38,61 +33,6 @@ pub struct VolumeSettings {
pub announcer_volume: f64, pub announcer_volume: f64,
} }
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct KeybindSettings {
pub move_left: Keybind,
pub move_right: Keybind,
pub move_forward: Keybind,
pub move_backward: Keybind,
pub select_raygun: Keybind,
pub select_shotgun: Keybind,
pub shoot: Keybind,
pub melee: Keybind,
pub jump: Keybind,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Keybind {
Key(i32),
MouseButton(i32),
}
impl ToString for Keybind {
fn to_string(&self) -> String {
match self {
Keybind::Key(value) => format!("{:?}", Key::from_godot(*value)),
Keybind::MouseButton(value) => format!("{:?}", MouseButton::from_godot(*value)),
}
}
}
impl Keybind {
pub fn from_event(event: Gd<InputEvent>) -> Option<Keybind> {
if let Ok(key) = event.clone().try_cast::<InputEventKey>() {
Some(Keybind::Key(key.get_keycode().to_godot()))
} else if let Ok(mouse) = event.clone().try_cast::<InputEventMouseButton>() {
Some(Keybind::MouseButton(mouse.get_button_index().to_godot()))
} else {
None
}
}
pub fn into_event(&self) -> Gd<InputEvent> {
match self {
Keybind::Key(key) => {
let mut event = InputEventKey::new_gd();
event.set_keycode(Key::from_godot(*key));
event.upcast()
}
Keybind::MouseButton(mb) => {
let mut event = InputEventMouseButton::new_gd();
event.set_button_index(MouseButton::from_godot(*mb));
event.upcast()
}
}
}
}
impl VolumeSettings { impl VolumeSettings {
pub fn master(&self) -> f32 { pub fn master(&self) -> f32 {
(self.master_volume / 100.) as f32 (self.master_volume / 100.) as f32
@ -129,17 +69,6 @@ impl Default for GameSettings {
music_volume: 100., music_volume: 100.,
announcer_volume: 100., announcer_volume: 100.,
}, },
keybinds: KeybindSettings {
move_left: Keybind::Key(Key::A.to_godot()),
move_right: Keybind::Key(Key::D.to_godot()),
move_forward: Keybind::Key(Key::W.to_godot()),
move_backward: Keybind::Key(Key::S.to_godot()),
select_raygun: Keybind::Key(Key::KEY_1.to_godot()),
select_shotgun: Keybind::Key(Key::KEY_2.to_godot()),
shoot: Keybind::MouseButton(MouseButton::LEFT.to_godot()),
melee: Keybind::Key(Key::F.to_godot()),
jump: Keybind::Key(Key::SPACE.to_godot()),
},
} }
} }
} }
@ -179,16 +108,6 @@ impl GameSettingsManager {
set_bus_volume(MUSIC_BUS, settings.volume.music()); set_bus_volume(MUSIC_BUS, settings.volume.music());
set_bus_volume(ANNOUNCER_BUS, settings.volume.announcer()); set_bus_volume(ANNOUNCER_BUS, settings.volume.announcer());
set_keybind("right", settings.keybinds.move_right);
set_keybind("left", settings.keybinds.move_left);
set_keybind("forward", settings.keybinds.move_forward);
set_keybind("backward", settings.keybinds.move_backward);
set_keybind("weapon_1", settings.keybinds.select_raygun);
set_keybind("weapon_2", settings.keybinds.select_shotgun);
set_keybind("shoot", settings.keybinds.shoot);
set_keybind("punch", settings.keybinds.melee);
set_keybind("jump", settings.keybinds.jump);
self.settings = settings; self.settings = settings;
if let Some(mut file) = FileAccess::open("user://config.toml", ModeFlags::WRITE) { if let Some(mut file) = FileAccess::open("user://config.toml", ModeFlags::WRITE) {
if let Ok(serialized) = toml::to_string(&self.settings) { if let Ok(serialized) = toml::to_string(&self.settings) {
@ -238,8 +157,3 @@ fn set_bus_volume(bus: &str, volume: f32) {
let idx = AudioServer::singleton().get_bus_index(bus); let idx = AudioServer::singleton().get_bus_index(bus);
AudioServer::singleton().set_bus_volume_linear(idx, volume); AudioServer::singleton().set_bus_volume_linear(idx, volume);
} }
fn set_keybind(action: &str, keybind: Keybind) {
InputMap::singleton().action_erase_events(action);
InputMap::singleton().action_add_event(action, &keybind.into_event());
}

View File

@ -2,46 +2,27 @@ use std::collections::HashMap;
use godot::{ use godot::{
classes::{ classes::{
Button, Control, GridContainer, IPanelContainer, InputEvent, InputEventKey, Control, GridContainer, IPanelContainer, Label, LineEdit, PanelContainer,
InputEventMouseButton, InputMap, Label, LineEdit, PanelContainer, control::SizeFlags,
control::{FocusBehaviorRecursive, FocusMode, SizeFlags},
}, },
prelude::*, prelude::*,
}; };
use crate::{ use crate::{game_settings::GameSettingsManager, ui::slider::SliderContainer};
game_settings::{GameSettingsManager, Keybind},
ui::slider::SliderContainer,
};
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash)]
pub enum Setting { pub enum Setting {
// General settings
FoV, FoV,
DefaultName, DefaultName,
// Volume settings
MasterVolume, MasterVolume,
SfxVolume, SfxVolume,
MusicVolume, MusicVolume,
AnnouncerVolume, AnnouncerVolume,
// Keybinds
MoveLeft,
MoveForward,
MoveRight,
MoveBackward,
Shoot,
Melee,
Jump,
SelectRaygun,
SelectShotgun,
} }
pub enum SettingsTab { pub enum SettingsTab {
General, General,
Volume, Volume,
Keybinds,
} }
impl ToString for Setting { impl ToString for Setting {
@ -49,21 +30,10 @@ impl ToString for Setting {
match self { match self {
Setting::FoV => "Field of View", Setting::FoV => "Field of View",
Setting::DefaultName => "Default Name", Setting::DefaultName => "Default Name",
Setting::MasterVolume => "Master volume", Setting::MasterVolume => "Master volume",
Setting::SfxVolume => "Sfx Volume", Setting::SfxVolume => "Sfx Volume",
Setting::MusicVolume => "Music Volume", Setting::MusicVolume => "Music Volume",
Setting::AnnouncerVolume => "Announcer Volume", Setting::AnnouncerVolume => "Announcer Volume",
Setting::MoveLeft => "Move left",
Setting::MoveRight => "Move right",
Setting::MoveForward => "Move forward",
Setting::MoveBackward => "Move backward",
Setting::SelectRaygun => "Select Raygun",
Setting::SelectShotgun => "Select Shotgun",
Setting::Shoot => "Shoot",
Setting::Melee => "Melee",
Setting::Jump => "Jump",
} }
.to_string() .to_string()
} }
@ -74,13 +44,10 @@ impl Setting {
match self { match self {
Setting::FoV => SettingsTab::General, Setting::FoV => SettingsTab::General,
Setting::DefaultName => SettingsTab::General, Setting::DefaultName => SettingsTab::General,
Setting::MasterVolume => SettingsTab::Volume, Setting::MasterVolume => SettingsTab::Volume,
Setting::SfxVolume => SettingsTab::Volume, Setting::SfxVolume => SettingsTab::Volume,
Setting::MusicVolume => SettingsTab::Volume, Setting::MusicVolume => SettingsTab::Volume,
Setting::AnnouncerVolume => SettingsTab::Volume, Setting::AnnouncerVolume => SettingsTab::Volume,
_ => SettingsTab::Keybinds,
} }
} }
} }
@ -89,7 +56,6 @@ impl Setting {
pub enum SettingValue { pub enum SettingValue {
SliderNumber(SliderValue), SliderNumber(SliderValue),
String(String), String(String),
Keybind(Keybind),
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -107,14 +73,10 @@ pub struct SettingsPanel {
#[export] #[export]
volume_grid: Option<Gd<GridContainer>>, volume_grid: Option<Gd<GridContainer>>,
#[export] #[export]
keybinds_grid: Option<Gd<GridContainer>>,
#[export]
slider_scene: Option<Gd<PackedScene>>, slider_scene: Option<Gd<PackedScene>>,
settings: HashMap<Setting, SettingValue>, settings: HashMap<Setting, SettingValue>,
keybind_button: Option<(Setting, Gd<Button>)>,
base: Base<PanelContainer>, base: Base<PanelContainer>,
} }
@ -168,41 +130,6 @@ impl IPanelContainer for SettingsPanel {
}), }),
); );
self.settings.insert(
Setting::MoveForward,
SettingValue::Keybind(settings.keybinds.move_forward),
);
self.settings.insert(
Setting::MoveBackward,
SettingValue::Keybind(settings.keybinds.move_backward),
);
self.settings.insert(
Setting::MoveLeft,
SettingValue::Keybind(settings.keybinds.move_left),
);
self.settings.insert(
Setting::MoveRight,
SettingValue::Keybind(settings.keybinds.move_right),
);
self.settings.insert(
Setting::SelectRaygun,
SettingValue::Keybind(settings.keybinds.select_raygun),
);
self.settings.insert(
Setting::SelectShotgun,
SettingValue::Keybind(settings.keybinds.select_shotgun),
);
self.settings.insert(
Setting::Shoot,
SettingValue::Keybind(settings.keybinds.shoot),
);
self.settings.insert(
Setting::Melee,
SettingValue::Keybind(settings.keybinds.melee),
);
self.settings
.insert(Setting::Jump, SettingValue::Keybind(settings.keybinds.jump));
let mut keys = self.settings.keys().collect::<Vec<_>>(); let mut keys = self.settings.keys().collect::<Vec<_>>();
keys.sort(); keys.sort();
@ -212,7 +139,6 @@ impl IPanelContainer for SettingsPanel {
let grid = match setting.tab() { let grid = match setting.tab() {
SettingsTab::General => self.general_grid.clone(), SettingsTab::General => self.general_grid.clone(),
SettingsTab::Volume => self.volume_grid.clone(), SettingsTab::Volume => self.volume_grid.clone(),
SettingsTab::Keybinds => self.keybinds_grid.clone(),
}; };
if let Some(mut grid) = grid { if let Some(mut grid) = grid {
@ -265,28 +191,6 @@ impl IPanelContainer for SettingsPanel {
line_edit.upcast() line_edit.upcast()
} }
SettingValue::Keybind(value) => {
let mut toggle_button = Button::new_alloc();
toggle_button.set_text(&value.to_string());
toggle_button.set_toggle_mode(true);
let setting_clone = setting.clone();
let button_clone = toggle_button.clone();
toggle_button
.signals()
.pressed()
.connect_other(self, move |s| {
if let Some((_, mut button)) = s.keybind_button.take() {
button.set_pressed(false);
}
s.base_mut().set_focus_behavior_recursive(
FocusBehaviorRecursive::DISABLED,
);
s.keybind_button = Some((setting_clone, button_clone.clone()))
});
toggle_button.upcast()
}
}; };
grid.add_child(&node); grid.add_child(&node);
@ -294,19 +198,6 @@ impl IPanelContainer for SettingsPanel {
} }
} }
} }
fn input(&mut self, event: Gd<InputEvent>) {
if let Some((setting, mut button)) = self.keybind_button.clone()
&& let Some(keybind) = Keybind::from_event(event)
{
self.keybind_button.take();
self.on_change(setting.clone(), SettingValue::Keybind(keybind));
button.set_text(&keybind.to_string());
button.set_pressed(false);
self.base_mut()
.set_focus_behavior_recursive(FocusBehaviorRecursive::INHERITED);
}
}
} }
#[godot_api] #[godot_api]
@ -323,7 +214,6 @@ impl SettingsPanel {
(Setting::DefaultName, SettingValue::String(value)) => { (Setting::DefaultName, SettingValue::String(value)) => {
new_settings.default_name = value.clone() new_settings.default_name = value.clone()
} }
(Setting::MasterVolume, SettingValue::SliderNumber(value)) => { (Setting::MasterVolume, SettingValue::SliderNumber(value)) => {
new_settings.volume.master_volume = value.value; new_settings.volume.master_volume = value.value;
} }
@ -336,34 +226,6 @@ impl SettingsPanel {
(Setting::AnnouncerVolume, SettingValue::SliderNumber(value)) => { (Setting::AnnouncerVolume, SettingValue::SliderNumber(value)) => {
new_settings.volume.announcer_volume = value.value; new_settings.volume.announcer_volume = value.value;
} }
(Setting::MoveRight, SettingValue::Keybind(keybind)) => {
new_settings.keybinds.move_right = *keybind;
}
(Setting::MoveLeft, SettingValue::Keybind(keybind)) => {
new_settings.keybinds.move_left = *keybind;
}
(Setting::MoveForward, SettingValue::Keybind(keybind)) => {
new_settings.keybinds.move_forward = *keybind;
}
(Setting::MoveBackward, SettingValue::Keybind(keybind)) => {
new_settings.keybinds.move_backward = *keybind;
}
(Setting::SelectRaygun, SettingValue::Keybind(keybind)) => {
new_settings.keybinds.select_raygun = *keybind;
}
(Setting::SelectShotgun, SettingValue::Keybind(keybind)) => {
new_settings.keybinds.select_raygun = *keybind;
}
(Setting::Shoot, SettingValue::Keybind(keybind)) => {
new_settings.keybinds.shoot = *keybind;
}
(Setting::Melee, SettingValue::Keybind(keybind)) => {
new_settings.keybinds.melee = *keybind;
}
(Setting::Jump, SettingValue::Keybind(keybind)) => {
new_settings.keybinds.jump = *keybind;
}
_ => {} _ => {}
} }
} }