stevenarella/src/screen/login.rs

225 lines
7.5 KiB
Rust
Raw Normal View History

2016-03-16 14:25:35 -04:00
// Copyright 2016 Matthew Collins
2015-09-28 18:37:14 -04:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::cell::Cell;
use std::rc::Rc;
use std::sync::mpsc;
use std::thread;
use rand::{self, Rng};
use crate::auth;
use crate::console;
use crate::protocol;
use crate::protocol::mojang;
use crate::render;
use crate::ui;
2015-09-27 14:38:58 -04:00
pub struct Login {
2015-10-07 14:36:59 -04:00
elements: Option<UIElements>,
vars: Rc<console::Vars>,
2015-09-27 14:38:58 -04:00
}
struct UIElements {
2015-10-07 14:36:59 -04:00
logo: ui::logo::Logo,
2016-04-24 07:22:04 -04:00
login_btn: ui::ButtonRef,
login_btn_text: ui::TextRef,
login_error: ui::TextRef,
username_txt: ui::TextBoxRef,
password_txt: ui::TextBoxRef,
_disclaimer: ui::TextRef,
try_login: Rc<Cell<bool>>,
refresh: bool,
login_res: Option<mpsc::Receiver<Result<mojang::Profile, protocol::Error>>>,
profile: mojang::Profile,
2015-09-27 14:38:58 -04:00
}
impl Login {
pub fn new(vars: Rc<console::Vars>) -> Login {
Login {
elements: None,
vars,
}
2015-10-07 14:36:59 -04:00
}
2015-09-27 14:38:58 -04:00
}
Add support for compiling WebAssembly wasm32-unknown-unknown target (#92) Note this only is the first step in web support, although the project compiles, it doesn't run! Merging now to avoid branch divergence, until dependencies can be updated for wasm support. * Add instructions to build for wasm32-unknown-unknown with wasm-pack in www/ * Update to rust-clipboard fork to compile with emscripten https://github.com/aweinstock314/rust-clipboard/pull/62 * Exclude reqwest dependency in wasm32 * Exclude compiling clipboard pasting on wasm32 * Exclude reqwest-using code from wasm32 * Install wasm target with rustup in Travis CI * Update to collision 0.19.0 Fixes wasm incompatibility in deprecated rustc-serialize crate: https://github.com/rustgd/collision-rs/issues/106 error[E0046]: not all trait items implemented, missing: `encode` --> github.com-1ecc6299db9ec823/rustc-serialize-0.3.24/src/serialize.rs:1358:1 * Increase travis_wait time even further, try 120 minutes * Set RUST_BACKTRACE=1 in main * Remove unused unneeded bzip2 features in zip crate To fix wasm32-unknown-unknown target compile error: error[E0432]: unresolved imports `libc::c_int`, `libc::c_uint`, `libc::c_void`, `libc::c_char` --> src/github.com-1ecc6299db9ec823/bzip2-sys-0.1.7/lib.rs:5:12 | 5 | use libc::{c_int, c_uint, c_void, c_char}; | ^^^^^ ^^^^^^ ^^^^^^ ^^^^^^ no `c_char` in the root | | | | | | | no `c_void` in the root | | no `c_uint` in the root | no `c_int` in the root * flate2 use Rust backend * Add console_error_panic_hook module for wasm backtraces * Build using wasm-pack, wasm-bindgen, run with wasm-app * Update to miniz_oxide 0.2.1, remove patch for https://github.com/Frommi/miniz_oxide/issues/42 * Update to official clipboard crate since https://github.com/aweinstock314/rust-clipboard/pull/62 was merged, but git revision pending release * Update to branch of glutin attempting to build for wasm https://github.com/iceiix/glutin/pull/1 * Update winit dependency of glutin to git master https://github.com/iceiix/winit/pull/2 * Update to glutin branch with working (compiles, doesn't run) wasm_stub * Add app name in title on web page * Add wasm to Travis-CI test matrix * Update glutin to fix Windows EGL compilation on AppVeyor https://github.com/iceiix/glutin/pull/1/commits/97797352b5242436cb82d8ecfb44242b69766e4c
2019-03-03 11:32:36 -05:00
#[cfg(not(target_arch = "wasm32"))]
2015-09-27 14:38:58 -04:00
impl super::Screen for Login {
2015-10-07 14:36:59 -04:00
fn on_active(&mut self, renderer: &mut render::Renderer, ui_container: &mut ui::Container) {
2016-04-24 07:22:04 -04:00
let logo = ui::logo::Logo::new(renderer.resources.clone(), ui_container);
2015-09-27 14:38:58 -04:00
let try_login = Rc::new(Cell::new(false));
// Login
2016-04-24 07:22:04 -04:00
let login_btn = ui::ButtonBuilder::new()
.position(0.0, 100.0)
.size(400.0, 40.0)
.alignment(ui::VAttach::Middle, ui::HAttach::Center)
.create(ui_container);
let login_btn_text = ui::TextBuilder::new()
.text("Login")
.position(0.0, 0.0)
.alignment(ui::VAttach::Middle, ui::HAttach::Center)
.attach(&mut *login_btn.borrow_mut());
{
let mut btn = login_btn.borrow_mut();
btn.add_text(login_btn_text.clone());
let tl = try_login.clone();
btn.add_click_func(move |_, _| {
tl.set(true);
true
});
}
// Login Error
2016-04-24 07:22:04 -04:00
let login_error = ui::TextBuilder::new()
.text("")
.position(0.0, 150.0)
.colour((255, 50, 50, 255))
.alignment(ui::VAttach::Middle, ui::HAttach::Center)
.create(ui_container);
// Username
2016-04-24 07:22:04 -04:00
let username_txt = ui::TextBoxBuilder::new()
.position(0.0, -20.0)
.size(400.0, 40.0)
.alignment(ui::VAttach::Middle, ui::HAttach::Center)
.create(ui_container);
ui::TextBox::make_focusable(&username_txt, ui_container);
ui::TextBuilder::new()
.text("Username/Email:")
.position(0.0, -18.0)
.attach(&mut *username_txt.borrow_mut());
// Password
2016-04-24 07:22:04 -04:00
let password_txt = ui::TextBoxBuilder::new()
.position(0.0, 40.0)
.size(400.0, 40.0)
.alignment(ui::VAttach::Middle, ui::HAttach::Center)
.password(true)
.create(ui_container);
ui::TextBox::make_focusable(&password_txt, ui_container);
ui::TextBuilder::new()
.text("Password:")
.position(0.0, -18.0)
.attach(&mut *password_txt.borrow_mut());
let tl = try_login.clone();
2016-04-24 07:22:04 -04:00
password_txt.borrow_mut().add_submit_func(move |_, _| {
tl.set(true);
});
// Disclaimer
2016-04-24 07:22:04 -04:00
let disclaimer = ui::TextBuilder::new()
.text("Not affiliated with Mojang/Minecraft")
.position(5.0, 5.0)
.colour((255, 200, 200, 255))
.alignment(ui::VAttach::Bottom, ui::HAttach::Right)
.create(ui_container);
2015-09-27 14:38:58 -04:00
let profile = mojang::Profile {
username: self.vars.get(auth::CL_USERNAME).clone(),
id: self.vars.get(auth::CL_UUID).clone(),
access_token: self.vars.get(auth::AUTH_TOKEN).clone(),
};
let refresh = profile.is_complete();
try_login.set(refresh);
2015-10-07 14:36:59 -04:00
self.elements = Some(UIElements {
logo,
profile,
login_btn,
login_btn_text,
login_error,
try_login,
refresh,
login_res: None,
2016-04-24 07:22:04 -04:00
_disclaimer: disclaimer,
username_txt,
password_txt,
2015-10-07 14:36:59 -04:00
});
}
2016-04-24 07:22:04 -04:00
fn on_deactive(&mut self, _renderer: &mut render::Renderer, _ui_container: &mut ui::Container) {
// Clean up
2015-10-07 14:36:59 -04:00
self.elements = None
}
2015-09-27 14:38:58 -04:00
fn tick(
&mut self,
_delta: f64,
renderer: &mut render::Renderer,
_ui_container: &mut ui::Container,
) -> Option<Box<dyn super::Screen>> {
2015-10-07 14:36:59 -04:00
let elements = self.elements.as_mut().unwrap();
2015-09-27 14:38:58 -04:00
if elements.try_login.get() && elements.login_res.is_none() {
elements.try_login.set(false);
let (tx, rx) = mpsc::channel();
elements.login_res = Some(rx);
2016-04-24 07:22:04 -04:00
elements.login_btn.borrow_mut().disabled = true;
elements.login_btn_text.borrow_mut().text = "Logging in...".into();
let mut client_token = self.vars.get(auth::AUTH_CLIENT_TOKEN).clone();
if client_token.is_empty() {
client_token = std::iter::repeat(())
.map(|()| rand::thread_rng().sample(&rand::distributions::Alphanumeric) as char)
.take(20)
.collect();
self.vars.set(auth::AUTH_CLIENT_TOKEN, client_token);
}
let client_token = self.vars.get(auth::AUTH_CLIENT_TOKEN).clone();
2016-04-24 07:22:04 -04:00
let username = elements.username_txt.borrow().input.clone();
let password = elements.password_txt.borrow().input.clone();
let refresh = elements.refresh;
let profile = elements.profile.clone();
thread::spawn(move || {
if refresh {
tx.send(profile.refresh(&client_token)).unwrap();
} else {
tx.send(mojang::Profile::login(&username, &password, &client_token))
.unwrap();
}
});
}
let mut done = false;
if let Some(rx) = elements.login_res.as_ref() {
if let Ok(res) = rx.try_recv() {
done = true;
2016-04-24 07:22:04 -04:00
elements.login_btn.borrow_mut().disabled = false;
elements.login_btn_text.borrow_mut().text = "Login".into();
match res {
Ok(val) => {
self.vars.set(auth::CL_USERNAME, val.username.clone());
self.vars.set(auth::CL_UUID, val.id.clone());
self.vars.set(auth::AUTH_TOKEN, val.access_token.clone());
elements.profile = val;
return Some(Box::new(super::ServerList::new(None)));
}
Err(err) => {
2016-04-24 07:22:04 -04:00
elements.login_error.borrow_mut().text = format!("{}", err);
}
}
}
}
if done {
elements.login_res = None;
}
2016-04-24 07:22:04 -04:00
elements.logo.tick(renderer);
None
2015-10-07 14:36:59 -04:00
}
}