1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 20:07:26 +08:00
osu-lazer/osu.Game/Overlays/Settings/Sections/General/LoginSettings.cs

175 lines
5.8 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-02-07 12:52:19 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Configuration;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using OpenTK;
using osu.Framework.Input;
2017-02-07 12:52:19 +08:00
namespace osu.Game.Overlays.Settings.Sections.General
2017-02-07 12:52:19 +08:00
{
public class LoginSettings : SettingsSubsection, IOnlineComponent
2017-02-07 12:52:19 +08:00
{
private bool bounding = true;
private LoginForm form;
2017-02-07 12:52:19 +08:00
2017-02-08 13:01:17 +08:00
protected override string Header => "Account";
2017-02-07 12:52:19 +08:00
public override RectangleF BoundingBox => bounding ? base.BoundingBox : RectangleF.Empty;
public bool Bounding
{
get { return bounding; }
set
{
bounding = value;
Invalidate(Invalidation.Geometry);
}
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(APIAccess api)
{
api?.Register(this);
}
public void APIStateChanged(APIAccess api, APIState state)
{
form = null;
2017-02-07 12:52:19 +08:00
switch (state)
{
case APIState.Offline:
Children = new Drawable[]
{
form = new LoginForm()
2017-02-07 12:52:19 +08:00
};
break;
case APIState.Failing:
Children = new Drawable[]
{
new OsuSpriteText
{
Text = "Connection failing :(",
},
};
break;
case APIState.Connecting:
Children = new Drawable[]
{
new OsuSpriteText
{
Text = "Connecting...",
},
};
break;
case APIState.Online:
Children = new Drawable[]
{
new OsuSpriteText
{
Text = $"Connected as {api.Username}!",
},
new OsuButton
{
RelativeSizeAxes = Axes.X,
Text = "Sign out",
Action = api.Logout
}
};
break;
}
form?.TriggerFocus();
}
protected override bool OnFocus(InputState state)
{
form?.TriggerFocus();
return base.OnFocus(state);
2017-02-07 12:52:19 +08:00
}
2017-03-07 09:59:19 +08:00
private class LoginForm : FillFlowContainer
2017-02-07 12:52:19 +08:00
{
private TextBox username;
private TextBox password;
private APIAccess api;
private void performLogin()
{
if (!string.IsNullOrEmpty(username.Text) && !string.IsNullOrEmpty(password.Text))
api.Login(username.Text, password.Text);
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(APIAccess api, OsuConfigManager config)
{
this.api = api;
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Vertical;
2017-03-02 02:33:01 +08:00
Spacing = new Vector2(0, 5);
2017-02-07 12:52:19 +08:00
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
Children = new Drawable[]
{
username = new OsuTextBox
{
2017-02-08 13:01:17 +08:00
PlaceholderText = "Username",
2017-02-07 12:52:19 +08:00
RelativeSizeAxes = Axes.X,
2017-02-23 16:50:09 +08:00
Text = api?.Username ?? string.Empty,
TabbableContentContainer = this
2017-02-07 12:52:19 +08:00
},
password = new OsuPasswordTextBox
{
2017-02-08 13:01:17 +08:00
PlaceholderText = "Password",
2017-02-23 16:50:09 +08:00
RelativeSizeAxes = Axes.X,
TabbableContentContainer = this,
OnCommit = (sender, newText) => performLogin()
2017-02-07 12:52:19 +08:00
},
new SettingsCheckbox
2017-02-07 12:52:19 +08:00
{
LabelText = "Remember username",
2017-05-15 09:56:27 +08:00
Bindable = config.GetBindable<bool>(OsuSetting.SaveUsername),
2017-02-07 12:52:19 +08:00
},
new SettingsCheckbox
2017-02-07 12:52:19 +08:00
{
LabelText = "Stay logged in",
2017-05-15 09:56:27 +08:00
Bindable = config.GetBindable<bool>(OsuSetting.SavePassword),
2017-02-07 12:52:19 +08:00
},
new OsuButton
{
RelativeSizeAxes = Axes.X,
Text = "Sign in",
Action = performLogin
},
new OsuButton
{
RelativeSizeAxes = Axes.X,
Text = "Register new account",
//Action = registerLink
}
};
}
protected override bool OnFocus(InputState state)
{
Schedule(() =>
{
if (string.IsNullOrEmpty(username.Text))
username.TriggerFocus();
else
password.TriggerFocus();
});
return base.OnFocus(state);
}
2017-02-07 12:52:19 +08:00
}
}
}