mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Merge branch 'Carbonhell/serverCancel' into 'master'
Adds a way to cancel a pending connection to a server. Closes #254 See merge request veloren/veloren!521
This commit is contained in:
commit
ff82e77ec9
@ -116,6 +116,13 @@ impl PlayState for MainMenuState {
|
|||||||
.login_error("Invalid username or password".to_string());
|
.login_error("Invalid username or password".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
MainMenuEvent::CancelLoginAttempt => {
|
||||||
|
// client_init contains Some(ClientInit), which spawns a thread which contains a TcpStream::connect() call
|
||||||
|
// This call is blocking
|
||||||
|
// TODO fix when the network rework happens
|
||||||
|
client_init = None;
|
||||||
|
self.main_menu_ui.cancel_connection();
|
||||||
|
}
|
||||||
#[cfg(feature = "singleplayer")]
|
#[cfg(feature = "singleplayer")]
|
||||||
MainMenuEvent::StartSingleplayer => {
|
MainMenuEvent::StartSingleplayer => {
|
||||||
return PlayStateResult::Push(Box::new(StartSingleplayerState::new()));
|
return PlayStateResult::Push(Box::new(StartSingleplayerState::new()));
|
||||||
|
@ -103,6 +103,7 @@ pub enum Event {
|
|||||||
password: String,
|
password: String,
|
||||||
server_address: String,
|
server_address: String,
|
||||||
},
|
},
|
||||||
|
CancelLoginAttempt,
|
||||||
#[cfg(feature = "singleplayer")]
|
#[cfg(feature = "singleplayer")]
|
||||||
StartSingleplayer,
|
StartSingleplayer,
|
||||||
Quit,
|
Quit,
|
||||||
@ -110,6 +111,17 @@ pub enum Event {
|
|||||||
DisclaimerClosed,
|
DisclaimerClosed,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum PopupType {
|
||||||
|
Error,
|
||||||
|
ConnectionInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PopupData {
|
||||||
|
msg: String,
|
||||||
|
button_text: String,
|
||||||
|
popup_type: PopupType,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct MainMenuUi {
|
pub struct MainMenuUi {
|
||||||
ui: Ui,
|
ui: Ui,
|
||||||
ids: Ids,
|
ids: Ids,
|
||||||
@ -119,7 +131,7 @@ pub struct MainMenuUi {
|
|||||||
username: String,
|
username: String,
|
||||||
password: String,
|
password: String,
|
||||||
server_address: String,
|
server_address: String,
|
||||||
login_error: Option<String>,
|
popup: Option<PopupData>,
|
||||||
connecting: Option<std::time::Instant>,
|
connecting: Option<std::time::Instant>,
|
||||||
show_servers: bool,
|
show_servers: bool,
|
||||||
show_disclaimer: bool,
|
show_disclaimer: bool,
|
||||||
@ -150,7 +162,7 @@ impl MainMenuUi {
|
|||||||
username: networking.username.clone(),
|
username: networking.username.clone(),
|
||||||
password: "".to_owned(),
|
password: "".to_owned(),
|
||||||
server_address: networking.servers[networking.default_server].clone(),
|
server_address: networking.servers[networking.default_server].clone(),
|
||||||
login_error: None,
|
popup: None,
|
||||||
connecting: None,
|
connecting: None,
|
||||||
show_servers: false,
|
show_servers: false,
|
||||||
show_disclaimer: global_state.settings.show_disclaimer,
|
show_disclaimer: global_state.settings.show_disclaimer,
|
||||||
@ -263,8 +275,12 @@ impl MainMenuUi {
|
|||||||
// Used when the login button is pressed, or enter is pressed within input field
|
// Used when the login button is pressed, or enter is pressed within input field
|
||||||
macro_rules! login {
|
macro_rules! login {
|
||||||
() => {
|
() => {
|
||||||
self.login_error = None;
|
|
||||||
self.connecting = Some(std::time::Instant::now());
|
self.connecting = Some(std::time::Instant::now());
|
||||||
|
self.popup = Some(PopupData {
|
||||||
|
msg: "Connecting...".to_string(),
|
||||||
|
button_text: "Cancel".to_string(),
|
||||||
|
popup_type: PopupType::ConnectionInfo,
|
||||||
|
});
|
||||||
events.push(Event::LoginAttempt {
|
events.push(Event::LoginAttempt {
|
||||||
username: self.username.clone(),
|
username: self.username.clone(),
|
||||||
password: self.password.clone(),
|
password: self.password.clone(),
|
||||||
@ -278,7 +294,6 @@ impl MainMenuUi {
|
|||||||
#[cfg(feature = "singleplayer")]
|
#[cfg(feature = "singleplayer")]
|
||||||
macro_rules! singleplayer {
|
macro_rules! singleplayer {
|
||||||
() => {
|
() => {
|
||||||
self.login_error = None;
|
|
||||||
events.push(Event::StartSingleplayer);
|
events.push(Event::StartSingleplayer);
|
||||||
events.push(Event::LoginAttempt {
|
events.push(Event::LoginAttempt {
|
||||||
username: "singleplayer".to_string(),
|
username: "singleplayer".to_string(),
|
||||||
@ -346,9 +361,9 @@ impl MainMenuUi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Login error
|
// Popup (Error/Info)
|
||||||
if let Some(msg) = &self.login_error {
|
if let Some(popup_data) = &self.popup {
|
||||||
let text = Text::new(&msg)
|
let text = Text::new(&popup_data.msg)
|
||||||
.rgba(1.0, 1.0, 1.0, 1.0)
|
.rgba(1.0, 1.0, 1.0, 1.0)
|
||||||
.font_size(30)
|
.font_size(30)
|
||||||
.font_id(self.fonts.opensans);
|
.font_id(self.fonts.opensans);
|
||||||
@ -369,13 +384,19 @@ impl MainMenuUi {
|
|||||||
.hover_image(self.imgs.button_hover)
|
.hover_image(self.imgs.button_hover)
|
||||||
.press_image(self.imgs.button_press)
|
.press_image(self.imgs.button_press)
|
||||||
.label_y(Relative::Scalar(2.0))
|
.label_y(Relative::Scalar(2.0))
|
||||||
.label("Okay")
|
.label(&popup_data.button_text)
|
||||||
.label_font_size(10)
|
.label_font_size(10)
|
||||||
.label_color(TEXT_COLOR)
|
.label_color(TEXT_COLOR)
|
||||||
.set(self.ids.button_ok, ui_widgets)
|
.set(self.ids.button_ok, ui_widgets)
|
||||||
.was_clicked()
|
.was_clicked()
|
||||||
{
|
{
|
||||||
self.login_error = None
|
match popup_data.popup_type {
|
||||||
|
PopupType::ConnectionInfo => {
|
||||||
|
events.push(Event::CancelLoginAttempt);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
self.popup = None;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if self.show_servers {
|
if self.show_servers {
|
||||||
@ -468,27 +489,6 @@ impl MainMenuUi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Login button
|
// Login button
|
||||||
// Change button text and remove hover/press images if a connection is in progress
|
|
||||||
if let Some(start) = self.connecting {
|
|
||||||
Button::image(self.imgs.button)
|
|
||||||
.w_h(258.0, 55.0)
|
|
||||||
.down_from(self.ids.address_bg, 20.0)
|
|
||||||
.align_middle_x_of(self.ids.address_bg)
|
|
||||||
.label("Connecting...")
|
|
||||||
.label_color({
|
|
||||||
let pulse =
|
|
||||||
((start.elapsed().as_millis() as f32 * 0.008).sin() + 1.0) / 2.0;
|
|
||||||
Color::Rgba(
|
|
||||||
TEXT_COLOR.red() * (pulse / 2.0 + 0.5),
|
|
||||||
TEXT_COLOR.green() * (pulse / 2.0 + 0.5),
|
|
||||||
TEXT_COLOR.blue() * (pulse / 2.0 + 0.5),
|
|
||||||
pulse / 4.0 + 0.75,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.label_font_size(22)
|
|
||||||
.label_y(Relative::Scalar(5.0))
|
|
||||||
.set(self.ids.login_button, ui_widgets);
|
|
||||||
} else {
|
|
||||||
if Button::image(self.imgs.button)
|
if Button::image(self.imgs.button)
|
||||||
.hover_image(self.imgs.button_hover)
|
.hover_image(self.imgs.button_hover)
|
||||||
.press_image(self.imgs.button_press)
|
.press_image(self.imgs.button_press)
|
||||||
@ -511,7 +511,6 @@ impl MainMenuUi {
|
|||||||
{
|
{
|
||||||
login!();
|
login!();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// Singleplayer button
|
// Singleplayer button
|
||||||
#[cfg(feature = "singleplayer")]
|
#[cfg(feature = "singleplayer")]
|
||||||
@ -586,11 +585,21 @@ impl MainMenuUi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn login_error(&mut self, msg: String) {
|
pub fn login_error(&mut self, msg: String) {
|
||||||
self.login_error = Some(msg);
|
self.popup = Some(PopupData {
|
||||||
|
msg,
|
||||||
|
button_text: "Okay".to_string(),
|
||||||
|
popup_type: PopupType::Error,
|
||||||
|
});
|
||||||
self.connecting = None;
|
self.connecting = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn connected(&mut self) {
|
pub fn connected(&mut self) {
|
||||||
|
self.popup = None;
|
||||||
|
self.connecting = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cancel_connection(&mut self) {
|
||||||
|
self.popup = None;
|
||||||
self.connecting = None;
|
self.connecting = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user