1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 08:32:54 +08:00
osu-lazer/osu.Game/Overlays/AccountCreation/ScreenEntry.cs

256 lines
9.8 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
2018-12-06 13:57:00 +08:00
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
using osu.Framework.Platform;
using osu.Framework.Screens;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
2023-01-17 02:31:01 +08:00
using osu.Game.Localisation;
using osu.Game.Online.API;
using osu.Game.Overlays.Settings;
using osu.Game.Resources.Localisation.Web;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Overlays.AccountCreation
{
2022-11-24 13:32:20 +08:00
public partial class ScreenEntry : AccountCreationScreen
{
2024-02-02 13:53:00 +08:00
private ErrorTextFlowContainer usernameDescription = null!;
private ErrorTextFlowContainer emailAddressDescription = null!;
private ErrorTextFlowContainer passwordDescription = null!;
2024-02-02 13:53:00 +08:00
private OsuTextBox usernameTextBox = null!;
private OsuTextBox emailTextBox = null!;
private OsuPasswordTextBox passwordTextBox = null!;
2020-02-14 21:14:00 +08:00
[Resolved]
2024-02-02 13:53:00 +08:00
private IAPIProvider api { get; set; } = null!;
2020-02-14 21:14:00 +08:00
private IBindable<APIState> apiState = null!;
2024-02-02 13:53:00 +08:00
private ShakeContainer registerShake = null!;
private ITextPart characterCheckText = null!;
2024-02-02 13:53:00 +08:00
private OsuTextBox[] textboxes = null!;
private LoadingLayer loadingLayer = null!;
2020-02-14 21:14:00 +08:00
[Resolved]
2024-02-02 13:53:00 +08:00
private GameHost? host { get; set; }
[Resolved]
private OsuGame? game { get; set; }
[BackgroundDependencyLoader]
2023-06-14 23:09:03 +08:00
private void load()
{
2019-01-23 19:52:00 +08:00
InternalChildren = new Drawable[]
{
2021-01-06 05:39:59 +08:00
new FillFlowContainer
{
RelativeSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Padding = new MarginPadding(20),
Spacing = new Vector2(0, 10),
Children = new Drawable[]
{
new OsuSpriteText
{
Margin = new MarginPadding { Vertical = 10 },
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Font = OsuFont.GetFont(size: 20),
2023-01-17 02:31:01 +08:00
Text = AccountCreationStrings.LetsCreateAnAccount
},
usernameTextBox = new OsuTextBox
{
PlaceholderText = UsersStrings.LoginUsername.ToLower(),
RelativeSizeAxes = Axes.X,
TabbableContentContainer = this
},
usernameDescription = new ErrorTextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y
},
emailTextBox = new OsuTextBox
{
2023-01-17 03:24:03 +08:00
PlaceholderText = ModelValidationStrings.UserAttributesUserEmail.ToLower(),
RelativeSizeAxes = Axes.X,
TabbableContentContainer = this
},
emailAddressDescription = new ErrorTextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y
},
passwordTextBox = new OsuPasswordTextBox
{
PlaceholderText = UsersStrings.LoginPassword.ToLower(),
RelativeSizeAxes = Axes.X,
TabbableContentContainer = this,
},
passwordDescription = new ErrorTextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
registerShake = new ShakeContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Child = new SettingsButton
{
2023-01-17 02:31:01 +08:00
Text = LoginPanelStrings.Register,
Margin = new MarginPadding { Vertical = 20 },
Action = performRegistration
}
}
}
},
},
},
loadingLayer = new LoadingLayer(true)
};
2018-12-06 13:57:00 +08:00
textboxes = new[] { usernameTextBox, emailTextBox, passwordTextBox };
2023-06-14 22:40:26 +08:00
usernameDescription.AddText(AccountCreationStrings.UsernameDescription);
2023-06-14 22:40:26 +08:00
emailAddressDescription.AddText(AccountCreationStrings.EmailDescription1);
emailAddressDescription.AddText(AccountCreationStrings.EmailDescription2, cp => cp.Font = cp.Font.With(Typeface.Torus, weight: FontWeight.Bold));
passwordDescription.AddText("At least ");
characterCheckText = passwordDescription.AddText("8 characters long");
passwordDescription.AddText(". Choose something long but also something you will remember, like a line from your favourite song.");
passwordTextBox.Current.BindValueChanged(_ => updateCharacterCheckTextColour(), true);
characterCheckText.DrawablePartsRecreated += _ => updateCharacterCheckTextColour();
apiState = api.State.GetBoundCopy();
}
private void updateCharacterCheckTextColour()
{
string password = passwordTextBox.Text;
foreach (var d in characterCheckText.Drawables)
d.Colour = password.Length == 0 ? Color4.White : Interpolation.ValueAt(password.Length, Color4.OrangeRed, Color4.YellowGreen, 0, 8, Easing.In);
}
public override void OnEntering(ScreenTransitionEvent e)
{
base.OnEntering(e);
loadingLayer.Hide();
2019-10-23 10:51:29 +08:00
if (host?.OnScreenKeyboardOverlapsGameWindow != true)
focusNextTextBox();
}
2018-12-06 13:57:00 +08:00
private void performRegistration()
{
if (focusNextTextBox())
{
registerShake.Shake();
return;
}
usernameDescription.ClearErrors();
emailAddressDescription.ClearErrors();
passwordDescription.ClearErrors();
loadingLayer.Show();
Task.Run(() =>
{
bool success;
2024-02-02 13:53:00 +08:00
RegistrationRequest.RegistrationRequestErrors? errors = null;
try
{
errors = api.CreateAccount(emailTextBox.Text, usernameTextBox.Text, passwordTextBox.Text);
success = errors == null;
}
catch (Exception)
{
success = false;
}
Schedule(() =>
{
if (!success)
{
if (errors != null)
{
if (errors.User != null)
{
usernameDescription.AddErrors(errors.User.Username);
emailAddressDescription.AddErrors(errors.User.Email);
passwordDescription.AddErrors(errors.User.Password);
}
if (!string.IsNullOrEmpty(errors.Redirect))
{
if (!string.IsNullOrEmpty(errors.Message))
passwordDescription.AddErrors(new[] { errors.Message });
game?.OpenUrlExternally($"{errors.Redirect}?username={usernameTextBox.Text}&email={emailTextBox.Text}", true);
}
}
else
{
passwordDescription.AddErrors(new[] { "Something happened... but we're not sure what." });
}
registerShake.Shake();
loadingLayer.Hide();
return;
}
apiState.BindValueChanged(state =>
{
if (state.NewValue == APIState.RequiresSecondFactorAuth)
this.Push(new ScreenEmailVerification());
});
api.Login(usernameTextBox.Text, passwordTextBox.Text);
});
});
}
private bool focusNextTextBox()
{
var nextTextBox = nextUnfilledTextBox();
2019-04-01 11:16:05 +08:00
if (nextTextBox != null)
{
Schedule(() => GetContainingFocusManager().ChangeFocus(nextTextBox));
2018-12-06 13:57:00 +08:00
return true;
}
2018-12-06 13:57:00 +08:00
return false;
}
2018-12-06 13:57:00 +08:00
2024-02-02 13:53:00 +08:00
private OsuTextBox? nextUnfilledTextBox() => textboxes.FirstOrDefault(t => string.IsNullOrEmpty(t.Text));
}
}