Add networksettings and integrate with main menu ui

Former-commit-id: f04532e03f8262b9751ae4a68259637f7de0344c
This commit is contained in:
Louis Pearson 2019-04-18 11:40:29 -06:00
parent f85e3546cf
commit 15c2cdb2ef
4 changed files with 28 additions and 7 deletions

View File

@ -100,7 +100,7 @@ fn main() {
// Set up the initial play state // Set up the initial play state
let mut states: Vec<Box<dyn PlayState>> = vec![Box::new(MainMenuState::new( let mut states: Vec<Box<dyn PlayState>> = vec![Box::new(MainMenuState::new(
&mut global_state.window, &mut global_state,
))]; ))];
states.last().map(|current_state| { states.last().map(|current_state| {
log::info!("Started game with state '{}'", current_state.name()) log::info!("Started game with state '{}'", current_state.name())

View File

@ -21,9 +21,9 @@ pub struct MainMenuState {
impl MainMenuState { impl MainMenuState {
/// Create a new `MainMenuState` /// Create a new `MainMenuState`
pub fn new(window: &mut Window) -> Self { pub fn new(global_state: &mut GlobalState) -> Self {
Self { Self {
main_menu_ui: MainMenuUi::new(window), main_menu_ui: MainMenuUi::new(global_state),
} }
} }
} }
@ -92,6 +92,12 @@ impl PlayState for MainMenuState {
username, username,
server_address, server_address,
} => { } => {
let mut net_settings = &mut global_state.settings.networking;
net_settings.username = username.clone();
if !net_settings.servers.contains(&server_address) {
net_settings.servers.push(server_address.clone());
}
global_state.settings.save_to_file();
const DEFAULT_PORT: u16 = 59003; const DEFAULT_PORT: u16 = 59003;
// Don't try to connect if there is already a connection in progress // Don't try to connect if there is already a connection in progress
client_init = client_init.or(Some(ClientInit::new( client_init = client_init.or(Some(ClientInit::new(

View File

@ -3,6 +3,7 @@ use crate::{
ui::{self, ScaleMode, Ui}, ui::{self, ScaleMode, Ui},
window::Window, window::Window,
DEFAULT_PUBLIC_SERVER, DEFAULT_PUBLIC_SERVER,
GlobalState,
}; };
use common::{ use common::{
assets, assets,
@ -137,7 +138,9 @@ pub struct MainMenuUi {
} }
impl MainMenuUi { impl MainMenuUi {
pub fn new(window: &mut Window) -> Self { pub fn new(global_state: &mut GlobalState) -> Self {
let mut window = &mut global_state.window;
let networking = &global_state.settings.networking;
let mut ui = Ui::new(window).unwrap(); let mut ui = Ui::new(window).unwrap();
// TODO: adjust/remove this, right now it is used to demonstrate window scaling functionality // TODO: adjust/remove this, right now it is used to demonstrate window scaling functionality
ui.scaling_mode(ScaleMode::RelativeToWindow([1920.0, 1080.0].into())); ui.scaling_mode(ScaleMode::RelativeToWindow([1920.0, 1080.0].into()));
@ -162,8 +165,8 @@ impl MainMenuUi {
ids, ids,
font_metamorph, font_metamorph,
font_opensans, font_opensans,
username: "Username".to_string(), username: networking.username.clone(),
server_address: DEFAULT_PUBLIC_SERVER.to_string(), server_address,
login_error: None, login_error: None,
connecting: None, connecting: None,
} }
@ -352,7 +355,6 @@ impl MainMenuUi {
.was_clicked() .was_clicked()
{ {
singleplayer!(); singleplayer!();
login!();
} }
// Quit // Quit
if Button::image(self.imgs.button) if Button::image(self.imgs.button)

View File

@ -14,6 +14,7 @@ use std::default::Default;
#[serde(default)] #[serde(default)]
pub struct Settings { pub struct Settings {
pub controls: ControlSettings, pub controls: ControlSettings,
pub networking: NetworkingSettings,
} }
/// ControlSettings contains keybindings /// ControlSettings contains keybindings
@ -37,6 +38,13 @@ pub struct ControlSettings {
pub toggle_interface: VirtualKeyCode, pub toggle_interface: VirtualKeyCode,
} }
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NetworkingSettings {
pub username: String,
pub servers: Vec<String>,
pub default_server: usize,
}
impl Default for Settings { impl Default for Settings {
fn default() -> Self { fn default() -> Self {
Settings { Settings {
@ -58,6 +66,11 @@ impl Default for Settings {
help: VirtualKeyCode::F1, help: VirtualKeyCode::F1,
toggle_interface: VirtualKeyCode::F2, toggle_interface: VirtualKeyCode::F2,
}, },
networking: NetworkingSettings {
username: "Username".to_string(),
servers: vec!("server.veloren.net".to_string()),
default_server: 0,
},
} }
} }