1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 17:32:54 +08:00

Add basic login flow.

This commit is contained in:
Dean Herbert 2016-11-30 16:54:15 +09:00
parent 88748499fa
commit 11f726ad45
2 changed files with 76 additions and 10 deletions

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
using osu.Framework; using osu.Framework;
@ -71,6 +72,7 @@ namespace osu.Game.Online.API
public void Register(IOnlineComponent component) public void Register(IOnlineComponent component)
{ {
components.Add(component); components.Add(component);
component.APIStateChanged(this, state);
} }
public void Unregister(IOnlineComponent component) public void Unregister(IOnlineComponent component)
@ -158,6 +160,16 @@ namespace osu.Game.Online.API
password = null; password = null;
} }
public void Login(string username, string password)
{
Debug.Assert(State == APIState.Offline);
Username = username;
Password = password;
State = APIState.Connecting;
}
/// <summary> /// <summary>
/// Handle a single API request. /// Handle a single API request.
/// </summary> /// </summary>
@ -167,6 +179,7 @@ namespace osu.Game.Online.API
{ {
try try
{ {
Logger.Log($@"Performing request {req}", LoggingTarget.Network);
req.Perform(this); req.Perform(this);
State = APIState.Online; State = APIState.Online;

View File

@ -1,4 +1,5 @@
using OpenTK; using System;
using OpenTK;
using osu.Framework; using osu.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -10,7 +11,7 @@ using osu.Game.Online.API;
namespace osu.Game.Overlays.Options.General namespace osu.Game.Overlays.Options.General
{ {
public class LoginOptions : OptionsSubsection public class LoginOptions : OptionsSubsection, IOnlineComponent
{ {
private Container loginForm; private Container loginForm;
protected override string Header => "Sign In"; protected override string Header => "Sign In";
@ -31,16 +32,56 @@ namespace osu.Game.Overlays.Options.General
[BackgroundDependencyLoader(permitNulls: true)] [BackgroundDependencyLoader(permitNulls: true)]
private void load(APIAccess api) private void load(APIAccess api)
{ {
if (api == null) api?.Register(this);
return; }
loginForm.Children = new Drawable[]
public void APIStateChanged(APIAccess api, APIState state)
{
switch (state)
{ {
new LoginForm(api) case APIState.Offline:
}; loginForm.Children = new Drawable[]
{
new LoginForm(api)
};
break;
case APIState.Failing:
loginForm.Children = new Drawable[]
{
new SpriteText
{
Text = @"Connection failing :(",
},
};
break;
case APIState.Connecting:
loginForm.Children = new Drawable[]
{
new SpriteText
{
Text = @"Connecting...",
},
};
break;
case APIState.Online:
loginForm.Children = new Drawable[]
{
new SpriteText
{
Text = $@"Connected as {api.Username}!",
},
};
break;
}
} }
class LoginForm : FlowContainer class LoginForm : FlowContainer
{ {
private APIAccess api;
private TextBox username;
private TextBox password;
public LoginForm(APIAccess api) public LoginForm(APIAccess api)
{ {
Direction = FlowDirection.VerticalOnly; Direction = FlowDirection.VerticalOnly;
@ -51,16 +92,28 @@ namespace osu.Game.Overlays.Options.General
Children = new Drawable[] Children = new Drawable[]
{ {
new SpriteText { Text = "Username" }, new SpriteText { Text = "Username" },
new TextBox { Height = 20, RelativeSizeAxes = Axes.X, Text = api?.Username ?? string.Empty }, username = new TextBox { Height = 20, RelativeSizeAxes = Axes.X, Text = api?.Username ?? string.Empty },
new SpriteText { Text = "Password" }, new SpriteText { Text = "Password" },
new TextBox { Height = 20, RelativeSizeAxes = Axes.X }, password = new PasswordTextBox { Height = 20, RelativeSizeAxes = Axes.X },
new OsuButton new OsuButton
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Text = "Log in", Text = "Log in",
Action = performLogin
} }
}; };
} }
private void performLogin()
{
api.Login(username.Text, password.Text);
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(APIAccess api)
{
this.api = api;
}
} }
} }
} }